blob: 47aa3b7e569196f6dd21a7b25db3cdfca37d4211 [file] [log] [blame]
Guido van Rossum1677e5b1997-05-26 00:07:18 +00001import sys, string, time, os, stat, regex, cgi, faqconf
2
3from cgi import escape
4
5class FileError:
6 def __init__(self, file):
7 self.file = file
8
9class InvalidFile(FileError):
10 pass
11
12class NoSuchFile(FileError):
13 def __init__(self, file, why=None):
14 FileError.__init__(self, file)
15 self.why = why
16
17def escapeq(s):
18 s = escape(s)
19 import regsub
20 s = regsub.gsub('"', '"', s)
21 return s
22
23def interpolate(format, entry={}, kwdict={}, **kw):
24 s = format % MDict(kw, entry, kwdict, faqconf.__dict__)
25 return s
26
27def emit(format, entry={}, kwdict={}, file=sys.stdout, **kw):
28 s = format % MDict(kw, entry, kwdict, faqconf.__dict__)
29 file.write(s)
30
31translate_prog = None
32
33def translate(text):
34 global translate_prog
35 if not translate_prog:
36 import regex
37 url = '\(http\|ftp\)://[^ \t\r\n]*'
38 email = '\<[-a-zA-Z0-9._]+@[-a-zA-Z0-9._]+'
39 translate_prog = prog = regex.compile(url + "\|" + email)
40 else:
41 prog = translate_prog
42 i = 0
43 list = []
44 while 1:
45 j = prog.search(text, i)
46 if j < 0:
47 break
48 list.append(cgi.escape(text[i:j]))
49 i = j
50 url = prog.group(0)
51 while url[-1] in ");:,.?'\"":
52 url = url[:-1]
53 url = escape(url)
54 if ':' in url:
55 repl = '<A HREF="%s">%s</A>' % (url, url)
56 else:
57 repl = '<A HREF="mailto:%s">&lt;%s&gt;</A>' % (url, url)
58 list.append(repl)
59 i = i + len(url)
60 j = len(text)
61 list.append(cgi.escape(text[i:j]))
62 return string.join(list, '')
63
64emphasize_prog = None
65
66def emphasize(line):
67 global emphasize_prog
68 import regsub
69 if not emphasize_prog:
70 import regex
71 pat = "\*\([a-zA-Z]+\)\*"
72 emphasize_prog = prog = regex.compile(pat)
73 else:
74 prog = emphasize_prog
75 return regsub.gsub(prog, "<I>\\1</I>", line)
76
77def load_cookies():
78 if not os.environ.has_key('HTTP_COOKIE'):
79 return {}
80 raw = os.environ['HTTP_COOKIE']
81 words = map(string.strip, string.split(raw, ';'))
82 cookies = {}
83 for word in words:
84 i = string.find(word, '=')
85 if i >= 0:
86 key, value = word[:i], word[i+1:]
87 cookies[key] = value
88 return cookies
89
90def load_my_cookie():
91 cookies = load_cookies()
92 try:
93 value = cookies[faqconf.COOKIE_NAME]
94 except KeyError:
95 return {}
96 import urllib
97 value = urllib.unquote(value)
98 words = string.split(value, '/')
99 while len(words) < 3:
100 words.append('')
101 author = string.join(words[:-2], '/')
102 email = words[-2]
103 password = words[-1]
104 return {'author': author,
105 'email': email,
106 'password': password}
107
108class MDict:
109
110 def __init__(self, *d):
111 self.__d = d
112
113 def __getitem__(self, key):
114 for d in self.__d:
115 try:
116 value = d[key]
117 if value:
118 return value
119 except KeyError:
120 pass
121 return ""
122
123class UserInput:
124
125 def __init__(self):
126 self.__form = cgi.FieldStorage()
127
128 def __getattr__(self, name):
129 if name[0] == '_':
130 raise AttributeError
131 try:
132 value = self.__form[name].value
133 except (TypeError, KeyError):
134 value = ''
135 else:
136 value = string.strip(value)
137 setattr(self, name, value)
138 return value
139
140 def __getitem__(self, key):
141 return getattr(self, key)
142
143class FaqFormatter:
144
145 def __init__(self, entry):
146 self.entry = entry
147
148 def show(self, edit=1):
149 entry = self.entry
150 print "<HR>"
151 print "<H2>%s</H2>" % escape(entry.title)
152 pre = 0
153 for line in string.split(entry.body, '\n'):
154 if not string.strip(line):
155 if pre:
156 print '</PRE>'
157 pre = 0
158 else:
159 print '<P>'
160 else:
161 if line[0] not in string.whitespace:
162 if pre:
163 print '</PRE>'
164 pre = 0
165 else:
166 if not pre:
167 print '<PRE>'
168 pre = 1
169 if '/' in line or '@' in line:
170 line = translate(line)
171 elif '<' in line or '&' in line:
172 line = escape(line)
173 if not pre and '*' in line:
174 line = emphasize(line)
175 print line
176 if pre:
177 print '</PRE>'
178 pre = 0
179 if edit:
180 print '<P>'
181 emit(faqconf.ENTRY_FOOTER, self.entry)
182 if self.entry.last_changed_date:
183 emit(faqconf.ENTRY_LOGINFO, self.entry)
184 print '<P>'
185
186class FaqEntry:
187
188 formatterclass = FaqFormatter
189
190 def __init__(self, fp, file, sec_num):
191 import rfc822
192 self.file = file
193 self.sec, self.num = sec_num
194 self.__headers = rfc822.Message(fp)
195 self.body = string.strip(fp.read())
196
197 def __getattr__(self, name):
198 if name[0] == '_':
199 raise AttributeError
200 key = string.join(string.split(name, '_'), '-')
201 try:
202 value = self.__headers[key]
203 except KeyError:
204 value = ''
205 setattr(self, name, value)
206 return value
207
208 def __getitem__(self, key):
209 return getattr(self, key)
210
211 def show(self, edit=1):
212 self.formatterclass(self).show(edit=edit)
213
214 def load_version(self):
215 command = interpolate(faqconf.SH_RLOG_H, self)
216 p = os.popen(command)
217 version = ""
218 while 1:
219 line = p.readline()
220 if not line:
221 break
222 if line[:5] == 'head:':
223 version = string.strip(line[5:])
224 p.close()
225 self.version = version
226
227class FaqDir:
228
229 entryclass = FaqEntry
230
231 __okprog = regex.compile('^faq\([0-9][0-9]\)\.\([0-9][0-9][0-9]\)\.htp$')
232
233 def __init__(self, dir=os.curdir):
234 self.__dir = dir
235 self.__files = None
236
237 def __fill(self):
238 if self.__files is not None:
239 return
240 self.__files = files = []
241 okprog = self.__okprog
242 for file in os.listdir(self.__dir):
243 if okprog.match(file) >= 0:
244 files.append(file)
245 files.sort()
246
247 def good(self, file):
248 return self.__okprog.match(file) >= 0
249
250 def parse(self, file):
251 if not self.good(file):
252 return None
253 sec, num = self.__okprog.group(1, 2)
254 return string.atoi(sec), string.atoi(num)
255
256 def roulette(self):
257 self.__fill()
258 import whrandom
259 return whrandom.choice(self.__files)
260
261 def list(self):
262 # XXX Caller shouldn't modify result
263 self.__fill()
264 return self.__files
265
266 def open(self, file):
267 sec_num = self.parse(file)
268 if not sec_num:
269 raise InvalidFile(file)
270 try:
271 fp = open(file)
272 except IOError, msg:
273 raise NoSuchFile(file, msg)
274 try:
275 return self.entryclass(fp, file, sec_num)
276 finally:
277 fp.close()
278
279 def show(self, file, edit=1):
280 self.open(file).show(edit=edit)
281
282 def new(self, sec):
283 XXX
284
285class FaqWizard:
286
287 def __init__(self):
288 self.ui = UserInput()
289 self.dir = FaqDir()
290
291 def go(self):
292 print "Content-type: text/html"
293 req = self.ui.req or "home"
294 mname = 'do_%s' % req
295 try:
296 meth = getattr(self, mname)
297 except AttributeError:
298 self.error("Bad request %s" % `req`)
299 else:
300 try:
301 meth()
302 except InvalidFile, exc:
303 self.error("Invalid entry file name %s" % exc.file)
304 except NoSuchFile, exc:
305 self.error("No entry with file name %s" % exc.file)
306 self.epilogue()
307
308 def error(self, message, **kw):
309 self.prologue(faqconf.T_ERROR)
310 apply(emit, (message,), kw)
311
312 def prologue(self, title, entry=None, **kw):
313 emit(faqconf.PROLOGUE, entry, kwdict=kw, title=escape(title))
314
315 def epilogue(self):
316 emit(faqconf.EPILOGUE)
317
318 def do_home(self):
319 self.prologue(faqconf.T_HOME)
320 emit(faqconf.HOME)
321
322 def do_search(self):
323 query = self.ui.query
324 if not query:
325 self.error("No query string")
326 return
327 self.prologue(faqconf.T_SEARCH)
328 if self.ui.casefold == "no":
329 p = regex.compile(query)
330 else:
331 p = regex.compile(query, regex.casefold)
332 hits = []
333 for file in self.dir.list():
334 try:
335 entry = self.dir.open(file)
336 except FileError:
337 constants
338 if p.search(entry.title) >= 0 or p.search(entry.body) >= 0:
339 hits.append(file)
340 if not hits:
341 emit(faqconf.NO_HITS, count=0)
342 elif len(hits) <= faqconf.MAXHITS:
343 if len(hits) == 1:
344 emit(faqconf.ONE_HIT, count=1)
345 else:
346 emit(faqconf.FEW_HITS, count=len(hits))
347 self.format_all(hits)
348 else:
349 emit(faqconf.MANY_HITS, count=len(hits))
350 self.format_index(hits)
351
352 def do_all(self):
353 self.prologue(faqconf.T_ALL)
354 files = self.dir.list()
355 self.last_changed(files)
356 self.format_all(files)
357
358 def do_compat(self):
359 files = self.dir.list()
360 emit(faqconf.COMPAT)
361 self.last_changed(files)
362 self.format_all(files, edit=0)
363 sys.exit(0)
364
365 def last_changed(self, files):
366 latest = 0
367 for file in files:
368 try:
369 st = os.stat(file)
370 except os.error:
371 continue
372 mtime = st[stat.ST_MTIME]
373 if mtime > latest:
374 latest = mtime
375 print time.strftime(faqconf.LAST_CHANGED,
376 time.localtime(time.time()))
377
378 def format_all(self, files, edit=1):
379 for file in files:
380 self.dir.show(file, edit=edit)
381
382 def do_index(self):
383 self.prologue(faqconf.T_INDEX)
384 self.format_index(self.dir.list())
385
386 def format_index(self, files):
387 sec = 0
388 for file in files:
389 try:
390 entry = self.dir.open(file)
391 except NoSuchFile:
392 continue
393 if entry.sec != sec:
394 if sec:
395 emit(faqconf.INDEX_ENDSECTION, sec=sec)
396 sec = entry.sec
397 emit(faqconf.INDEX_SECTION,
398 sec=sec,
399 title=faqconf.SECTION_TITLES[sec])
400 emit(faqconf.INDEX_ENTRY, entry)
401 if sec:
402 emit(faqconf.INDEX_ENDSECTION, sec=sec)
403
404 def do_recent(self):
405 if not self.ui.days:
406 days = 1
407 else:
408 days = string.atof(self.ui.days)
409 now = time.time()
410 try:
411 cutoff = now - days * 24 * 3600
412 except OverflowError:
413 cutoff = 0
414 list = []
415 for file in self.dir.list():
416 try:
417 st = os.stat(file)
418 except os.error:
419 continue
420 mtime = st[stat.ST_MTIME]
421 if mtime >= cutoff:
422 list.append((mtime, file))
423 list.sort()
424 list.reverse()
425 self.prologue(faqconf.T_RECENT)
426 if days <= 1:
427 period = "%.2g hours" % (days*24)
428 else:
429 period = "%.6g days" % days
430 if not list:
431 emit(faqconf.NO_RECENT, period=period)
432 elif len(list) == 1:
433 emit(faqconf.ONE_RECENT, period=period)
434 else:
435 emit(faqconf.SOME_RECENT, period=period, count=len(list))
436 self.format_all(map(lambda (mtime, file): file, list))
437 emit(faqconf.TAIL_RECENT)
438
439 def do_roulette(self):
440 self.prologue(faqconf.T_ROULETTE)
441 file = self.dir.roulette()
442 self.dir.show(file)
443
444 def do_help(self):
445 self.prologue(faqconf.T_HELP)
446 emit(faqconf.HELP)
447
448 def do_show(self):
449 entry = self.dir.open(self.ui.file)
450 self.prologue("Python FAQ Entry")
451 entry.show()
452
453 def do_add(self):
454 self.prologue(T_ADD)
455 self.error("Not yet implemented")
456
457 def do_delete(self):
458 self.prologue(T_DELETE)
459 self.error("Not yet implemented")
460
461 def do_log(self):
462 entry = self.dir.open(self.ui.file)
463 self.prologue(faqconf.T_LOG, entry)
464 emit(faqconf.LOG, entry)
465 self.rlog(interpolate(faqconf.SH_RLOG, entry), entry)
466
467 def rlog(self, command, entry=None):
468 output = os.popen(command).read()
469 sys.stdout.write("<PRE>")
470 athead = 0
471 lines = string.split(output, "\n")
472 while lines and not lines[-1]:
473 del lines[-1]
474 if lines:
475 line = lines[-1]
476 if line[:1] == '=' and len(line) >= 40 and \
477 line == line[0]*len(line):
478 del lines[-1]
479 for line in lines:
480 if entry and athead and line[:9] == 'revision ':
481 rev = string.strip(line[9:])
482 if rev != "1.1":
483 emit(faqconf.DIFFLINK, entry, rev=rev, line=line)
484 else:
485 print line
486 athead = 0
487 else:
488 athead = 0
489 if line[:1] == '-' and len(line) >= 20 and \
490 line == len(line) * line[0]:
491 athead = 1
492 sys.stdout.write("<HR>")
493 else:
494 print line
495 print "</PRE>"
496
497 def do_diff(self):
498 entry = self.dir.open(self.ui.file)
499 rev = self.ui.rev
500 r = regex.compile(
501 "^\([1-9][0-9]?[0-9]?\)\.\([1-9][0-9]?[0-9]?[0-9]?\)$")
502 if r.match(rev) < 0:
503 self.error("Invalid revision number: %s" % `rev`)
504 [major, minor] = map(string.atoi, r.group(1, 2))
505 if minor == 1:
506 self.error("No previous revision")
507 return
508 prev = "%d.%d" % (major, minor-1)
509 self.prologue(faqconf.T_DIFF, entry)
510 self.shell(interpolate(faqconf.SH_RDIFF, entry, rev=rev, prev=prev))
511
512 def shell(self, command):
513 output = os.popen(command).read()
514 sys.stdout.write("<PRE>")
515 print escape(output)
516 print "</PRE>"
517
518 def do_new(self):
519 editor = FaqEditor(self.ui, self.dir.new(self.file))
520 self.prologue(faqconf.T_NEW)
521 self.error("Not yet implemented")
522
523 def do_edit(self):
524 entry = self.dir.open(self.ui.file)
525 entry.load_version()
526 self.prologue(faqconf.T_EDIT)
527 emit(faqconf.EDITHEAD)
528 emit(faqconf.EDITFORM1, entry, editversion=entry.version)
529 emit(faqconf.EDITFORM2, entry, load_my_cookie(), log=self.ui.log)
530 emit(faqconf.EDITFORM3)
531 entry.show(edit=0)
532
533 def do_review(self):
534 entry = self.dir.open(self.ui.file)
535 entry.load_version()
536 # Check that the FAQ entry number didn't change
537 if string.split(self.ui.title)[:1] != string.split(entry.title)[:1]:
538 self.error("Don't change the FAQ entry number please.")
539 return
540 # Check that the edited version is the current version
541 if entry.version != self.ui.editversion:
542 self.error("Version conflict.")
543 emit(faqconf.VERSIONCONFLICT, entry, self.ui)
544 return
545 commit_ok = ((not faqconf.PASSWORD
546 or self.ui.password == faqconf.PASSWORD)
547 and self.ui.author
548 and '@' in self.ui.email
549 and self.ui.log)
550 if self.ui.commit:
551 if not commit_ok:
552 self.cantcommit()
553 else:
554 self.commit()
555 return
556 self.prologue(faqconf.T_REVIEW)
557 emit(faqconf.REVIEWHEAD)
558 entry.body = self.ui.body
559 entry.title = self.ui.title
560 entry.show(edit=0)
561 emit(faqconf.EDITFORM1, entry, self.ui)
562 if commit_ok:
563 emit(faqconf.COMMIT)
564 else:
565 emit(faqconf.NOCOMMIT)
566 emit(faqconf.EDITFORM2, entry, load_my_cookie(), log=self.ui.log)
567 emit(faqconf.EDITFORM3)
568
569 def cantcommit(self):
570 self.prologue(faqconf.T_CANTCOMMIT)
571 print faqconf.CANTCOMMIT_HEAD
572 if not self.ui.passwd:
573 emit(faqconf.NEED_PASSWD)
574 if not self.ui.log:
575 emit(faqconf.NEED_LOG)
576 if not self.ui.author:
577 emit(faqconf.NEED_AUTHOR)
578 if not self.ui.email:
579 emit(faqconf.NEED_EMAIL)
580 print faqconf.CANTCOMMIT_TAIL
581
582 def commit(self):
583 file = self.ui.file
584 entry = self.dir.open(file)
585 # Chech that there were any changes
586 if self.ui.body == entry.body and self.ui.title == entry.title:
587 self.error("No changes.")
588 return
589 # XXX Should lock here
590 try:
591 os.unlink(file)
592 except os.error:
593 pass
594 try:
595 f = open(file, "w")
596 except IOError, why:
597 self.error(faqconf.CANTWRITE, file=file, why=why)
598 return
599 date = time.ctime(time.time())
600 emit(faqconf.FILEHEADER, self.ui, os.environ, date=date, file=f)
601 f.write("\n")
602 f.write(self.ui.body)
603 f.write("\n")
604 f.close()
605
606 import tempfile
607 tfn = tempfile.mktemp()
608 f = open(tfn, "w")
609 emit(faqconf.LOGHEADER, self.ui, os.environ, date=date, file=f)
610 f.close()
611
612 command = interpolate(
613 faqconf.SH_LOCK + "\n" + faqconf.SH_CHECKIN,
614 file=file, tfn=tfn)
615
616 p = os.popen(command)
617 output = p.read()
618 sts = p.close()
619 # XXX Should unlock here
620 if not sts:
621 self.prologue(faqconf.T_COMMITTED)
622 emit(faqconf.COMMITTED)
623 else:
624 self.error(faqconf.T_COMMITFAILED)
625 emit(faqconf.COMMITFAILED, sts=sts)
626 print "<PRE>%s</PRE>" % cgi.escape(output)
627
628 try:
629 os.unlink(tfn)
630 except os.error:
631 pass
632
633 entry = self.dir.open(file)
634 entry.show()
635
636wiz = FaqWizard()
637wiz.go()
638
639BOOTSTRAP = """\
640#! /usr/local/bin/python
641FAQDIR = "/usr/people/guido/python/FAQ"
642
643# This bootstrap script should be placed in your cgi-bin directory.
644# You only need to edit the first two lines (above): Change
645# /usr/local/bin/python to where your Python interpreter lives (you
646# can't use /usr/bin/env here!); change FAQDIR to where your FAQ
647# lives. The faqwiz.py and faqconf.py files should live there, too.
648
649import posix
650t1 = posix.times()
651import os, sys, time, operator
652os.chdir(FAQDIR)
653sys.path.insert(0, FAQDIR)
654try:
655 import faqwiz
656except SystemExit, n:
657 sys.exit(n)
658except:
659 t, v, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
660 print
661 import cgi
662 cgi.print_exception(t, v, tb)
663t2 = posix.times()
664fmt = "<BR>(times: user %.3g, sys %.3g, ch-user %.3g, ch-sys %.3g, real %.3g)"
665print fmt % tuple(map(operator.sub, t2, t1))
666"""