Mercurial > mozilla > hg > DocumentIt
annotate document_it.py @ 24:045544951756
this actually works, by god
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 03 Aug 2011 14:39:56 -0700 |
parents | e6ed732e8ce6 |
children | 0ce6b501f62d |
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 | |
22
67e4becc7d49
first steps for cleaning up this awful mess
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
31 # import markdown |
0 | 32 try: |
33 import markdown | |
34 except ImportError: | |
35 raise ImportError("markdown is not installed, run (e.g.):\neasy_install Markdown") | |
36 | |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
37 destinations = {'stage': 'https://developer-stage9.mozilla.org/@api/deki/pages/=%(page)s/contents?edittime=now', |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
38 'MDN': 'https://developer.mozilla.org/@api/deki/pages/=%(page)s/contents?edittime=now'} |
22
67e4becc7d49
first steps for cleaning up this awful mess
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
39 |
0 | 40 |
41 def find_readme(directory): | |
42 """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
|
43 # XXX currently unused |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
44 README=['README.md', 'README.txt', 'README'] |
0 | 45 for name in README: |
46 path = os.path.join(directory, name) | |
47 if os.path.exists(path): | |
48 return path | |
49 | |
18 | 50 def all_files(directory): |
22
67e4becc7d49
first steps for cleaning up this awful mess
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
51 """get all files in a directory tree""" |
18 | 52 filenames = [] |
53 for dirpath, dirnames, files in os.walk(directory): | |
54 filenames.extend([os.path.join(dirpath, f) for f in files]) | |
55 return sorted(filenames) | |
56 | |
22
67e4becc7d49
first steps for cleaning up this awful mess
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
57 def parse_manifest(filename, directory=None): |
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 reads a documentation manifest; returns a list of two-tuples: |
6 | 60 [(filename, destination)] |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
61 """ |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
62 |
6 | 63 assert os.path.exists(filename) and os.path.isfile(filename), "%s not found" % filename |
64 | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
65 if directory is None: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
66 directory = os.path.dirname(os.path.abspath(filename)) |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 items = [] |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
71 for line in lines: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
72 try: |
22
67e4becc7d49
first steps for cleaning up this awful mess
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
73 f, page = line.split() |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
74 # 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
|
75 except ValueError: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
76 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
|
77 |
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): |
22
67e4becc7d49
first steps for cleaning up this awful mess
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
80 raise NotImplementedError |
18 | 81 files = all_files(filename) |
82 for i in files: | |
83 relpath = os.path.relpath(i, filename) | |
22
67e4becc7d49
first steps for cleaning up this awful mess
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
84 items.append((i, relpath)) |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
85 else: |
22
67e4becc7d49
first steps for cleaning up this awful mess
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
86 items.append((filename, page)) |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
87 return items |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
88 |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
89 def item_url(item, dest): |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
90 if '://' in dest: |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
91 if '%(page)s' in dest: |
24
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
92 return dest % {'page': item.replace('/', r'%25%32%66')} |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
93 else: |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
94 return '%s/%s' % (dest.lstrip('/'), item.rstrip('/')) |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
95 else: |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
96 return 'file://%s' % (os.path.join(dest, item)) |
24
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
97 |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
98 |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
99 def render(filename): |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
100 """render a file in markdown""" |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
101 return markdown.Markdown().convert(file(filename).read()) |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
102 |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
103 def post(content, url, user, password): |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
104 """post to the specified URL""" |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
105 # XXX cheat and use curl for now |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
106 try: |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
107 from subprocess import check_call as call |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
108 except: |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
109 from subprocess import call |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
110 content = content.replace('\r', '').replace('\n', '\r\n') # just to make sure |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
111 fd, filename = tempfile.mkstemp() |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
112 os.write(fd, content) |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
113 os.close(fd) |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
114 command = ["curl", "-u", "%s:%s" % (user, password), |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
115 "--data-binary", "@%s" % filename, |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
116 "-i", url] |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
117 call(command) |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
118 os.remove(filename) |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
119 |
0 | 120 def main(args=sys.argv[1:]): |
121 | |
9 | 122 # default output directory |
123 default_dir = tempfile.mktemp() | |
124 | |
0 | 125 # parse command line options |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
126 usage = '%prog [options] manifest <manifest> <...>' |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
127 |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
128 # description formatter |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
129 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
130 def format_description(self, description): |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
131 if description: |
13
1efb669ab1c8
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
132 return description.strip() + '\n' |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
133 else: |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
134 return '' |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
135 |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
136 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
0 | 137 parser.add_option('-d', '--directory', dest='directory', |
6 | 138 help='render the documentation from this directory') |
15
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
139 parser.add_option('-o', '--dest', dest='dest', |
9 | 140 default=default_dir, |
141 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
|
142 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
|
143 help='user name') |
20 | 144 parser.add_option('-p', '--password', dest='password', |
145 help='user password') | |
6 | 146 parser.add_option('--list', dest='list', action='store_true', default=False, |
147 help="list files") | |
24
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
148 parser.add_option('-l', '--list-destinations', dest='list_destinations', |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
149 action='store_true', default=False, |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
150 help='list preconfigured destinations') |
9 | 151 parser.add_option('--validate', dest='validate', # TODO unused |
152 action='store_true', default=False, | |
153 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
|
154 options, manifests = parser.parse_args(args) |
0 | 155 |
6 | 156 # 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
|
157 if not args: |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
158 parser.print_help() |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
159 parser.exit() |
0 | 160 |
24
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
161 # print preconfigured destinations if asked |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
162 if options.list_destinations: |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
163 for key in sorted(destinations.keys()): |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
164 print '%s: %s' % (key, destinations[key]) |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
165 return # you're done |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
166 |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
167 # get destination |
11 | 168 assert options.dest |
24
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
169 if options.dest in destinations: |
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
170 options.dest = destinations[options.dest] |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
171 if options.dest.startswith('file://'): |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
172 options.dest = options.dest[len('file://'):] |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
173 if '://' not in options.dest: |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
174 options.dest = os.path.abspath(options.dest) |
7 | 175 |
6 | 176 # read the manifests |
177 files = [] | |
178 for manifest in manifests: | |
22
67e4becc7d49
first steps for cleaning up this awful mess
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
179 for item in parse_manifest(manifest, options.directory): |
6 | 180 if item not in files: |
181 files.append(item) | |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
182 files = [(i, item_url(j, options.dest)) for i, j in files] |
6 | 183 if options.list: |
184 for item in files: | |
14 | 185 print '%s -> %s' % item |
17
0a1aecef2c52
youre done after listing files
Jeff Hammel <jhammel@mozilla.com>
parents:
16
diff
changeset
|
186 return |
0 | 187 |
9 | 188 if not files: |
189 return # you're done | |
190 | |
0 | 191 # render and upload READMEs |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
192 if '://' in options.dest: |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
193 |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
194 # check credentials |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
195 assert options.user and options.password, "Please supply your --user and --password" |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
196 |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
197 # upload the files |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
198 for src, dest in files: |
24
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
199 post(render(src), dest, options.user, options.password) |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
200 |
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
201 else: |
8 | 202 |
11 | 203 # ensure a directory |
204 if os.path.exists(options.dest): | |
205 assert os.path.isdir(options.dest), "'%s' - not a directory" % options.dest | |
8 | 206 |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
207 # render to directory |
9 | 208 for src, dest in files: |
15
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
209 |
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
210 if dest.startswith('file://'): |
d9026d114655
deal with eccentricities of local directories
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
211 dest = dest[len('file://'):] |
11 | 212 |
213 # create a directory if needed | |
214 dirname = os.path.dirname(dest) | |
16
d6528dd74592
only create the directory if we need to
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
215 if os.path.exists(dirname): |
19
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
216 if not os.path.isdir(dirname): |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
217 # deal with filesystem directories vs PATH_INFO |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
218 f = file(dirname) |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
219 buffer = f.read() |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
220 f.close() |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
221 os.remove(dirname) |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
222 os.makedirs(dirname) |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
223 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
|
224 f.write(buffer) |
493683e17eae
now renders correctly to filesystem for preview
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
225 f.close() |
16
d6528dd74592
only create the directory if we need to
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
226 else: |
d6528dd74592
only create the directory if we need to
Jeff Hammel <jhammel@mozilla.com>
parents:
15
diff
changeset
|
227 os.makedirs(dirname) |
11 | 228 |
229 # render | |
230 f = file(dest, 'w') | |
24
045544951756
this actually works, by god
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
231 f.write(render(src)) |
11 | 232 f.close() |
20 | 233 |
23
e6ed732e8ce6
now works with files anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
234 # print out destination directory if using the temporary default |
10 | 235 if options.dest == default_dir: |
11 | 236 print "Files rendered to\n%s" % default_dir |
9 | 237 |
0 | 238 if __name__ == '__main__': |
239 main() |