##// END OF EJS Templates
statichttprepo: cleanups, use url.py (proxy, password support)...
Benoit Boissinot -
r7274:95f3694c default
parent child Browse files
Show More
@@ -1,90 +1,117 b''
1 1 # statichttprepo.py - simple http repository class for mercurial
2 2 #
3 3 # This provides read-only repo access to repositories exported via static http
4 4 #
5 5 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
6 6 #
7 7 # This software may be used and distributed according to the terms
8 8 # of the GNU General Public License, incorporated herein by reference.
9 9
10 10 from i18n import _
11 import changelog, httprangereader
11 import changelog, byterange, url
12 12 import repo, localrepo, manifest, util, store
13 13 import urllib, urllib2, errno
14 14
15 class rangereader(httprangereader.httprangereader):
16 def read(self, size=None):
15 class httprangereader(object):
16 def __init__(self, url, opener):
17 # we assume opener has HTTPRangeHandler
18 self.url = url
19 self.pos = 0
20 self.opener = opener
21 def seek(self, pos):
22 self.pos = pos
23 def read(self, bytes=None):
24 req = urllib2.Request(self.url)
25 end = ''
26 if bytes:
27 end = self.pos + bytes - 1
28 req.add_header('Range', 'bytes=%d-%s' % (self.pos, end))
29
17 30 try:
18 return httprangereader.httprangereader.read(self, size)
31 f = self.opener.open(req)
32 data = f.read()
19 33 except urllib2.HTTPError, inst:
20 34 num = inst.code == 404 and errno.ENOENT or None
21 35 raise IOError(num, inst)
22 36 except urllib2.URLError, inst:
23 37 raise IOError(None, inst.reason[1])
24 38
25 def opener(base):
26 """return a function that opens files over http"""
27 p = base
28 def o(path, mode="r"):
29 f = "/".join((p, urllib.quote(path)))
30 return rangereader(f)
31 return o
39 if bytes:
40 data = data[:bytes]
41 return data
42
43 def build_opener(ui, authinfo):
44 # urllib cannot handle URLs with embedded user or passwd
45 urlopener = url.opener(ui, authinfo)
46 urlopener.add_handler(byterange.HTTPRangeHandler())
47
48 def opener(base):
49 """return a function that opens files over http"""
50 p = base
51 def o(path, mode="r"):
52 f = "/".join((p, urllib.quote(path)))
53 return httprangereader(f, urlopener)
54 return o
55
56 return opener
32 57
33 58 class statichttprepository(localrepo.localrepository):
34 59 def __init__(self, ui, path):
35 60 self._url = path
36 61 self.ui = ui
37 62
38 self.path = path.rstrip('/') + "/.hg"
63 self.path, authinfo = url.getauthinfo(path.rstrip('/') + "/.hg")
64
65 opener = build_opener(ui, authinfo)
39 66 self.opener = opener(self.path)
40 67
41 68 # find requirements
42 69 try:
43 70 requirements = self.opener("requires").read().splitlines()
44 71 except IOError, inst:
45 72 if inst.errno != errno.ENOENT:
46 73 raise
47 74 # check if it is a non-empty old-style repository
48 75 try:
49 76 self.opener("00changelog.i").read(1)
50 77 except IOError, inst:
51 78 if inst.errno != errno.ENOENT:
52 79 raise
53 80 # we do not care about empty old-style repositories here
54 81 msg = _("'%s' does not appear to be an hg repository") % path
55 82 raise repo.RepoError(msg)
56 83 requirements = []
57 84
58 85 # check them
59 86 for r in requirements:
60 87 if r not in self.supported:
61 88 raise repo.RepoError(_("requirement '%s' not supported") % r)
62 89
63 90 # setup store
64 91 def pjoin(a, b):
65 92 return a + '/' + b
66 93 self.store = store.store(requirements, self.path, opener, pjoin)
67 94 self.spath = self.store.path
68 95 self.sopener = self.store.opener
69 96 self.sjoin = self.store.join
70 97
71 98 self.manifest = manifest.manifest(self.sopener)
72 99 self.changelog = changelog.changelog(self.sopener)
73 100 self.tagscache = None
74 101 self.nodetagscache = None
75 102 self.encodepats = None
76 103 self.decodepats = None
77 104
78 105 def url(self):
79 106 return self._url
80 107
81 108 def local(self):
82 109 return False
83 110
84 111 def lock(self, wait=True):
85 112 raise util.Abort(_('cannot lock static-http repository'))
86 113
87 114 def instance(ui, path, create):
88 115 if create:
89 116 raise util.Abort(_('cannot create new static-http repository'))
90 117 return statichttprepository(ui, path[7:])
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now