Mercurial > mozilla > hg > DocumentIt
comparison document_it.py @ 23:e6ed732e8ce6
now works with files anyway
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 03 Aug 2011 12:15:50 -0700 |
parents | 67e4becc7d49 |
children | 045544951756 |
comparison
equal
deleted
inserted
replaced
22:67e4becc7d49 | 23:e6ed732e8ce6 |
---|---|
32 try: | 32 try: |
33 import markdown | 33 import markdown |
34 except ImportError: | 34 except ImportError: |
35 raise ImportError("markdown is not installed, run (e.g.):\neasy_install Markdown") | 35 raise ImportError("markdown is not installed, run (e.g.):\neasy_install Markdown") |
36 | 36 |
37 destinations = {'stage': 'https://developer-stage9.mozilla.org/@api/deki/pages/=%(page)s/contents', | 37 destinations = {'stage': 'https://developer-stage9.mozilla.org/@api/deki/pages/=%(page)s/contents?edittime=now', |
38 'MDN': 'https://developer.mozilla.org/@api/deki/pages/=%(page)s/contents'} | 38 'MDN': 'https://developer.mozilla.org/@api/deki/pages/=%(page)s/contents?edittime=now'} |
39 | 39 |
40 | 40 |
41 def find_readme(directory): | 41 def find_readme(directory): |
42 """find a README file in a directory""" | 42 """find a README file in a directory""" |
43 # XXX currently unused | 43 # XXX currently unused |
84 items.append((i, relpath)) | 84 items.append((i, relpath)) |
85 else: | 85 else: |
86 items.append((filename, page)) | 86 items.append((filename, page)) |
87 return items | 87 return items |
88 | 88 |
89 def item_url(item, dest): | |
90 if '://' in dest: | |
91 if '%(page)s' in dest: | |
92 return dest % {'page': item} | |
93 else: | |
94 return '%s/%s' % (dest.lstrip('/'), item.rstrip('/')) | |
95 else: | |
96 return 'file://%s' % (os.path.join(dest, item)) | |
97 | |
98 | |
89 def main(args=sys.argv[1:]): | 99 def main(args=sys.argv[1:]): |
90 | 100 |
91 # default output directory | 101 # default output directory |
92 default_dir = tempfile.mktemp() | 102 default_dir = tempfile.mktemp() |
93 | 103 |
124 parser.print_help() | 134 parser.print_help() |
125 parser.exit() | 135 parser.exit() |
126 | 136 |
127 # get base url | 137 # get base url |
128 assert options.dest | 138 assert options.dest |
129 if '://' in options.dest: | 139 if options.dest.startswith('file://'): |
130 baseurl = options.dest | 140 options.dest = options.dest[len('file://'):] |
131 else: | 141 if '://' not in options.dest: |
132 baseurl = 'file://' + os.path.abspath(options.dest) | 142 options.dest = os.path.abspath(options.dest) |
133 | 143 |
134 # read the manifests | 144 # read the manifests |
135 files = [] | 145 files = [] |
136 for manifest in manifests: | 146 for manifest in manifests: |
137 for item in parse_manifest(manifest, options.directory): | 147 for item in parse_manifest(manifest, options.directory): |
138 if item not in files: | 148 if item not in files: |
139 files.append(item) | 149 files.append(item) |
150 files = [(i, item_url(j, options.dest)) for i, j in files] | |
140 if options.list: | 151 if options.list: |
141 for item in files: | 152 for item in files: |
142 print '%s -> %s' % item | 153 print '%s -> %s' % item |
143 return | 154 return |
144 | 155 |
145 if not files: | 156 if not files: |
146 return # you're done | 157 return # you're done |
147 | 158 |
148 # render and upload READMEs | 159 # render and upload READMEs |
149 if baseurl.startswith('file://'): | 160 if '://' in options.dest: |
150 options.dest = baseurl[len('file://'):] # deals with --dest file:///foo from command line | 161 |
162 # check credentials | |
163 assert options.user and options.password, "Please supply your --user and --password" | |
164 | |
165 # upload the files | |
166 # XXX just use curl for now | |
167 import subprocess | |
168 for src, dest in files: | |
169 dest += '/contents?edittime=now' | |
170 print src, dest | |
171 | |
172 else: | |
151 | 173 |
152 # ensure a directory | 174 # ensure a directory |
153 if os.path.exists(options.dest): | 175 if os.path.exists(options.dest): |
154 assert os.path.isdir(options.dest), "'%s' - not a directory" % options.dest | 176 assert os.path.isdir(options.dest), "'%s' - not a directory" % options.dest |
155 | 177 |
156 # TODO render to directory | 178 # render to directory |
157 for src, dest in files: | 179 for src, dest in files: |
158 | 180 |
159 if dest.startswith('file://'): | 181 if dest.startswith('file://'): |
160 dest = dest[len('file://'):] | 182 dest = dest[len('file://'):] |
161 | 183 |
178 # render | 200 # render |
179 f = file(dest, 'w') | 201 f = file(dest, 'w') |
180 buffer = markdown.Markdown().convert(file(src).read()) | 202 buffer = markdown.Markdown().convert(file(src).read()) |
181 f.write(buffer) | 203 f.write(buffer) |
182 f.close() | 204 f.close() |
183 else: | 205 |
184 # check credentials | 206 # print out destination directory if using the temporary default |
185 assert options.user and options.password, "Please supply your --user and --password" | |
186 | |
187 # upload the files | |
188 # XXX just use curl for now | |
189 import subprocess | |
190 for src, dest in files: | |
191 dest += '/contents?edittime=now' | |
192 print src, dest | |
193 | |
194 if options.dest == default_dir: | 207 if options.dest == default_dir: |
195 print "Files rendered to\n%s" % default_dir | 208 print "Files rendered to\n%s" % default_dir |
196 | 209 |
197 if __name__ == '__main__': | 210 if __name__ == '__main__': |
198 main() | 211 main() |