/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: John Arbash Meinel
  • Date: 2010-03-25 12:32:24 UTC
  • mfrom: (5115 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5116.
  • Revision ID: john@arbash-meinel.com-20100325123224-km80yrpbn7cm0jcr
Merge bzr.dev to be ready for NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
    cache_utf8,
52
52
    errors,
53
53
    win32utils,
 
54
    trace,
54
55
    )
 
56
 
55
57
""")
56
58
 
57
59
# sha and md5 modules are deprecated in python2.6 but hashlib is available as
85
87
# be opened in binary mode, rather than text mode.
86
88
# On other platforms, O_BINARY doesn't exist, because
87
89
# they always open in binary mode, so it is okay to
88
 
# OR with 0 on those platforms
 
90
# OR with 0 on those platforms.
 
91
# O_NOINHERIT and O_TEXT exists only on win32 too.
89
92
O_BINARY = getattr(os, 'O_BINARY', 0)
 
93
O_TEXT = getattr(os, 'O_TEXT', 0)
 
94
O_NOINHERIT = getattr(os, 'O_NOINHERIT', 0)
90
95
 
91
96
 
92
97
def get_unicode_argv():
663
668
def sha_file_by_name(fname):
664
669
    """Calculate the SHA1 of a file by reading the full text"""
665
670
    s = sha()
666
 
    f = os.open(fname, os.O_RDONLY | O_BINARY)
 
671
    f = os.open(fname, os.O_RDONLY | O_BINARY | O_NOINHERIT)
667
672
    try:
668
673
        while True:
669
674
            b = os.read(f, 1<<16)
1346
1351
    normalized_filename = _inaccessible_normalized_filename
1347
1352
 
1348
1353
 
 
1354
def set_signal_handler(signum, handler, restart_syscall=True):
 
1355
    """A wrapper for signal.signal that also calls siginterrupt(signum, False)
 
1356
    on platforms that support that.
 
1357
 
 
1358
    :param restart_syscall: if set, allow syscalls interrupted by a signal to
 
1359
        automatically restart (by calling `signal.siginterrupt(signum,
 
1360
        False)`).  May be ignored if the feature is not available on this
 
1361
        platform or Python version.
 
1362
    """
 
1363
    old_handler = signal.signal(signum, handler)
 
1364
    if restart_syscall:
 
1365
        try:
 
1366
            siginterrupt = signal.siginterrupt
 
1367
        except AttributeError: # siginterrupt doesn't exist on this platform, or for this version of
 
1368
            # Python.
 
1369
            pass
 
1370
        else:
 
1371
            siginterrupt(signum, False)
 
1372
    return old_handler
 
1373
 
 
1374
 
1349
1375
default_terminal_width = 80
1350
1376
"""The default terminal width for ttys.
1351
1377
 
1453
1479
            # the current design -- vila 20091216
1454
1480
            pass
1455
1481
        else:
1456
 
            signal.signal(signal.SIGWINCH, _terminal_size_changed)
 
1482
            set_signal_handler(signal.SIGWINCH, _terminal_size_changed)
1457
1483
        _registered_sigwinch = True
1458
1484
 
1459
1485
 
1780
1806
            real_handlers[kind](abspath, relpath)
1781
1807
 
1782
1808
 
 
1809
def copy_ownership(dst, src=None):
 
1810
    """Copy usr/grp ownership from src file/dir to dst file/dir.
 
1811
 
 
1812
    If src is None, the containing directory is used as source. If chown
 
1813
    fails, the error is ignored and a warning is printed.
 
1814
    """
 
1815
    has_chown = getattr(os, 'chown')
 
1816
    if has_chown is None: return
 
1817
 
 
1818
    if src == None:
 
1819
        src = os.path.dirname(dst)
 
1820
        if src == '':
 
1821
            src = '.'
 
1822
 
 
1823
    try:
 
1824
        s = os.stat(src)
 
1825
        os.chown(dst, s.st_uid, s.st_gid)
 
1826
    except OSError, e:
 
1827
        trace.warning("Unable to copy ownership from '%s' to '%s': IOError: %s." % (src, dst, e))
 
1828
 
 
1829
 
 
1830
def mkdir_with_ownership(path, ownership_src=None):
 
1831
    """Create the directory 'path' with specified ownership.
 
1832
 
 
1833
    If ownership_src is given, copies (chown) usr/grp ownership
 
1834
    from 'ownership_src' to 'path'. If ownership_src is None, use the
 
1835
    containing dir ownership.
 
1836
    """
 
1837
    os.mkdir(path)
 
1838
    copy_ownership(path, ownership_src)
 
1839
 
 
1840
 
 
1841
def open_with_ownership(filename, mode='r', bufsize=-1, ownership_src=None):
 
1842
    """Open the file 'filename' with the specified ownership.
 
1843
 
 
1844
    If ownership_src is specified, copy usr/grp ownership from ownership_src
 
1845
    to filename. If ownership_src is None, copy ownership from containing
 
1846
    directory.
 
1847
    Returns the opened file object.
 
1848
    """
 
1849
    f = open(filename, mode, bufsize)
 
1850
    copy_ownership(filename, ownership_src)
 
1851
    return f
 
1852
 
 
1853
 
1783
1854
def path_prefix_key(path):
1784
1855
    """Generate a prefix-order path key for path.
1785
1856
 
2117
2188
        else:
2118
2189
            data, _ = self.encode(object, self.errors)
2119
2190
            self.stream.write(data)
 
2191
 
 
2192
if sys.platform == 'win32':
 
2193
    def open_file(filename, mode='r', bufsize=-1):
 
2194
        """This function is used to override the ``open`` builtin.
 
2195
        
 
2196
        But it uses O_NOINHERIT flag so the file handle is not inherited by
 
2197
        child processes.  Deleting or renaming a closed file opened with this
 
2198
        function is not blocking child processes.
 
2199
        """
 
2200
        writing = 'w' in mode
 
2201
        appending = 'a' in mode
 
2202
        updating = '+' in mode
 
2203
        binary = 'b' in mode
 
2204
 
 
2205
        flags = O_NOINHERIT
 
2206
        # see http://msdn.microsoft.com/en-us/library/yeby3zcb%28VS.71%29.aspx
 
2207
        # for flags for each modes.
 
2208
        if binary:
 
2209
            flags |= O_BINARY
 
2210
        else:
 
2211
            flags |= O_TEXT
 
2212
 
 
2213
        if writing:
 
2214
            if updating:
 
2215
                flags |= os.O_RDWR
 
2216
            else:
 
2217
                flags |= os.O_WRONLY
 
2218
            flags |= os.O_CREAT | os.O_TRUNC
 
2219
        elif appending:
 
2220
            if updating:
 
2221
                flags |= os.O_RDWR
 
2222
            else:
 
2223
                flags |= os.O_WRONLY
 
2224
            flags |= os.O_CREAT | os.O_APPEND
 
2225
        else: #reading
 
2226
            if updating:
 
2227
                flags |= os.O_RDWR
 
2228
            else:
 
2229
                flags |= os.O_RDONLY
 
2230
 
 
2231
        return os.fdopen(os.open(filename, flags), mode, bufsize)
 
2232
else:
 
2233
    open_file = open