hgext/schemes.py
author Alex Orange <crazycasta@gmail.com>
Mon, 12 Jan 2015 18:01:20 -0700
changeset 23834 bf07c19b4c82
parent 18910 b52404a914a9
child 25186 80c5b2666a96
permissions -rw-r--r--
https: support tls sni (server name indication) for https urls (issue3090) SNI is a common way of sharing servers across multiple domains using separate SSL certificates. As of Python 2.7.9 SSLContext has been backported from Python 3. This patch changes sslutil's ssl_wrap_socket to use SSLContext and take a server hostname as and argument. It also changes the url module to make use of this argument. The new code for 2.7.9 achieves it's task by attempting to get the SSLContext object from the ssl module. If this fails the try/except goes back to what was there before with the exception that the ssl_wrap_socket functions take a server_hostname argument that doesn't get used. Assuming the SSLContext exists, the arguments to wrap_socket at the module level are emulated on the SSLContext. The SSLContext is initialized with the specified ssl_version. If certfile is not None load_cert_chain is called with certfile and keyfile. keyfile being None is not a problem, load_cert_chain will simply expect the private key to be in the certificate file. verify_mode is set to cert_reqs. If ca_certs is not None load_verify_locations is called with ca_certs as the cafile. Finally the wrap_socket method of the SSLContext is called with the socket and server hostname. Finally, this fails test-check-commit-hg.t because the "new" function ssl_wrap_socket has underscores in its names and underscores in its arguments. All the underscore identifiers are taken from the other functions and as such can't be changed to match naming conventions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     1
# Copyright 2009, Alexander Solovyov <piranha@piranha.org.ua>
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     2
#
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     3
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10070
diff changeset
     4
# GNU General Public License version 2 or any later version.
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     5
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     6
"""extend schemes with shortcuts to repository swarms
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     7
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     8
This extension allows you to specify shortcuts for parent URLs with a
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     9
lot of repositories to act like a scheme, for example::
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    10
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    11
  [schemes]
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    12
  py = http://code.python.org/hg/
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    13
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    14
After that you can use it like::
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    15
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    16
  hg clone py://trunk/
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    17
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    18
Additionally there is support for some more complex schemas, for
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    19
example used by Google Code::
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    20
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    21
  [schemes]
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    22
  gcode = http://{1}.googlecode.com/hg/
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    23
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    24
The syntax is taken from Mercurial templates, and you have unlimited
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    25
number of variables, starting with ``{1}`` and continuing with
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    26
``{2}``, ``{3}`` and so on. This variables will receive parts of URL
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    27
supplied, split by ``/``. Anything not specified as ``{part}`` will be
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    28
just appended to an URL.
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    29
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    30
For convenience, the extension adds these schemes by default::
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    31
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    32
  [schemes]
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    33
  py = http://hg.python.org/
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    34
  bb = https://bitbucket.org/
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    35
  bb+ssh = ssh://hg@bitbucket.org/
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    36
  gcode = https://{1}.googlecode.com/hg/
10777
bdc3256a318e schemes: add Kiln On Demand to default schemes
Benjamin Pollack <benjamin@bitquabit.com>
parents: 10282
diff changeset
    37
  kiln = https://{1}.kilnhg.com/Repo/
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    38
9965
963ed04a8fde schemes: fixed typos in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9964
diff changeset
    39
You can override a predefined scheme by defining a new scheme with the
963ed04a8fde schemes: fixed typos in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9964
diff changeset
    40
same name.
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    41
"""
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    42
13822
fbf32a6c903e schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents: 10777
diff changeset
    43
import os, re
14076
924c82157d46 url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
    44
from mercurial import extensions, hg, templater, util
13822
fbf32a6c903e schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents: 10777
diff changeset
    45
from mercurial.i18n import _
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    46
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 15609
diff changeset
    47
testedwith = 'internal'
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 15609
diff changeset
    48
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    49
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    50
class ShortRepository(object):
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    51
    def __init__(self, url, scheme, templater):
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    52
        self.scheme = scheme
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    53
        self.templater = templater
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    54
        self.url = url
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    55
        try:
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    56
            self.parts = max(map(int, re.findall(r'\{(\d+)\}', self.url)))
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    57
        except ValueError:
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    58
            self.parts = 0
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    59
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    60
    def __repr__(self):
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    61
        return '<ShortRepository: %s>' % self.scheme
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    62
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    63
    def instance(self, ui, url, create):
17425
e95ec38f86b0 fix wording and not-completely-trivial spelling errors and bad docstrings
Mads Kiilerich <mads@kiilerich.com>
parents: 16743
diff changeset
    64
        # Should this use the util.url class, or is manual parsing better?
18910
b52404a914a9 scheme: don't crash on invalid URLs
Mads Kiilerich <madski@unity3d.com>
parents: 17425
diff changeset
    65
        try:
b52404a914a9 scheme: don't crash on invalid URLs
Mads Kiilerich <madski@unity3d.com>
parents: 17425
diff changeset
    66
            url = url.split('://', 1)[1]
b52404a914a9 scheme: don't crash on invalid URLs
Mads Kiilerich <madski@unity3d.com>
parents: 17425
diff changeset
    67
        except IndexError:
b52404a914a9 scheme: don't crash on invalid URLs
Mads Kiilerich <madski@unity3d.com>
parents: 17425
diff changeset
    68
            raise util.Abort(_("no '://' in scheme url '%s'") % url)
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    69
        parts = url.split('/', self.parts)
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    70
        if len(parts) > self.parts:
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    71
            tail = parts[-1]
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    72
            parts = parts[:-1]
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    73
        else:
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    74
            tail = ''
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    75
        context = dict((str(i + 1), v) for i, v in enumerate(parts))
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    76
        url = ''.join(self.templater.process(self.url, context)) + tail
14568
5f002e3336ba hg: split peer and repo lookup tables
Matt Mackall <mpm@selenic.com>
parents: 14076
diff changeset
    77
        return hg._peerlookup(url).instance(ui, url, create)
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    78
13827
f1823b9f073b url: nuke some newly-introduced underbars in identifiers
Matt Mackall <mpm@selenic.com>
parents: 13822
diff changeset
    79
def hasdriveletter(orig, path):
15609
8f4bad72d8b1 util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents: 14606
diff changeset
    80
    if path:
8f4bad72d8b1 util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents: 14606
diff changeset
    81
        for scheme in schemes:
8f4bad72d8b1 util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents: 14606
diff changeset
    82
            if path.startswith(scheme + ':'):
8f4bad72d8b1 util: fix url.__str__() for windows file URLs
Patrick Mezard <pmezard@gmail.com>
parents: 14606
diff changeset
    83
                return False
13822
fbf32a6c903e schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents: 10777
diff changeset
    84
    return orig(path)
fbf32a6c903e schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents: 10777
diff changeset
    85
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    86
schemes = {
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    87
    'py': 'http://hg.python.org/',
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    88
    'bb': 'https://bitbucket.org/',
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    89
    'bb+ssh': 'ssh://hg@bitbucket.org/',
10777
bdc3256a318e schemes: add Kiln On Demand to default schemes
Benjamin Pollack <benjamin@bitquabit.com>
parents: 10282
diff changeset
    90
    'gcode': 'https://{1}.googlecode.com/hg/',
bdc3256a318e schemes: add Kiln On Demand to default schemes
Benjamin Pollack <benjamin@bitquabit.com>
parents: 10282
diff changeset
    91
    'kiln': 'https://{1}.kilnhg.com/Repo/'
9964
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    92
    }
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    93
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    94
def extsetup(ui):
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    95
    schemes.update(dict(ui.configitems('schemes')))
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    96
    t = templater.engine(lambda x: x)
e600ad9bc257 schemes extension
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    97
    for scheme, url in schemes.items():
13822
fbf32a6c903e schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents: 10777
diff changeset
    98
        if (os.name == 'nt' and len(scheme) == 1 and scheme.isalpha()
fbf32a6c903e schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents: 10777
diff changeset
    99
            and os.path.exists('%s:\\' % scheme)):
fbf32a6c903e schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents: 10777
diff changeset
   100
            raise util.Abort(_('custom scheme %s:// conflicts with drive '
fbf32a6c903e schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents: 10777
diff changeset
   101
                               'letter %s:\\\n') % (scheme, scheme.upper()))
14606
6e631c24c6d9 hg: move peerschemes back to schemes
Matt Mackall <mpm@selenic.com>
parents: 14605
diff changeset
   102
        hg.schemes[scheme] = ShortRepository(url, scheme, t)
13822
fbf32a6c903e schemes: prevent one letter schemes from being interpreted as drive letters
Brodie Rao <brodie@bitheap.org>
parents: 10777
diff changeset
   103
14076
924c82157d46 url: move URL parsing functions into util to improve startup time
Brodie Rao <brodie@bitheap.org>
parents: 13827
diff changeset
   104
    extensions.wrapfunction(util, 'hasdriveletter', hasdriveletter)