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

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-02-03 23:21:15 UTC
  • mfrom: (7290.42.6 paramiko-compat)
  • Revision ID: breezy.the.bot@gmail.com-20200203232115-g7k11bhsfeiqcprv
Fix compatibility with newer versions of paramiko, which break on noise before keys in pem files.

Merged from https://code.launchpad.net/~jelmer/brz/paramiko-compat/+merge/378480

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
from breezy.i18n import gettext
53
53
""")
54
54
 
 
55
from .sixish import (
 
56
    PY3,
 
57
    text_type,
 
58
    )
 
59
 
55
60
from hashlib import (
56
61
    md5,
57
62
    sha1 as sha,
86
91
 
87
92
 
88
93
def get_unicode_argv():
89
 
    return sys.argv[1:]
 
94
    if PY3:
 
95
        return sys.argv[1:]
 
96
    try:
 
97
        user_encoding = get_user_encoding()
 
98
        return [a.decode(user_encoding) for a in sys.argv[1:]]
 
99
    except UnicodeDecodeError:
 
100
        raise errors.BzrError(gettext("Parameter {0!r} encoding is unsupported by {1} "
 
101
                                      "application locale.").format(a, user_encoding))
90
102
 
91
103
 
92
104
def make_readonly(filename):
319
331
    Note that posix systems use arbitrary byte strings for filesystem objects,
320
332
    so a path that raises BadFilenameEncoding here may still be accessible.
321
333
    """
322
 
    return os.environ.get(key, None)
 
334
    val = os.environ.get(key, None)
 
335
    if PY3 or val is None:
 
336
        return val
 
337
    try:
 
338
        return val.decode(_fs_enc)
 
339
    except UnicodeDecodeError:
 
340
        # GZ 2011-12-12:Ideally want to include `key` in the exception message
 
341
        raise errors.BadFilenameEncoding(val, _fs_enc)
323
342
 
324
343
 
325
344
def _posix_get_home_dir():
335
354
 
336
355
def _posix_getuser_unicode():
337
356
    """Get username from environment or password database as unicode"""
338
 
    return getpass.getuser()
 
357
    name = getpass.getuser()
 
358
    if PY3:
 
359
        return name
 
360
    user_encoding = get_user_encoding()
 
361
    try:
 
362
        return name.decode(user_encoding)
 
363
    except UnicodeDecodeError:
 
364
        raise errors.BzrError("Encoding of username %r is unsupported by %s "
 
365
                              "application locale." % (name, user_encoding))
339
366
 
340
367
 
341
368
def _win32_fixdrive(path):
420
447
    return _rename_wrapper
421
448
 
422
449
 
423
 
_getcwd = os.getcwd
 
450
if sys.version_info > (3,):
 
451
    _getcwd = os.getcwd
 
452
else:
 
453
    _getcwd = os.getcwdu
424
454
 
425
455
 
426
456
# Default rename wraps os.rename()
708
738
 
709
739
# GZ 2017-09-16: Makes sense in general for hexdigest() result to be text, but
710
740
# used as bytes through most interfaces so encode with this wrapper.
711
 
def _hexdigest(hashobj):
712
 
    return hashobj.hexdigest().encode()
 
741
if PY3:
 
742
    def _hexdigest(hashobj):
 
743
        return hashobj.hexdigest().encode()
 
744
else:
 
745
    def _hexdigest(hashobj):
 
746
        return hashobj.hexdigest()
713
747
 
714
748
 
715
749
def sha_file(f):
861
895
    (date_fmt, tt, offset_str) = \
862
896
        _format_date(t, offset, timezone, date_fmt, show_offset)
863
897
    date_str = time.strftime(date_fmt, tt)
864
 
    if not isinstance(date_str, str):
 
898
    if not isinstance(date_str, text_type):
865
899
        date_str = date_str.decode(get_user_encoding(), 'replace')
866
900
    return date_str + offset_str
867
901
 
978
1012
    """
979
1013
    s = ''
980
1014
    for raw_byte in rand_bytes(num):
981
 
        s += ALNUM[raw_byte % 36]
 
1015
        if not PY3:
 
1016
            s += ALNUM[ord(raw_byte) % 36]
 
1017
        else:
 
1018
            s += ALNUM[raw_byte % 36]
982
1019
    return s
983
1020
 
984
1021
 
1339
1376
    Otherwise it is decoded from the the filesystem's encoding. If decoding
1340
1377
    fails, a errors.BadFilenameEncoding exception is raised.
1341
1378
    """
1342
 
    if isinstance(filename, str):
 
1379
    if isinstance(filename, text_type):
1343
1380
        return filename
1344
1381
    try:
1345
1382
        return filename.decode(_fs_enc)
1354
1391
    Otherwise it is decoded from utf-8. If decoding fails, the exception is
1355
1392
    wrapped in a BzrBadParameterNotUnicode exception.
1356
1393
    """
1357
 
    if isinstance(unicode_or_utf8_string, str):
 
1394
    if isinstance(unicode_or_utf8_string, text_type):
1358
1395
        return unicode_or_utf8_string
1359
1396
    try:
1360
1397
        return unicode_or_utf8_string.decode('utf8')
1693
1730
        if orig_val is not None:
1694
1731
            del os.environ[env_variable]
1695
1732
    else:
 
1733
        if not PY3 and isinstance(value, text_type):
 
1734
            value = value.encode(get_user_encoding())
1696
1735
        os.environ[env_variable] = value
1697
1736
    return orig_val
1698
1737
 
2087
2126
        return win32utils.get_host_name()
2088
2127
    else:
2089
2128
        import socket
2090
 
        return socket.gethostname()
 
2129
        if PY3:
 
2130
            return socket.gethostname()
 
2131
        return socket.gethostname().decode(get_user_encoding())
2091
2132
 
2092
2133
 
2093
2134
# We must not read/write any more than 64k at a time from/to a socket so we
2672
2713
    return _FILESYSTEM_FINDER.find(path)
2673
2714
 
2674
2715
 
2675
 
perf_counter = time.perf_counter
 
2716
if PY3:
 
2717
    perf_counter = time.perf_counter
 
2718
else:
 
2719
    perf_counter = time.clock