statichttprepo.py
250 lines
| 7.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / statichttprepo.py
mpm@selenic.com
|
r1101 | # statichttprepo.py - simple http repository class for mercurial | ||
# | ||||
# This provides read-only repo access to repositories exported via static http | ||||
# | ||||
Thomas Arendsen Hein
|
r4635 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||
mpm@selenic.com
|
r1101 | # | ||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
mpm@selenic.com
|
r1101 | |||
Gregory Szorc
|
r25978 | from __future__ import absolute_import | ||
import errno | ||||
from .i18n import _ | ||||
from . import ( | ||||
Martijn Pieters
|
r41764 | branchmap, | ||
Gregory Szorc
|
r25978 | changelog, | ||
error, | ||||
localrepo, | ||||
manifest, | ||||
namespaces, | ||||
Yuya Nishihara
|
r34944 | pathutil, | ||
Gregory Szorc
|
r41452 | pycompat, | ||
Gregory Szorc
|
r25978 | url, | ||
util, | ||||
Pierre-Yves David
|
r31241 | vfs as vfsmod, | ||
Gregory Szorc
|
r25978 | ) | ||
Bryan O'Sullivan
|
r1325 | |||
timeless
|
r28883 | urlerr = util.urlerr | ||
urlreq = util.urlreq | ||||
Augie Fackler
|
r43346 | |||
Benoit Boissinot
|
r7274 | class httprangereader(object): | ||
def __init__(self, url, opener): | ||||
# we assume opener has HTTPRangeHandler | ||||
self.url = url | ||||
self.pos = 0 | ||||
self.opener = opener | ||||
Nicolas Dumazet
|
r11066 | self.name = url | ||
Gregory Szorc
|
r27705 | |||
def __enter__(self): | ||||
return self | ||||
def __exit__(self, exc_type, exc_value, traceback): | ||||
self.close() | ||||
Benoit Boissinot
|
r7274 | def seek(self, pos): | ||
self.pos = pos | ||||
Augie Fackler
|
r43346 | |||
Benoit Boissinot
|
r7274 | def read(self, bytes=None): | ||
Gregory Szorc
|
r41452 | req = urlreq.request(pycompat.strurl(self.url)) | ||
Augie Fackler
|
r43347 | end = b'' | ||
Benoit Boissinot
|
r7274 | if bytes: | ||
end = self.pos + bytes - 1 | ||||
Alexander Boyd
|
r16882 | if self.pos or end: | ||
Gregory Szorc
|
r41452 | req.add_header(r'Range', r'bytes=%d-%s' % (self.pos, end)) | ||
Benoit Boissinot
|
r7274 | |||
Bryan O'Sullivan
|
r1325 | try: | ||
Benoit Boissinot
|
r7274 | f = self.opener.open(req) | ||
data = f.read() | ||||
Augie Fackler
|
r25196 | code = f.code | ||
timeless
|
r28883 | except urlerr.httperror as inst: | ||
Dirkjan Ochtman
|
r6028 | num = inst.code == 404 and errno.ENOENT or None | ||
raise IOError(num, inst) | ||||
timeless
|
r28883 | except urlerr.urlerror as inst: | ||
Gregory Szorc
|
r41451 | raise IOError(None, inst.reason) | ||
mpm@selenic.com
|
r1101 | |||
Patrick Mezard
|
r8612 | if code == 200: | ||
# HTTPRangeHandler does nothing if remote does not support | ||||
# Range headers and returns the full entity. Let's slice it. | ||||
if bytes: | ||||
Augie Fackler
|
r43346 | data = data[self.pos : self.pos + bytes] | ||
Patrick Mezard
|
r8612 | else: | ||
Augie Fackler
|
r43346 | data = data[self.pos :] | ||
Patrick Mezard
|
r8612 | elif bytes: | ||
Benoit Boissinot
|
r7274 | data = data[:bytes] | ||
Patrick Mezard
|
r8612 | self.pos += len(data) | ||
Benoit Boissinot
|
r7274 | return data | ||
Augie Fackler
|
r43346 | |||
Siddharth Agarwal
|
r20055 | def readlines(self): | ||
return self.read().splitlines(True) | ||||
Augie Fackler
|
r43346 | |||
Nicolas Dumazet
|
r11066 | def __iter__(self): | ||
Siddharth Agarwal
|
r20055 | return iter(self.readlines()) | ||
Augie Fackler
|
r43346 | |||
Nicolas Dumazet
|
r11066 | def close(self): | ||
pass | ||||
Benoit Boissinot
|
r7274 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36443 | # _RangeError and _HTTPRangeHandler were originally in byterange.py, | ||
# which was itself extracted from urlgrabber. See the last version of | ||||
# byterange.py from history if you need more information. | ||||
class _RangeError(IOError): | ||||
"""Error raised when an unsatisfiable range is requested.""" | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r36443 | class _HTTPRangeHandler(urlreq.basehandler): | ||
"""Handler that enables HTTP Range headers. | ||||
This was extremely simple. The Range header is a HTTP feature to | ||||
begin with so all this class does is tell urllib2 that the | ||||
"206 Partial Content" response from the HTTP server is what we | ||||
expected. | ||||
""" | ||||
def http_error_206(self, req, fp, code, msg, hdrs): | ||||
# 206 Partial Content Response | ||||
r = urlreq.addinfourl(fp, hdrs, req.get_full_url()) | ||||
r.code = code | ||||
r.msg = msg | ||||
return r | ||||
def http_error_416(self, req, fp, code, msg, hdrs): | ||||
# HTTP's Range Not Satisfiable error | ||||
Augie Fackler
|
r43347 | raise _RangeError(b'Requested Range Not Satisfiable') | ||
Augie Fackler
|
r36443 | |||
Augie Fackler
|
r43346 | |||
Benoit Boissinot
|
r7274 | def build_opener(ui, authinfo): | ||
# urllib cannot handle URLs with embedded user or passwd | ||||
urlopener = url.opener(ui, authinfo) | ||||
Augie Fackler
|
r36443 | urlopener.add_handler(_HTTPRangeHandler()) | ||
Benoit Boissinot
|
r7274 | |||
Pierre-Yves David
|
r31241 | class statichttpvfs(vfsmod.abstractvfs): | ||
Dan Villiom Podlaski Christiansen
|
r14091 | def __init__(self, base): | ||
self.base = base | ||||
r43295 | self.options = {} | |||
Dan Villiom Podlaski Christiansen
|
r14091 | |||
Augie Fackler
|
r43347 | def __call__(self, path, mode=b'r', *args, **kw): | ||
if mode not in (b'r', b'rb'): | ||||
raise IOError(b'Permission denied') | ||||
f = b"/".join((self.base, urlreq.quote(path))) | ||||
Benoit Boissinot
|
r7274 | return httprangereader(f, urlopener) | ||
FUJIWARA Katsunori
|
r17725 | def join(self, path): | ||
if path: | ||||
Yuya Nishihara
|
r34944 | return pathutil.join(self.base, path) | ||
FUJIWARA Katsunori
|
r17725 | else: | ||
return self.base | ||||
FUJIWARA Katsunori
|
r17649 | return statichttpvfs | ||
mpm@selenic.com
|
r1101 | |||
Augie Fackler
|
r43346 | |||
Peter Arrenbrecht
|
r17192 | class statichttppeer(localrepo.localpeer): | ||
def local(self): | ||||
return None | ||||
Augie Fackler
|
r43346 | |||
Sune Foldager
|
r17193 | def canpush(self): | ||
return False | ||||
Peter Arrenbrecht
|
r17192 | |||
Augie Fackler
|
r43346 | |||
class statichttprepository( | ||||
localrepo.localrepository, localrepo.revlogfilestorage | ||||
): | ||||
FUJIWARA Katsunori
|
r19778 | supported = localrepo.localrepository._basesupported | ||
mpm@selenic.com
|
r1101 | def __init__(self, ui, path): | ||
Vadim Gelfer
|
r2673 | self._url = path | ||
mpm@selenic.com
|
r1101 | self.ui = ui | ||
Benoit Boissinot
|
r3853 | |||
Nicolas Dumazet
|
r11066 | self.root = path | ||
Augie Fackler
|
r43347 | u = util.url(path.rstrip(b'/') + b"/.hg") | ||
Brodie Rao
|
r13819 | self.path, authinfo = u.authinfo() | ||
Benoit Boissinot
|
r7274 | |||
Pierre-Yves David
|
r31147 | vfsclass = build_opener(ui, authinfo) | ||
self.vfs = vfsclass(self.path) | ||||
Augie Fackler
|
r43347 | self.cachevfs = vfsclass(self.vfs.join(b'cache')) | ||
Pierre-Yves David
|
r15922 | self._phasedefaults = [] | ||
Dirkjan Ochtman
|
r6028 | |||
Ryan McElroy
|
r23561 | self.names = namespaces.namespaces() | ||
Gregory Szorc
|
r32730 | self.filtername = None | ||
r42417 | self._extrafilterid = None | |||
Sean Farley
|
r23558 | |||
Benoit Boissinot
|
r3851 | try: | ||
Gregory Szorc
|
r39730 | requirements = set(self.vfs.read(b'requires').splitlines()) | ||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Thomas Arendsen Hein
|
r7178 | if inst.errno != errno.ENOENT: | ||
raise | ||||
Adrian Buehlmann
|
r14482 | requirements = set() | ||
Thomas Arendsen Hein
|
r7178 | # check if it is a non-empty old-style repository | ||
try: | ||||
Augie Fackler
|
r43347 | fp = self.vfs(b"00changelog.i") | ||
Dan Villiom Podlaski Christiansen
|
r13400 | fp.read(1) | ||
fp.close() | ||||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Thomas Arendsen Hein
|
r7178 | if inst.errno != errno.ENOENT: | ||
raise | ||||
# we do not care about empty old-style repositories here | ||||
Augie Fackler
|
r43347 | msg = _(b"'%s' does not appear to be an hg repository") % path | ||
Matt Mackall
|
r7637 | raise error.RepoError(msg) | ||
Benoit Boissinot
|
r3851 | |||
Gregory Szorc
|
r39730 | supportedrequirements = localrepo.gathersupportedrequirements(ui) | ||
Augie Fackler
|
r43346 | localrepo.ensurerequirementsrecognized( | ||
requirements, supportedrequirements | ||||
) | ||||
Gregory Szorc
|
r39731 | localrepo.ensurerequirementscompatible(ui, requirements) | ||
Gregory Szorc
|
r39730 | |||
Benoit Boissinot
|
r3851 | # setup store | ||
Gregory Szorc
|
r39734 | self.store = localrepo.makestore(requirements, self.path, vfsclass) | ||
Matt Mackall
|
r6897 | self.spath = self.store.path | ||
Angel Ezquerra
|
r23878 | self.svfs = self.store.opener | ||
Matt Mackall
|
r6897 | self.sjoin = self.store.join | ||
Idan Kamara
|
r16115 | self._filecache = {} | ||
Peter Arrenbrecht
|
r17192 | self.requirements = requirements | ||
Benoit Boissinot
|
r3851 | |||
Gregory Szorc
|
r39799 | rootmanifest = manifest.manifestrevlog(self.svfs) | ||
Augie Fackler
|
r43346 | self.manifestlog = manifest.manifestlog( | ||
self.svfs, self, rootmanifest, self.narrowmatch() | ||||
) | ||||
Angel Ezquerra
|
r23878 | self.changelog = changelog.changelog(self.svfs) | ||
Greg Ward
|
r9146 | self._tags = None | ||
mpm@selenic.com
|
r1101 | self.nodetagscache = None | ||
Martijn Pieters
|
r41764 | self._branchcaches = branchmap.BranchMapCache() | ||
Durham Goode
|
r24373 | self._revbranchcache = None | ||
Benoit Boissinot
|
r1598 | self.encodepats = None | ||
self.decodepats = None | ||||
Durham Goode
|
r24377 | self._transref = None | ||
Peter Arrenbrecht
|
r17192 | |||
def _restrictcapabilities(self, caps): | ||||
Pierre-Yves David
|
r20962 | caps = super(statichttprepository, self)._restrictcapabilities(caps) | ||
Augie Fackler
|
r43347 | return caps.difference([b"pushkey"]) | ||
mpm@selenic.com
|
r1101 | |||
Vadim Gelfer
|
r2673 | def url(self): | ||
Matt Mackall
|
r7211 | return self._url | ||
Vadim Gelfer
|
r2673 | |||
mpm@selenic.com
|
r1101 | def local(self): | ||
return False | ||||
Vadim Gelfer
|
r2740 | |||
Peter Arrenbrecht
|
r17192 | def peer(self): | ||
return statichttppeer(self) | ||||
Gregory Szorc
|
r33605 | def wlock(self, wait=True): | ||
Augie Fackler
|
r43346 | raise error.LockUnavailable( | ||
0, | ||||
Augie Fackler
|
r43347 | _(b'lock not available'), | ||
b'lock', | ||||
_(b'cannot lock static-http repository'), | ||||
Augie Fackler
|
r43346 | ) | ||
Gregory Szorc
|
r33605 | |||
Martin Geisler
|
r7005 | def lock(self, wait=True): | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'cannot lock static-http repository')) | ||
Martin Geisler
|
r7005 | |||
Pierre-Yves David
|
r29738 | def _writecaches(self): | ||
Augie Fackler
|
r43346 | pass # statichttprepository are read only | ||
Pierre-Yves David
|
r29738 | |||
Gregory Szorc
|
r39585 | def instance(ui, path, create, intents=None, createopts=None): | ||
Vadim Gelfer
|
r2740 | if create: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'cannot create new static-http repository')) | ||
Thomas Arendsen Hein
|
r4853 | return statichttprepository(ui, path[7:]) | ||