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