xref: /aosp_15_r20/prebuilts/build-tools/common/py3-stdlib/nturl2path.py (revision cda5da8d549138a6648c5ee6d7a49cf8f4a657be)
1*cda5da8dSAndroid Build Coastguard Worker"""Convert a NT pathname to a file URL and vice versa.
2*cda5da8dSAndroid Build Coastguard Worker
3*cda5da8dSAndroid Build Coastguard WorkerThis module only exists to provide OS-specific code
4*cda5da8dSAndroid Build Coastguard Workerfor urllib.requests, thus do not use directly.
5*cda5da8dSAndroid Build Coastguard Worker"""
6*cda5da8dSAndroid Build Coastguard Worker# Testing is done through test_urllib.
7*cda5da8dSAndroid Build Coastguard Worker
8*cda5da8dSAndroid Build Coastguard Workerdef url2pathname(url):
9*cda5da8dSAndroid Build Coastguard Worker    """OS-specific conversion from a relative URL of the 'file' scheme
10*cda5da8dSAndroid Build Coastguard Worker    to a file system path; not recommended for general use."""
11*cda5da8dSAndroid Build Coastguard Worker    # e.g.
12*cda5da8dSAndroid Build Coastguard Worker    #   ///C|/foo/bar/spam.foo
13*cda5da8dSAndroid Build Coastguard Worker    # and
14*cda5da8dSAndroid Build Coastguard Worker    #   ///C:/foo/bar/spam.foo
15*cda5da8dSAndroid Build Coastguard Worker    # become
16*cda5da8dSAndroid Build Coastguard Worker    #   C:\foo\bar\spam.foo
17*cda5da8dSAndroid Build Coastguard Worker    import string, urllib.parse
18*cda5da8dSAndroid Build Coastguard Worker    # Windows itself uses ":" even in URLs.
19*cda5da8dSAndroid Build Coastguard Worker    url = url.replace(':', '|')
20*cda5da8dSAndroid Build Coastguard Worker    if not '|' in url:
21*cda5da8dSAndroid Build Coastguard Worker        # No drive specifier, just convert slashes
22*cda5da8dSAndroid Build Coastguard Worker        if url[:4] == '////':
23*cda5da8dSAndroid Build Coastguard Worker            # path is something like ////host/path/on/remote/host
24*cda5da8dSAndroid Build Coastguard Worker            # convert this to \\host\path\on\remote\host
25*cda5da8dSAndroid Build Coastguard Worker            # (notice halving of slashes at the start of the path)
26*cda5da8dSAndroid Build Coastguard Worker            url = url[2:]
27*cda5da8dSAndroid Build Coastguard Worker        components = url.split('/')
28*cda5da8dSAndroid Build Coastguard Worker        # make sure not to convert quoted slashes :-)
29*cda5da8dSAndroid Build Coastguard Worker        return urllib.parse.unquote('\\'.join(components))
30*cda5da8dSAndroid Build Coastguard Worker    comp = url.split('|')
31*cda5da8dSAndroid Build Coastguard Worker    if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
32*cda5da8dSAndroid Build Coastguard Worker        error = 'Bad URL: ' + url
33*cda5da8dSAndroid Build Coastguard Worker        raise OSError(error)
34*cda5da8dSAndroid Build Coastguard Worker    drive = comp[0][-1].upper()
35*cda5da8dSAndroid Build Coastguard Worker    components = comp[1].split('/')
36*cda5da8dSAndroid Build Coastguard Worker    path = drive + ':'
37*cda5da8dSAndroid Build Coastguard Worker    for comp in components:
38*cda5da8dSAndroid Build Coastguard Worker        if comp:
39*cda5da8dSAndroid Build Coastguard Worker            path = path + '\\' + urllib.parse.unquote(comp)
40*cda5da8dSAndroid Build Coastguard Worker    # Issue #11474 - handing url such as |c/|
41*cda5da8dSAndroid Build Coastguard Worker    if path.endswith(':') and url.endswith('/'):
42*cda5da8dSAndroid Build Coastguard Worker        path += '\\'
43*cda5da8dSAndroid Build Coastguard Worker    return path
44*cda5da8dSAndroid Build Coastguard Worker
45*cda5da8dSAndroid Build Coastguard Workerdef pathname2url(p):
46*cda5da8dSAndroid Build Coastguard Worker    """OS-specific conversion from a file system path to a relative URL
47*cda5da8dSAndroid Build Coastguard Worker    of the 'file' scheme; not recommended for general use."""
48*cda5da8dSAndroid Build Coastguard Worker    # e.g.
49*cda5da8dSAndroid Build Coastguard Worker    #   C:\foo\bar\spam.foo
50*cda5da8dSAndroid Build Coastguard Worker    # becomes
51*cda5da8dSAndroid Build Coastguard Worker    #   ///C:/foo/bar/spam.foo
52*cda5da8dSAndroid Build Coastguard Worker    import urllib.parse
53*cda5da8dSAndroid Build Coastguard Worker    # First, clean up some special forms. We are going to sacrifice
54*cda5da8dSAndroid Build Coastguard Worker    # the additional information anyway
55*cda5da8dSAndroid Build Coastguard Worker    if p[:4] == '\\\\?\\':
56*cda5da8dSAndroid Build Coastguard Worker        p = p[4:]
57*cda5da8dSAndroid Build Coastguard Worker        if p[:4].upper() == 'UNC\\':
58*cda5da8dSAndroid Build Coastguard Worker            p = '\\' + p[4:]
59*cda5da8dSAndroid Build Coastguard Worker        elif p[1:2] != ':':
60*cda5da8dSAndroid Build Coastguard Worker            raise OSError('Bad path: ' + p)
61*cda5da8dSAndroid Build Coastguard Worker    if not ':' in p:
62*cda5da8dSAndroid Build Coastguard Worker        # No drive specifier, just convert slashes and quote the name
63*cda5da8dSAndroid Build Coastguard Worker        if p[:2] == '\\\\':
64*cda5da8dSAndroid Build Coastguard Worker        # path is something like \\host\path\on\remote\host
65*cda5da8dSAndroid Build Coastguard Worker        # convert this to ////host/path/on/remote/host
66*cda5da8dSAndroid Build Coastguard Worker        # (notice doubling of slashes at the start of the path)
67*cda5da8dSAndroid Build Coastguard Worker            p = '\\\\' + p
68*cda5da8dSAndroid Build Coastguard Worker        components = p.split('\\')
69*cda5da8dSAndroid Build Coastguard Worker        return urllib.parse.quote('/'.join(components))
70*cda5da8dSAndroid Build Coastguard Worker    comp = p.split(':', maxsplit=2)
71*cda5da8dSAndroid Build Coastguard Worker    if len(comp) != 2 or len(comp[0]) > 1:
72*cda5da8dSAndroid Build Coastguard Worker        error = 'Bad path: ' + p
73*cda5da8dSAndroid Build Coastguard Worker        raise OSError(error)
74*cda5da8dSAndroid Build Coastguard Worker
75*cda5da8dSAndroid Build Coastguard Worker    drive = urllib.parse.quote(comp[0].upper())
76*cda5da8dSAndroid Build Coastguard Worker    components = comp[1].split('\\')
77*cda5da8dSAndroid Build Coastguard Worker    path = '///' + drive + ':'
78*cda5da8dSAndroid Build Coastguard Worker    for comp in components:
79*cda5da8dSAndroid Build Coastguard Worker        if comp:
80*cda5da8dSAndroid Build Coastguard Worker            path = path + '/' + urllib.parse.quote(comp)
81*cda5da8dSAndroid Build Coastguard Worker    return path
82