2019-09-06 23:08:48 +02:00
|
|
|
#!/usr/bin/env python3
|
2018-10-01 21:48:54 +02:00
|
|
|
"""
|
|
|
|
Make backend documentation
|
|
|
|
"""
|
|
|
|
|
2022-06-15 15:01:16 +02:00
|
|
|
import sys
|
2018-10-01 21:48:54 +02:00
|
|
|
import os
|
2020-04-28 13:58:34 +02:00
|
|
|
import io
|
2018-10-01 21:48:54 +02:00
|
|
|
import subprocess
|
2022-06-15 15:01:16 +02:00
|
|
|
from pathlib import Path
|
2018-10-01 21:48:54 +02:00
|
|
|
|
2020-05-22 13:22:52 +02:00
|
|
|
marker = "{{< rem autogenerated options"
|
2018-10-01 21:48:54 +02:00
|
|
|
start = marker + " start"
|
|
|
|
stop = marker + " stop"
|
2020-05-22 13:22:52 +02:00
|
|
|
end = ">}}"
|
2018-10-01 21:48:54 +02:00
|
|
|
|
|
|
|
def find_backends():
|
|
|
|
"""Return a list of all backends"""
|
|
|
|
return [ x for x in os.listdir("backend") if x not in ("all",) ]
|
|
|
|
|
2022-06-15 15:01:16 +02:00
|
|
|
def output_docs(backend, out, cwd):
|
2018-10-01 21:48:54 +02:00
|
|
|
"""Output documentation for backend options to out"""
|
|
|
|
out.flush()
|
2022-06-15 15:01:16 +02:00
|
|
|
subprocess.check_call(["./rclone", "help", "backend", backend], stdout=out)
|
2018-10-01 21:48:54 +02:00
|
|
|
|
2022-06-15 15:01:16 +02:00
|
|
|
def output_backend_tool_docs(backend, out, cwd):
|
2020-04-28 13:58:34 +02:00
|
|
|
"""Output documentation for backend tool to out"""
|
|
|
|
out.flush()
|
2022-06-15 15:01:16 +02:00
|
|
|
subprocess.call(["./rclone", "backend", "help", backend], stdout=out, stderr=subprocess.DEVNULL)
|
2020-04-28 13:58:34 +02:00
|
|
|
|
2018-10-01 21:48:54 +02:00
|
|
|
def alter_doc(backend):
|
|
|
|
"""Alter the documentation for backend"""
|
2022-06-15 15:01:16 +02:00
|
|
|
rclone_bin_dir = Path(sys.path[0]).parent.absolute()
|
2018-10-01 21:48:54 +02:00
|
|
|
doc_file = "docs/content/"+backend+".md"
|
|
|
|
if not os.path.exists(doc_file):
|
|
|
|
raise ValueError("Didn't find doc file %s" % (doc_file,))
|
|
|
|
new_file = doc_file+"~new~"
|
|
|
|
altered = False
|
2021-10-15 18:51:57 +02:00
|
|
|
with open(doc_file, "r", encoding="utf_8") as in_file, open(new_file, "w", encoding="utf_8") as out_file:
|
2018-10-01 21:48:54 +02:00
|
|
|
in_docs = False
|
|
|
|
for line in in_file:
|
|
|
|
if not in_docs:
|
|
|
|
if start in line:
|
|
|
|
in_docs = True
|
2020-05-22 13:22:52 +02:00
|
|
|
start_full = (start + "\" - DO NOT EDIT - instead edit fs.RegInfo in backend/%s/%s.go then run make backenddocs\" " + end + "\n") % (backend, backend)
|
2018-10-01 21:48:54 +02:00
|
|
|
out_file.write(start_full)
|
2022-06-15 15:01:16 +02:00
|
|
|
output_docs(backend, out_file, rclone_bin_dir)
|
|
|
|
output_backend_tool_docs(backend, out_file, rclone_bin_dir)
|
2020-05-22 13:22:52 +02:00
|
|
|
out_file.write(stop+" "+end+"\n")
|
2018-10-01 21:48:54 +02:00
|
|
|
altered = True
|
|
|
|
if not in_docs:
|
|
|
|
out_file.write(line)
|
|
|
|
if in_docs:
|
|
|
|
if stop in line:
|
|
|
|
in_docs = False
|
|
|
|
os.rename(doc_file, doc_file+"~")
|
|
|
|
os.rename(new_file, doc_file)
|
|
|
|
if not altered:
|
|
|
|
raise ValueError("Didn't find '%s' markers for in %s" % (start, doc_file))
|
|
|
|
|
2022-06-15 15:01:16 +02:00
|
|
|
|
|
|
|
def main(args):
|
|
|
|
# single backend
|
|
|
|
if (len(args) == 2):
|
|
|
|
try:
|
|
|
|
alter_doc(args[1])
|
|
|
|
print("Added docs for %s backend" % args[1])
|
|
|
|
except Exception as e:
|
|
|
|
print("Failed adding docs for %s backend: %s" % (args[1], e))
|
|
|
|
return
|
|
|
|
|
|
|
|
# all backends
|
2018-10-01 21:48:54 +02:00
|
|
|
failed, success = 0, 0
|
|
|
|
for backend in find_backends():
|
|
|
|
try:
|
|
|
|
alter_doc(backend)
|
2019-09-06 23:08:48 +02:00
|
|
|
except Exception as e:
|
|
|
|
print("Failed adding docs for %s backend: %s" % (backend, e))
|
2018-10-01 21:48:54 +02:00
|
|
|
failed += 1
|
|
|
|
else:
|
|
|
|
success += 1
|
2019-09-06 23:08:48 +02:00
|
|
|
print("Added docs for %d backends with %d failures" % (success, failed))
|
2022-06-15 15:01:16 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv)
|