Mercurial > mozilla > hg > DocumentIt
annotate document_it.py @ 15:d9026d114655
deal with eccentricities of local directories
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 02 Aug 2011 16:46:40 -0700 |
parents | 145a11393a73 |
children | d6528dd74592 |
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 | |
14 | 48 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
|
49 """ |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
50 reads a documentation manifest; returns a list of two-tuples: |
6 | 51 [(filename, destination)] |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
52 """ |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
53 |
6 | 54 assert os.path.exists(filename) and os.path.isfile(filename), "%s not found" % filename |
55 | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
56 if directory is None: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
57 directory = os.path.dirname(os.path.abspath(filename)) |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
58 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
|
59 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
|
60 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
|
61 items = [] |
14 | 62 baseurl = baseurl.rstrip('/') |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
63 for line in lines: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
64 try: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
65 f, url = line.split() |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
66 # 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
|
67 except ValueError: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
68 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
|
69 |
14 | 70 if '://' not in url: |
71 url = '%s/%s' % (baseurl, url.lstrip('/')) | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
72 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
|
73 if os.path.isdir(filename): |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
74 pass # TODO |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
75 else: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
76 items.append((filename, url)) |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
77 return items |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
78 |
0 | 79 def main(args=sys.argv[1:]): |
80 | |
9 | 81 # default output directory |
82 default_dir = tempfile.mktemp() | |
83 | |
0 | 84 # parse command line options |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
85 usage = '%prog [options] manifest <manifest> <...>' |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
86 |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
87 # description formatter |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
88 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
89 def format_description(self, description): |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
90 if description: |
13
1efb669ab1c8
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
91 return description.strip() + '\n' |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
92 else: |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
93 return '' |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
94 |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
95 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
0 | 96 parser.add_option('-d', '--directory', dest='directory', |
6 | 97 help='render the documentation from this directory') |
15
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
98 parser.add_option('-o', '--dest', dest='dest', |
9 | 99 default=default_dir, |
100 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
|
101 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
|
102 help='user name') |
6 | 103 parser.add_option('--list', dest='list', action='store_true', default=False, |
104 help="list files") | |
9 | 105 parser.add_option('--validate', dest='validate', # TODO unused |
106 action='store_true', default=False, | |
107 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
|
108 options, manifests = parser.parse_args(args) |
0 | 109 |
6 | 110 # 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
|
111 if not args: |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
112 parser.print_help() |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
113 parser.exit() |
0 | 114 |
7 | 115 # get base url |
11 | 116 assert options.dest |
7 | 117 if '://' in options.dest: |
118 baseurl = options.dest | |
119 else: | |
15
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
120 baseurl = 'file://' + os.path.abspath(options.dest) |
7 | 121 |
6 | 122 # read the manifests |
123 files = [] | |
124 for manifest in manifests: | |
14 | 125 for item in parse_manifest(manifest, baseurl, options.directory): |
6 | 126 if item not in files: |
127 files.append(item) | |
128 if options.list: | |
129 for item in files: | |
14 | 130 print '%s -> %s' % item |
0 | 131 |
9 | 132 if not files: |
133 return # you're done | |
134 | |
0 | 135 # render and upload READMEs |
11 | 136 if baseurl.startswith('file://'): |
137 options.dest = baseurl[len('file://'):] # deals with --dest file:///foo from command line | |
8 | 138 |
11 | 139 # ensure a directory |
140 if os.path.exists(options.dest): | |
141 assert os.path.isdir(options.dest), "'%s' - not a directory" % options.dest | |
8 | 142 |
143 # TODO render to directory | |
9 | 144 for src, dest in files: |
15
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
145 |
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
146 if dest.startswith('file://'): |
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
147 dest = dest[len('file://'):] |
11 | 148 |
149 # create a directory if needed | |
150 dirname = os.path.dirname(dest) | |
151 os.makedirs(dirname) | |
152 | |
153 # render | |
154 f = file(dest, 'w') | |
155 buffer = markdown.Markdown().convert(file(src).read()) | |
156 f.write(buffer) | |
157 f.close() | |
6 | 158 else: |
159 # TODO check credentials | |
12 | 160 for src, dest in files: |
161 pass | |
6 | 162 raise NotImplementedError |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
163 |
11 | 164 |
10 | 165 if options.dest == default_dir: |
11 | 166 print "Files rendered to\n%s" % default_dir |
9 | 167 |
0 | 168 if __name__ == '__main__': |
169 main() |