comparison 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
comparison
equal deleted inserted replaced
23:e6ed732e8ce6 24:045544951756
87 return items 87 return items
88 88
89 def item_url(item, dest): 89 def item_url(item, dest):
90 if '://' in dest: 90 if '://' in dest:
91 if '%(page)s' in dest: 91 if '%(page)s' in dest:
92 return dest % {'page': item} 92 return dest % {'page': item.replace('/', r'%25%32%66')}
93 else: 93 else:
94 return '%s/%s' % (dest.lstrip('/'), item.rstrip('/')) 94 return '%s/%s' % (dest.lstrip('/'), item.rstrip('/'))
95 else: 95 else:
96 return 'file://%s' % (os.path.join(dest, item)) 96 return 'file://%s' % (os.path.join(dest, item))
97 97
98
99 def render(filename):
100 """render a file in markdown"""
101 return markdown.Markdown().convert(file(filename).read())
102
103 def post(content, url, user, password):
104 """post to the specified URL"""
105 # XXX cheat and use curl for now
106 try:
107 from subprocess import check_call as call
108 except:
109 from subprocess import call
110 content = content.replace('\r', '').replace('\n', '\r\n') # just to make sure
111 fd, filename = tempfile.mkstemp()
112 os.write(fd, content)
113 os.close(fd)
114 command = ["curl", "-u", "%s:%s" % (user, password),
115 "--data-binary", "@%s" % filename,
116 "-i", url]
117 call(command)
118 os.remove(filename)
98 119
99 def main(args=sys.argv[1:]): 120 def main(args=sys.argv[1:]):
100 121
101 # default output directory 122 # default output directory
102 default_dir = tempfile.mktemp() 123 default_dir = tempfile.mktemp()
122 help='user name') 143 help='user name')
123 parser.add_option('-p', '--password', dest='password', 144 parser.add_option('-p', '--password', dest='password',
124 help='user password') 145 help='user password')
125 parser.add_option('--list', dest='list', action='store_true', default=False, 146 parser.add_option('--list', dest='list', action='store_true', default=False,
126 help="list files") 147 help="list files")
148 parser.add_option('-l', '--list-destinations', dest='list_destinations',
149 action='store_true', default=False,
150 help='list preconfigured destinations')
127 parser.add_option('--validate', dest='validate', # TODO unused 151 parser.add_option('--validate', dest='validate', # TODO unused
128 action='store_true', default=False, 152 action='store_true', default=False,
129 help="validate the rendering but don't output") 153 help="validate the rendering but don't output")
130 options, manifests = parser.parse_args(args) 154 options, manifests = parser.parse_args(args)
131 155
132 # print help if no manifests given 156 # print help if no manifests given
133 if not args: 157 if not args:
134 parser.print_help() 158 parser.print_help()
135 parser.exit() 159 parser.exit()
136 160
137 # get base url 161 # print preconfigured destinations if asked
162 if options.list_destinations:
163 for key in sorted(destinations.keys()):
164 print '%s: %s' % (key, destinations[key])
165 return # you're done
166
167 # get destination
138 assert options.dest 168 assert options.dest
169 if options.dest in destinations:
170 options.dest = destinations[options.dest]
139 if options.dest.startswith('file://'): 171 if options.dest.startswith('file://'):
140 options.dest = options.dest[len('file://'):] 172 options.dest = options.dest[len('file://'):]
141 if '://' not in options.dest: 173 if '://' not in options.dest:
142 options.dest = os.path.abspath(options.dest) 174 options.dest = os.path.abspath(options.dest)
143 175
161 193
162 # check credentials 194 # check credentials
163 assert options.user and options.password, "Please supply your --user and --password" 195 assert options.user and options.password, "Please supply your --user and --password"
164 196
165 # upload the files 197 # upload the files
166 # XXX just use curl for now
167 import subprocess
168 for src, dest in files: 198 for src, dest in files:
169 dest += '/contents?edittime=now' 199 post(render(src), dest, options.user, options.password)
170 print src, dest
171 200
172 else: 201 else:
173 202
174 # ensure a directory 203 # ensure a directory
175 if os.path.exists(options.dest): 204 if os.path.exists(options.dest):
197 else: 226 else:
198 os.makedirs(dirname) 227 os.makedirs(dirname)
199 228
200 # render 229 # render
201 f = file(dest, 'w') 230 f = file(dest, 'w')
202 buffer = markdown.Markdown().convert(file(src).read()) 231 f.write(render(src))
203 f.write(buffer)
204 f.close() 232 f.close()
205 233
206 # print out destination directory if using the temporary default 234 # print out destination directory if using the temporary default
207 if options.dest == default_dir: 235 if options.dest == default_dir:
208 print "Files rendered to\n%s" % default_dir 236 print "Files rendered to\n%s" % default_dir