make_backend_docs.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python3
  2. """
  3. Make backend documentation
  4. """
  5. import sys
  6. import os
  7. import io
  8. import subprocess
  9. from pathlib import Path
  10. marker = "{{< rem autogenerated options"
  11. start = marker + " start"
  12. stop = marker + " stop"
  13. end = ">}}"
  14. def find_backends():
  15. """Return a list of all backends"""
  16. return [ x for x in os.listdir("backend") if x not in ("all",) ]
  17. def output_docs(backend, out, cwd):
  18. """Output documentation for backend options to out"""
  19. out.flush()
  20. subprocess.check_call(["./rclone", "help", "backend", backend], stdout=out)
  21. def output_backend_tool_docs(backend, out, cwd):
  22. """Output documentation for backend tool to out"""
  23. out.flush()
  24. subprocess.call(["./rclone", "backend", "help", backend], stdout=out, stderr=subprocess.DEVNULL)
  25. def alter_doc(backend):
  26. """Alter the documentation for backend"""
  27. rclone_bin_dir = Path(sys.path[0]).parent.absolute()
  28. doc_file = "docs/content/"+backend+".md"
  29. if not os.path.exists(doc_file):
  30. raise ValueError("Didn't find doc file %s" % (doc_file,))
  31. new_file = doc_file+"~new~"
  32. altered = False
  33. with open(doc_file, "r", encoding="utf_8") as in_file, open(new_file, "w", encoding="utf_8") as out_file:
  34. in_docs = False
  35. for line in in_file:
  36. if not in_docs:
  37. if start in line:
  38. in_docs = True
  39. start_full = (start + "\" - DO NOT EDIT - instead edit fs.RegInfo in backend/%s/%s.go then run make backenddocs\" " + end + "\n") % (backend, backend)
  40. out_file.write(start_full)
  41. output_docs(backend, out_file, rclone_bin_dir)
  42. output_backend_tool_docs(backend, out_file, rclone_bin_dir)
  43. out_file.write(stop+" "+end+"\n")
  44. altered = True
  45. if not in_docs:
  46. out_file.write(line)
  47. if in_docs:
  48. if stop in line:
  49. in_docs = False
  50. os.rename(doc_file, doc_file+"~")
  51. os.rename(new_file, doc_file)
  52. if not altered:
  53. raise ValueError("Didn't find '%s' markers for in %s" % (start, doc_file))
  54. def main(args):
  55. # single backend
  56. if (len(args) == 2):
  57. try:
  58. alter_doc(args[1])
  59. print("Added docs for %s backend" % args[1])
  60. except Exception as e:
  61. print("Failed adding docs for %s backend: %s" % (args[1], e))
  62. return
  63. # all backends
  64. failed, success = 0, 0
  65. for backend in find_backends():
  66. try:
  67. alter_doc(backend)
  68. except Exception as e:
  69. print("Failed adding docs for %s backend: %s" % (backend, e))
  70. failed += 1
  71. else:
  72. success += 1
  73. print("Added docs for %d backends with %d failures" % (success, failed))
  74. if __name__ == "__main__":
  75. main(sys.argv)