/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: Aaron Bentley
  • Date: 2006-09-09 18:52:57 UTC
  • mfrom: (1996 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1997.
  • Revision ID: aaron.bentley@utoronto.ca-20060909185257-ce0ee03ee5125ff1
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
    Windows."""
85
85
    # TODO: I'm not really sure this is the best format either.x
86
86
    global _QUOTE_RE
87
 
    if _QUOTE_RE == None:
 
87
    if _QUOTE_RE is None:
88
88
        _QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/\\_~-])')
89
89
        
90
90
    if _QUOTE_RE.search(f):
150
150
if lexists is None:
151
151
    def lexists(f):
152
152
        try:
153
 
            if hasattr(os, 'lstat'):
 
153
            if getattr(os, 'lstat') is not None:
154
154
                os.lstat(f)
155
155
            else:
156
156
                os.stat(f)
190
190
        pass
191
191
    except IOError, e:
192
192
        # RBC 20060103 abstraction leakage: the paramiko SFTP clients rename
193
 
        # function raises an IOError with errno == None when a rename fails.
 
193
        # function raises an IOError with errno is None when a rename fails.
194
194
        # This then gets caught here.
195
195
        if e.errno not in (None, errno.ENOENT, errno.ENOTDIR):
196
196
            raise
197
197
    except Exception, e:
198
 
        if (not hasattr(e, 'errno') 
 
198
        if (getattr(e, 'errno', None) is None
199
199
            or e.errno not in (errno.ENOENT, errno.ENOTDIR)):
200
200
            raise
201
201
    else:
370
370
 
371
371
 
372
372
def normalizepath(f):
373
 
    if hasattr(os.path, 'realpath'):
 
373
    if getattr(os.path, 'realpath', None) is not None:
374
374
        F = realpath
375
375
    else:
376
376
        F = abspath
505
505
 
506
506
 
507
507
def sha_file(f):
508
 
    if hasattr(f, 'tell'):
 
508
    if getattr(f, 'tell', None) is not None:
509
509
        assert f.tell() == 0
510
510
    s = sha.new()
511
511
    BUFSIZE = 128<<10
555
555
def local_time_offset(t=None):
556
556
    """Return offset of local zone from GMT, either at present or at time t."""
557
557
    # python2.3 localtime() can't take None
558
 
    if t == None:
 
558
    if t is None:
559
559
        t = time.time()
560
560
        
561
561
    if time.localtime(t).tm_isdst and time.daylight:
574
574
        tt = time.gmtime(t)
575
575
        offset = 0
576
576
    elif timezone == 'original':
577
 
        if offset == None:
 
577
        if offset is None:
578
578
            offset = 0
579
579
        tt = time.gmtime(t + offset)
580
580
    elif timezone == 'local':
676
676
def joinpath(p):
677
677
    assert isinstance(p, list)
678
678
    for f in p:
679
 
        if (f == '..') or (f == None) or (f == ''):
 
679
        if (f == '..') or (f is None) or (f == ''):
680
680
            raise BzrError("sorry, %r not allowed in path" % f)
681
681
    return pathjoin(*p)
682
682
 
726
726
 
727
727
 
728
728
def has_symlinks():
729
 
    if hasattr(os, 'symlink'):
 
729
    if getattr(os, 'symlink', None) is not None:
730
730
        return True
731
731
    else:
732
732
        return False
867
867
 
868
868
    return width
869
869
 
 
870
 
870
871
def supports_executable():
871
872
    return sys.platform != "win32"
872
873
 
873
874
 
 
875
def set_or_unset_env(env_variable, value):
 
876
    """Modify the environment, setting or removing the env_variable.
 
877
 
 
878
    :param env_variable: The environment variable in question
 
879
    :param value: The value to set the environment to. If None, then
 
880
        the variable will be removed.
 
881
    :return: The original value of the environment variable.
 
882
    """
 
883
    orig_val = os.environ.get(env_variable)
 
884
    if value is None:
 
885
        if orig_val is not None:
 
886
            del os.environ[env_variable]
 
887
    else:
 
888
        if isinstance(value, unicode):
 
889
            value = value.encode(bzrlib.user_encoding)
 
890
        os.environ[env_variable] = value
 
891
    return orig_val
 
892
 
 
893
 
874
894
_validWin32PathRE = re.compile(r'^([A-Za-z]:[/\\])?[^:<>*"?\|]*$')
875
895
 
876
896
 
1000
1020
    key_a = path_prefix_key(path_a)
1001
1021
    key_b = path_prefix_key(path_b)
1002
1022
    return cmp(key_a, key_b)
 
1023
 
 
1024
 
 
1025
_cached_user_encoding = None
 
1026
 
 
1027
 
 
1028
def get_user_encoding():
 
1029
    """Find out what the preferred user encoding is.
 
1030
 
 
1031
    This is generally the encoding that is used for command line parameters
 
1032
    and file contents. This may be different from the terminal encoding
 
1033
    or the filesystem encoding.
 
1034
 
 
1035
    :return: A string defining the preferred user encoding
 
1036
    """
 
1037
    global _cached_user_encoding
 
1038
    if _cached_user_encoding is not None:
 
1039
        return _cached_user_encoding
 
1040
 
 
1041
    if sys.platform == 'darwin':
 
1042
        # work around egregious python 2.4 bug
 
1043
        sys.platform = 'posix'
 
1044
        try:
 
1045
            import locale
 
1046
        finally:
 
1047
            sys.platform = 'darwin'
 
1048
    else:
 
1049
        import locale
 
1050
 
 
1051
    try:
 
1052
        _cached_user_encoding = locale.getpreferredencoding()
 
1053
    except locale.Error, e:
 
1054
        sys.stderr.write('bzr: warning: %s\n'
 
1055
                         '  Could not what text encoding to use.\n'
 
1056
                         '  This error usually means your Python interpreter\n'
 
1057
                         '  doesn\'t support the locale set by $LANG (%s)\n'
 
1058
                         "  Continuing with ascii encoding.\n"
 
1059
                         % (e, os.environ.get('LANG')))
 
1060
 
 
1061
    if _cached_user_encoding is None:
 
1062
        _cached_user_encoding = 'ascii'
 
1063
    return _cached_user_encoding