blob: e721425190772e08754b76284eeaa6986710fdad [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 Rossum7a241071997-05-26 19:46:56 +000017now = time.time()
Guido van Rossum1677e5b1997-05-26 00:07:18 +000018
19class FileError:
20 def __init__(self, file):
21 self.file = file
22
23class InvalidFile(FileError):
24 pass
25
Guido van Rossumea31ea21997-05-26 05:43:29 +000026class NoSuchSection(FileError):
27 def __init__(self, section):
28 FileError.__init__(self, NEWFILENAME %(section, 1))
29 self.section = section
30
Guido van Rossum1677e5b1997-05-26 00:07:18 +000031class NoSuchFile(FileError):
32 def __init__(self, file, why=None):
33 FileError.__init__(self, file)
34 self.why = why
35
Guido van Rossumea31ea21997-05-26 05:43:29 +000036def replace(s, old, new):
37 try:
38 return string.replace(s, old, new)
39 except AttributeError:
40 return string.join(string.split(s, old), new)
41
42def escape(s):
43 s = replace(s, '&', '&')
44 s = replace(s, '<', '&lt;')
45 s = replace(s, '>', '&gt')
46 return s
47
Guido van Rossum1677e5b1997-05-26 00:07:18 +000048def escapeq(s):
49 s = escape(s)
Guido van Rossumea31ea21997-05-26 05:43:29 +000050 s = replace(s, '"', '&quot;')
Guido van Rossum1677e5b1997-05-26 00:07:18 +000051 return s
52
Guido van Rossumea31ea21997-05-26 05:43:29 +000053def _interpolate(format, args, kw):
54 try:
55 quote = kw['_quote']
56 except KeyError:
57 quote = 1
58 d = (kw,) + args + (faqconf.__dict__,)
59 m = MagicDict(d, quote)
60 return format % m
Guido van Rossum1677e5b1997-05-26 00:07:18 +000061
Guido van Rossumea31ea21997-05-26 05:43:29 +000062def interpolate(format, *args, **kw):
63 return _interpolate(format, args, kw)
64
65def emit(format, *args, **kw):
66 try:
67 f = kw['_file']
68 except KeyError:
69 f = sys.stdout
70 f.write(_interpolate(format, args, kw))
Guido van Rossum1677e5b1997-05-26 00:07:18 +000071
72translate_prog = None
73
74def translate(text):
75 global translate_prog
76 if not translate_prog:
Guido van Rossum1677e5b1997-05-26 00:07:18 +000077 url = '\(http\|ftp\)://[^ \t\r\n]*'
78 email = '\<[-a-zA-Z0-9._]+@[-a-zA-Z0-9._]+'
Guido van Rossumea31ea21997-05-26 05:43:29 +000079 translate_prog = prog = regex.compile(url + '\|' + email)
Guido van Rossum1677e5b1997-05-26 00:07:18 +000080 else:
81 prog = translate_prog
82 i = 0
83 list = []
84 while 1:
85 j = prog.search(text, i)
86 if j < 0:
87 break
Guido van Rossumea31ea21997-05-26 05:43:29 +000088 list.append(escape(text[i:j]))
Guido van Rossum1677e5b1997-05-26 00:07:18 +000089 i = j
90 url = prog.group(0)
Guido van Rossumea31ea21997-05-26 05:43:29 +000091 while url[-1] in ');:,.?\'"':
Guido van Rossum1677e5b1997-05-26 00:07:18 +000092 url = url[:-1]
93 url = escape(url)
94 if ':' in url:
95 repl = '<A HREF="%s">%s</A>' % (url, url)
96 else:
97 repl = '<A HREF="mailto:%s">&lt;%s&gt;</A>' % (url, url)
98 list.append(repl)
99 i = i + len(url)
100 j = len(text)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000101 list.append(escape(text[i:j]))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000102 return string.join(list, '')
103
104emphasize_prog = None
105
106def emphasize(line):
107 global emphasize_prog
108 import regsub
109 if not emphasize_prog:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000110 pat = '\*\([a-zA-Z]+\)\*'
111 emphasize_prog = regex.compile(pat)
112 return regsub.gsub(emphasize_prog, '<I>\\1</I>', line)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000113
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000114revparse_prog = None
115
116def revparse(rev):
117 global revparse_prog
118 if not revparse_prog:
119 revparse_prog = regex.compile(
120 '^\([1-9][0-9]?[0-9]?\)\.\([1-9][0-9]?[0-9]?[0-9]?\)$')
121 if revparse_prog.match(rev) < 0:
122 return None
123 [major, minor] = map(string.atoi, revparse_prog.group(1, 2))
124 return major, minor
125
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000126def load_cookies():
127 if not os.environ.has_key('HTTP_COOKIE'):
128 return {}
129 raw = os.environ['HTTP_COOKIE']
130 words = map(string.strip, string.split(raw, ';'))
131 cookies = {}
132 for word in words:
133 i = string.find(word, '=')
134 if i >= 0:
135 key, value = word[:i], word[i+1:]
136 cookies[key] = value
137 return cookies
138
139def load_my_cookie():
140 cookies = load_cookies()
141 try:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000142 value = cookies[COOKIE_NAME]
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000143 except KeyError:
144 return {}
145 import urllib
146 value = urllib.unquote(value)
147 words = string.split(value, '/')
148 while len(words) < 3:
149 words.append('')
150 author = string.join(words[:-2], '/')
151 email = words[-2]
152 password = words[-1]
153 return {'author': author,
154 'email': email,
155 'password': password}
156
Guido van Rossumea31ea21997-05-26 05:43:29 +0000157def send_my_cookie(ui):
158 name = COOKIE_NAME
159 value = "%s/%s/%s" % (ui.author, ui.email, ui.password)
160 import urllib
161 value = urllib.quote(value)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000162 then = now + COOKIE_LIFETIME
163 gmt = time.gmtime(then)
164 print "Set-Cookie: %s=%s; path=/cgi-bin/;" % (name, value),
165 print time.strftime("expires=%a, %d-%b-%x %X GMT", gmt)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000166
Guido van Rossumea31ea21997-05-26 05:43:29 +0000167class MagicDict:
168
169 def __init__(self, d, quote):
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000170 self.__d = d
Guido van Rossumea31ea21997-05-26 05:43:29 +0000171 self.__quote = quote
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000172
173 def __getitem__(self, key):
174 for d in self.__d:
175 try:
176 value = d[key]
177 if value:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000178 value = str(value)
179 if self.__quote:
180 value = escapeq(value)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000181 return value
182 except KeyError:
183 pass
Guido van Rossumea31ea21997-05-26 05:43:29 +0000184 return ''
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000185
186class UserInput:
187
188 def __init__(self):
189 self.__form = cgi.FieldStorage()
190
191 def __getattr__(self, name):
192 if name[0] == '_':
193 raise AttributeError
194 try:
195 value = self.__form[name].value
196 except (TypeError, KeyError):
197 value = ''
198 else:
199 value = string.strip(value)
200 setattr(self, name, value)
201 return value
202
203 def __getitem__(self, key):
204 return getattr(self, key)
205
Guido van Rossumea31ea21997-05-26 05:43:29 +0000206class FaqEntry:
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000207
Guido van Rossumea31ea21997-05-26 05:43:29 +0000208 def __init__(self, fp, file, sec_num):
209 self.file = file
210 self.sec, self.num = sec_num
211 if fp:
212 import rfc822
213 self.__headers = rfc822.Message(fp)
214 self.body = string.strip(fp.read())
215 else:
216 self.__headers = {'title': "%d.%d. " % sec_num}
217 self.body = ''
218
219 def __getattr__(self, name):
220 if name[0] == '_':
221 raise AttributeError
222 key = string.join(string.split(name, '_'), '-')
223 try:
224 value = self.__headers[key]
225 except KeyError:
226 value = ''
227 setattr(self, name, value)
228 return value
229
230 def __getitem__(self, key):
231 return getattr(self, key)
232
233 def load_version(self):
234 command = interpolate(SH_RLOG_H, self)
235 p = os.popen(command)
236 version = ''
237 while 1:
238 line = p.readline()
239 if not line:
240 break
241 if line[:5] == 'head:':
242 version = string.strip(line[5:])
243 p.close()
244 self.version = version
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000245
Guido van Rossum7a241071997-05-26 19:46:56 +0000246 def getmtime(self):
247 if not self.last_changed_date:
248 return 0
249 try:
250 return os.stat(self.file)[stat.ST_MTIME]
251 except os.error:
252 return 0
253
254 def emit_marks(self):
255 mtime = self.getmtime()
256 if mtime >= now - DT_VERY_RECENT:
257 emit(MARK_VERY_RECENT, self)
258 elif mtime >= now - DT_RECENT:
259 emit(MARK_RECENT, self)
260
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000261 def show(self, edit=1):
Guido van Rossum7a241071997-05-26 19:46:56 +0000262 emit(ENTRY_HEADER1, self)
263 self.emit_marks()
264 emit(ENTRY_HEADER2, self)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000265 pre = 0
Guido van Rossumea31ea21997-05-26 05:43:29 +0000266 for line in string.split(self.body, '\n'):
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000267 if not string.strip(line):
268 if pre:
269 print '</PRE>'
270 pre = 0
271 else:
272 print '<P>'
273 else:
274 if line[0] not in string.whitespace:
275 if pre:
276 print '</PRE>'
277 pre = 0
278 else:
279 if not pre:
280 print '<PRE>'
281 pre = 1
282 if '/' in line or '@' in line:
283 line = translate(line)
284 elif '<' in line or '&' in line:
285 line = escape(line)
286 if not pre and '*' in line:
287 line = emphasize(line)
288 print line
289 if pre:
290 print '</PRE>'
291 pre = 0
292 if edit:
293 print '<P>'
Guido van Rossumea31ea21997-05-26 05:43:29 +0000294 emit(ENTRY_FOOTER, self)
295 if self.last_changed_date:
296 emit(ENTRY_LOGINFO, self)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000297 print '<P>'
298
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000299class FaqDir:
300
301 entryclass = FaqEntry
302
Guido van Rossumea31ea21997-05-26 05:43:29 +0000303 __okprog = regex.compile(OKFILENAME)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000304
305 def __init__(self, dir=os.curdir):
306 self.__dir = dir
307 self.__files = None
308
309 def __fill(self):
310 if self.__files is not None:
311 return
312 self.__files = files = []
313 okprog = self.__okprog
314 for file in os.listdir(self.__dir):
315 if okprog.match(file) >= 0:
316 files.append(file)
317 files.sort()
318
319 def good(self, file):
320 return self.__okprog.match(file) >= 0
321
322 def parse(self, file):
323 if not self.good(file):
324 return None
325 sec, num = self.__okprog.group(1, 2)
326 return string.atoi(sec), string.atoi(num)
327
328 def roulette(self):
329 self.__fill()
330 import whrandom
Guido van Rossum72a342f1997-05-30 11:58:21 +0000331 if not self.__files: return None
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000332 return whrandom.choice(self.__files)
333
334 def list(self):
335 # XXX Caller shouldn't modify result
336 self.__fill()
337 return self.__files
338
339 def open(self, file):
340 sec_num = self.parse(file)
341 if not sec_num:
342 raise InvalidFile(file)
343 try:
344 fp = open(file)
345 except IOError, msg:
346 raise NoSuchFile(file, msg)
347 try:
348 return self.entryclass(fp, file, sec_num)
349 finally:
350 fp.close()
351
352 def show(self, file, edit=1):
353 self.open(file).show(edit=edit)
354
Guido van Rossumea31ea21997-05-26 05:43:29 +0000355 def new(self, section):
356 if not SECTION_TITLES.has_key(section):
357 raise NoSuchSection(section)
358 maxnum = 0
359 for file in self.list():
360 sec, num = self.parse(file)
361 if sec == section:
362 maxnum = max(maxnum, num)
363 sec_num = (section, maxnum+1)
364 file = NEWFILENAME % sec_num
365 return self.entryclass(None, file, sec_num)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000366
367class FaqWizard:
368
369 def __init__(self):
370 self.ui = UserInput()
371 self.dir = FaqDir()
372
373 def go(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000374 print 'Content-type: text/html'
375 req = self.ui.req or 'home'
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000376 mname = 'do_%s' % req
377 try:
378 meth = getattr(self, mname)
379 except AttributeError:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000380 self.error("Bad request type %s." % `req`)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000381 else:
382 try:
383 meth()
384 except InvalidFile, exc:
385 self.error("Invalid entry file name %s" % exc.file)
386 except NoSuchFile, exc:
387 self.error("No entry with file name %s" % exc.file)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000388 except NoSuchSection, exc:
389 self.error("No section number %s" % exc.section)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000390 self.epilogue()
391
392 def error(self, message, **kw):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000393 self.prologue(T_ERROR)
394 emit(message, kw)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000395
396 def prologue(self, title, entry=None, **kw):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000397 emit(PROLOGUE, entry, kwdict=kw, title=escape(title))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000398
399 def epilogue(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000400 emit(EPILOGUE)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000401
402 def do_home(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000403 self.prologue(T_HOME)
404 emit(HOME)
405
406 def do_debug(self):
407 self.prologue("FAQ Wizard Debugging")
408 form = cgi.FieldStorage()
409 cgi.print_form(form)
410 cgi.print_environ(os.environ)
411 cgi.print_directory()
412 cgi.print_arguments()
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000413
414 def do_search(self):
415 query = self.ui.query
416 if not query:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000417 self.error("Empty query string!")
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000418 return
Guido van Rossum8cde0b41997-05-26 16:35:46 +0000419 if self.ui.querytype == 'simple':
Guido van Rossumea31ea21997-05-26 05:43:29 +0000420 for c in '\\.[]?+^$*':
421 if c in query:
422 query = replace(query, c, '\\'+c)
Guido van Rossum8cde0b41997-05-26 16:35:46 +0000423 queries = [query]
424 elif self.ui.querytype in ('anykeywords', 'allkeywords'):
425 import regsub
426 words = string.split(regsub.gsub('[^a-zA-Z0-9]+', ' ', query))
427 if not words:
428 self.error("No keywords specified!")
429 return
430 words = map(lambda w: '\<%s\>' % w, words)
431 if self.ui.querytype[:3] == 'any':
432 queries = [string.join(words, '\|')]
433 else:
434 queries = words
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000435 else:
Guido van Rossum8cde0b41997-05-26 16:35:46 +0000436 # Default to regex
437 queries = [query]
438 self.prologue(T_SEARCH)
439 progs = []
440 for query in queries:
441 if self.ui.casefold == 'no':
442 p = regex.compile(query)
443 else:
444 p = regex.compile(query, regex.casefold)
445 progs.append(p)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000446 hits = []
447 for file in self.dir.list():
448 try:
449 entry = self.dir.open(file)
450 except FileError:
451 constants
Guido van Rossum8cde0b41997-05-26 16:35:46 +0000452 for p in progs:
453 if p.search(entry.title) < 0 and p.search(entry.body) < 0:
454 break
455 else:
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000456 hits.append(file)
457 if not hits:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000458 emit(NO_HITS, self.ui, count=0)
459 elif len(hits) <= MAXHITS:
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000460 if len(hits) == 1:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000461 emit(ONE_HIT, count=1)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000462 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000463 emit(FEW_HITS, count=len(hits))
Guido van Rossum21c4b5f1997-05-26 06:28:40 +0000464 self.format_all(hits, headers=0)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000465 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000466 emit(MANY_HITS, count=len(hits))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000467 self.format_index(hits)
468
469 def do_all(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000470 self.prologue(T_ALL)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000471 files = self.dir.list()
472 self.last_changed(files)
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000473 self.format_index(files, localrefs=1)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000474 self.format_all(files)
475
476 def do_compat(self):
477 files = self.dir.list()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000478 emit(COMPAT)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000479 self.last_changed(files)
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000480 self.format_index(files, localrefs=1)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000481 self.format_all(files, edit=0)
Guido van Rossum7a241071997-05-26 19:46:56 +0000482 sys.exit(0) # XXX Hack to suppress epilogue
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000483
484 def last_changed(self, files):
485 latest = 0
486 for file in files:
Guido van Rossum7a241071997-05-26 19:46:56 +0000487 entry = self.dir.open(file)
488 if entry:
489 mtime = mtime = entry.getmtime()
490 if mtime > latest:
491 latest = mtime
492 print time.strftime(LAST_CHANGED, time.localtime(now))
493 emit(EXPLAIN_MARKS)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000494
Guido van Rossum21c4b5f1997-05-26 06:28:40 +0000495 def format_all(self, files, edit=1, headers=1):
496 sec = 0
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000497 for file in files:
Guido van Rossum21c4b5f1997-05-26 06:28:40 +0000498 try:
499 entry = self.dir.open(file)
500 except NoSuchFile:
501 continue
502 if headers and entry.sec != sec:
503 sec = entry.sec
504 try:
505 title = SECTION_TITLES[sec]
506 except KeyError:
507 title = "Untitled"
508 emit("\n<HR>\n<H1>%(sec)s. %(title)s</H1>\n",
509 sec=sec, title=title)
510 entry.show(edit=edit)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000511
512 def do_index(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000513 self.prologue(T_INDEX)
Guido van Rossum7a241071997-05-26 19:46:56 +0000514 files = self.dir.list()
515 self.last_changed(files)
516 self.format_index(files, add=1)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000517
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000518 def format_index(self, files, add=0, localrefs=0):
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000519 sec = 0
520 for file in files:
521 try:
522 entry = self.dir.open(file)
523 except NoSuchFile:
524 continue
525 if entry.sec != sec:
526 if sec:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000527 if add:
528 emit(INDEX_ADDSECTION, sec=sec)
529 emit(INDEX_ENDSECTION, sec=sec)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000530 sec = entry.sec
Guido van Rossum21c4b5f1997-05-26 06:28:40 +0000531 try:
532 title = SECTION_TITLES[sec]
533 except KeyError:
534 title = "Untitled"
535 emit(INDEX_SECTION, sec=sec, title=title)
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000536 if localrefs:
537 emit(LOCAL_ENTRY, entry)
538 else:
539 emit(INDEX_ENTRY, entry)
Guido van Rossum7a241071997-05-26 19:46:56 +0000540 entry.emit_marks()
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000541 if sec:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000542 if add:
543 emit(INDEX_ADDSECTION, sec=sec)
544 emit(INDEX_ENDSECTION, sec=sec)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000545
546 def do_recent(self):
547 if not self.ui.days:
548 days = 1
549 else:
550 days = string.atof(self.ui.days)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000551 try:
552 cutoff = now - days * 24 * 3600
553 except OverflowError:
554 cutoff = 0
555 list = []
556 for file in self.dir.list():
Guido van Rossum7a241071997-05-26 19:46:56 +0000557 entry = self.dir.open(file)
558 if not entry:
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000559 continue
Guido van Rossum7a241071997-05-26 19:46:56 +0000560 mtime = entry.getmtime()
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000561 if mtime >= cutoff:
562 list.append((mtime, file))
563 list.sort()
564 list.reverse()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000565 self.prologue(T_RECENT)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000566 if days <= 1:
567 period = "%.2g hours" % (days*24)
568 else:
569 period = "%.6g days" % days
570 if not list:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000571 emit(NO_RECENT, period=period)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000572 elif len(list) == 1:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000573 emit(ONE_RECENT, period=period)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000574 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000575 emit(SOME_RECENT, period=period, count=len(list))
Guido van Rossum1f047721997-05-26 16:02:00 +0000576 self.format_all(map(lambda (mtime, file): file, list), headers=0)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000577 emit(TAIL_RECENT)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000578
579 def do_roulette(self):
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000580 file = self.dir.roulette()
Guido van Rossum72a342f1997-05-30 11:58:21 +0000581 if not file:
582 self.error("No entries.")
583 return
584 self.prologue(T_ROULETTE)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000585 self.dir.show(file)
586
587 def do_help(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000588 self.prologue(T_HELP)
589 emit(HELP)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000590
591 def do_show(self):
592 entry = self.dir.open(self.ui.file)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000593 self.prologue(T_SHOW)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000594 entry.show()
595
596 def do_add(self):
597 self.prologue(T_ADD)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000598 emit(ADD_HEAD)
599 sections = SECTION_TITLES.items()
600 sections.sort()
601 for section, title in sections:
602 emit(ADD_SECTION, section=section, title=title)
603 emit(ADD_TAIL)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000604
605 def do_delete(self):
606 self.prologue(T_DELETE)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000607 emit(DELETE)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000608
609 def do_log(self):
610 entry = self.dir.open(self.ui.file)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000611 self.prologue(T_LOG, entry)
612 emit(LOG, entry)
613 self.rlog(interpolate(SH_RLOG, entry), entry)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000614
615 def rlog(self, command, entry=None):
616 output = os.popen(command).read()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000617 sys.stdout.write('<PRE>')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000618 athead = 0
Guido van Rossumea31ea21997-05-26 05:43:29 +0000619 lines = string.split(output, '\n')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000620 while lines and not lines[-1]:
621 del lines[-1]
622 if lines:
623 line = lines[-1]
624 if line[:1] == '=' and len(line) >= 40 and \
625 line == line[0]*len(line):
626 del lines[-1]
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000627 headrev = None
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000628 for line in lines:
629 if entry and athead and line[:9] == 'revision ':
630 rev = string.strip(line[9:])
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000631 mami = revparse(rev)
632 if not mami:
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000633 print line
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000634 else:
635 emit(REVISIONLINK, entry, rev=rev, line=line)
636 if mami[1] > 1:
637 prev = "%d.%d" % (mami[0], mami[1]-1)
638 emit(DIFFLINK, entry, prev=prev, rev=rev)
639 if headrev:
640 emit(DIFFLINK, entry, prev=rev, rev=headrev)
641 else:
642 headrev = rev
643 print
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000644 athead = 0
645 else:
646 athead = 0
647 if line[:1] == '-' and len(line) >= 20 and \
648 line == len(line) * line[0]:
649 athead = 1
Guido van Rossumea31ea21997-05-26 05:43:29 +0000650 sys.stdout.write('<HR>')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000651 else:
652 print line
Guido van Rossumea31ea21997-05-26 05:43:29 +0000653 print '</PRE>'
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000654
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000655 def do_revision(self):
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000656 entry = self.dir.open(self.ui.file)
657 rev = self.ui.rev
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000658 mami = revparse(rev)
659 if not mami:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000660 self.error("Invalid revision number: %s." % `rev`)
Guido van Rossum8bc49c81997-05-26 19:10:37 +0000661 self.prologue(T_REVISION, entry)
662 self.shell(interpolate(SH_REVISION, entry, rev=rev))
663
664 def do_diff(self):
665 entry = self.dir.open(self.ui.file)
666 prev = self.ui.prev
667 rev = self.ui.rev
668 mami = revparse(rev)
669 if not mami:
670 self.error("Invalid revision number: %s." % `rev`)
671 if prev:
672 if not revparse(prev):
673 self.error("Invalid previous revision number: %s." % `prev`)
674 else:
675 prev = '%d.%d' % (mami[0], mami[1])
Guido van Rossumea31ea21997-05-26 05:43:29 +0000676 self.prologue(T_DIFF, entry)
677 self.shell(interpolate(SH_RDIFF, entry, rev=rev, prev=prev))
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000678
679 def shell(self, command):
680 output = os.popen(command).read()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000681 sys.stdout.write('<PRE>')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000682 print escape(output)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000683 print '</PRE>'
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000684
685 def do_new(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000686 entry = self.dir.new(section=string.atoi(self.ui.section))
687 entry.version = '*new*'
688 self.prologue(T_EDIT)
689 emit(EDITHEAD)
690 emit(EDITFORM1, entry, editversion=entry.version)
691 emit(EDITFORM2, entry, load_my_cookie())
692 emit(EDITFORM3)
693 entry.show(edit=0)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000694
695 def do_edit(self):
696 entry = self.dir.open(self.ui.file)
697 entry.load_version()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000698 self.prologue(T_EDIT)
699 emit(EDITHEAD)
700 emit(EDITFORM1, entry, editversion=entry.version)
701 emit(EDITFORM2, entry, load_my_cookie())
702 emit(EDITFORM3)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000703 entry.show(edit=0)
704
705 def do_review(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000706 send_my_cookie(self.ui)
707 if self.ui.editversion == '*new*':
708 sec, num = self.dir.parse(self.ui.file)
709 entry = self.dir.new(section=sec)
710 entry.version = "*new*"
711 if entry.file != self.ui.file:
712 self.error("Commit version conflict!")
713 emit(NEWCONFLICT, self.ui, sec=sec, num=num)
714 return
715 else:
716 entry = self.dir.open(self.ui.file)
717 entry.load_version()
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000718 # Check that the FAQ entry number didn't change
719 if string.split(self.ui.title)[:1] != string.split(entry.title)[:1]:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000720 self.error("Don't change the entry number please!")
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000721 return
722 # Check that the edited version is the current version
723 if entry.version != self.ui.editversion:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000724 self.error("Commit version conflict!")
725 emit(VERSIONCONFLICT, entry, self.ui)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000726 return
Guido van Rossumea31ea21997-05-26 05:43:29 +0000727 commit_ok = ((not PASSWORD
728 or self.ui.password == PASSWORD)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000729 and self.ui.author
730 and '@' in self.ui.email
731 and self.ui.log)
732 if self.ui.commit:
733 if not commit_ok:
734 self.cantcommit()
735 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000736 self.commit(entry)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000737 return
Guido van Rossumea31ea21997-05-26 05:43:29 +0000738 self.prologue(T_REVIEW)
739 emit(REVIEWHEAD)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000740 entry.body = self.ui.body
741 entry.title = self.ui.title
742 entry.show(edit=0)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000743 emit(EDITFORM1, self.ui, entry)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000744 if commit_ok:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000745 emit(COMMIT)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000746 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000747 emit(NOCOMMIT)
748 emit(EDITFORM2, self.ui, entry, load_my_cookie())
749 emit(EDITFORM3)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000750
751 def cantcommit(self):
Guido van Rossumea31ea21997-05-26 05:43:29 +0000752 self.prologue(T_CANTCOMMIT)
753 print CANTCOMMIT_HEAD
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000754 if not self.ui.passwd:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000755 emit(NEED_PASSWD)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000756 if not self.ui.log:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000757 emit(NEED_LOG)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000758 if not self.ui.author:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000759 emit(NEED_AUTHOR)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000760 if not self.ui.email:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000761 emit(NEED_EMAIL)
762 print CANTCOMMIT_TAIL
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000763
Guido van Rossumea31ea21997-05-26 05:43:29 +0000764 def commit(self, entry):
765 file = entry.file
766 # Normalize line endings in body
767 if '\r' in self.ui.body:
768 import regsub
769 self.ui.body = regsub.gsub('\r\n?', '\n', self.ui.body)
770 # Normalize whitespace in title
771 self.ui.title = string.join(string.split(self.ui.title))
772 # Check that there were any changes
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000773 if self.ui.body == entry.body and self.ui.title == entry.title:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000774 self.error("You didn't make any changes!")
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000775 return
776 # XXX Should lock here
777 try:
778 os.unlink(file)
779 except os.error:
780 pass
781 try:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000782 f = open(file, 'w')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000783 except IOError, why:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000784 self.error(CANTWRITE, file=file, why=why)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000785 return
Guido van Rossum7a241071997-05-26 19:46:56 +0000786 date = time.ctime(now)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000787 emit(FILEHEADER, self.ui, os.environ, date=date, _file=f, _quote=0)
788 f.write('\n')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000789 f.write(self.ui.body)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000790 f.write('\n')
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000791 f.close()
792
793 import tempfile
794 tfn = tempfile.mktemp()
Guido van Rossumea31ea21997-05-26 05:43:29 +0000795 f = open(tfn, 'w')
796 emit(LOGHEADER, self.ui, os.environ, date=date, _file=f)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000797 f.close()
798
799 command = interpolate(
Guido van Rossumea31ea21997-05-26 05:43:29 +0000800 SH_LOCK + '\n' + SH_CHECKIN,
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000801 file=file, tfn=tfn)
802
803 p = os.popen(command)
804 output = p.read()
805 sts = p.close()
806 # XXX Should unlock here
807 if not sts:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000808 self.prologue(T_COMMITTED)
809 emit(COMMITTED)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000810 else:
Guido van Rossumea31ea21997-05-26 05:43:29 +0000811 self.error(T_COMMITFAILED)
812 emit(COMMITFAILED, sts=sts)
813 print '<PRE>%s</PRE>' % escape(output)
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000814
815 try:
816 os.unlink(tfn)
817 except os.error:
818 pass
819
820 entry = self.dir.open(file)
821 entry.show()
822
823wiz = FaqWizard()
824wiz.go()
825
Guido van Rossumea31ea21997-05-26 05:43:29 +0000826# This bootstrap script should be placed in your cgi-bin directory.
827# You only need to edit the first two lines: change
828# /usr/local/bin/python to where your Python interpreter lives change
829# the value for FAQDIR to where your FAQ lives. The faqwiz.py and
830# faqconf.py files should live there, too.
831
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000832BOOTSTRAP = """\
833#! /usr/local/bin/python
834FAQDIR = "/usr/people/guido/python/FAQ"
Guido van Rossumea31ea21997-05-26 05:43:29 +0000835import sys, os
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000836os.chdir(FAQDIR)
837sys.path.insert(0, FAQDIR)
Guido van Rossumea31ea21997-05-26 05:43:29 +0000838import faqwiz
Guido van Rossum1677e5b1997-05-26 00:07:18 +0000839"""