/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 bzrlib/urlutils.py

  • Committer: John Arbash Meinel
  • Date: 2006-06-22 18:24:25 UTC
  • mfrom: (1803 +trunk)
  • mto: (1793.3.6 bundle-fixes)
  • mto: This revision was merged to the branch mainline in revision 1806.
  • Revision ID: john@arbash-meinel.com-20060622182425-6f45cb7acd587216
[merge] bzr.dev 1804 and fix conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
"""A collection of function for handling URL operations."""
20
20
 
21
21
import os
22
 
from posixpath import split as _posix_split
 
22
from posixpath import split as _posix_split, normpath as _posix_normpath
23
23
import re
24
24
import sys
25
25
import urllib
165
165
    """
166
166
    # importing directly from posixpath allows us to test this 
167
167
    # on non-posix platforms
168
 
    from posixpath import normpath
169
 
    return 'file://' + escape(normpath(bzrlib.osutils._posix_abspath(path)))
 
168
    return 'file://' + escape(_posix_normpath(
 
169
        bzrlib.osutils._posix_abspath(path)))
170
170
 
171
171
 
172
172
def _win32_local_path_from_url(url):
173
 
    """Convert a url like file:///C|/path/to/foo into C:/path/to/foo"""
 
173
    """Convert a url like file:///C:/path/to/foo into C:/path/to/foo"""
174
174
    if not url.startswith('file:///'):
175
175
        raise errors.InvalidURL(url, 'local urls must start with file:///')
176
176
    # We strip off all 3 slashes
177
177
    win32_url = url[len('file:///'):]
178
 
    if (win32_url[0] not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
178
    if (win32_url[0] not in ('abcdefghijklmnopqrstuvwxyz'
 
179
                             'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
179
180
        or win32_url[1] not in  '|:'
180
181
        or win32_url[2] != '/'):
181
 
        raise errors.InvalidURL(url, 'Win32 file urls start with file:///X|/, where X is a valid drive letter')
182
 
    # TODO: jam 20060426, we could .upper() or .lower() the drive letter
183
 
    #       for better consistency.
184
 
    return win32_url[0].upper() + u':' + unescape(win32_url[2:])
 
182
        raise errors.InvalidURL(url, 'Win32 file urls start with'
 
183
                ' file:///x:/, where x is a valid drive letter')
 
184
    # Preferentially using .lower() because os.getcwd() returns
 
185
    # paths with lowercase drive letters, and that helps
 
186
    # bzrlib.osutils.relpath() work correctly
 
187
    return win32_url[0].lower() + u':' + unescape(win32_url[2:])
185
188
 
186
189
 
187
190
def _win32_local_path_to_url(path):
188
 
    """Convert a local path like ./foo into a URL like file:///C|/path/to/foo
 
191
    """Convert a local path like ./foo into a URL like file:///C:/path/to/foo
189
192
 
190
193
    This also handles transforming escaping unicode characters, etc.
191
194
    """
192
195
    # importing directly from ntpath allows us to test this 
193
 
    # on non-win32 platforms
 
196
    # on non-win32 platform
 
197
    # FIXME: It turns out that on nt, ntpath.abspath uses nt._getfullpathname
 
198
    #       which actually strips trailing space characters.
 
199
    #       The worst part is that under linux ntpath.abspath has different
 
200
    #       semantics, since 'nt' is not an available module.
194
201
    win32_path = bzrlib.osutils._nt_normpath(
195
202
        bzrlib.osutils._win32_abspath(path)).replace('\\', '/')
196
 
    return 'file:///' + win32_path[0].upper() + ':' + escape(win32_path[2:])
 
203
    return 'file:///' + win32_path[0].lower() + ':' + escape(win32_path[2:])
197
204
 
198
205
 
199
206
local_path_to_url = _posix_local_path_to_url
200
207
local_path_from_url = _posix_local_path_from_url
201
208
MIN_ABS_FILEURL_LENGTH = len('file:///')
202
 
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C|/')
 
209
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C:/')
203
210
 
204
211
if sys.platform == 'win32':
205
212
    local_path_to_url = _win32_local_path_to_url
376
383
        file:///foo/      => file:///foo
377
384
        # This is unique on win32 platforms, and is the only URL
378
385
        # format which does it differently.
379
 
        file:///C|/       => file:///C|/
 
386
        file:///c|/       => file:///c:/
380
387
    """
381
388
    if not url.endswith('/'):
382
389
        # Nothing to do