~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Jelmer Vernooij
  • Date: 2011-06-16 11:57:36 UTC
  • mto: This revision was merged to the branch mainline in revision 5987.
  • Revision ID: jelmer@samba.org-20110616115736-5xuzwdf87qb81su4
Use iter_ancestry rather than get_ancestry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
 
43
43
from bzrlib import (
44
44
    cache_utf8,
45
 
    config,
46
45
    errors,
47
46
    trace,
48
47
    win32utils,
49
48
    )
50
 
from bzrlib.i18n import gettext
51
49
""")
52
50
 
53
51
from bzrlib.symbol_versioning import (
90
88
        user_encoding = get_user_encoding()
91
89
        return [a.decode(user_encoding) for a in sys.argv[1:]]
92
90
    except UnicodeDecodeError:
93
 
        raise errors.BzrError(gettext("Parameter {0!r} encoding is unsupported by {1} "
94
 
            "application locale.").format(a, user_encoding))
 
91
        raise errors.BzrError("Parameter %r encoding is unsupported by %s "
 
92
            "application locale." % (a, user_encoding))
95
93
 
96
94
 
97
95
def make_readonly(filename):
191
189
            if e.errno == errno.ENOENT:
192
190
                return False;
193
191
            else:
194
 
                raise errors.BzrError(gettext("lstat/stat of ({0!r}): {1!r}").format(f, e))
 
192
                raise errors.BzrError("lstat/stat of (%r): %r" % (f, e))
195
193
 
196
194
 
197
195
def fancy_rename(old, new, rename_func, unlink_func):
279
277
    # copy posixpath.abspath, but use os.getcwdu instead
280
278
    if not posixpath.isabs(path):
281
279
        path = posixpath.join(getcwd(), path)
282
 
    return _posix_normpath(path)
 
280
    return posixpath.normpath(path)
283
281
 
284
282
 
285
283
def _posix_realpath(path):
286
284
    return posixpath.realpath(path.encode(_fs_enc)).decode(_fs_enc)
287
285
 
288
286
 
289
 
def _posix_normpath(path):
290
 
    path = posixpath.normpath(path)
291
 
    # Bug 861008: posixpath.normpath() returns a path normalized according to
292
 
    # the POSIX standard, which stipulates (for compatibility reasons) that two
293
 
    # leading slashes must not be simplified to one, and only if there are 3 or
294
 
    # more should they be simplified as one. So we treat the leading 2 slashes
295
 
    # as a special case here by simply removing the first slash, as we consider
296
 
    # that breaking POSIX compatibility for this obscure feature is acceptable.
297
 
    # This is not a paranoid precaution, as we notably get paths like this when
298
 
    # the repo is hosted at the root of the filesystem, i.e. in "/".    
299
 
    if path.startswith('//'):
300
 
        path = path[1:]
301
 
    return path
302
 
 
303
 
 
304
287
def _win32_fixdrive(path):
305
288
    """Force drive letters to be consistent.
306
289
 
394
377
abspath = _posix_abspath
395
378
realpath = _posix_realpath
396
379
pathjoin = os.path.join
397
 
normpath = _posix_normpath
 
380
normpath = os.path.normpath
398
381
getcwd = os.getcwdu
399
382
rename = os.rename
400
383
dirname = os.path.dirname
941
924
    rps = []
942
925
    for f in ps:
943
926
        if f == '..':
944
 
            raise errors.BzrError(gettext("sorry, %r not allowed in path") % f)
 
927
            raise errors.BzrError("sorry, %r not allowed in path" % f)
945
928
        elif (f == '.') or (f == ''):
946
929
            pass
947
930
        else:
952
935
def joinpath(p):
953
936
    for f in p:
954
937
        if (f == '..') or (f is None) or (f == ''):
955
 
            raise errors.BzrError(gettext("sorry, %r not allowed in path") % f)
 
938
            raise errors.BzrError("sorry, %r not allowed in path" % f)
956
939
    return pathjoin(*p)
957
940
 
958
941
 
1002
985
def report_extension_load_failures():
1003
986
    if not _extension_load_failures:
1004
987
        return
1005
 
    if config.GlobalStack().get('ignore_missing_extensions'):
 
988
    from bzrlib.config import GlobalConfig
 
989
    if GlobalConfig().get_user_option_as_bool('ignore_missing_extensions'):
1006
990
        return
1007
991
    # the warnings framework should by default show this only once
1008
992
    from bzrlib.trace import warning
1170
1154
 
1171
1155
    if len(base) < MIN_ABS_PATHLENGTH:
1172
1156
        # must have space for e.g. a drive letter
1173
 
        raise ValueError(gettext('%r is too short to calculate a relative path')
 
1157
        raise ValueError('%r is too short to calculate a relative path'
1174
1158
            % (base,))
1175
1159
 
1176
1160
    rp = abspath(path)
2194
2178
    return file_kind_from_stat_mode(mode)
2195
2179
file_kind_from_stat_mode = file_kind_from_stat_mode_thunk
2196
2180
 
2197
 
def file_stat(f, _lstat=os.lstat):
 
2181
 
 
2182
def file_kind(f, _lstat=os.lstat):
2198
2183
    try:
2199
 
        # XXX cache?
2200
 
        return _lstat(f)
 
2184
        return file_kind_from_stat_mode(_lstat(f).st_mode)
2201
2185
    except OSError, e:
2202
2186
        if getattr(e, 'errno', None) in (errno.ENOENT, errno.ENOTDIR):
2203
2187
            raise errors.NoSuchFile(f)
2204
2188
        raise
2205
2189
 
2206
 
def file_kind(f, _lstat=os.lstat):
2207
 
    stat_value = file_stat(f, _lstat)
2208
 
    return file_kind_from_stat_mode(stat_value.st_mode)
2209
2190
 
2210
2191
def until_no_eintr(f, *a, **kw):
2211
2192
    """Run f(*a, **kw), retrying if an EINTR error occurs.
2271
2252
            termios.tcsetattr(fd, termios.TCSADRAIN, settings)
2272
2253
        return ch
2273
2254
 
2274
 
if sys.platform.startswith('linux'):
 
2255
if sys.platform == 'linux2':
2275
2256
    def _local_concurrency():
2276
2257
        try:
2277
2258
            return os.sysconf('SC_NPROCESSORS_ONLN')
2400
2381
    except UnicodeDecodeError:
2401
2382
        raise errors.BzrError("Can't decode username as %s." % \
2402
2383
                user_encoding)
2403
 
    except ImportError, e:
2404
 
        if sys.platform != 'win32':
2405
 
            raise
2406
 
        if str(e) != 'No module named pwd':
2407
 
            raise
2408
 
        # https://bugs.launchpad.net/bzr/+bug/660174
2409
 
        # getpass.getuser() is unable to return username on Windows
2410
 
        # if there is no USERNAME environment variable set.
2411
 
        # That could be true if bzr is running as a service,
2412
 
        # e.g. running `bzr serve` as a service on Windows.
2413
 
        # We should not fail with traceback in this case.
2414
 
        username = u'UNKNOWN'
2415
2384
    return username
2416
2385
 
2417
2386
 
2506
2475
    is_local_pid_dead = win32utils.is_local_pid_dead
2507
2476
else:
2508
2477
    is_local_pid_dead = _posix_is_local_pid_dead
2509
 
 
2510
 
 
2511
 
def fdatasync(fileno):
2512
 
    """Flush file contents to disk if possible.
2513
 
    
2514
 
    :param fileno: Integer OS file handle.
2515
 
    :raises TransportNotPossible: If flushing to disk is not possible.
2516
 
    """
2517
 
    fn = getattr(os, 'fdatasync', getattr(os, 'fsync', None))
2518
 
    if fn is not None:
2519
 
        fn(fileno)