~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/urlutils.py

  • Committer: Robey Pointer
  • Date: 2006-07-01 19:03:33 UTC
  • mfrom: (1829 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1830.
  • Revision ID: robey@lag.net-20060701190333-f58465aec4bd3412
merge from bzr.dev

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.
 
182
        raise errors.InvalidURL(url, 'Win32 file urls start with'
 
183
                ' file:///x:/, where x is a valid drive letter')
184
184
    return win32_url[0].upper() + u':' + unescape(win32_url[2:])
185
185
 
186
186
 
187
187
def _win32_local_path_to_url(path):
188
 
    """Convert a local path like ./foo into a URL like file:///C|/path/to/foo
 
188
    """Convert a local path like ./foo into a URL like file:///C:/path/to/foo
189
189
 
190
190
    This also handles transforming escaping unicode characters, etc.
191
191
    """
192
192
    # importing directly from ntpath allows us to test this 
193
 
    # on non-win32 platforms
 
193
    # on non-win32 platform
 
194
    # FIXME: It turns out that on nt, ntpath.abspath uses nt._getfullpathname
 
195
    #       which actually strips trailing space characters.
 
196
    #       The worst part is that under linux ntpath.abspath has different
 
197
    #       semantics, since 'nt' is not an available module.
194
198
    win32_path = bzrlib.osutils._nt_normpath(
195
199
        bzrlib.osutils._win32_abspath(path)).replace('\\', '/')
196
200
    return 'file:///' + win32_path[0].upper() + ':' + escape(win32_path[2:])
199
203
local_path_to_url = _posix_local_path_to_url
200
204
local_path_from_url = _posix_local_path_from_url
201
205
MIN_ABS_FILEURL_LENGTH = len('file:///')
202
 
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C|/')
 
206
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C:/')
203
207
 
204
208
if sys.platform == 'win32':
205
209
    local_path_to_url = _win32_local_path_to_url
376
380
        file:///foo/      => file:///foo
377
381
        # This is unique on win32 platforms, and is the only URL
378
382
        # format which does it differently.
379
 
        file:///C|/       => file:///C|/
 
383
        file:///c|/       => file:///c:/
380
384
    """
381
385
    if not url.endswith('/'):
382
386
        # Nothing to do