##// END OF EJS Templates
Fix TypeError
Vincent Wagelaar -
r1160:0da98529 default
parent child Browse files
Show More
@@ -1,989 +1,991 b''
1 1 # hgweb.py - web interface to a mercurial repository
2 2 #
3 3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
4 4 # Copyright 2005 Matt Mackall <mpm@selenic.com>
5 5 #
6 6 # This software may be used and distributed according to the terms
7 7 # of the GNU General Public License, incorporated herein by reference.
8 8
9 9 import os, cgi, time, re, socket, sys, zlib
10 10 import mdiff
11 11 from hg import *
12 12 from ui import *
13 13
14 14
15 15 def templatepath():
16 16 for f in "templates", "../templates":
17 17 p = os.path.join(os.path.dirname(__file__), f)
18 18 if os.path.isdir(p):
19 19 return p
20 20
21 21 def age(t):
22 22 def plural(t, c):
23 23 if c == 1:
24 24 return t
25 25 return t + "s"
26 26 def fmt(t, c):
27 27 return "%d %s" % (c, plural(t, c))
28 28
29 29 now = time.time()
30 30 delta = max(1, int(now - t))
31 31
32 32 scales = [["second", 1],
33 33 ["minute", 60],
34 34 ["hour", 3600],
35 35 ["day", 3600 * 24],
36 36 ["week", 3600 * 24 * 7],
37 37 ["month", 3600 * 24 * 30],
38 38 ["year", 3600 * 24 * 365]]
39 39
40 40 scales.reverse()
41 41
42 42 for t, s in scales:
43 43 n = delta / s
44 44 if n >= 2 or s == 1:
45 45 return fmt(t, n)
46 46
47 47 def nl2br(text):
48 48 return text.replace('\n', '<br/>\n')
49 49
50 50 def obfuscate(text):
51 51 return ''.join(['&#%d;' % ord(c) for c in text])
52 52
53 53 def up(p):
54 54 if p[0] != "/":
55 55 p = "/" + p
56 56 if p[-1] == "/":
57 57 p = p[:-1]
58 58 up = os.path.dirname(p)
59 59 if up == "/":
60 60 return "/"
61 61 return up + "/"
62 62
63 63 class hgrequest:
64 64 def __init__(self, inp=None, out=None, env=None):
65 65 self.inp = inp or sys.stdin
66 66 self.out = out or sys.stdout
67 67 self.env = env or os.environ
68 68 self.form = cgi.parse(self.inp, self.env)
69 69
70 70 def write(self, *things):
71 71 for thing in things:
72 72 if hasattr(thing, "__iter__"):
73 73 for part in thing:
74 74 self.write(part)
75 75 else:
76 76 try:
77 77 self.out.write(thing)
78 except TypeError:
79 self.out.write(str(thing))
78 80 except socket.error, x:
79 81 if x[0] != errno.ECONNRESET:
80 82 raise
81 83
82 84 def header(self, headers=[('Content-type','text/html')]):
83 85 for header in headers:
84 86 self.out.write("%s: %s\r\n" % header)
85 87 self.out.write("\r\n")
86 88
87 89 def httphdr(self, type, file="", size=0):
88 90
89 91 headers = [('Content-type', type)]
90 92 if file:
91 93 headers.append(('Content-disposition', 'attachment; filename=%s' % file))
92 94 if size > 0:
93 95 headers.append(('Content-length', str(size)))
94 96 self.header(headers)
95 97
96 98 class templater:
97 99 def __init__(self, mapfile, filters={}, defaults={}):
98 100 self.cache = {}
99 101 self.map = {}
100 102 self.base = os.path.dirname(mapfile)
101 103 self.filters = filters
102 104 self.defaults = defaults
103 105
104 106 for l in file(mapfile):
105 107 m = re.match(r'(\S+)\s*=\s*"(.*)"$', l)
106 108 if m:
107 109 self.cache[m.group(1)] = m.group(2)
108 110 else:
109 111 m = re.match(r'(\S+)\s*=\s*(\S+)', l)
110 112 if m:
111 113 self.map[m.group(1)] = os.path.join(self.base, m.group(2))
112 114 else:
113 115 raise LookupError("unknown map entry '%s'" % l)
114 116
115 117 def __call__(self, t, **map):
116 118 m = self.defaults.copy()
117 119 m.update(map)
118 120 try:
119 121 tmpl = self.cache[t]
120 122 except KeyError:
121 123 tmpl = self.cache[t] = file(self.map[t]).read()
122 124 return self.template(tmpl, self.filters, **m)
123 125
124 126 def template(self, tmpl, filters={}, **map):
125 127 while tmpl:
126 128 m = re.search(r"#([a-zA-Z0-9]+)((%[a-zA-Z0-9]+)*)((\|[a-zA-Z0-9]+)*)#", tmpl)
127 129 if m:
128 130 yield tmpl[:m.start(0)]
129 131 v = map.get(m.group(1), "")
130 132 v = callable(v) and v(**map) or v
131 133
132 134 format = m.group(2)
133 135 fl = m.group(4)
134 136
135 137 if format:
136 138 q = v.__iter__
137 139 for i in q():
138 140 lm = map.copy()
139 141 lm.update(i)
140 142 yield self(format[1:], **lm)
141 143
142 144 v = ""
143 145
144 146 elif fl:
145 147 for f in fl.split("|")[1:]:
146 148 v = filters[f](v)
147 149
148 150 yield v
149 151 tmpl = tmpl[m.end(0):]
150 152 else:
151 153 yield tmpl
152 154 return
153 155
154 156 def rfc822date(x):
155 157 return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(x))
156 158
157 159 common_filters = {
158 160 "escape": cgi.escape,
159 161 "age": age,
160 162 "date": (lambda x: time.asctime(time.gmtime(x))),
161 163 "addbreaks": nl2br,
162 164 "obfuscate": obfuscate,
163 165 "short": (lambda x: x[:12]),
164 166 "firstline": (lambda x: x.splitlines(1)[0]),
165 167 "permissions": (lambda x: x and "-rwxr-xr-x" or "-rw-r--r--"),
166 168 "rfc822date": rfc822date,
167 169 }
168 170
169 171
170 172
171 173 class hgweb:
172 174 def __init__(self, repo, name=None):
173 175 if type(repo) == type(""):
174 176 self.repo = repository(ui(), repo)
175 177 else:
176 178 self.repo = repo
177 179
178 180 self.mtime = -1
179 181 self.reponame = name or self.repo.ui.config("web", "name",
180 182 self.repo.root)
181 183 self.archives = 'zip', 'gz', 'bz2'
182 184
183 185 def refresh(self):
184 186 s = os.stat(os.path.join(self.repo.root, ".hg", "00changelog.i"))
185 187 if s.st_mtime != self.mtime:
186 188 self.mtime = s.st_mtime
187 189 self.repo = repository(self.repo.ui, self.repo.root)
188 190 self.maxchanges = self.repo.ui.config("web", "maxchanges", 10)
189 191 self.maxfiles = self.repo.ui.config("web", "maxchanges", 10)
190 192 self.allowpull = self.repo.ui.configbool("web", "allowpull", True)
191 193
192 194 def date(self, cs):
193 195 return time.asctime(time.gmtime(float(cs[2].split(' ')[0])))
194 196
195 197 def listfiles(self, files, mf):
196 198 for f in files[:self.maxfiles]:
197 199 yield self.t("filenodelink", node=hex(mf[f]), file=f)
198 200 if len(files) > self.maxfiles:
199 201 yield self.t("fileellipses")
200 202
201 203 def listfilediffs(self, files, changeset):
202 204 for f in files[:self.maxfiles]:
203 205 yield self.t("filedifflink", node=hex(changeset), file=f)
204 206 if len(files) > self.maxfiles:
205 207 yield self.t("fileellipses")
206 208
207 209 def parents(self, t1, nodes=[], rev=None,**args):
208 210 if not rev:
209 211 rev = lambda x: ""
210 212 for node in nodes:
211 213 if node != nullid:
212 214 yield self.t(t1, node=hex(node), rev=rev(node), **args)
213 215
214 216 def showtag(self, t1, node=nullid, **args):
215 217 for t in self.repo.nodetags(node):
216 218 yield self.t(t1, tag=t, **args)
217 219
218 220 def diff(self, node1, node2, files):
219 221 def filterfiles(list, files):
220 222 l = [x for x in list if x in files]
221 223
222 224 for f in files:
223 225 if f[-1] != os.sep:
224 226 f += os.sep
225 227 l += [x for x in list if x.startswith(f)]
226 228 return l
227 229
228 230 parity = [0]
229 231 def diffblock(diff, f, fn):
230 232 yield self.t("diffblock",
231 233 lines=prettyprintlines(diff),
232 234 parity=parity[0],
233 235 file=f,
234 236 filenode=hex(fn or nullid))
235 237 parity[0] = 1 - parity[0]
236 238
237 239 def prettyprintlines(diff):
238 240 for l in diff.splitlines(1):
239 241 if l.startswith('+'):
240 242 yield self.t("difflineplus", line=l)
241 243 elif l.startswith('-'):
242 244 yield self.t("difflineminus", line=l)
243 245 elif l.startswith('@'):
244 246 yield self.t("difflineat", line=l)
245 247 else:
246 248 yield self.t("diffline", line=l)
247 249
248 250 r = self.repo
249 251 cl = r.changelog
250 252 mf = r.manifest
251 253 change1 = cl.read(node1)
252 254 change2 = cl.read(node2)
253 255 mmap1 = mf.read(change1[0])
254 256 mmap2 = mf.read(change2[0])
255 257 date1 = self.date(change1)
256 258 date2 = self.date(change2)
257 259
258 260 c, a, d, u = r.changes(node1, node2)
259 261 if files:
260 262 c, a, d = map(lambda x: filterfiles(x, files), (c, a, d))
261 263
262 264 for f in c:
263 265 to = r.file(f).read(mmap1[f])
264 266 tn = r.file(f).read(mmap2[f])
265 267 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn)
266 268 for f in a:
267 269 to = None
268 270 tn = r.file(f).read(mmap2[f])
269 271 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn)
270 272 for f in d:
271 273 to = r.file(f).read(mmap1[f])
272 274 tn = None
273 275 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn)
274 276
275 277 def changelog(self, pos):
276 278 def changenav(**map):
277 279 def seq(factor=1):
278 280 yield 1 * factor
279 281 yield 3 * factor
280 282 #yield 5 * factor
281 283 for f in seq(factor * 10):
282 284 yield f
283 285
284 286 l = []
285 287 for f in seq():
286 288 if f < self.maxchanges / 2:
287 289 continue
288 290 if f > count:
289 291 break
290 292 r = "%d" % f
291 293 if pos + f < count:
292 294 l.append(("+" + r, pos + f))
293 295 if pos - f >= 0:
294 296 l.insert(0, ("-" + r, pos - f))
295 297
296 298 yield {"rev": 0, "label": "(0)"}
297 299
298 300 for label, rev in l:
299 301 yield {"label": label, "rev": rev}
300 302
301 303 yield {"label": "tip", "rev": ""}
302 304
303 305 def changelist(**map):
304 306 parity = (start - end) & 1
305 307 cl = self.repo.changelog
306 308 l = [] # build a list in forward order for efficiency
307 309 for i in range(start, end):
308 310 n = cl.node(i)
309 311 changes = cl.read(n)
310 312 hn = hex(n)
311 313 t = float(changes[2].split(' ')[0])
312 314
313 315 l.insert(0, {"parity": parity,
314 316 "author": changes[1],
315 317 "parent": self.parents("changelogparent",
316 318 cl.parents(n), cl.rev),
317 319 "changelogtag": self.showtag("changelogtag",n),
318 320 "manifest": hex(changes[0]),
319 321 "desc": changes[4],
320 322 "date": t,
321 323 "files": self.listfilediffs(changes[3], n),
322 324 "rev": i,
323 325 "node": hn})
324 326 parity = 1 - parity
325 327
326 328 for e in l:
327 329 yield e
328 330
329 331 cl = self.repo.changelog
330 332 mf = cl.read(cl.tip())[0]
331 333 count = cl.count()
332 334 start = max(0, pos - self.maxchanges + 1)
333 335 end = min(count, start + self.maxchanges)
334 336 pos = end - 1
335 337
336 338 yield self.t('changelog',
337 339 changenav=changenav,
338 340 manifest=hex(mf),
339 341 rev=pos, changesets=count, entries=changelist)
340 342
341 343 def search(self, query):
342 344
343 345 def changelist(**map):
344 346 cl = self.repo.changelog
345 347 count = 0
346 348 qw = query.lower().split()
347 349
348 350 def revgen():
349 351 for i in range(cl.count() - 1, 0, -100):
350 352 l = []
351 353 for j in range(max(0, i - 100), i):
352 354 n = cl.node(j)
353 355 changes = cl.read(n)
354 356 l.append((n, j, changes))
355 357 l.reverse()
356 358 for e in l:
357 359 yield e
358 360
359 361 for n, i, changes in revgen():
360 362 miss = 0
361 363 for q in qw:
362 364 if not (q in changes[1].lower() or
363 365 q in changes[4].lower() or
364 366 q in " ".join(changes[3][:20]).lower()):
365 367 miss = 1
366 368 break
367 369 if miss:
368 370 continue
369 371
370 372 count += 1
371 373 hn = hex(n)
372 374 t = float(changes[2].split(' ')[0])
373 375
374 376 yield self.t('searchentry',
375 377 parity=count & 1,
376 378 author=changes[1],
377 379 parent=self.parents("changelogparent",
378 380 cl.parents(n), cl.rev),
379 381 changelogtag=self.showtag("changelogtag",n),
380 382 manifest=hex(changes[0]),
381 383 desc=changes[4],
382 384 date=t,
383 385 files=self.listfilediffs(changes[3], n),
384 386 rev=i,
385 387 node=hn)
386 388
387 389 if count >= self.maxchanges:
388 390 break
389 391
390 392 cl = self.repo.changelog
391 393 mf = cl.read(cl.tip())[0]
392 394
393 395 yield self.t('search',
394 396 query=query,
395 397 manifest=hex(mf),
396 398 entries=changelist)
397 399
398 400 def changeset(self, nodeid):
399 401 n = bin(nodeid)
400 402 cl = self.repo.changelog
401 403 changes = cl.read(n)
402 404 p1 = cl.parents(n)[0]
403 405 t = float(changes[2].split(' ')[0])
404 406
405 407 files = []
406 408 mf = self.repo.manifest.read(changes[0])
407 409 for f in changes[3]:
408 410 files.append(self.t("filenodelink",
409 411 filenode=hex(mf.get(f, nullid)), file=f))
410 412
411 413 def diff(**map):
412 414 yield self.diff(p1, n, None)
413 415
414 416 def archivelist():
415 417 for i in self.archives:
416 418 if self.repo.ui.configbool("web", "allow" + i, False):
417 419 yield {"type" : i, "node" : nodeid}
418 420
419 421 yield self.t('changeset',
420 422 diff=diff,
421 423 rev=cl.rev(n),
422 424 node=nodeid,
423 425 parent=self.parents("changesetparent",
424 426 cl.parents(n), cl.rev),
425 427 changesettag=self.showtag("changesettag",n),
426 428 manifest=hex(changes[0]),
427 429 author=changes[1],
428 430 desc=changes[4],
429 431 date=t,
430 432 files=files,
431 433 archives=archivelist())
432 434
433 435 def filelog(self, f, filenode):
434 436 cl = self.repo.changelog
435 437 fl = self.repo.file(f)
436 438 count = fl.count()
437 439
438 440 def entries(**map):
439 441 l = []
440 442 parity = (count - 1) & 1
441 443
442 444 for i in range(count):
443 445 n = fl.node(i)
444 446 lr = fl.linkrev(n)
445 447 cn = cl.node(lr)
446 448 cs = cl.read(cl.node(lr))
447 449 t = float(cs[2].split(' ')[0])
448 450
449 451 l.insert(0, {"parity": parity,
450 452 "filenode": hex(n),
451 453 "filerev": i,
452 454 "file": f,
453 455 "node": hex(cn),
454 456 "author": cs[1],
455 457 "date": t,
456 458 "parent": self.parents("filelogparent",
457 459 fl.parents(n),
458 460 fl.rev, file=f),
459 461 "desc": cs[4]})
460 462 parity = 1 - parity
461 463
462 464 for e in l:
463 465 yield e
464 466
465 467 yield self.t("filelog", file=f, filenode=filenode, entries=entries)
466 468
467 469 def filerevision(self, f, node):
468 470 fl = self.repo.file(f)
469 471 n = bin(node)
470 472 text = fl.read(n)
471 473 changerev = fl.linkrev(n)
472 474 cl = self.repo.changelog
473 475 cn = cl.node(changerev)
474 476 cs = cl.read(cn)
475 477 t = float(cs[2].split(' ')[0])
476 478 mfn = cs[0]
477 479
478 480 def lines():
479 481 for l, t in enumerate(text.splitlines(1)):
480 482 yield {"line": t,
481 483 "linenumber": "% 6d" % (l + 1),
482 484 "parity": l & 1}
483 485
484 486 yield self.t("filerevision",
485 487 file=f,
486 488 filenode=node,
487 489 path=up(f),
488 490 text=lines(),
489 491 rev=changerev,
490 492 node=hex(cn),
491 493 manifest=hex(mfn),
492 494 author=cs[1],
493 495 date=t,
494 496 parent=self.parents("filerevparent",
495 497 fl.parents(n), fl.rev, file=f),
496 498 permissions=self.repo.manifest.readflags(mfn)[f])
497 499
498 500 def fileannotate(self, f, node):
499 501 bcache = {}
500 502 ncache = {}
501 503 fl = self.repo.file(f)
502 504 n = bin(node)
503 505 changerev = fl.linkrev(n)
504 506
505 507 cl = self.repo.changelog
506 508 cn = cl.node(changerev)
507 509 cs = cl.read(cn)
508 510 t = float(cs[2].split(' ')[0])
509 511 mfn = cs[0]
510 512
511 513 def annotate(**map):
512 514 parity = 1
513 515 last = None
514 516 for r, l in fl.annotate(n):
515 517 try:
516 518 cnode = ncache[r]
517 519 except KeyError:
518 520 cnode = ncache[r] = self.repo.changelog.node(r)
519 521
520 522 try:
521 523 name = bcache[r]
522 524 except KeyError:
523 525 cl = self.repo.changelog.read(cnode)
524 526 bcache[r] = name = self.repo.ui.shortuser(cl[1])
525 527
526 528 if last != cnode:
527 529 parity = 1 - parity
528 530 last = cnode
529 531
530 532 yield {"parity": parity,
531 533 "node": hex(cnode),
532 534 "rev": r,
533 535 "author": name,
534 536 "file": f,
535 537 "line": l}
536 538
537 539 yield self.t("fileannotate",
538 540 file=f,
539 541 filenode=node,
540 542 annotate=annotate,
541 543 path=up(f),
542 544 rev=changerev,
543 545 node=hex(cn),
544 546 manifest=hex(mfn),
545 547 author=cs[1],
546 548 date=t,
547 549 parent=self.parents("fileannotateparent",
548 550 fl.parents(n), fl.rev, file=f),
549 551 permissions=self.repo.manifest.readflags(mfn)[f])
550 552
551 553 def manifest(self, mnode, path):
552 554 mf = self.repo.manifest.read(bin(mnode))
553 555 rev = self.repo.manifest.rev(bin(mnode))
554 556 node = self.repo.changelog.node(rev)
555 557 mff=self.repo.manifest.readflags(bin(mnode))
556 558
557 559 files = {}
558 560
559 561 p = path[1:]
560 562 l = len(p)
561 563
562 564 for f,n in mf.items():
563 565 if f[:l] != p:
564 566 continue
565 567 remain = f[l:]
566 568 if "/" in remain:
567 569 short = remain[:remain.find("/") + 1] # bleah
568 570 files[short] = (f, None)
569 571 else:
570 572 short = os.path.basename(remain)
571 573 files[short] = (f, n)
572 574
573 575 def filelist(**map):
574 576 parity = 0
575 577 fl = files.keys()
576 578 fl.sort()
577 579 for f in fl:
578 580 full, fnode = files[f]
579 581 if not fnode:
580 582 continue
581 583
582 584 yield {"file": full,
583 585 "manifest": mnode,
584 586 "filenode": hex(fnode),
585 587 "parity": parity,
586 588 "basename": f,
587 589 "permissions": mff[full]}
588 590 parity = 1 - parity
589 591
590 592 def dirlist(**map):
591 593 parity = 0
592 594 fl = files.keys()
593 595 fl.sort()
594 596 for f in fl:
595 597 full, fnode = files[f]
596 598 if fnode:
597 599 continue
598 600
599 601 yield {"parity": parity,
600 602 "path": os.path.join(path, f),
601 603 "manifest": mnode,
602 604 "basename": f[:-1]}
603 605 parity = 1 - parity
604 606
605 607 yield self.t("manifest",
606 608 manifest=mnode,
607 609 rev=rev,
608 610 node=hex(node),
609 611 path=path,
610 612 up=up(path),
611 613 fentries=filelist,
612 614 dentries=dirlist)
613 615
614 616 def tags(self):
615 617 cl = self.repo.changelog
616 618 mf = cl.read(cl.tip())[0]
617 619
618 620 i = self.repo.tagslist()
619 621 i.reverse()
620 622
621 623 def entries(**map):
622 624 parity = 0
623 625 for k,n in i:
624 626 yield {"parity": parity,
625 627 "tag": k,
626 628 "node": hex(n)}
627 629 parity = 1 - parity
628 630
629 631 yield self.t("tags",
630 632 manifest=hex(mf),
631 633 entries=entries)
632 634
633 635 def filediff(self, file, changeset):
634 636 n = bin(changeset)
635 637 cl = self.repo.changelog
636 638 p1 = cl.parents(n)[0]
637 639 cs = cl.read(n)
638 640 mf = self.repo.manifest.read(cs[0])
639 641
640 642 def diff(**map):
641 643 yield self.diff(p1, n, file)
642 644
643 645 yield self.t("filediff",
644 646 file=file,
645 647 filenode=hex(mf.get(file, nullid)),
646 648 node=changeset,
647 649 rev=self.repo.changelog.rev(n),
648 650 parent=self.parents("filediffparent",
649 651 cl.parents(n), cl.rev),
650 652 diff=diff)
651 653
652 654 def archive(self, req, cnode, type):
653 655 cs = self.repo.changelog.read(cnode)
654 656 mnode = cs[0]
655 657 mf = self.repo.manifest.read(mnode)
656 658 rev = self.repo.manifest.rev(mnode)
657 659 reponame = re.sub(r"\W+", "-", self.reponame)
658 660 name = "%s-%s/" % (reponame, short(cnode))
659 661
660 662 files = mf.keys()
661 663 files.sort()
662 664
663 665 if type == 'zip':
664 666 import zipfile
665 667
666 668 try:
667 669 tmp = tempfile.mkstemp()[1]
668 670 zf = zipfile.ZipFile(tmp, "w", zipfile.ZIP_DEFLATED)
669 671
670 672 for f in files:
671 673 zf.writestr(name + f, self.repo.file(f).read(mf[f]))
672 674 zf.close()
673 675
674 676 f = open(tmp, 'r')
675 677 req.httphdr('application/zip', name[:-1] + '.zip',
676 678 os.path.getsize(tmp))
677 679 req.write(f.read())
678 680 f.close()
679 681 finally:
680 682 os.unlink(tmp)
681 683
682 684 else:
683 685 import StringIO
684 686 import time
685 687 import tarfile
686 688
687 689 tf = tarfile.TarFile.open(mode='w|' + type, fileobj=req.out)
688 690 mff = self.repo.manifest.readflags(mnode)
689 691 mtime = int(time.time())
690 692
691 693 req.httphdr('application/octet-stream', name[:-1] + '.tar.' + type)
692 694 for fname in files:
693 695 rcont = self.repo.file(fname).read(mf[fname])
694 696 finfo = tarfile.TarInfo(name + fname)
695 697 finfo.mtime = mtime
696 698 finfo.size = len(rcont)
697 699 finfo.mode = mff[fname] and 0755 or 0644
698 700 tf.addfile(finfo, StringIO.StringIO(rcont))
699 701 tf.close()
700 702
701 703 # add tags to things
702 704 # tags -> list of changesets corresponding to tags
703 705 # find tag, changeset, file
704 706
705 707 def run(self, req=hgrequest()):
706 708 def header(**map):
707 709 yield self.t("header", **map)
708 710
709 711 def footer(**map):
710 712 yield self.t("footer", **map)
711 713
712 714 self.refresh()
713 715
714 716 t = self.repo.ui.config("web", "templates", templatepath())
715 717 m = os.path.join(t, "map")
716 718 style = self.repo.ui.config("web", "style", "")
717 719 if req.form.has_key('style'):
718 720 style = req.form['style'][0]
719 721 if style:
720 722 b = os.path.basename("map-" + style)
721 723 p = os.path.join(t, b)
722 724 if os.path.isfile(p):
723 725 m = p
724 726
725 727 port = req.env["SERVER_PORT"]
726 728 port = port != "80" and (":" + port) or ""
727 729 uri = req.env["REQUEST_URI"]
728 730 if "?" in uri:
729 731 uri = uri.split("?")[0]
730 732 url = "http://%s%s%s" % (req.env["SERVER_NAME"], port, uri)
731 733
732 734 self.t = templater(m, common_filters,
733 735 {"url": url,
734 736 "repo": self.reponame,
735 737 "header": header,
736 738 "footer": footer,
737 739 })
738 740
739 741 if not req.form.has_key('cmd'):
740 742 req.form['cmd'] = [self.t.cache['default'],]
741 743
742 744 if req.form['cmd'][0] == 'changelog':
743 745 c = self.repo.changelog.count() - 1
744 746 hi = c
745 747 if req.form.has_key('rev'):
746 748 hi = req.form['rev'][0]
747 749 try:
748 750 hi = self.repo.changelog.rev(self.repo.lookup(hi))
749 751 except RepoError:
750 752 req.write(self.search(hi))
751 753 return
752 754
753 755 req.write(self.changelog(hi))
754 756
755 757 elif req.form['cmd'][0] == 'changeset':
756 758 req.write(self.changeset(req.form['node'][0]))
757 759
758 760 elif req.form['cmd'][0] == 'manifest':
759 761 req.write(self.manifest(req.form['manifest'][0], req.form['path'][0]))
760 762
761 763 elif req.form['cmd'][0] == 'tags':
762 764 req.write(self.tags())
763 765
764 766 elif req.form['cmd'][0] == 'filediff':
765 767 req.write(self.filediff(req.form['file'][0], req.form['node'][0]))
766 768
767 769 elif req.form['cmd'][0] == 'file':
768 770 req.write(self.filerevision(req.form['file'][0], req.form['filenode'][0]))
769 771
770 772 elif req.form['cmd'][0] == 'annotate':
771 773 req.write(self.fileannotate(req.form['file'][0], req.form['filenode'][0]))
772 774
773 775 elif req.form['cmd'][0] == 'filelog':
774 776 req.write(self.filelog(req.form['file'][0], req.form['filenode'][0]))
775 777
776 778 elif req.form['cmd'][0] == 'heads':
777 779 req.httphdr("application/mercurial-0.1")
778 780 h = self.repo.heads()
779 781 req.write(" ".join(map(hex, h)) + "\n")
780 782
781 783 elif req.form['cmd'][0] == 'branches':
782 784 req.httphdr("application/mercurial-0.1")
783 785 nodes = []
784 786 if req.form.has_key('nodes'):
785 787 nodes = map(bin, req.form['nodes'][0].split(" "))
786 788 for b in self.repo.branches(nodes):
787 789 req.write(" ".join(map(hex, b)) + "\n")
788 790
789 791 elif req.form['cmd'][0] == 'between':
790 792 req.httphdr("application/mercurial-0.1")
791 793 nodes = []
792 794 if req.form.has_key('pairs'):
793 795 pairs = [map(bin, p.split("-"))
794 796 for p in req.form['pairs'][0].split(" ")]
795 797 for b in self.repo.between(pairs):
796 798 req.write(" ".join(map(hex, b)) + "\n")
797 799
798 800 elif req.form['cmd'][0] == 'changegroup':
799 801 req.httphdr("application/mercurial-0.1")
800 802 nodes = []
801 803 if not self.allowpull:
802 804 return
803 805
804 806 if req.form.has_key('roots'):
805 807 nodes = map(bin, req.form['roots'][0].split(" "))
806 808
807 809 z = zlib.compressobj()
808 810 f = self.repo.changegroup(nodes)
809 811 while 1:
810 812 chunk = f.read(4096)
811 813 if not chunk:
812 814 break
813 815 req.write(z.compress(chunk))
814 816
815 817 req.write(z.flush())
816 818
817 819 elif req.form['cmd'][0] == 'archive':
818 820 changeset = bin(req.form['node'][0])
819 821 type = req.form['type'][0]
820 822 if (type in self.archives and
821 823 self.repo.ui.configbool("web", "allow" + type, False)):
822 824 self.archive(req, changeset, type)
823 825 return
824 826
825 827 req.write(self.t("error"))
826 828
827 829 else:
828 830 req.write(self.t("error"))
829 831
830 832 def create_server(repo):
831 833
832 834 def openlog(opt, default):
833 835 if opt and opt != '-':
834 836 return open(opt, 'w')
835 837 return default
836 838
837 839 address = repo.ui.config("web", "address", "")
838 840 port = int(repo.ui.config("web", "port", 8000))
839 841 use_ipv6 = repo.ui.configbool("web", "ipv6")
840 842 accesslog = openlog(repo.ui.config("web", "accesslog", "-"), sys.stdout)
841 843 errorlog = openlog(repo.ui.config("web", "errorlog", "-"), sys.stderr)
842 844
843 845 import BaseHTTPServer
844 846
845 847 class IPv6HTTPServer(BaseHTTPServer.HTTPServer):
846 848 address_family = getattr(socket, 'AF_INET6', None)
847 849
848 850 def __init__(self, *args, **kwargs):
849 851 if self.address_family is None:
850 852 raise RepoError('IPv6 not available on this system')
851 853 BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs)
852 854
853 855 class hgwebhandler(BaseHTTPServer.BaseHTTPRequestHandler):
854 856 def log_error(self, format, *args):
855 857 errorlog.write("%s - - [%s] %s\n" % (self.address_string(),
856 858 self.log_date_time_string(),
857 859 format % args))
858 860
859 861 def log_message(self, format, *args):
860 862 accesslog.write("%s - - [%s] %s\n" % (self.address_string(),
861 863 self.log_date_time_string(),
862 864 format % args))
863 865
864 866 def do_POST(self):
865 867 try:
866 868 self.do_hgweb()
867 869 except socket.error, inst:
868 870 if inst.args[0] != 32:
869 871 raise
870 872
871 873 def do_GET(self):
872 874 self.do_POST()
873 875
874 876 def do_hgweb(self):
875 877 query = ""
876 878 p = self.path.find("?")
877 879 if p:
878 880 query = self.path[p + 1:]
879 881 query = query.replace('+', ' ')
880 882
881 883 env = {}
882 884 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
883 885 env['REQUEST_METHOD'] = self.command
884 886 env['SERVER_NAME'] = self.server.server_name
885 887 env['SERVER_PORT'] = str(self.server.server_port)
886 888 env['REQUEST_URI'] = "/"
887 889 if query:
888 890 env['QUERY_STRING'] = query
889 891 host = self.address_string()
890 892 if host != self.client_address[0]:
891 893 env['REMOTE_HOST'] = host
892 894 env['REMOTE_ADDR'] = self.client_address[0]
893 895
894 896 if self.headers.typeheader is None:
895 897 env['CONTENT_TYPE'] = self.headers.type
896 898 else:
897 899 env['CONTENT_TYPE'] = self.headers.typeheader
898 900 length = self.headers.getheader('content-length')
899 901 if length:
900 902 env['CONTENT_LENGTH'] = length
901 903 accept = []
902 904 for line in self.headers.getallmatchingheaders('accept'):
903 905 if line[:1] in "\t\n\r ":
904 906 accept.append(line.strip())
905 907 else:
906 908 accept = accept + line[7:].split(',')
907 909 env['HTTP_ACCEPT'] = ','.join(accept)
908 910
909 911 os.environ.update(env)
910 912
911 913 save = sys.argv, sys.stdin, sys.stdout, sys.stderr
912 914 try:
913 915 sys.stdin = self.rfile
914 916 sys.stdout = self.wfile
915 917 sys.argv = ["hgweb.py"]
916 918 if '=' not in query:
917 919 sys.argv.append(query)
918 920 self.send_response(200, "Script output follows")
919 921 hg.run()
920 922 finally:
921 923 sys.argv, sys.stdin, sys.stdout, sys.stderr = save
922 924
923 925 hg = hgweb(repo)
924 926 if use_ipv6:
925 927 return IPv6HTTPServer((address, port), hgwebhandler)
926 928 else:
927 929 return BaseHTTPServer.HTTPServer((address, port), hgwebhandler)
928 930
929 931 def server(path, name, templates, address, port, use_ipv6=False,
930 932 accesslog=sys.stdout, errorlog=sys.stderr):
931 933 httpd = create_server(path, name, templates, address, port, use_ipv6,
932 934 accesslog, errorlog)
933 935 httpd.serve_forever()
934 936
935 937 # This is a stopgap
936 938 class hgwebdir:
937 939 def __init__(self, config):
938 940 if type(config) == type([]):
939 941 self.repos = config
940 942 elif type(config) == type({}):
941 943 self.repos = config.items()
942 944 self.repos.sort()
943 945 else:
944 946 cp = ConfigParser.SafeConfigParser()
945 947 cp.read(config)
946 948 self.repos = cp.items("paths")
947 949 self.repos.sort()
948 950
949 951 def run(self, req=hgrequest()):
950 952 def header(**map):
951 953 yield tmpl("header", **map)
952 954
953 955 def footer(**map):
954 956 yield tmpl("footer", **map)
955 957
956 958 m = os.path.join(templatepath(), "map")
957 959 tmpl = templater(m, common_filters,
958 960 {"header": header, "footer": footer})
959 961
960 962 def entries(**map):
961 963 parity = 0
962 964 for name, path in self.repos:
963 965 u = ui()
964 966 u.readconfig(file(os.path.join(path, '.hg', 'hgrc')))
965 967 get = u.config
966 968
967 969 url = ('/'.join([req.env["REQUEST_URI"], name])
968 970 .replace("//", "/"))
969 971
970 972 yield dict(contact=get("web", "contact") or
971 973 get("web", "author", "unknown"),
972 974 name=get("web", "name", name),
973 975 url=url,
974 976 parity=parity,
975 977 shortdesc=get("web", "description", "unknown"),
976 978 lastupdate=os.stat(os.path.join(path, ".hg",
977 979 "00changelog.d")).st_mtime)
978 980
979 981 parity = 1 - parity
980 982
981 983 virtual = req.env.get("PATH_INFO", "").strip('/')
982 984 if virtual:
983 985 real = dict(self.repos).get(virtual)
984 986 if real:
985 987 hgweb(real).run(req)
986 988 else:
987 989 req.write(tmpl("notfound", repo=virtual))
988 990 else:
989 991 req.write(tmpl("index", entries=entries))
General Comments 0
You need to be logged in to leave comments. Login now