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