github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/bin/make_backend_docs.py (about)

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