/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-14 03:16:54 UTC
  • mfrom: (7479.2.3 no-more-python2)
  • Revision ID: breezy.the.bot@gmail.com-20200214031654-bp1xtv2jr9nmhto3
Drop python2 support.

Merged from https://code.launchpad.net/~jelmer/brz/no-more-python2/+merge/378694

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