mercurial/statichttprepo.py
author Thomas Arendsen Hein <thomas@intevation.de>
Sun, 19 Oct 2008 12:19:55 +0200
changeset 7164 cae820101762
parent 7005 7739b61897df
child 7179 3d080733a339
permissions -rw-r--r--
Add util.popen3 fallback, simplify import of Popen3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1101
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
     1
# statichttprepo.py - simple http repository class for mercurial
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
     2
#
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
     3
# This provides read-only repo access to repositories exported via static http
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
     4
#
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4258
diff changeset
     5
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
1101
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
     6
#
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
     7
# This software may be used and distributed according to the terms
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
     8
# of the GNU General Public License, incorporated herein by reference.
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
     9
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
    10
from i18n import _
6212
e75aab656f46 Remove unused imports
Joel Rosdahl <joel@rosdahl.net>
parents: 6028
diff changeset
    11
import changelog, httprangereader
6839
01db3e101362 move filename encoding functions from util.py to new store.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 6312
diff changeset
    12
import repo, localrepo, manifest, util, store
6028
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    13
import urllib, urllib2, errno
1325
57220daf40e9 Move urllib error handling from revlog into statichttprepo, where it belongs.
Bryan O'Sullivan <bos@serpentine.com>
parents: 1101
diff changeset
    14
57220daf40e9 Move urllib error handling from revlog into statichttprepo, where it belongs.
Bryan O'Sullivan <bos@serpentine.com>
parents: 1101
diff changeset
    15
class rangereader(httprangereader.httprangereader):
57220daf40e9 Move urllib error handling from revlog into statichttprepo, where it belongs.
Bryan O'Sullivan <bos@serpentine.com>
parents: 1101
diff changeset
    16
    def read(self, size=None):
57220daf40e9 Move urllib error handling from revlog into statichttprepo, where it belongs.
Bryan O'Sullivan <bos@serpentine.com>
parents: 1101
diff changeset
    17
        try:
57220daf40e9 Move urllib error handling from revlog into statichttprepo, where it belongs.
Bryan O'Sullivan <bos@serpentine.com>
parents: 1101
diff changeset
    18
            return httprangereader.httprangereader.read(self, size)
1821
0b3f4be5c5bf Catch urllib errors for old-http in a nicer way.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1598
diff changeset
    19
        except urllib2.HTTPError, inst:
6028
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    20
            num = inst.code == 404 and errno.ENOENT or None
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    21
            raise IOError(num, inst)
1325
57220daf40e9 Move urllib error handling from revlog into statichttprepo, where it belongs.
Bryan O'Sullivan <bos@serpentine.com>
parents: 1101
diff changeset
    22
        except urllib2.URLError, inst:
1821
0b3f4be5c5bf Catch urllib errors for old-http in a nicer way.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1598
diff changeset
    23
            raise IOError(None, inst.reason[1])
1101
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    24
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    25
def opener(base):
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    26
    """return a function that opens files over http"""
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    27
    p = base
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    28
    def o(path, mode="r"):
3794
630caaf29815 use forward "/" for internal path and static http, fix issue437
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3791
diff changeset
    29
        f = "/".join((p, urllib.quote(path)))
1325
57220daf40e9 Move urllib error handling from revlog into statichttprepo, where it belongs.
Bryan O'Sullivan <bos@serpentine.com>
parents: 1101
diff changeset
    30
        return rangereader(f)
1101
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    31
    return o
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    32
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    33
class statichttprepository(localrepo.localrepository):
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    34
    def __init__(self, ui, path):
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2072
diff changeset
    35
        self._url = path
1101
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    36
        self.ui = ui
3853
c0b449154a90 switch to the .hg/store layout, fix the tests
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3851
diff changeset
    37
5315
121f961b358c statichttprepo: fix calls on '/' URI (issue 747)
Paul Bx <pb@e-scribe.com>
parents: 4635
diff changeset
    38
        self.path = path.rstrip('/') + "/.hg"
1101
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    39
        self.opener = opener(self.path)
6028
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    40
3851
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    41
        # find requirements
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    42
        try:
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    43
            requirements = self.opener("requires").read().splitlines()
6028
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    44
        except IOError, inst:
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    45
            if inst.errno == errno.ENOENT:
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    46
                msg = _("'%s' does not appear to be an hg repository") % path
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    47
                raise repo.RepoError(msg)
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    48
            else:
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    49
                requirements = []
6605a03cbf87 make static-http work with empty repos (issue965)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5321
diff changeset
    50
3851
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    51
        # check them
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    52
        for r in requirements:
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    53
            if r not in self.supported:
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    54
                raise repo.RepoError(_("requirement '%s' not supported") % r)
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    55
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    56
        # setup store
6988
907e4e9bd3c4 Fix for Issue1260
Adrian Buehlmann <adrian@cadifra.com>
parents: 6897
diff changeset
    57
        def pjoin(a, b):
907e4e9bd3c4 Fix for Issue1260
Adrian Buehlmann <adrian@cadifra.com>
parents: 6897
diff changeset
    58
            return a + '/' + b
907e4e9bd3c4 Fix for Issue1260
Adrian Buehlmann <adrian@cadifra.com>
parents: 6897
diff changeset
    59
        self.store = store.store(requirements, self.path, opener, pjoin)
6897
faea0d27e38f statichttp: use store class
Matt Mackall <mpm@selenic.com>
parents: 6840
diff changeset
    60
        self.spath = self.store.path
faea0d27e38f statichttp: use store class
Matt Mackall <mpm@selenic.com>
parents: 6840
diff changeset
    61
        self.sopener = self.store.opener
faea0d27e38f statichttp: use store class
Matt Mackall <mpm@selenic.com>
parents: 6840
diff changeset
    62
        self.sjoin = self.store.join
3851
8f18e31c4441 add "requires" file to the repo, specifying the requirements
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3794
diff changeset
    63
3791
8643b9f90b51 introduce localrepo.spath for the store path, sopener fixes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3457
diff changeset
    64
        self.manifest = manifest.manifest(self.sopener)
8643b9f90b51 introduce localrepo.spath for the store path, sopener fixes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3457
diff changeset
    65
        self.changelog = changelog.changelog(self.sopener)
1101
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    66
        self.tagscache = None
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    67
        self.nodetagscache = None
1598
14d1f1868bf6 cleanup of revlog.group when repository is local
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1325
diff changeset
    68
        self.encodepats = None
14d1f1868bf6 cleanup of revlog.group when repository is local
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1325
diff changeset
    69
        self.decodepats = None
1101
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    70
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2072
diff changeset
    71
    def url(self):
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2072
diff changeset
    72
        return 'static-' + self._url
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2072
diff changeset
    73
1101
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    74
    def local(self):
2cf5c8a4eae5 Separate out old-http support
mpm@selenic.com
parents:
diff changeset
    75
        return False
2740
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
    76
7005
7739b61897df do not pretend to lock static-http repositories (issue994)
Martin Geisler <mg@daimi.au.dk>
parents: 6988
diff changeset
    77
    def lock(self, wait=True):
7739b61897df do not pretend to lock static-http repositories (issue994)
Martin Geisler <mg@daimi.au.dk>
parents: 6988
diff changeset
    78
        raise util.Abort(_('cannot lock static-http repository'))
7739b61897df do not pretend to lock static-http repositories (issue994)
Martin Geisler <mg@daimi.au.dk>
parents: 6988
diff changeset
    79
2740
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
    80
def instance(ui, path, create):
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
    81
    if create:
386f04d6ecb3 clean up hg.py: move repo constructor code into each repo module
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
    82
        raise util.Abort(_('cannot create new static-http repository'))
4853
bf10a03a6b24 Removed deprecated hg:// and old-http:// protocols (issue406)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4635
diff changeset
    83
    return statichttprepository(ui, path[7:])