github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/bin/make_backend_docs.py (about)

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