Mercurial > mozilla > hg > DocumentIt
annotate document_it.py @ 20:7299c4529b41
more improvements
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 02 Aug 2011 21:34:59 -0700 |
parents | 493683e17eae |
children | 67e4becc7d49 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
9 | 4 update MDN documentation from markdown |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
5 |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
6 see: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
7 http://developer.mindtouch.com/en/ref/MindTouch_API/POST%3Apages%2F%2F%7Bpageid%7D%2F%2Fcontents |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
8 |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
9 The manifest format is in the form: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
10 |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
11 mozrunner/README.txt https://developer.mozilla.org/en/Mozrunner |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
12 jsbridge/README.txt https://developer.mozilla.org/en/JSbridge |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
13 mozmill/README.txt https://developer.mozilla.org/en/Mozmill |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
14 mozmill/docs/ https://developer.mozilla.org/en/Mozmill/ |
9 | 15 |
16 --dest sets the destination, e.g. | |
17 | |
18 --dest http://developer.mozilla.org/ | |
19 --dest https://developer-stage9.mozilla.org/jhammel | |
20 --dest path/to directory | |
21 | |
22 By default, a new temporary directory will be created | |
0 | 23 """ |
24 | |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
25 import optparse |
0 | 26 import os |
27 import sys | |
9 | 28 import tempfile |
0 | 29 import urllib2 |
30 | |
31 # necessary imports | |
32 try: | |
33 import markdown | |
34 except ImportError: | |
35 raise ImportError("markdown is not installed, run (e.g.):\neasy_install Markdown") | |
36 | |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
37 DIR=os.path.dirname(os.path.abspath(__file__)) # XXX currently unused |
0 | 38 |
39 def find_readme(directory): | |
40 """find a README file in a directory""" | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
41 # XXX currently unused |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
42 README=['README.md', 'README.txt', 'README'] |
0 | 43 for name in README: |
44 path = os.path.join(directory, name) | |
45 if os.path.exists(path): | |
46 return path | |
47 | |
18 | 48 def all_files(directory): |
49 filenames = [] | |
50 for dirpath, dirnames, files in os.walk(directory): | |
51 filenames.extend([os.path.join(dirpath, f) for f in files]) | |
52 return sorted(filenames) | |
53 | |
14 | 54 def parse_manifest(filename, baseurl, directory=None): |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
55 """ |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
56 reads a documentation manifest; returns a list of two-tuples: |
6 | 57 [(filename, destination)] |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
58 """ |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
59 |
6 | 60 assert os.path.exists(filename) and os.path.isfile(filename), "%s not found" % filename |
61 | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
62 if directory is None: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
63 directory = os.path.dirname(os.path.abspath(filename)) |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
64 lines = [line.strip() for line in file(filename).readlines()] |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
65 lines = [line for line in lines |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
66 if line and not line.startswith('#')] |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
67 items = [] |
14 | 68 baseurl = baseurl.rstrip('/') |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
69 for line in lines: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
70 try: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
71 f, url = line.split() |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
72 # TODO: include options as third segment (e.g. format=ReST) |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
73 except ValueError: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
74 raise ValueError("illegal manifest line: '%s'" % line) |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
75 |
14 | 76 if '://' not in url: |
20 | 77 url = '%s%s' % (baseurl, url) |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
78 filename = os.path.join(directory, f) |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
79 if os.path.isdir(filename): |
18 | 80 files = all_files(filename) |
81 for i in files: | |
82 relpath = os.path.relpath(i, filename) | |
20 | 83 items.append((i, '%s%s' % (url.rstrip('/'), relpath.lstrip('/')))) |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
84 else: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
85 items.append((filename, url)) |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
86 return items |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
87 |
0 | 88 def main(args=sys.argv[1:]): |
89 | |
9 | 90 # default output directory |
91 default_dir = tempfile.mktemp() | |
92 | |
0 | 93 # parse command line options |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
94 usage = '%prog [options] manifest <manifest> <...>' |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
95 |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
96 # description formatter |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
97 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
98 def format_description(self, description): |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
99 if description: |
13
1efb669ab1c8
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
100 return description.strip() + '\n' |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
101 else: |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
102 return '' |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
103 |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
104 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
0 | 105 parser.add_option('-d', '--directory', dest='directory', |
6 | 106 help='render the documentation from this directory') |
15
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
107 parser.add_option('-o', '--dest', dest='dest', |
9 | 108 default=default_dir, |
109 help='base directory or URL of destination [DEFAULT: %default]') | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
110 parser.add_option('-u', '--user', dest='user', |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
111 help='user name') |
20 | 112 parser.add_option('-p', '--password', dest='password', |
113 help='user password') | |
6 | 114 parser.add_option('--list', dest='list', action='store_true', default=False, |
115 help="list files") | |
9 | 116 parser.add_option('--validate', dest='validate', # TODO unused |
117 action='store_true', default=False, | |
118 help="validate the rendering but don't output") | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
119 options, manifests = parser.parse_args(args) |
0 | 120 |
6 | 121 # print help if no manifests given |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
122 if not args: |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
123 parser.print_help() |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
124 parser.exit() |
0 | 125 |
7 | 126 # get base url |
11 | 127 assert options.dest |
7 | 128 if '://' in options.dest: |
129 baseurl = options.dest | |
130 else: | |
15
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
131 baseurl = 'file://' + os.path.abspath(options.dest) |
7 | 132 |
6 | 133 # read the manifests |
134 files = [] | |
135 for manifest in manifests: | |
14 | 136 for item in parse_manifest(manifest, baseurl, options.directory): |
6 | 137 if item not in files: |
138 files.append(item) | |
139 if options.list: | |
140 for item in files: | |
14 | 141 print '%s -> %s' % item |
17
0a1aecef2c52
youre done after listing files
Jeff Hammel <jhammel@mozilla.com>
parents:
16
diff
changeset
|
142 return |
0 | 143 |
9 | 144 if not files: |
145 return # you're done | |
146 | |
0 | 147 # render and upload READMEs |
11 | 148 if baseurl.startswith('file://'): |
149 options.dest = baseurl[len('file://'):] # deals with --dest file:///foo from command line | |
8 | 150 |
11 | 151 # ensure a directory |
152 if os.path.exists(options.dest): | |
153 assert os.path.isdir(options.dest), "'%s' - not a directory" % options.dest | |
8 | 154 |
155 # TODO render to directory | |
9 | 156 for src, dest in files: |
15
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
157 |
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
158 if dest.startswith('file://'): |
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
159 dest = dest[len('file://'):] |
11 | 160 |
161 # create a directory if needed | |
162 dirname = os.path.dirname(dest) | |
16
d6528dd74592
only create the directory if we need to
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
163 if os.path.exists(dirname): |
19
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
164 if not os.path.isdir(dirname): |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
165 # deal with filesystem directories vs PATH_INFO |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
166 f = file(dirname) |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
167 buffer = f.read() |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
168 f.close() |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
169 os.remove(dirname) |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
170 os.makedirs(dirname) |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
171 f = file(os.path.join(dirname, 'index.html'), 'w') |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
172 f.write(buffer) |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
173 f.close() |
16
d6528dd74592
only create the directory if we need to
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
174 else: |
d6528dd74592
only create the directory if we need to
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
175 os.makedirs(dirname) |
11 | 176 |
177 # render | |
178 f = file(dest, 'w') | |
179 buffer = markdown.Markdown().convert(file(src).read()) | |
180 f.write(buffer) | |
181 f.close() | |
6 | 182 else: |
20 | 183 # check credentials |
184 assert options.user and options.password, "Please supply your --user and --password" | |
185 | |
186 # upload the files | |
187 # XXX just use curl for now | |
188 import subprocess | |
12 | 189 for src, dest in files: |
20 | 190 dest += '/contents?edittime=now' |
191 print src, dest | |
11 | 192 |
10 | 193 if options.dest == default_dir: |
11 | 194 print "Files rendered to\n%s" % default_dir |
9 | 195 |
0 | 196 if __name__ == '__main__': |
197 main() |