annotate document_it.py @ 26:ce7cbf3e08e1 default tip

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