~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

Merge propagate-exceptions into http-leaks resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import getpass
29
29
import ntpath
30
30
import posixpath
 
31
# We need to import both shutil and rmtree as we export the later on posix
 
32
# and need the former on windows
31
33
import shutil
 
34
from shutil import rmtree
32
35
import socket
33
36
import subprocess
 
37
# We need to import both tempfile and mkdtemp as we export the later on posix
 
38
# and need the former on windows
34
39
import tempfile
 
40
from tempfile import mkdtemp
35
41
import unicodedata
36
42
 
37
43
from bzrlib import (
346
352
 
347
353
 
348
354
def _win32_mkdtemp(*args, **kwargs):
349
 
    return _win32_fixdrive(tempfile.mkdtemp(*args, **kwargs).replace('\\', '/'))
 
355
    return _win32_fixdrive(mkdtemp(*args, **kwargs).replace('\\', '/'))
350
356
 
351
357
 
352
358
def _win32_rename(old, new):
383
389
basename = os.path.basename
384
390
split = os.path.split
385
391
splitext = os.path.splitext
386
 
mkdtemp = tempfile.mkdtemp
387
 
rmtree = shutil.rmtree
 
392
# These were already lazily imported into local scope
 
393
# mkdtemp = tempfile.mkdtemp
 
394
# rmtree = shutil.rmtree
388
395
 
389
396
MIN_ABS_PATHLENGTH = 1
390
397
 
428
435
    getcwd = _mac_getcwd
429
436
 
430
437
 
431
 
def get_terminal_encoding():
 
438
def get_terminal_encoding(trace=False):
432
439
    """Find the best encoding for printing to the screen.
433
440
 
434
441
    This attempts to check both sys.stdout and sys.stdin to see
440
447
 
441
448
    On my standard US Windows XP, the preferred encoding is
442
449
    cp1252, but the console is cp437
 
450
 
 
451
    :param trace: If True trace the selected encoding via mutter().
443
452
    """
444
453
    from bzrlib.trace import mutter
445
454
    output_encoding = getattr(sys.stdout, 'encoding', None)
447
456
        input_encoding = getattr(sys.stdin, 'encoding', None)
448
457
        if not input_encoding:
449
458
            output_encoding = get_user_encoding()
450
 
            mutter('encoding stdout as osutils.get_user_encoding() %r',
 
459
            if trace:
 
460
                mutter('encoding stdout as osutils.get_user_encoding() %r',
451
461
                   output_encoding)
452
462
        else:
453
463
            output_encoding = input_encoding
454
 
            mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
 
464
            if trace:
 
465
                mutter('encoding stdout as sys.stdin encoding %r',
 
466
                    output_encoding)
455
467
    else:
456
 
        mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
 
468
        if trace:
 
469
            mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
457
470
    if output_encoding == 'cp0':
458
471
        # invalid encoding (cp0 means 'no codepage' on Windows)
459
472
        output_encoding = get_user_encoding()
460
 
        mutter('cp0 is invalid encoding.'
 
473
        if trace:
 
474
            mutter('cp0 is invalid encoding.'
461
475
               ' encoding stdout as osutils.get_user_encoding() %r',
462
476
               output_encoding)
463
477
    # check encoding
2051
2065
            report_activity(sent, 'write')
2052
2066
 
2053
2067
 
2054
 
def connect_socket(address, timeout=None):
2055
 
    # Slight variation of the socket.create_connection() function (provided
2056
 
    # by python-2.6) that can fail if getaddrinfo returns an empty list. We
2057
 
    # also provide it for previous python versions. Also, we don't use the
2058
 
    # timeout parameter so we don't implement it either.
 
2068
def connect_socket(address):
 
2069
    # Slight variation of the socket.create_connection() function (provided by
 
2070
    # python-2.6) that can fail if getaddrinfo returns an empty list. We also
 
2071
    # provide it for previous python versions. Also, we don't use the timeout
 
2072
    # parameter (provided by the python implementation) so we don't implement
 
2073
    # it either).
2059
2074
    err = socket.error('getaddrinfo returns an empty list')
2060
2075
    host, port = address
2061
2076
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):