Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 1 | """Generic FAQ Wizard. |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 3 | This is a CGI program that maintains a user-editable FAQ. It uses RCS |
| 4 | to keep track of changes to individual FAQ entries. It is fully |
| 5 | configurable; everything you might want to change when using this |
| 6 | program to maintain some other FAQ than the Python FAQ is contained in |
| 7 | the configuration module, faqconf.py. |
| 8 | |
| 9 | Note that this is not an executable script; it's an importable module. |
Guido van Rossum | f1ead1a | 1997-08-28 02:38:01 +0000 | [diff] [blame] | 10 | The actual script to place in cgi-bin is faqw.py. |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 11 | |
| 12 | """ |
| 13 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 14 | import sys, time, os, stat, re, cgi, faqconf |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 15 | from faqconf import * # This imports all uppercase names |
Guido van Rossum | 7a24107 | 1997-05-26 19:46:56 +0000 | [diff] [blame] | 16 | now = time.time() |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 17 | |
| 18 | class FileError: |
| 19 | def __init__(self, file): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 20 | self.file = file |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 21 | |
| 22 | class InvalidFile(FileError): |
| 23 | pass |
| 24 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 25 | class NoSuchSection(FileError): |
| 26 | def __init__(self, section): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 27 | FileError.__init__(self, NEWFILENAME %(section, 1)) |
| 28 | self.section = section |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 30 | class NoSuchFile(FileError): |
| 31 | def __init__(self, file, why=None): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 32 | FileError.__init__(self, file) |
| 33 | self.why = why |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 35 | def escape(s): |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 36 | s = s.replace('&', '&') |
| 37 | s = s.replace('<', '<') |
| 38 | s = s.replace('>', '>') |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 39 | return s |
| 40 | |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 41 | def escapeq(s): |
| 42 | s = escape(s) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 43 | s = s.replace('"', '"') |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 44 | return s |
| 45 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 46 | def _interpolate(format, args, kw): |
| 47 | try: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 48 | quote = kw['_quote'] |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 49 | except KeyError: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 50 | quote = 1 |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 51 | d = (kw,) + args + (faqconf.__dict__,) |
| 52 | m = MagicDict(d, quote) |
| 53 | return format % m |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 55 | def interpolate(format, *args, **kw): |
| 56 | return _interpolate(format, args, kw) |
| 57 | |
| 58 | def emit(format, *args, **kw): |
| 59 | try: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 60 | f = kw['_file'] |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 61 | except KeyError: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 62 | f = sys.stdout |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 63 | f.write(_interpolate(format, args, kw)) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 64 | |
| 65 | translate_prog = None |
| 66 | |
Guido van Rossum | b1823ad | 1997-12-09 16:04:46 +0000 | [diff] [blame] | 67 | def translate(text, pre=0): |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 68 | global translate_prog |
| 69 | if not translate_prog: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 70 | translate_prog = prog = re.compile( |
| 71 | r'\b(http|ftp|https)://\S+(\b|/)|\b[-.\w]+@[-.\w]+') |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 72 | else: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 73 | prog = translate_prog |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 74 | i = 0 |
| 75 | list = [] |
| 76 | while 1: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 77 | m = prog.search(text, i) |
| 78 | if not m: |
| 79 | break |
| 80 | j = m.start() |
| 81 | list.append(escape(text[i:j])) |
| 82 | i = j |
| 83 | url = m.group(0) |
| 84 | while url[-1] in '();:,.?\'"<>': |
| 85 | url = url[:-1] |
| 86 | i = i + len(url) |
| 87 | url = escape(url) |
| 88 | if not pre or (pre and PROCESS_PREFORMAT): |
| 89 | if ':' in url: |
| 90 | repl = '<A HREF="%s">%s</A>' % (url, url) |
| 91 | else: |
Guido van Rossum | 0922a56 | 1998-07-07 22:39:21 +0000 | [diff] [blame] | 92 | repl = '<A HREF="mailto:%s">%s</A>' % (url, url) |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 93 | else: |
| 94 | repl = url |
| 95 | list.append(repl) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 96 | j = len(text) |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 97 | list.append(escape(text[i:j])) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 98 | return ''.join(list) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 100 | def emphasize(line): |
Guido van Rossum | 80e57fb | 1997-12-21 07:05:32 +0000 | [diff] [blame] | 101 | return re.sub(r'\*([a-zA-Z]+)\*', r'<I>\1</I>', line) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | 8bc49c8 | 1997-05-26 19:10:37 +0000 | [diff] [blame] | 103 | revparse_prog = None |
| 104 | |
| 105 | def revparse(rev): |
| 106 | global revparse_prog |
| 107 | if not revparse_prog: |
Guido van Rossum | 55b40b0 | 1998-05-22 19:43:21 +0000 | [diff] [blame] | 108 | revparse_prog = re.compile(r'^(\d{1,3})\.(\d{1,4})$') |
Guido van Rossum | 80e57fb | 1997-12-21 07:05:32 +0000 | [diff] [blame] | 109 | m = revparse_prog.match(rev) |
| 110 | if not m: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 111 | return None |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 112 | [major, minor] = map(int, m.group(1, 2)) |
Guido van Rossum | 8bc49c8 | 1997-05-26 19:10:37 +0000 | [diff] [blame] | 113 | return major, minor |
| 114 | |
Guido van Rossum | 2b6004a | 2000-03-31 00:58:00 +0000 | [diff] [blame] | 115 | logon = 0 |
| 116 | def log(text): |
| 117 | if logon: |
| 118 | logfile = open("logfile", "a") |
| 119 | logfile.write(text + "\n") |
| 120 | logfile.close() |
| 121 | |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 122 | def load_cookies(): |
| 123 | if not os.environ.has_key('HTTP_COOKIE'): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 124 | return {} |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 125 | raw = os.environ['HTTP_COOKIE'] |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 126 | words = [s.strip() for s in raw.split(';')] |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 127 | cookies = {} |
| 128 | for word in words: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 129 | i = word.find('=') |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 130 | if i >= 0: |
| 131 | key, value = word[:i], word[i+1:] |
| 132 | cookies[key] = value |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 133 | return cookies |
| 134 | |
| 135 | def load_my_cookie(): |
| 136 | cookies = load_cookies() |
| 137 | try: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 138 | value = cookies[COOKIE_NAME] |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 139 | except KeyError: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 140 | return {} |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 141 | import urllib |
| 142 | value = urllib.unquote(value) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 143 | words = value.split('/') |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 144 | while len(words) < 3: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 145 | words.append('') |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 146 | author = '/'.join(words[:-2]) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 147 | email = words[-2] |
| 148 | password = words[-1] |
| 149 | return {'author': author, |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 150 | 'email': email, |
| 151 | 'password': password} |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 153 | def send_my_cookie(ui): |
| 154 | name = COOKIE_NAME |
| 155 | value = "%s/%s/%s" % (ui.author, ui.email, ui.password) |
| 156 | import urllib |
| 157 | value = urllib.quote(value) |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 158 | then = now + COOKIE_LIFETIME |
| 159 | gmt = time.gmtime(then) |
Guido van Rossum | 2053aa6 | 1998-09-04 21:19:55 +0000 | [diff] [blame] | 160 | path = os.environ.get('SCRIPT_NAME', '/cgi-bin/') |
| 161 | print "Set-Cookie: %s=%s; path=%s;" % (name, value, path), |
Guido van Rossum | 48b1cde | 1998-02-02 03:19:06 +0000 | [diff] [blame] | 162 | print time.strftime("expires=%a, %d-%b-%y %X GMT", gmt) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 164 | class MagicDict: |
| 165 | |
| 166 | def __init__(self, d, quote): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 167 | self.__d = d |
| 168 | self.__quote = quote |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 169 | |
| 170 | def __getitem__(self, key): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 171 | for d in self.__d: |
| 172 | try: |
| 173 | value = d[key] |
| 174 | if value: |
| 175 | value = str(value) |
| 176 | if self.__quote: |
| 177 | value = escapeq(value) |
| 178 | return value |
| 179 | except KeyError: |
| 180 | pass |
| 181 | return '' |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 182 | |
| 183 | class UserInput: |
| 184 | |
| 185 | def __init__(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 186 | self.__form = cgi.FieldStorage() |
Guido van Rossum | 2b6004a | 2000-03-31 00:58:00 +0000 | [diff] [blame] | 187 | #log("\n\nbody: " + self.body) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 188 | |
| 189 | def __getattr__(self, name): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 190 | if name[0] == '_': |
| 191 | raise AttributeError |
| 192 | try: |
| 193 | value = self.__form[name].value |
| 194 | except (TypeError, KeyError): |
| 195 | value = '' |
| 196 | else: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 197 | value = value.strip() |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 198 | setattr(self, name, value) |
| 199 | return value |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 200 | |
| 201 | def __getitem__(self, key): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 202 | return getattr(self, key) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 204 | class FaqEntry: |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 206 | def __init__(self, fp, file, sec_num): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 207 | self.file = file |
| 208 | self.sec, self.num = sec_num |
| 209 | if fp: |
| 210 | import rfc822 |
| 211 | self.__headers = rfc822.Message(fp) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 212 | self.body = fp.read().strip() |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 213 | else: |
| 214 | self.__headers = {'title': "%d.%d. " % sec_num} |
| 215 | self.body = '' |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 216 | |
| 217 | def __getattr__(self, name): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 218 | if name[0] == '_': |
| 219 | raise AttributeError |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 220 | key = '-'.join(name.split('_')) |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 221 | try: |
| 222 | value = self.__headers[key] |
| 223 | except KeyError: |
| 224 | value = '' |
| 225 | setattr(self, name, value) |
| 226 | return value |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 227 | |
| 228 | def __getitem__(self, key): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 229 | return getattr(self, key) |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 230 | |
| 231 | def load_version(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 232 | command = interpolate(SH_RLOG_H, self) |
| 233 | p = os.popen(command) |
| 234 | version = '' |
| 235 | while 1: |
| 236 | line = p.readline() |
| 237 | if not line: |
| 238 | break |
| 239 | if line[:5] == 'head:': |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 240 | version = line[5:].strip() |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 241 | p.close() |
| 242 | self.version = version |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | 7a24107 | 1997-05-26 19:46:56 +0000 | [diff] [blame] | 244 | def getmtime(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 245 | if not self.last_changed_date: |
| 246 | return 0 |
| 247 | try: |
| 248 | return os.stat(self.file)[stat.ST_MTIME] |
| 249 | except os.error: |
| 250 | return 0 |
Guido van Rossum | 7a24107 | 1997-05-26 19:46:56 +0000 | [diff] [blame] | 251 | |
| 252 | def emit_marks(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 253 | mtime = self.getmtime() |
| 254 | if mtime >= now - DT_VERY_RECENT: |
| 255 | emit(MARK_VERY_RECENT, self) |
| 256 | elif mtime >= now - DT_RECENT: |
| 257 | emit(MARK_RECENT, self) |
Guido van Rossum | 7a24107 | 1997-05-26 19:46:56 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 259 | def show(self, edit=1): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 260 | emit(ENTRY_HEADER1, self) |
| 261 | self.emit_marks() |
| 262 | emit(ENTRY_HEADER2, self) |
| 263 | pre = 0 |
| 264 | raw = 0 |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 265 | for line in self.body.split('\n'): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 266 | # Allow the user to insert raw html into a FAQ answer |
| 267 | # (Skip Montanaro, with changes by Guido) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 268 | tag = line.rstrip().lower() |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 269 | if tag == '<html>': |
| 270 | raw = 1 |
| 271 | continue |
| 272 | if tag == '</html>': |
| 273 | raw = 0 |
| 274 | continue |
| 275 | if raw: |
| 276 | print line |
| 277 | continue |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 278 | if not line.strip(): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 279 | if pre: |
| 280 | print '</PRE>' |
| 281 | pre = 0 |
| 282 | else: |
| 283 | print '<P>' |
| 284 | else: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 285 | if not line[0].isspace(): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 286 | if pre: |
| 287 | print '</PRE>' |
| 288 | pre = 0 |
| 289 | else: |
| 290 | if not pre: |
| 291 | print '<PRE>' |
| 292 | pre = 1 |
| 293 | if '/' in line or '@' in line: |
| 294 | line = translate(line, pre) |
| 295 | elif '<' in line or '&' in line: |
| 296 | line = escape(line) |
| 297 | if not pre and '*' in line: |
| 298 | line = emphasize(line) |
| 299 | print line |
| 300 | if pre: |
| 301 | print '</PRE>' |
| 302 | pre = 0 |
| 303 | if edit: |
| 304 | print '<P>' |
| 305 | emit(ENTRY_FOOTER, self) |
| 306 | if self.last_changed_date: |
| 307 | emit(ENTRY_LOGINFO, self) |
| 308 | print '<P>' |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 309 | |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 310 | class FaqDir: |
| 311 | |
| 312 | entryclass = FaqEntry |
| 313 | |
Guido van Rossum | 80e57fb | 1997-12-21 07:05:32 +0000 | [diff] [blame] | 314 | __okprog = re.compile(OKFILENAME) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 315 | |
| 316 | def __init__(self, dir=os.curdir): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 317 | self.__dir = dir |
| 318 | self.__files = None |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 319 | |
| 320 | def __fill(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 321 | if self.__files is not None: |
| 322 | return |
| 323 | self.__files = files = [] |
| 324 | okprog = self.__okprog |
| 325 | for file in os.listdir(self.__dir): |
| 326 | if self.__okprog.match(file): |
| 327 | files.append(file) |
| 328 | files.sort() |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 329 | |
| 330 | def good(self, file): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 331 | return self.__okprog.match(file) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 332 | |
| 333 | def parse(self, file): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 334 | m = self.good(file) |
| 335 | if not m: |
| 336 | return None |
| 337 | sec, num = m.group(1, 2) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 338 | return int(sec), int(num) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 340 | def list(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 341 | # XXX Caller shouldn't modify result |
| 342 | self.__fill() |
| 343 | return self.__files |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 344 | |
| 345 | def open(self, file): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 346 | sec_num = self.parse(file) |
| 347 | if not sec_num: |
| 348 | raise InvalidFile(file) |
| 349 | try: |
| 350 | fp = open(file) |
| 351 | except IOError, msg: |
| 352 | raise NoSuchFile(file, msg) |
| 353 | try: |
| 354 | return self.entryclass(fp, file, sec_num) |
| 355 | finally: |
| 356 | fp.close() |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 357 | |
| 358 | def show(self, file, edit=1): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 359 | self.open(file).show(edit=edit) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 361 | def new(self, section): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 362 | if not SECTION_TITLES.has_key(section): |
| 363 | raise NoSuchSection(section) |
| 364 | maxnum = 0 |
| 365 | for file in self.list(): |
| 366 | sec, num = self.parse(file) |
| 367 | if sec == section: |
| 368 | maxnum = max(maxnum, num) |
| 369 | sec_num = (section, maxnum+1) |
| 370 | file = NEWFILENAME % sec_num |
| 371 | return self.entryclass(None, file, sec_num) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 372 | |
| 373 | class FaqWizard: |
| 374 | |
| 375 | def __init__(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 376 | self.ui = UserInput() |
| 377 | self.dir = FaqDir() |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 378 | |
| 379 | def go(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 380 | print 'Content-type: text/html' |
| 381 | req = self.ui.req or 'home' |
| 382 | mname = 'do_%s' % req |
| 383 | try: |
| 384 | meth = getattr(self, mname) |
| 385 | except AttributeError: |
| 386 | self.error("Bad request type %s." % `req`) |
| 387 | else: |
| 388 | try: |
| 389 | meth() |
| 390 | except InvalidFile, exc: |
| 391 | self.error("Invalid entry file name %s" % exc.file) |
| 392 | except NoSuchFile, exc: |
| 393 | self.error("No entry with file name %s" % exc.file) |
| 394 | except NoSuchSection, exc: |
| 395 | self.error("No section number %s" % exc.section) |
| 396 | self.epilogue() |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 397 | |
| 398 | def error(self, message, **kw): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 399 | self.prologue(T_ERROR) |
| 400 | emit(message, kw) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 401 | |
| 402 | def prologue(self, title, entry=None, **kw): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 403 | emit(PROLOGUE, entry, kwdict=kw, title=escape(title)) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 404 | |
| 405 | def epilogue(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 406 | emit(EPILOGUE) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 407 | |
| 408 | def do_home(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 409 | self.prologue(T_HOME) |
| 410 | emit(HOME) |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 411 | |
| 412 | def do_debug(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 413 | self.prologue("FAQ Wizard Debugging") |
| 414 | form = cgi.FieldStorage() |
| 415 | cgi.print_form(form) |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 416 | cgi.print_environ(os.environ) |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 417 | cgi.print_directory() |
| 418 | cgi.print_arguments() |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 419 | |
| 420 | def do_search(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 421 | query = self.ui.query |
| 422 | if not query: |
| 423 | self.error("Empty query string!") |
| 424 | return |
| 425 | if self.ui.querytype == 'simple': |
| 426 | query = re.escape(query) |
| 427 | queries = [query] |
| 428 | elif self.ui.querytype in ('anykeywords', 'allkeywords'): |
| 429 | words = filter(None, re.split('\W+', query)) |
| 430 | if not words: |
| 431 | self.error("No keywords specified!") |
| 432 | return |
| 433 | words = map(lambda w: r'\b%s\b' % w, words) |
| 434 | if self.ui.querytype[:3] == 'any': |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 435 | queries = ['|'.join(words)] |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 436 | else: |
| 437 | # Each of the individual queries must match |
| 438 | queries = words |
| 439 | else: |
| 440 | # Default to regular expression |
| 441 | queries = [query] |
| 442 | self.prologue(T_SEARCH) |
| 443 | progs = [] |
| 444 | for query in queries: |
| 445 | if self.ui.casefold == 'no': |
| 446 | p = re.compile(query) |
| 447 | else: |
| 448 | p = re.compile(query, re.IGNORECASE) |
| 449 | progs.append(p) |
| 450 | hits = [] |
| 451 | for file in self.dir.list(): |
| 452 | try: |
| 453 | entry = self.dir.open(file) |
| 454 | except FileError: |
| 455 | constants |
| 456 | for p in progs: |
| 457 | if not p.search(entry.title) and not p.search(entry.body): |
| 458 | break |
| 459 | else: |
| 460 | hits.append(file) |
| 461 | if not hits: |
| 462 | emit(NO_HITS, self.ui, count=0) |
| 463 | elif len(hits) <= MAXHITS: |
| 464 | if len(hits) == 1: |
| 465 | emit(ONE_HIT, count=1) |
| 466 | else: |
| 467 | emit(FEW_HITS, count=len(hits)) |
| 468 | self.format_all(hits, headers=0) |
| 469 | else: |
| 470 | emit(MANY_HITS, count=len(hits)) |
| 471 | self.format_index(hits) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 472 | |
| 473 | def do_all(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 474 | self.prologue(T_ALL) |
| 475 | files = self.dir.list() |
| 476 | self.last_changed(files) |
| 477 | self.format_index(files, localrefs=1) |
| 478 | self.format_all(files) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 479 | |
| 480 | def do_compat(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 481 | files = self.dir.list() |
| 482 | emit(COMPAT) |
| 483 | self.last_changed(files) |
| 484 | self.format_index(files, localrefs=1) |
| 485 | self.format_all(files, edit=0) |
| 486 | sys.exit(0) # XXX Hack to suppress epilogue |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 487 | |
| 488 | def last_changed(self, files): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 489 | latest = 0 |
| 490 | for file in files: |
| 491 | entry = self.dir.open(file) |
| 492 | if entry: |
| 493 | mtime = mtime = entry.getmtime() |
| 494 | if mtime > latest: |
| 495 | latest = mtime |
| 496 | print time.strftime(LAST_CHANGED, time.localtime(latest)) |
| 497 | emit(EXPLAIN_MARKS) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 498 | |
Guido van Rossum | 21c4b5f | 1997-05-26 06:28:40 +0000 | [diff] [blame] | 499 | def format_all(self, files, edit=1, headers=1): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 500 | sec = 0 |
| 501 | for file in files: |
| 502 | try: |
| 503 | entry = self.dir.open(file) |
| 504 | except NoSuchFile: |
| 505 | continue |
| 506 | if headers and entry.sec != sec: |
| 507 | sec = entry.sec |
| 508 | try: |
| 509 | title = SECTION_TITLES[sec] |
| 510 | except KeyError: |
| 511 | title = "Untitled" |
| 512 | emit("\n<HR>\n<H1>%(sec)s. %(title)s</H1>\n", |
| 513 | sec=sec, title=title) |
| 514 | entry.show(edit=edit) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 515 | |
| 516 | def do_index(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 517 | self.prologue(T_INDEX) |
| 518 | files = self.dir.list() |
| 519 | self.last_changed(files) |
| 520 | self.format_index(files, add=1) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 521 | |
Guido van Rossum | 8bc49c8 | 1997-05-26 19:10:37 +0000 | [diff] [blame] | 522 | def format_index(self, files, add=0, localrefs=0): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 523 | sec = 0 |
| 524 | for file in files: |
| 525 | try: |
| 526 | entry = self.dir.open(file) |
| 527 | except NoSuchFile: |
| 528 | continue |
| 529 | if entry.sec != sec: |
| 530 | if sec: |
| 531 | if add: |
| 532 | emit(INDEX_ADDSECTION, sec=sec) |
| 533 | emit(INDEX_ENDSECTION, sec=sec) |
| 534 | sec = entry.sec |
| 535 | try: |
| 536 | title = SECTION_TITLES[sec] |
| 537 | except KeyError: |
| 538 | title = "Untitled" |
| 539 | emit(INDEX_SECTION, sec=sec, title=title) |
| 540 | if localrefs: |
| 541 | emit(LOCAL_ENTRY, entry) |
| 542 | else: |
| 543 | emit(INDEX_ENTRY, entry) |
| 544 | entry.emit_marks() |
| 545 | if sec: |
| 546 | if add: |
| 547 | emit(INDEX_ADDSECTION, sec=sec) |
| 548 | emit(INDEX_ENDSECTION, sec=sec) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 549 | |
| 550 | def do_recent(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 551 | if not self.ui.days: |
| 552 | days = 1 |
| 553 | else: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 554 | days = float(self.ui.days) |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 555 | try: |
| 556 | cutoff = now - days * 24 * 3600 |
| 557 | except OverflowError: |
| 558 | cutoff = 0 |
| 559 | list = [] |
| 560 | for file in self.dir.list(): |
| 561 | entry = self.dir.open(file) |
| 562 | if not entry: |
| 563 | continue |
| 564 | mtime = entry.getmtime() |
| 565 | if mtime >= cutoff: |
| 566 | list.append((mtime, file)) |
| 567 | list.sort() |
| 568 | list.reverse() |
| 569 | self.prologue(T_RECENT) |
| 570 | if days <= 1: |
| 571 | period = "%.2g hours" % (days*24) |
| 572 | else: |
| 573 | period = "%.6g days" % days |
| 574 | if not list: |
| 575 | emit(NO_RECENT, period=period) |
| 576 | elif len(list) == 1: |
| 577 | emit(ONE_RECENT, period=period) |
| 578 | else: |
| 579 | emit(SOME_RECENT, period=period, count=len(list)) |
| 580 | self.format_all(map(lambda (mtime, file): file, list), headers=0) |
| 581 | emit(TAIL_RECENT) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 582 | |
| 583 | def do_roulette(self): |
Guido van Rossum | 6c3a2cb | 1998-05-20 17:13:01 +0000 | [diff] [blame] | 584 | import random |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 585 | files = self.dir.list() |
| 586 | if not files: |
| 587 | self.error("No entries.") |
| 588 | return |
Guido van Rossum | 6c3a2cb | 1998-05-20 17:13:01 +0000 | [diff] [blame] | 589 | file = random.choice(files) |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 590 | self.prologue(T_ROULETTE) |
| 591 | emit(ROULETTE) |
| 592 | self.dir.show(file) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 593 | |
| 594 | def do_help(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 595 | self.prologue(T_HELP) |
| 596 | emit(HELP) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 597 | |
| 598 | def do_show(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 599 | entry = self.dir.open(self.ui.file) |
| 600 | self.prologue(T_SHOW) |
| 601 | entry.show() |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 602 | |
| 603 | def do_add(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 604 | self.prologue(T_ADD) |
| 605 | emit(ADD_HEAD) |
| 606 | sections = SECTION_TITLES.items() |
| 607 | sections.sort() |
| 608 | for section, title in sections: |
| 609 | emit(ADD_SECTION, section=section, title=title) |
| 610 | emit(ADD_TAIL) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 611 | |
| 612 | def do_delete(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 613 | self.prologue(T_DELETE) |
| 614 | emit(DELETE) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 615 | |
| 616 | def do_log(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 617 | entry = self.dir.open(self.ui.file) |
| 618 | self.prologue(T_LOG, entry) |
| 619 | emit(LOG, entry) |
| 620 | self.rlog(interpolate(SH_RLOG, entry), entry) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 621 | |
| 622 | def rlog(self, command, entry=None): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 623 | output = os.popen(command).read() |
| 624 | sys.stdout.write('<PRE>') |
| 625 | athead = 0 |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 626 | lines = output.split('\n') |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 627 | while lines and not lines[-1]: |
| 628 | del lines[-1] |
| 629 | if lines: |
| 630 | line = lines[-1] |
| 631 | if line[:1] == '=' and len(line) >= 40 and \ |
| 632 | line == line[0]*len(line): |
| 633 | del lines[-1] |
| 634 | headrev = None |
| 635 | for line in lines: |
| 636 | if entry and athead and line[:9] == 'revision ': |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 637 | rev = line[9:].split() |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 638 | mami = revparse(rev) |
| 639 | if not mami: |
| 640 | print line |
| 641 | else: |
| 642 | emit(REVISIONLINK, entry, rev=rev, line=line) |
| 643 | if mami[1] > 1: |
| 644 | prev = "%d.%d" % (mami[0], mami[1]-1) |
| 645 | emit(DIFFLINK, entry, prev=prev, rev=rev) |
| 646 | if headrev: |
| 647 | emit(DIFFLINK, entry, prev=rev, rev=headrev) |
| 648 | else: |
| 649 | headrev = rev |
| 650 | print |
| 651 | athead = 0 |
| 652 | else: |
| 653 | athead = 0 |
| 654 | if line[:1] == '-' and len(line) >= 20 and \ |
| 655 | line == len(line) * line[0]: |
| 656 | athead = 1 |
| 657 | sys.stdout.write('<HR>') |
| 658 | else: |
| 659 | print line |
| 660 | print '</PRE>' |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 661 | |
Guido van Rossum | 8bc49c8 | 1997-05-26 19:10:37 +0000 | [diff] [blame] | 662 | def do_revision(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 663 | entry = self.dir.open(self.ui.file) |
| 664 | rev = self.ui.rev |
| 665 | mami = revparse(rev) |
| 666 | if not mami: |
| 667 | self.error("Invalid revision number: %s." % `rev`) |
| 668 | self.prologue(T_REVISION, entry) |
| 669 | self.shell(interpolate(SH_REVISION, entry, rev=rev)) |
Guido van Rossum | 8bc49c8 | 1997-05-26 19:10:37 +0000 | [diff] [blame] | 670 | |
| 671 | def do_diff(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 672 | entry = self.dir.open(self.ui.file) |
| 673 | prev = self.ui.prev |
| 674 | rev = self.ui.rev |
| 675 | mami = revparse(rev) |
| 676 | if not mami: |
| 677 | self.error("Invalid revision number: %s." % `rev`) |
| 678 | if prev: |
| 679 | if not revparse(prev): |
| 680 | self.error("Invalid previous revision number: %s." % `prev`) |
| 681 | else: |
| 682 | prev = '%d.%d' % (mami[0], mami[1]) |
| 683 | self.prologue(T_DIFF, entry) |
| 684 | self.shell(interpolate(SH_RDIFF, entry, rev=rev, prev=prev)) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 685 | |
| 686 | def shell(self, command): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 687 | output = os.popen(command).read() |
| 688 | sys.stdout.write('<PRE>') |
| 689 | print escape(output) |
| 690 | print '</PRE>' |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 691 | |
| 692 | def do_new(self): |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 693 | entry = self.dir.new(section=int(self.ui.section)) |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 694 | entry.version = '*new*' |
| 695 | self.prologue(T_EDIT) |
| 696 | emit(EDITHEAD) |
| 697 | emit(EDITFORM1, entry, editversion=entry.version) |
| 698 | emit(EDITFORM2, entry, load_my_cookie()) |
| 699 | emit(EDITFORM3) |
| 700 | entry.show(edit=0) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 701 | |
| 702 | def do_edit(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 703 | entry = self.dir.open(self.ui.file) |
| 704 | entry.load_version() |
| 705 | self.prologue(T_EDIT) |
| 706 | emit(EDITHEAD) |
| 707 | emit(EDITFORM1, entry, editversion=entry.version) |
| 708 | emit(EDITFORM2, entry, load_my_cookie()) |
| 709 | emit(EDITFORM3) |
| 710 | entry.show(edit=0) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 711 | |
| 712 | def do_review(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 713 | send_my_cookie(self.ui) |
| 714 | if self.ui.editversion == '*new*': |
| 715 | sec, num = self.dir.parse(self.ui.file) |
| 716 | entry = self.dir.new(section=sec) |
| 717 | entry.version = "*new*" |
| 718 | if entry.file != self.ui.file: |
| 719 | self.error("Commit version conflict!") |
| 720 | emit(NEWCONFLICT, self.ui, sec=sec, num=num) |
| 721 | return |
| 722 | else: |
| 723 | entry = self.dir.open(self.ui.file) |
| 724 | entry.load_version() |
| 725 | # Check that the FAQ entry number didn't change |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 726 | if self.ui.title.split()[:1] != entry.title.split()[:1]: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 727 | self.error("Don't change the entry number please!") |
| 728 | return |
| 729 | # Check that the edited version is the current version |
| 730 | if entry.version != self.ui.editversion: |
| 731 | self.error("Commit version conflict!") |
| 732 | emit(VERSIONCONFLICT, entry, self.ui) |
| 733 | return |
| 734 | commit_ok = ((not PASSWORD |
| 735 | or self.ui.password == PASSWORD) |
| 736 | and self.ui.author |
| 737 | and '@' in self.ui.email |
| 738 | and self.ui.log) |
| 739 | if self.ui.commit: |
| 740 | if not commit_ok: |
| 741 | self.cantcommit() |
| 742 | else: |
| 743 | self.commit(entry) |
| 744 | return |
| 745 | self.prologue(T_REVIEW) |
| 746 | emit(REVIEWHEAD) |
| 747 | entry.body = self.ui.body |
| 748 | entry.title = self.ui.title |
| 749 | entry.show(edit=0) |
| 750 | emit(EDITFORM1, self.ui, entry) |
| 751 | if commit_ok: |
| 752 | emit(COMMIT) |
| 753 | else: |
Guido van Rossum | 2d3b0d7 | 1998-12-23 21:33:09 +0000 | [diff] [blame] | 754 | emit(NOCOMMIT_HEAD) |
| 755 | self.errordetail() |
| 756 | emit(NOCOMMIT_TAIL) |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 757 | emit(EDITFORM2, self.ui, entry, load_my_cookie()) |
| 758 | emit(EDITFORM3) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 759 | |
| 760 | def cantcommit(self): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 761 | self.prologue(T_CANTCOMMIT) |
| 762 | print CANTCOMMIT_HEAD |
Guido van Rossum | 2d3b0d7 | 1998-12-23 21:33:09 +0000 | [diff] [blame] | 763 | self.errordetail() |
| 764 | print CANTCOMMIT_TAIL |
| 765 | |
| 766 | def errordetail(self): |
| 767 | if PASSWORD and self.ui.password != PASSWORD: |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 768 | emit(NEED_PASSWD) |
| 769 | if not self.ui.log: |
| 770 | emit(NEED_LOG) |
| 771 | if not self.ui.author: |
| 772 | emit(NEED_AUTHOR) |
| 773 | if not self.ui.email: |
| 774 | emit(NEED_EMAIL) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 775 | |
Guido van Rossum | ea31ea2 | 1997-05-26 05:43:29 +0000 | [diff] [blame] | 776 | def commit(self, entry): |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 777 | file = entry.file |
| 778 | # Normalize line endings in body |
| 779 | if '\r' in self.ui.body: |
| 780 | self.ui.body = re.sub('\r\n?', '\n', self.ui.body) |
| 781 | # Normalize whitespace in title |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 782 | self.ui.title = ' '.join(self.ui.title.split()) |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 783 | # Check that there were any changes |
| 784 | if self.ui.body == entry.body and self.ui.title == entry.title: |
| 785 | self.error("You didn't make any changes!") |
| 786 | return |
Guido van Rossum | 2b6004a | 2000-03-31 00:58:00 +0000 | [diff] [blame] | 787 | |
| 788 | # need to lock here because otherwise the file exists and is not writable (on NT) |
| 789 | command = interpolate(SH_LOCK, file=file) |
| 790 | p = os.popen(command) |
| 791 | output = p.read() |
| 792 | |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 793 | try: |
| 794 | os.unlink(file) |
| 795 | except os.error: |
| 796 | pass |
| 797 | try: |
| 798 | f = open(file, 'w') |
| 799 | except IOError, why: |
| 800 | self.error(CANTWRITE, file=file, why=why) |
| 801 | return |
| 802 | date = time.ctime(now) |
| 803 | emit(FILEHEADER, self.ui, os.environ, date=date, _file=f, _quote=0) |
| 804 | f.write('\n') |
| 805 | f.write(self.ui.body) |
| 806 | f.write('\n') |
| 807 | f.close() |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 808 | |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 809 | import tempfile |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 810 | tf = tempfile.NamedTemporaryFile() |
| 811 | emit(LOGHEADER, self.ui, os.environ, date=date, _file=tfn) |
| 812 | tf.flush() |
| 813 | tf.seek(0) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 814 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 815 | command = interpolate(SH_CHECKIN, file=file, tfn=tf.name) |
Guido van Rossum | 2b6004a | 2000-03-31 00:58:00 +0000 | [diff] [blame] | 816 | log("\n\n" + command) |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 817 | p = os.popen(command) |
| 818 | output = p.read() |
| 819 | sts = p.close() |
Guido van Rossum | 2b6004a | 2000-03-31 00:58:00 +0000 | [diff] [blame] | 820 | log("output: " + output) |
| 821 | log("done: " + str(sts)) |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 822 | log("TempFile:\n" + tf.read() + "end") |
Guido van Rossum | 2b6004a | 2000-03-31 00:58:00 +0000 | [diff] [blame] | 823 | |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 824 | if not sts: |
| 825 | self.prologue(T_COMMITTED) |
| 826 | emit(COMMITTED) |
| 827 | else: |
| 828 | self.error(T_COMMITFAILED) |
| 829 | emit(COMMITFAILED, sts=sts) |
| 830 | print '<PRE>%s</PRE>' % escape(output) |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 831 | |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 832 | try: |
| 833 | os.unlink(tfn) |
| 834 | except os.error: |
| 835 | pass |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 836 | |
Guido van Rossum | 72dc60c | 1998-04-06 14:24:36 +0000 | [diff] [blame] | 837 | entry = self.dir.open(file) |
| 838 | entry.show() |
Guido van Rossum | 1677e5b | 1997-05-26 00:07:18 +0000 | [diff] [blame] | 839 | |
| 840 | wiz = FaqWizard() |
| 841 | wiz.go() |