docs-update.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env python
  2. #
  3. # Sven's quick hack script to update the documentation
  4. #
  5. # call with:
  6. # ./docs/update.py /usr/bin/docker
  7. #
  8. import datetime
  9. import re
  10. from sys import argv
  11. import subprocess
  12. import os
  13. import os.path
  14. script, docker_cmd = argv
  15. date_string = datetime.date.today().strftime('%B %Y')
  16. def print_usage(outtext, docker_cmd, command):
  17. try:
  18. help_string = subprocess.check_output(
  19. "".join((docker_cmd, " ", command, " --help")),
  20. stderr=subprocess.STDOUT,
  21. shell=True
  22. )
  23. except subprocess.CalledProcessError, e:
  24. help_string = e.output
  25. for l in str(help_string).strip().split("\n"):
  26. l = l.rstrip()
  27. if l == '':
  28. outtext.write("\n")
  29. else:
  30. # `docker --help` tells the user the path they called it with
  31. l = re.sub(docker_cmd, "docker", l)
  32. outtext.write(" {}\n".format(l))
  33. outtext.write("\n")
  34. # TODO: look for an complain about any missing commands
  35. def update_cli_reference():
  36. originalFile = "docs/sources/reference/commandline/cli.md"
  37. os.rename(originalFile, originalFile+".bak")
  38. intext = open("{}.bak".format(originalFile), "r")
  39. outtext = open(originalFile, "w")
  40. mode = 'p'
  41. space = " "
  42. command = ""
  43. # 2 mode line-by line parser
  44. for line in intext:
  45. if mode == 'p':
  46. # Prose
  47. match = re.match("( \s*)Usage: docker ([a-z]+)", line)
  48. if match:
  49. # the begining of a Docker command usage block
  50. space = match.group(1)
  51. command = match.group(2)
  52. mode = 'c'
  53. else:
  54. match = re.match("( \s*)Usage of .*docker.*:", line)
  55. if match:
  56. # the begining of the Docker --help usage block
  57. space = match.group(1)
  58. command = ""
  59. mode = 'c'
  60. else:
  61. outtext.write(line)
  62. else:
  63. # command usage block
  64. match = re.match("("+space+")(.*)|^$", line)
  65. if not match:
  66. # The end of the current usage block
  67. # Shell out to run docker to see the new output
  68. print_usage(outtext, docker_cmd, command)
  69. outtext.write(line)
  70. mode = 'p'
  71. if mode == 'c':
  72. print_usage(outtext, docker_cmd, command)
  73. def update_man_pages():
  74. cmds = []
  75. try:
  76. help_string = subprocess.check_output(
  77. "".join((docker_cmd)),
  78. stderr=subprocess.STDOUT,
  79. shell=True
  80. )
  81. except subprocess.CalledProcessError, e:
  82. help_string = e.output
  83. for l in str(help_string).strip().split("\n"):
  84. l = l.rstrip()
  85. if l != "":
  86. match = re.match(" (.*?) .*", l)
  87. if match:
  88. cmds.append(match.group(1))
  89. desc_re = re.compile(
  90. r".*# DESCRIPTION(.*?)# (OPTIONS|EXAMPLES?).*",
  91. re.MULTILINE | re.DOTALL
  92. )
  93. example_re = re.compile(
  94. r".*# EXAMPLES?(.*)# HISTORY.*",
  95. re.MULTILINE | re.DOTALL
  96. )
  97. history_re = re.compile(
  98. r".*# HISTORY(.*)",
  99. re.MULTILINE | re.DOTALL
  100. )
  101. for command in cmds:
  102. print "COMMAND: "+command
  103. history = ""
  104. description = ""
  105. examples = ""
  106. if os.path.isfile("docs/man/docker-"+command+".1.md"):
  107. intext = open("docs/man/docker-"+command+".1.md", "r")
  108. txt = intext.read()
  109. intext.close()
  110. match = desc_re.match(txt)
  111. if match:
  112. description = match.group(1)
  113. match = example_re.match(txt)
  114. if match:
  115. examples = match.group(1)
  116. match = history_re.match(txt)
  117. if match:
  118. history = match.group(1).strip()
  119. usage = ""
  120. usage_description = ""
  121. params = {}
  122. key_params = {}
  123. try:
  124. help_string = subprocess.check_output(
  125. "".join((docker_cmd, " ", command, " --help")),
  126. stderr=subprocess.STDOUT,
  127. shell=True
  128. )
  129. except subprocess.CalledProcessError, e:
  130. help_string = e.output
  131. last_key = ""
  132. for l in str(help).split("\n"):
  133. l = l.rstrip()
  134. if l != "":
  135. match = re.match("Usage: docker {}(.*)".format(command), l)
  136. if match:
  137. usage = match.group(1).strip()
  138. else:
  139. match = re.match(" (-+)(.*) \s+(.*)", l)
  140. if match:
  141. last_key = match.group(2).rstrip()
  142. key_params[last_key] = match.group(1)+last_key
  143. params[last_key] = match.group(3)
  144. else:
  145. if last_key != "":
  146. params[last_key] = "{}\n{}".format(params[last_key], l)
  147. else:
  148. if usage_description != "":
  149. usage_description = usage_description + "\n"
  150. usage_description = usage_description + l
  151. # replace [OPTIONS] with the list of params
  152. options = ""
  153. match = re.match("\[OPTIONS\](.*)", usage)
  154. if match:
  155. usage = match.group(1)
  156. new_usage = ""
  157. # TODO: sort without the `-`'s
  158. for key in sorted(params.keys(), key=lambda s: s.lower()):
  159. # split on commas, remove --?.*=.*, put in *'s mumble
  160. ps = []
  161. opts = []
  162. for k in key_params[key].split(","):
  163. match = re.match("(-+)([A-Za-z-0-9]*)(?:=(.*))?", k.lstrip())
  164. if match:
  165. p = "**{}{}**".format(match.group(1), match.group(2))
  166. o = "**{}{}**".format(match.group(1), match.group(2))
  167. if match.group(3):
  168. val = match.group(3)
  169. if val == "\"\"":
  170. val = match.group(2).upper()
  171. p = "{}[=*{}*]".format(p, val)
  172. val = match.group(3)
  173. if val in ("true", "false"):
  174. params[key] = params[key].rstrip()
  175. if not params[key].endswith('.'):
  176. params[key] = params[key]+ "."
  177. params[key] = "{} The default is *{}*.".format(params[key], val)
  178. val = "*true*|*false*"
  179. o = "{}={}".format(o, val)
  180. ps.append(p)
  181. opts.append(o)
  182. else:
  183. print "nomatch:{}".format(k)
  184. new_usage = "{}\n[{}]".format(new_usage, "|".join(ps))
  185. options = "{}{}\n {}\n\n".format(options, ", ".join(opts), params[key])
  186. if new_usage != "":
  187. new_usage = "{}\n".format(new_usage.strip())
  188. usage = new_usage + usage
  189. outtext = open("docs/man/docker-{}.1.md".format(command), "w")
  190. outtext.write("""% DOCKER(1) Docker User Manuals
  191. % Docker Community
  192. % JUNE 2014
  193. # NAME
  194. """)
  195. outtext.write("docker-{} - {}\n\n".format(command, usage_description))
  196. outtext.write("# SYNOPSIS\n**docker {}**\n{}\n\n".format(command, usage))
  197. if description != "":
  198. outtext.write("# DESCRIPTION{}".format(description))
  199. if options == "":
  200. options = "There are no available options.\n\n"
  201. outtext.write("# OPTIONS\n{}".format(options))
  202. if examples != "":
  203. outtext.write("# EXAMPLES{}".format(examples))
  204. outtext.write("# HISTORY\n")
  205. if history != "":
  206. outtext.write("{}\n".format(history))
  207. recent_history_re = re.compile(
  208. ".*{}.*".format(date_string),
  209. re.MULTILINE | re.DOTALL
  210. )
  211. if not recent_history_re.match(history):
  212. outtext.write("{}, updated by Sven Dowideit <SvenDowideit@home.org.au>\n".format(date_string))
  213. outtext.close()
  214. # main
  215. update_cli_reference()
  216. update_man_pages()