~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

Merge cleanup into description

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 subprocess
44
43
import tempfile
45
44
from tempfile import (
202
201
    :param old: The old path, to rename from
203
202
    :param new: The new path, to rename to
204
203
    :param rename_func: The potentially non-atomic rename function
205
 
    :param unlink_func: A way to delete the target file if the full rename
206
 
        succeeds
 
204
    :param unlink_func: A way to delete the target file if the full rename succeeds
207
205
    """
 
206
 
208
207
    # sftp rename doesn't allow overwriting, so play tricks:
209
208
    base = os.path.basename(new)
210
209
    dirname = os.path.dirname(new)
211
 
    # callers use different encodings for the paths so the following MUST
212
 
    # respect that. We rely on python upcasting to unicode if new is unicode
213
 
    # and keeping a str if not.
214
 
    tmp_name = 'tmp.%s.%.9f.%d.%s' % (base, time.time(),
215
 
                                      os.getpid(), rand_chars(10))
 
210
    tmp_name = u'tmp.%s.%.9f.%d.%s' % (base, time.time(), os.getpid(), rand_chars(10))
216
211
    tmp_name = pathjoin(dirname, tmp_name)
217
212
 
218
213
    # Rename the file out of the way, but keep track if it didn't exist
1432
1427
    _terminal_size = _ioctl_terminal_size
1433
1428
 
1434
1429
 
1435
 
def _terminal_size_changed(signum, frame):
1436
 
    """Set COLUMNS upon receiving a SIGnal for WINdow size CHange."""
1437
 
    width, height = _terminal_size(None, None)
1438
 
    if width is not None:
1439
 
        os.environ['COLUMNS'] = str(width)
1440
 
 
1441
 
if sys.platform == 'win32':
1442
 
    # Martin (gz) mentioned WINDOW_BUFFER_SIZE_RECORD from ReadConsoleInput but
1443
 
    # I've no idea how to plug that in the current design -- vila 20091216
1444
 
    pass
1445
 
else:
1446
 
    signal.signal(signal.SIGWINCH, _terminal_size_changed)
1447
 
 
1448
 
 
1449
1430
def supports_executable():
1450
1431
    return sys.platform != "win32"
1451
1432
 
2091
2072
    if use_cache:
2092
2073
        _cached_concurrency = concurrency
2093
2074
    return concurrency
2094
 
 
2095
 
 
2096
 
class UnicodeOrBytesToBytesWriter(codecs.StreamWriter):
2097
 
    """A stream writer that doesn't decode str arguments."""
2098
 
 
2099
 
    def __init__(self, encode, stream, errors='strict'):
2100
 
        codecs.StreamWriter.__init__(self, stream, errors)
2101
 
        self.encode = encode
2102
 
 
2103
 
    def write(self, object):
2104
 
        if type(object) is str:
2105
 
            self.stream.write(object)
2106
 
        else:
2107
 
            data, _ = self.encode(object, self.errors)
2108
 
            self.stream.write(data)