Browse code

Prettify docs-update.py

Remove commented code, use format for string concatenation, split long lines, fix how-to-run instructions
Signed-off-by: Davide Ceretti <dav.ceretti@gmail.com>

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