/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/osutils.py

  • Committer: Andrew Bennetts
  • Date: 2010-04-29 05:52:35 UTC
  • mfrom: (5192 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5193.
  • Revision ID: andrew.bennetts@canonical.com-20100429055235-24e81jfdse3h3ugt
MergeĀ lp:bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
from shutil import (
40
40
    rmtree,
41
41
    )
42
 
import signal
43
42
import socket
44
43
import subprocess
45
44
import tempfile
363
362
    return _win32_fixdrive(tempfile.mkdtemp(*args, **kwargs).replace('\\', '/'))
364
363
 
365
364
 
 
365
def _add_rename_error_details(e, old, new):
 
366
    new_e = OSError(e.errno, "failed to rename %s to %s: %s"
 
367
        % (old, new, e.strerror))
 
368
    new_e.filename = old
 
369
    new_e.to_filename = new
 
370
    return new_e
 
371
 
 
372
 
366
373
def _win32_rename(old, new):
367
374
    """We expect to be able to atomically replace 'new' with old.
368
375
 
370
377
    and then deleted.
371
378
    """
372
379
    try:
373
 
        fancy_rename(old, new, rename_func=os.rename, unlink_func=os.unlink)
 
380
        fancy_rename(old, new, rename_func=_wrapped_rename, unlink_func=os.unlink)
374
381
    except OSError, e:
375
382
        if e.errno in (errno.EPERM, errno.EACCES, errno.EBUSY, errno.EINVAL):
376
383
            # If we try to rename a non-existant file onto cwd, we get
381
388
        raise
382
389
 
383
390
 
 
391
def _wrapped_rename(old, new):
 
392
    """Rename a file or directory"""
 
393
    try:
 
394
        os.rename(old, new)
 
395
    except (IOError, OSError), e:
 
396
        # this is eventually called by all rename-like functions, so should 
 
397
        # catch all of them
 
398
        raise _add_rename_error_details(e, old, new)
 
399
 
 
400
 
384
401
def _mac_getcwd():
385
402
    return unicodedata.normalize('NFC', os.getcwdu())
386
403
 
391
408
realpath = _posix_realpath
392
409
pathjoin = os.path.join
393
410
normpath = os.path.normpath
 
411
rename = _wrapped_rename # overridden below on win32
394
412
getcwd = os.getcwdu
395
 
rename = os.rename
396
413
dirname = os.path.dirname
397
414
basename = os.path.basename
398
415
split = os.path.split
1366
1383
        platform or Python version.
1367
1384
    """
1368
1385
    try:
 
1386
        import signal
1369
1387
        siginterrupt = signal.siginterrupt
 
1388
    except ImportError:
 
1389
        # This python implementation doesn't provide signal support, hence no
 
1390
        # handler exists
 
1391
        return None
1370
1392
    except AttributeError:
1371
1393
        # siginterrupt doesn't exist on this platform, or for this version
1372
1394
        # of Python.
1483
1505
 
1484
1506
 
1485
1507
_registered_sigwinch = False
1486
 
 
1487
1508
def watch_sigwinch():
1488
 
    """Register for SIGWINCH, once and only once."""
 
1509
    """Register for SIGWINCH, once and only once.
 
1510
 
 
1511
    Do nothing if the signal module is not available.
 
1512
    """
1489
1513
    global _registered_sigwinch
1490
1514
    if not _registered_sigwinch:
1491
 
        if sys.platform == 'win32':
1492
 
            # Martin (gz) mentioned WINDOW_BUFFER_SIZE_RECORD from
1493
 
            # ReadConsoleInput but I've no idea how to plug that in
1494
 
            # the current design -- vila 20091216
 
1515
        try:
 
1516
            import signal
 
1517
            if getattr(signal, "SIGWINCH", None) is not None:
 
1518
                set_signal_handler(signal.SIGWINCH, _terminal_size_changed)
 
1519
        except ImportError:
 
1520
            # python doesn't provide signal support, nothing we can do about it
1495
1521
            pass
1496
 
        else:
1497
 
            set_signal_handler(signal.SIGWINCH, _terminal_size_changed)
1498
1522
        _registered_sigwinch = True
1499
1523
 
1500
1524
 
1821
1845
            real_handlers[kind](abspath, relpath)
1822
1846
 
1823
1847
 
1824
 
def copy_ownership(dst, src=None):
 
1848
def copy_ownership_from_path(dst, src=None):
1825
1849
    """Copy usr/grp ownership from src file/dir to dst file/dir.
1826
1850
 
1827
1851
    If src is None, the containing directory is used as source. If chown
1843
1867
        trace.warning("Unable to copy ownership from '%s' to '%s': IOError: %s." % (src, dst, e))
1844
1868
 
1845
1869
 
1846
 
def mkdir_with_ownership(path, ownership_src=None):
1847
 
    """Create the directory 'path' with specified ownership.
1848
 
 
1849
 
    If ownership_src is given, copies (chown) usr/grp ownership
1850
 
    from 'ownership_src' to 'path'. If ownership_src is None, use the
1851
 
    containing dir ownership.
1852
 
    """
1853
 
    os.mkdir(path)
1854
 
    copy_ownership(path, ownership_src)
1855
 
 
1856
 
 
1857
 
def open_with_ownership(filename, mode='r', bufsize=-1, ownership_src=None):
1858
 
    """Open the file 'filename' with the specified ownership.
1859
 
 
1860
 
    If ownership_src is specified, copy usr/grp ownership from ownership_src
1861
 
    to filename. If ownership_src is None, copy ownership from containing
1862
 
    directory.
1863
 
    Returns the opened file object.
1864
 
    """
1865
 
    f = open(filename, mode, bufsize)
1866
 
    copy_ownership(filename, ownership_src)
1867
 
    return f
1868
 
 
1869
 
 
1870
1870
def path_prefix_key(path):
1871
1871
    """Generate a prefix-order path key for path.
1872
1872