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