/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/location.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""UI location string handling."""
19
19
 
 
20
from __future__ import absolute_import
 
21
 
20
22
import re
21
23
 
22
24
from . import (
23
25
    urlutils,
24
26
    )
25
27
from .hooks import Hooks
 
28
from .sixish import (
 
29
    PY3,
 
30
    string_types,
 
31
    )
26
32
 
27
33
 
28
34
class LocationHooks(Hooks):
39
45
 
40
46
hooks = LocationHooks()
41
47
 
42
 
def parse_rcp_location(location):
 
48
 
 
49
def rcp_location_to_url(location, scheme='ssh'):
43
50
    """Convert a rcp-style location to a URL.
44
51
 
45
52
    :param location: Location to convert, e.g. "foo:bar"
46
 
    :param scheme: URL scheme to return, defaults to "ssh"
 
53
    :param schenme: URL scheme to return, defaults to "ssh"
47
54
    :return: A URL, e.g. "ssh://foo/bar"
48
55
    :raises ValueError: if this is not a RCP-style URL
49
56
    """
52
59
        raise ValueError("Not a RCP URL")
53
60
    if m.group('path').startswith('//'):
54
61
        raise ValueError("Not a RCP URL: already looks like a URL")
55
 
    return (m.group('host'),
56
 
            m.group('user')[:-1] if m.group('user') else None,
57
 
            m.group('path'))
58
 
 
59
 
 
60
 
def rcp_location_to_url(location, scheme='ssh'):
61
 
    """Convert a rcp-style location to a URL.
62
 
 
63
 
    :param location: Location to convert, e.g. "foo:bar"
64
 
    :param scheme: URL scheme to return, defaults to "ssh"
65
 
    :return: A URL, e.g. "ssh://foo/bar"
66
 
    :raises ValueError: if this is not a RCP-style URL
67
 
    """
68
 
    (host, user, path) = parse_rcp_location(location)
69
 
    quoted_user = urlutils.quote(user) if user else None
 
62
    quoted_user = urlutils.quote(m.group('user')[:-1]) if m.group('user') else None
70
63
    url = urlutils.URL(
71
64
        scheme=scheme, quoted_user=quoted_user,
72
65
        port=None, quoted_password=None,
73
 
        quoted_host=urlutils.quote(host),
74
 
        quoted_path=urlutils.quote(path))
 
66
        quoted_host=urlutils.quote(m.group('host')),
 
67
        quoted_path=urlutils.quote(m.group('path')))
75
68
    return str(url)
76
69
 
77
70
 
78
 
def parse_cvs_location(location):
 
71
def pserver_to_url(location):
 
72
    """Convert a CVS pserver location string to a URL.
 
73
 
 
74
    :param location: pserver URL
 
75
    :return: A cvs+pserver URL
 
76
    """
79
77
    parts = location.split(':')
80
 
    if parts[0] or parts[1] not in ('pserver', 'ssh'):
 
78
    if parts[0] or parts[1] != 'pserver':
81
79
        raise ValueError('not a valid pserver location string')
82
80
    try:
83
81
        (username, hostname) = parts[2].split('@', 1)
84
82
    except IndexError:
85
83
        hostname = parts[2]
86
84
        username = None
87
 
    scheme = parts[1]
88
 
    path = parts[3]
89
 
    return (scheme, hostname, username, path)
90
 
 
91
 
 
92
 
def cvs_to_url(location):
93
 
    """Convert a CVS pserver location string to a URL.
94
 
 
95
 
    :param location: pserver URL
96
 
    :return: A cvs+pserver URL
97
 
    """
98
 
    (scheme, host, user, path) = parse_cvs_location(location)
99
85
    return str(urlutils.URL(
100
 
        scheme='cvs+' + scheme,
101
 
        quoted_user=urlutils.quote(user) if user else None,
102
 
        quoted_host=urlutils.quote(host),
 
86
        scheme='cvs+pserver',
 
87
        quoted_user=urlutils.quote(username) if username else None,
 
88
        quoted_host=urlutils.quote(hostname),
103
89
        quoted_password=None,
104
90
        port=None,
105
 
        quoted_path=urlutils.quote(path)))
 
91
        quoted_path=urlutils.quote(parts[3])))
106
92
 
107
93
 
108
94
def location_to_url(location, purpose=None):
116
102
    :raise InvalidURL: If the location is already a URL, but not valid.
117
103
    :return: Byte string with resulting URL
118
104
    """
119
 
    if not isinstance(location, str):
 
105
    if not isinstance(location, string_types):
120
106
        raise AssertionError("location not a byte or unicode string")
121
107
 
122
108
    if location.startswith(':pserver:'):
123
 
        return cvs_to_url(location)
 
109
        return pserver_to_url(location)
124
110
 
125
111
    from .directory_service import directories
126
112
    location = directories.dereference(location, purpose)
134
120
                path=location, extra='URLs must be properly escaped')
135
121
        location = urlutils.local_path_to_url(location)
136
122
    else:
137
 
        location = location.decode('ascii')
 
123
        if PY3:
 
124
            location = location.decode('ascii')
138
125
 
139
126
    if location.startswith("file:") and not location.startswith("file://"):
140
127
        return urlutils.join(urlutils.local_path_to_url("."), location[5:])