blob: a901a28712a3f69ef3fff00dcc3bc9eccb2d5f77 [file] [log] [blame]
Guido van Rossumea31ea21997-05-26 05:43:29 +00001"""Generic FAQ Wizard.
Guido van Rossum1677e5b1997-05-26 00:07:18 +00002
Guido van Rossumea31ea21997-05-26 05:43:29 +00003This is a CGI program that maintains a user-editable FAQ. It uses RCS
4to keep track of changes to individual FAQ entries. It is fully
5configurable; everything you might want to change when using this
6program to maintain some other FAQ than the Python FAQ is contained in
7the configuration module, faqconf.py.
8
9Note that this is not an executable script; it's an importable module.
10The actual script in cgi-bin minimal; it's appended at the end of this
11file as a string literal.
12
13"""
14
15import sys, string, time, os, stat, regex, cgi, faqconf
16from faqconf import * # This imports all uppercase names
Guido van Rossum1677e5b1997-05-26 00:07:18 +000017
18class FileError:
19 def __init__(self, file):
20 self.file = file
21
22class InvalidFile(FileError):
23 pass
24
Guido van Rossumea31ea21997-05-26 05:43:29 +000025class NoSuchSection(FileError):
26 def __init__(self, section):
27 FileError.__init__(self, NEWFILENAME %(section, 1))
28 self.section = section
29
Guido van Rossum1677e5b1997-05-26 00:07:18 +000030class NoSuchFile(FileError):
31 def __init__(self, file, why=None):
32 FileError.__init__(self, file)
33 self.why = why
34
Guido van Rossumea31ea21997-05-26 05:43:29 +000035def replace(s, old, new):
36 try:
37 return string.replace(s, old, new)
38 except AttributeError:
39 return string.join(string.split(s, old), new)
40
41def escape(s):
42 s = replace(s, '&', '&')
43 s = replace(s, '<', '&lt;')
44 s = replace(s, '>', '&gt')
45 return s
46
Guido van Rossum1677e5b1997-05-26 00:07:18 +000047def escapeq(s):
48 s = escape(s)
Guido van Rossumea31ea21997-05-26 05:43:29 +000049 s = replace(s, '"', '&quot;')
Guido van Rossum1677e5b1997-05-26 00:07:18 +000050 return s
51
Guido van Rossumea31ea21997-05-26 05:43:29 +000052def _interpolate(format, args, kw):
53 try:
54 quote = kw['_quote']
55 except KeyError:
56 quote = 1
57 d = (kw,) + args + (faqconf.__dict__,)
58 m = MagicDict(d, quote)
59 return format % m
Guido van Rossum1677e5b1997-05-26 00:07:18 +000060
Guido van Rossumea31ea21997-05-26 05:43:29 +000061def interpolate(format, *args, **kw):
62 return _interpolate(format, args, kw)
63
64def emit(format, *args, **kw):
65 try:
66 f = kw['_file']
67 except KeyError:
68 f = sys.stdout
69 f.write(_interpolate(format, args, kw))
Guido van Rossum1677e5b1997-05-26 00:07:18 +000070
71translate_prog = None
72
73def translate(text):
74 global translate_prog
75 if not translate_prog:
Guido van Rossum1677e5b1997-05-26 00:07:18 +000076 url = '\(http\|ftp\)://[^ \t\r\n]*'
77 email = '\<[-a-zA-Z0-9._]+@[-a-zA-Z0-9._]+'
Guido van Rossumea31ea21997-05-26 05:43:29 +000078 translate_prog = prog = regex.compile(url + '\|' + email)
Guido van Rossum1677e5b1997-05-26 00:07:18 +000079 else:
80 prog = translate_prog
81 i = 0
82 list = []
83 while 1:
84 j = prog.search(text, i)
85 if j < 0:
86 break
Guido van Rossumea31ea21997-05-26 05:43:29 +000087 list.append(escape(text[i:j]))
Guido van Rossum1677e5b1997-05-26 00:07:18 +000088 i = j
89 url = prog.group(0)
Guido van Rossumea31ea21997-05-26 05:43:29 +000090 while url[-1] in ');:,.?\'"':
Guido van Rossum1677e5b1997-05-26 00:07:18 +000091 url = url[:-1]
92 url = escape(url)
93 if ':' in url:
94 repl = '<A HREF="%s">%s</A>' % (url, url)
95 else:
96 repl = '<A HREF="mailto:%s">&lt;%s&gt;</A>' % (url, url)
97 list.append(repl)
98 i = i + len(url)
99 j = len(text)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000100 list.append(escape(text[i:j]))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000101 return string.join(list, '')
102
103emphasize_prog = None
104
105def emphasize(line):
106 global emphasize_prog
107 import regsub
108 if not emphasize_prog:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000109 pat = '\*\([a-zA-Z]+\)\*'
110 emphasize_prog = regex.compile(pat)
111 return regsub.gsub(emphasize_prog, '<I>\\1</I>', line)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000112
113def load_cookies():
114 if not os.environ.has_key('HTTP_COOKIE'):
115 return {}
116 raw = os.environ['HTTP_COOKIE']
117 words = map(string.strip, string.split(raw, ';'))
118 cookies = {}
119 for word in words:
120 i = string.find(word, '=')
121 if i >= 0:
122 key, value = word[:i], word[i+1:]
123 cookies[key] = value
124 return cookies
125
126def load_my_cookie():
127 cookies = load_cookies()
128 try:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000129 value = cookies[COOKIE_NAME]
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000130 except KeyError:
131 return {}
132 import urllib
133 value = urllib.unquote(value)
134 words = string.split(value, '/')
135 while len(words) < 3:
136 words.append('')
137 author = string.join(words[:-2], '/')
138 email = words[-2]
139 password = words[-1]
140 return {'author': author,
141 'email': email,
142 'password': password}
143
Guido van Rossumea31ea21997-05-26 05:43:29 +0000144def send_my_cookie(ui):
145 name = COOKIE_NAME
146 value = "%s/%s/%s" % (ui.author, ui.email, ui.password)
147 import urllib
148 value = urllib.quote(value)
149 now = time.time()
150 then = now + COOKIE_LIFETIME
151 gmt = time.gmtime(then)
152 print "Set-Cookie: %s=%s; path=/cgi-bin/;" % (name, value),
153 print time.strftime("expires=%a, %d-%b-%x %X GMT", gmt)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000154
Guido van Rossumea31ea21997-05-26 05:43:29 +0000155class MagicDict:
156
157 def __init__(self, d, quote):
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000158 self.__d = d
Guido van Rossumea31ea21997-05-26 05:43:29 +0000159 self.__quote = quote
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000160
161 def __getitem__(self, key):
162 for d in self.__d:
163 try:
164 value = d[key]
165 if value:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000166 value = str(value)
167 if self.__quote:
168 value = escapeq(value)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000169 return value
170 except KeyError:
171 pass
Guido van Rossumea31ea21997-05-26 05:43:29 +0000172 return ''
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000173
174class UserInput:
175
176 def __init__(self):
177 self.__form = cgi.FieldStorage()
178
179 def __getattr__(self, name):
180 if name[0] == '_':
181 raise AttributeError
182 try:
183 value = self.__form[name].value
184 except (TypeError, KeyError):
185 value = ''
186 else:
187 value = string.strip(value)
188 setattr(self, name, value)
189 return value
190
191 def __getitem__(self, key):
192 return getattr(self, key)
193
Guido van Rossumea31ea21997-05-26 05:43:29 +0000194class FaqEntry:
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000195
Guido van Rossumea31ea21997-05-26 05:43:29 +0000196 def __init__(self, fp, file, sec_num):
197 self.file = file
198 self.sec, self.num = sec_num
199 if fp:
200 import rfc822
201 self.__headers = rfc822.Message(fp)
202 self.body = string.strip(fp.read())
203 else:
204 self.__headers = {'title': "%d.%d. " % sec_num}
205 self.body = ''
206
207 def __getattr__(self, name):
208 if name[0] == '_':
209 raise AttributeError
210 key = string.join(string.split(name, '_'), '-')
211 try:
212 value = self.__headers[key]
213 except KeyError:
214 value = ''
215 setattr(self, name, value)
216 return value
217
218 def __getitem__(self, key):
219 return getattr(self, key)
220
221 def load_version(self):
222 command = interpolate(SH_RLOG_H, self)
223 p = os.popen(command)
224 version = ''
225 while 1:
226 line = p.readline()
227 if not line:
228 break
229 if line[:5] == 'head:':
230 version = string.strip(line[5:])
231 p.close()
232 self.version = version
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000233
234 def show(self, edit=1):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000235 emit(ENTRY_HEADER, self)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000236 pre = 0
Guido van Rossumea31ea21997-05-26 05:43:29 +0000237 for line in string.split(self.body, '\n'):
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000238 if not string.strip(line):
239 if pre:
240 print '</PRE>'
241 pre = 0
242 else:
243 print '<P>'
244 else:
245 if line[0] not in string.whitespace:
246 if pre:
247 print '</PRE>'
248 pre = 0
249 else:
250 if not pre:
251 print '<PRE>'
252 pre = 1
253 if '/' in line or '@' in line:
254 line = translate(line)
255 elif '<' in line or '&' in line:
256 line = escape(line)
257 if not pre and '*' in line:
258 line = emphasize(line)
259 print line
260 if pre:
261 print '</PRE>'
262 pre = 0
263 if edit:
264 print '<P>'
Guido van Rossumea31ea21997-05-26 05:43:29 +0000265 emit(ENTRY_FOOTER, self)
266 if self.last_changed_date:
267 emit(ENTRY_LOGINFO, self)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000268 print '<P>'
269
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000270class FaqDir:
271
272 entryclass = FaqEntry
273
Guido van Rossumea31ea21997-05-26 05:43:29 +0000274 __okprog = regex.compile(OKFILENAME)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000275
276 def __init__(self, dir=os.curdir):
277 self.__dir = dir
278 self.__files = None
279
280 def __fill(self):
281 if self.__files is not None:
282 return
283 self.__files = files = []
284 okprog = self.__okprog
285 for file in os.listdir(self.__dir):
286 if okprog.match(file) >= 0:
287 files.append(file)
288 files.sort()
289
290 def good(self, file):
291 return self.__okprog.match(file) >= 0
292
293 def parse(self, file):
294 if not self.good(file):
295 return None
296 sec, num = self.__okprog.group(1, 2)
297 return string.atoi(sec), string.atoi(num)
298
299 def roulette(self):
300 self.__fill()
301 import whrandom
302 return whrandom.choice(self.__files)
303
304 def list(self):
305 # XXX Caller shouldn't modify result
306 self.__fill()
307 return self.__files
308
309 def open(self, file):
310 sec_num = self.parse(file)
311 if not sec_num:
312 raise InvalidFile(file)
313 try:
314 fp = open(file)
315 except IOError, msg:
316 raise NoSuchFile(file, msg)
317 try:
318 return self.entryclass(fp, file, sec_num)
319 finally:
320 fp.close()
321
322 def show(self, file, edit=1):
323 self.open(file).show(edit=edit)
324
Guido van Rossumea31ea21997-05-26 05:43:29 +0000325 def new(self, section):
326 if not SECTION_TITLES.has_key(section):
327 raise NoSuchSection(section)
328 maxnum = 0
329 for file in self.list():
330 sec, num = self.parse(file)
331 if sec == section:
332 maxnum = max(maxnum, num)
333 sec_num = (section, maxnum+1)
334 file = NEWFILENAME % sec_num
335 return self.entryclass(None, file, sec_num)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000336
337class FaqWizard:
338
339 def __init__(self):
340 self.ui = UserInput()
341 self.dir = FaqDir()
342
343 def go(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000344 print 'Content-type: text/html'
345 req = self.ui.req or 'home'
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000346 mname = 'do_%s' % req
347 try:
348 meth = getattr(self, mname)
349 except AttributeError:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000350 self.error("Bad request type %s." % `req`)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000351 else:
352 try:
353 meth()
354 except InvalidFile, exc:
355 self.error("Invalid entry file name %s" % exc.file)
356 except NoSuchFile, exc:
357 self.error("No entry with file name %s" % exc.file)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000358 except NoSuchSection, exc:
359 self.error("No section number %s" % exc.section)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000360 self.epilogue()
361
362 def error(self, message, **kw):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000363 self.prologue(T_ERROR)
364 emit(message, kw)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000365
366 def prologue(self, title, entry=None, **kw):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000367 emit(PROLOGUE, entry, kwdict=kw, title=escape(title))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000368
369 def epilogue(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000370 emit(EPILOGUE)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000371
372 def do_home(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000373 self.prologue(T_HOME)
374 emit(HOME)
375
376 def do_debug(self):
377 self.prologue("FAQ Wizard Debugging")
378 form = cgi.FieldStorage()
379 cgi.print_form(form)
380 cgi.print_environ(os.environ)
381 cgi.print_directory()
382 cgi.print_arguments()
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000383
384 def do_search(self):
385 query = self.ui.query
386 if not query:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000387 self.error("Empty query string!")
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000388 return
Guido van Rossumea31ea21997-05-26 05:43:29 +0000389 self.prologue(T_SEARCH)
390 if self.ui.querytype != 'regex':
391 for c in '\\.[]?+^$*':
392 if c in query:
393 query = replace(query, c, '\\'+c)
394 if self.ui.casefold == 'no':
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000395 p = regex.compile(query)
396 else:
397 p = regex.compile(query, regex.casefold)
398 hits = []
399 for file in self.dir.list():
400 try:
401 entry = self.dir.open(file)
402 except FileError:
403 constants
404 if p.search(entry.title) >= 0 or p.search(entry.body) >= 0:
405 hits.append(file)
406 if not hits:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000407 emit(NO_HITS, self.ui, count=0)
408 elif len(hits) <= MAXHITS:
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000409 if len(hits) == 1:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000410 emit(ONE_HIT, count=1)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000411 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000412 emit(FEW_HITS, count=len(hits))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000413 self.format_all(hits)
414 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000415 emit(MANY_HITS, count=len(hits))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000416 self.format_index(hits)
417
418 def do_all(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000419 self.prologue(T_ALL)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000420 files = self.dir.list()
421 self.last_changed(files)
422 self.format_all(files)
423
424 def do_compat(self):
425 files = self.dir.list()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000426 emit(COMPAT)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000427 self.last_changed(files)
428 self.format_all(files, edit=0)
429 sys.exit(0)
430
431 def last_changed(self, files):
432 latest = 0
433 for file in files:
434 try:
435 st = os.stat(file)
436 except os.error:
437 continue
438 mtime = st[stat.ST_MTIME]
439 if mtime > latest:
440 latest = mtime
Guido van Rossumea31ea21997-05-26 05:43:29 +0000441 print time.strftime(LAST_CHANGED,
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000442 time.localtime(time.time()))
443
444 def format_all(self, files, edit=1):
445 for file in files:
446 self.dir.show(file, edit=edit)
447
448 def do_index(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000449 self.prologue(T_INDEX)
450 self.format_index(self.dir.list(), add=1)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000451
Guido van Rossumea31ea21997-05-26 05:43:29 +0000452 def format_index(self, files, add=0):
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000453 sec = 0
454 for file in files:
455 try:
456 entry = self.dir.open(file)
457 except NoSuchFile:
458 continue
459 if entry.sec != sec:
460 if sec:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000461 if add:
462 emit(INDEX_ADDSECTION, sec=sec)
463 emit(INDEX_ENDSECTION, sec=sec)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000464 sec = entry.sec
Guido van Rossumea31ea21997-05-26 05:43:29 +0000465 emit(INDEX_SECTION, sec=sec, title=SECTION_TITLES[sec])
466 emit(INDEX_ENTRY, entry)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000467 if sec:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000468 if add:
469 emit(INDEX_ADDSECTION, sec=sec)
470 emit(INDEX_ENDSECTION, sec=sec)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000471
472 def do_recent(self):
473 if not self.ui.days:
474 days = 1
475 else:
476 days = string.atof(self.ui.days)
477 now = time.time()
478 try:
479 cutoff = now - days * 24 * 3600
480 except OverflowError:
481 cutoff = 0
482 list = []
483 for file in self.dir.list():
484 try:
485 st = os.stat(file)
486 except os.error:
487 continue
488 mtime = st[stat.ST_MTIME]
489 if mtime >= cutoff:
490 list.append((mtime, file))
491 list.sort()
492 list.reverse()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000493 self.prologue(T_RECENT)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000494 if days <= 1:
495 period = "%.2g hours" % (days*24)
496 else:
497 period = "%.6g days" % days
498 if not list:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000499 emit(NO_RECENT, period=period)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000500 elif len(list) == 1:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000501 emit(ONE_RECENT, period=period)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000502 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000503 emit(SOME_RECENT, period=period, count=len(list))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000504 self.format_all(map(lambda (mtime, file): file, list))
Guido van Rossumea31ea21997-05-26 05:43:29 +0000505 emit(TAIL_RECENT)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000506
507 def do_roulette(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000508 self.prologue(T_ROULETTE)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000509 file = self.dir.roulette()
510 self.dir.show(file)
511
512 def do_help(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000513 self.prologue(T_HELP)
514 emit(HELP)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000515
516 def do_show(self):
517 entry = self.dir.open(self.ui.file)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000518 self.prologue(T_SHOW)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000519 entry.show()
520
521 def do_add(self):
522 self.prologue(T_ADD)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000523 emit(ADD_HEAD)
524 sections = SECTION_TITLES.items()
525 sections.sort()
526 for section, title in sections:
527 emit(ADD_SECTION, section=section, title=title)
528 emit(ADD_TAIL)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000529
530 def do_delete(self):
531 self.prologue(T_DELETE)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000532 emit(DELETE)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000533
534 def do_log(self):
535 entry = self.dir.open(self.ui.file)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000536 self.prologue(T_LOG, entry)
537 emit(LOG, entry)
538 self.rlog(interpolate(SH_RLOG, entry), entry)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000539
540 def rlog(self, command, entry=None):
541 output = os.popen(command).read()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000542 sys.stdout.write('<PRE>')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000543 athead = 0
Guido van Rossumea31ea21997-05-26 05:43:29 +0000544 lines = string.split(output, '\n')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000545 while lines and not lines[-1]:
546 del lines[-1]
547 if lines:
548 line = lines[-1]
549 if line[:1] == '=' and len(line) >= 40 and \
550 line == line[0]*len(line):
551 del lines[-1]
552 for line in lines:
553 if entry and athead and line[:9] == 'revision ':
554 rev = string.strip(line[9:])
Guido van Rossumea31ea21997-05-26 05:43:29 +0000555 if rev != '1.1':
556 emit(DIFFLINK, entry, rev=rev, line=line)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000557 else:
558 print line
559 athead = 0
560 else:
561 athead = 0
562 if line[:1] == '-' and len(line) >= 20 and \
563 line == len(line) * line[0]:
564 athead = 1
Guido van Rossumea31ea21997-05-26 05:43:29 +0000565 sys.stdout.write('<HR>')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000566 else:
567 print line
Guido van Rossumea31ea21997-05-26 05:43:29 +0000568 print '</PRE>'
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000569
570 def do_diff(self):
571 entry = self.dir.open(self.ui.file)
572 rev = self.ui.rev
573 r = regex.compile(
Guido van Rossumea31ea21997-05-26 05:43:29 +0000574 '^\([1-9][0-9]?[0-9]?\)\.\([1-9][0-9]?[0-9]?[0-9]?\)$')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000575 if r.match(rev) < 0:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000576 self.error("Invalid revision number: %s." % `rev`)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000577 [major, minor] = map(string.atoi, r.group(1, 2))
578 if minor == 1:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000579 self.error("No previous revision.")
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000580 return
Guido van Rossumea31ea21997-05-26 05:43:29 +0000581 prev = '%d.%d' % (major, minor-1)
582 self.prologue(T_DIFF, entry)
583 self.shell(interpolate(SH_RDIFF, entry, rev=rev, prev=prev))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000584
585 def shell(self, command):
586 output = os.popen(command).read()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000587 sys.stdout.write('<PRE>')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000588 print escape(output)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000589 print '</PRE>'
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000590
591 def do_new(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000592 entry = self.dir.new(section=string.atoi(self.ui.section))
593 entry.version = '*new*'
594 self.prologue(T_EDIT)
595 emit(EDITHEAD)
596 emit(EDITFORM1, entry, editversion=entry.version)
597 emit(EDITFORM2, entry, load_my_cookie())
598 emit(EDITFORM3)
599 entry.show(edit=0)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000600
601 def do_edit(self):
602 entry = self.dir.open(self.ui.file)
603 entry.load_version()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000604 self.prologue(T_EDIT)
605 emit(EDITHEAD)
606 emit(EDITFORM1, entry, editversion=entry.version)
607 emit(EDITFORM2, entry, load_my_cookie())
608 emit(EDITFORM3)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000609 entry.show(edit=0)
610
611 def do_review(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000612 send_my_cookie(self.ui)
613 if self.ui.editversion == '*new*':
614 sec, num = self.dir.parse(self.ui.file)
615 entry = self.dir.new(section=sec)
616 entry.version = "*new*"
617 if entry.file != self.ui.file:
618 self.error("Commit version conflict!")
619 emit(NEWCONFLICT, self.ui, sec=sec, num=num)
620 return
621 else:
622 entry = self.dir.open(self.ui.file)
623 entry.load_version()
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000624 # Check that the FAQ entry number didn't change
625 if string.split(self.ui.title)[:1] != string.split(entry.title)[:1]:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000626 self.error("Don't change the entry number please!")
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000627 return
628 # Check that the edited version is the current version
629 if entry.version != self.ui.editversion:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000630 self.error("Commit version conflict!")
631 emit(VERSIONCONFLICT, entry, self.ui)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000632 return
Guido van Rossumea31ea21997-05-26 05:43:29 +0000633 commit_ok = ((not PASSWORD
634 or self.ui.password == PASSWORD)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000635 and self.ui.author
636 and '@' in self.ui.email
637 and self.ui.log)
638 if self.ui.commit:
639 if not commit_ok:
640 self.cantcommit()
641 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000642 self.commit(entry)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000643 return
Guido van Rossumea31ea21997-05-26 05:43:29 +0000644 self.prologue(T_REVIEW)
645 emit(REVIEWHEAD)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000646 entry.body = self.ui.body
647 entry.title = self.ui.title
648 entry.show(edit=0)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000649 emit(EDITFORM1, self.ui, entry)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000650 if commit_ok:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000651 emit(COMMIT)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000652 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000653 emit(NOCOMMIT)
654 emit(EDITFORM2, self.ui, entry, load_my_cookie())
655 emit(EDITFORM3)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000656
657 def cantcommit(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000658 self.prologue(T_CANTCOMMIT)
659 print CANTCOMMIT_HEAD
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000660 if not self.ui.passwd:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000661 emit(NEED_PASSWD)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000662 if not self.ui.log:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000663 emit(NEED_LOG)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000664 if not self.ui.author:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000665 emit(NEED_AUTHOR)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000666 if not self.ui.email:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000667 emit(NEED_EMAIL)
668 print CANTCOMMIT_TAIL
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000669
Guido van Rossumea31ea21997-05-26 05:43:29 +0000670 def commit(self, entry):
671 file = entry.file
672 # Normalize line endings in body
673 if '\r' in self.ui.body:
674 import regsub
675 self.ui.body = regsub.gsub('\r\n?', '\n', self.ui.body)
676 # Normalize whitespace in title
677 self.ui.title = string.join(string.split(self.ui.title))
678 # Check that there were any changes
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000679 if self.ui.body == entry.body and self.ui.title == entry.title:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000680 self.error("You didn't make any changes!")
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000681 return
682 # XXX Should lock here
683 try:
684 os.unlink(file)
685 except os.error:
686 pass
687 try:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000688 f = open(file, 'w')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000689 except IOError, why:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000690 self.error(CANTWRITE, file=file, why=why)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000691 return
692 date = time.ctime(time.time())
Guido van Rossumea31ea21997-05-26 05:43:29 +0000693 emit(FILEHEADER, self.ui, os.environ, date=date, _file=f, _quote=0)
694 f.write('\n')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000695 f.write(self.ui.body)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000696 f.write('\n')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000697 f.close()
698
699 import tempfile
700 tfn = tempfile.mktemp()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000701 f = open(tfn, 'w')
702 emit(LOGHEADER, self.ui, os.environ, date=date, _file=f)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000703 f.close()
704
705 command = interpolate(
Guido van Rossumea31ea21997-05-26 05:43:29 +0000706 SH_LOCK + '\n' + SH_CHECKIN,
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000707 file=file, tfn=tfn)
708
709 p = os.popen(command)
710 output = p.read()
711 sts = p.close()
712 # XXX Should unlock here
713 if not sts:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000714 self.prologue(T_COMMITTED)
715 emit(COMMITTED)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000716 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000717 self.error(T_COMMITFAILED)
718 emit(COMMITFAILED, sts=sts)
719 print '<PRE>%s</PRE>' % escape(output)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000720
721 try:
722 os.unlink(tfn)
723 except os.error:
724 pass
725
726 entry = self.dir.open(file)
727 entry.show()
728
729wiz = FaqWizard()
730wiz.go()
731
Guido van Rossumea31ea21997-05-26 05:43:29 +0000732# This bootstrap script should be placed in your cgi-bin directory.
733# You only need to edit the first two lines: change
734# /usr/local/bin/python to where your Python interpreter lives change
735# the value for FAQDIR to where your FAQ lives. The faqwiz.py and
736# faqconf.py files should live there, too.
737
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000738BOOTSTRAP = """\
739#! /usr/local/bin/python
740FAQDIR = "/usr/people/guido/python/FAQ"
Guido van Rossumea31ea21997-05-26 05:43:29 +0000741import sys, os
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000742os.chdir(FAQDIR)
743sys.path.insert(0, FAQDIR)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000744import faqwiz
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000745"""