/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-07-28 02:47:10 UTC
  • mfrom: (7519.1.1 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200728024710-a2ylds219f1lsl62
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/388173

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