28
|
1 # statichttprepo.py - simple http repository class for mercurial
|
|
2 #
|
|
3 # This provides read-only repo access to repositories exported via static http
|
|
4 #
|
|
5 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
|
|
6 #
|
|
7 # This software may be used and distributed according to the terms of the
|
|
8 # GNU General Public License version 2, incorporated herein by reference.
|
|
9
|
|
10 from i18n import _
|
|
11 import changelog, byterange, url, error
|
|
12 import localrepo, manifest, util, store
|
|
13 import urllib, urllib2, errno
|
|
14
|
|
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
|
|
30 try:
|
|
31 f = self.opener.open(req)
|
|
32 data = f.read()
|
|
33 if hasattr(f, 'getcode'):
|
|
34 # python 2.6+
|
|
35 code = f.getcode()
|
|
36 elif hasattr(f, 'code'):
|
|
37 # undocumented attribute, seems to be set in 2.4 and 2.5
|
|
38 code = f.code
|
|
39 else:
|
|
40 # Don't know how to check, hope for the best.
|
|
41 code = 206
|
|
42 except urllib2.HTTPError, inst:
|
|
43 num = inst.code == 404 and errno.ENOENT or None
|
|
44 raise IOError(num, inst)
|
|
45 except urllib2.URLError, inst:
|
|
46 raise IOError(None, inst.reason[1])
|
|
47
|
|
48 if code == 200:
|
|
49 # HTTPRangeHandler does nothing if remote does not support
|
|
50 # Range headers and returns the full entity. Let's slice it.
|
|
51 if bytes:
|
|
52 data = data[self.pos:self.pos + bytes]
|
|
53 else:
|
|
54 data = data[self.pos:]
|
|
55 elif bytes:
|
|
56 data = data[:bytes]
|
|
57 self.pos += len(data)
|
|
58 return data
|
|
59
|
|
60 def build_opener(ui, authinfo):
|
|
61 # urllib cannot handle URLs with embedded user or passwd
|
|
62 urlopener = url.opener(ui, authinfo)
|
|
63 urlopener.add_handler(byterange.HTTPRangeHandler())
|
|
64
|
|
65 def opener(base):
|
|
66 """return a function that opens files over http"""
|
|
67 p = base
|
|
68 def o(path, mode="r"):
|
|
69 f = "/".join((p, urllib.quote(path)))
|
|
70 return httprangereader(f, urlopener)
|
|
71 return o
|
|
72
|
|
73 return opener
|
|
74
|
|
75 class statichttprepository(localrepo.localrepository):
|
|
76 def __init__(self, ui, path):
|
|
77 self._url = path
|
|
78 self.ui = ui
|
|
79
|
|
80 self.path, authinfo = url.getauthinfo(path.rstrip('/') + "/.hg")
|
|
81
|
|
82 opener = build_opener(ui, authinfo)
|
|
83 self.opener = opener(self.path)
|
|
84
|
|
85 # find requirements
|
|
86 try:
|
|
87 requirements = self.opener("requires").read().splitlines()
|
|
88 except IOError, inst:
|
|
89 if inst.errno != errno.ENOENT:
|
|
90 raise
|
|
91 # check if it is a non-empty old-style repository
|
|
92 try:
|
|
93 self.opener("00changelog.i").read(1)
|
|
94 except IOError, inst:
|
|
95 if inst.errno != errno.ENOENT:
|
|
96 raise
|
|
97 # we do not care about empty old-style repositories here
|
|
98 msg = _("'%s' does not appear to be an hg repository") % path
|
|
99 raise error.RepoError(msg)
|
|
100 requirements = []
|
|
101
|
|
102 # check them
|
|
103 for r in requirements:
|
|
104 if r not in self.supported:
|
|
105 raise error.RepoError(_("requirement '%s' not supported") % r)
|
|
106
|
|
107 # setup store
|
|
108 def pjoin(a, b):
|
|
109 return a + '/' + b
|
|
110 self.store = store.store(requirements, self.path, opener, pjoin)
|
|
111 self.spath = self.store.path
|
|
112 self.sopener = self.store.opener
|
|
113 self.sjoin = self.store.join
|
|
114
|
|
115 self.manifest = manifest.manifest(self.sopener)
|
|
116 self.changelog = changelog.changelog(self.sopener)
|
|
117 self.tagscache = None
|
|
118 self.nodetagscache = None
|
|
119 self.encodepats = None
|
|
120 self.decodepats = None
|
|
121
|
|
122 def url(self):
|
|
123 return self._url
|
|
124
|
|
125 def local(self):
|
|
126 return False
|
|
127
|
|
128 def lock(self, wait=True):
|
|
129 raise util.Abort(_('cannot lock static-http repository'))
|
|
130
|
|
131 def instance(ui, path, create):
|
|
132 if create:
|
|
133 raise util.Abort(_('cannot create new static-http repository'))
|
|
134 return statichttprepository(ui, path[7:])
|