/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: Martin Pool
  • Date: 2007-02-15 01:23:29 UTC
  • mfrom: (2284 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2309.
  • Revision ID: mbp@sourcefrog.net-20070215012329-blt2xfhup97r6w8h
merge up from bzr.dev to get metadir changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
from bzrlib.lazy_import import lazy_import
27
27
lazy_import(globals(), """
 
28
import codecs
 
29
from datetime import datetime
28
30
import errno
29
31
from ntpath import (abspath as _nt_abspath,
30
32
                    join as _nt_join,
38
40
from shutil import (
39
41
    rmtree,
40
42
    )
41
 
import string
42
43
import tempfile
43
44
from tempfile import (
44
45
    mkdtemp,
47
48
 
48
49
from bzrlib import (
49
50
    errors,
 
51
    win32utils,
50
52
    )
51
53
""")
52
54
 
379
381
        output_encoding = bzrlib.user_encoding
380
382
        mutter('cp0 is invalid encoding.'
381
383
               ' encoding stdout as bzrlib.user_encoding %r', output_encoding)
 
384
    # check encoding
 
385
    try:
 
386
        codecs.lookup(output_encoding)
 
387
    except LookupError:
 
388
        sys.stderr.write('bzr: warning:'
 
389
                         ' unknown terminal encoding %s.\n'
 
390
                         '  Using encoding %s instead.\n'
 
391
                         % (output_encoding, bzrlib.user_encoding)
 
392
                        )
 
393
        output_encoding = bzrlib.user_encoding
 
394
 
382
395
    return output_encoding
383
396
 
384
397
 
554
567
 
555
568
def local_time_offset(t=None):
556
569
    """Return offset of local zone from GMT, either at present or at time t."""
557
 
    # python2.3 localtime() can't take None
558
570
    if t is None:
559
571
        t = time.time()
560
 
        
561
 
    if time.localtime(t).tm_isdst and time.daylight:
562
 
        return -time.altzone
563
 
    else:
564
 
        return -time.timezone
 
572
    offset = datetime.fromtimestamp(t) - datetime.utcfromtimestamp(t)
 
573
    return offset.days * 86400 + offset.seconds
565
574
 
566
575
    
567
576
def format_date(t, offset=0, timezone='original', date_fmt=None, 
768
777
 
769
778
def contains_whitespace(s):
770
779
    """True if there are any whitespace characters in s."""
771
 
    for ch in string.whitespace:
 
780
    # string.whitespace can include '\xa0' in certain locales, because it is
 
781
    # considered "non-breaking-space" as part of ISO-8859-1. But it
 
782
    # 1) Isn't a breaking whitespace
 
783
    # 2) Isn't one of ' \t\r\n' which are characters we sometimes use as
 
784
    #    separators
 
785
    # 3) '\xa0' isn't unicode safe since it is >128.
 
786
    # So we are following textwrap's example and hard-coding our own.
 
787
    # We probably could ignore \v and \f, too.
 
788
    for ch in u' \t\n\r\v\f':
772
789
        if ch in s:
773
790
            return True
774
791
    else:
881
898
def terminal_width():
882
899
    """Return estimated terminal width."""
883
900
    if sys.platform == 'win32':
884
 
        import bzrlib.win32console
885
 
        return bzrlib.win32console.get_console_size()[0]
 
901
        return win32utils.get_console_size()[0]
886
902
    width = 0
887
903
    try:
888
904
        import struct, fcntl, termios
906
922
    return sys.platform != "win32"
907
923
 
908
924
 
 
925
def supports_posix_readonly():
 
926
    """Return True if 'readonly' has POSIX semantics, False otherwise.
 
927
 
 
928
    Notably, a win32 readonly file cannot be deleted, unlike POSIX where the
 
929
    directory controls creation/deletion, etc.
 
930
 
 
931
    And under win32, readonly means that the directory itself cannot be
 
932
    deleted.  The contents of a readonly directory can be changed, unlike POSIX
 
933
    where files in readonly directories cannot be added, deleted or renamed.
 
934
    """
 
935
    return sys.platform != "win32"
 
936
 
 
937
 
909
938
def set_or_unset_env(env_variable, value):
910
939
    """Modify the environment, setting or removing the env_variable.
911
940
 
1059
1088
_cached_user_encoding = None
1060
1089
 
1061
1090
 
1062
 
def get_user_encoding():
 
1091
def get_user_encoding(use_cache=True):
1063
1092
    """Find out what the preferred user encoding is.
1064
1093
 
1065
1094
    This is generally the encoding that is used for command line parameters
1066
1095
    and file contents. This may be different from the terminal encoding
1067
1096
    or the filesystem encoding.
1068
1097
 
 
1098
    :param  use_cache:  Enable cache for detected encoding.
 
1099
                        (This parameter is turned on by default,
 
1100
                        and required only for selftesting)
 
1101
 
1069
1102
    :return: A string defining the preferred user encoding
1070
1103
    """
1071
1104
    global _cached_user_encoding
1072
 
    if _cached_user_encoding is not None:
 
1105
    if _cached_user_encoding is not None and use_cache:
1073
1106
        return _cached_user_encoding
1074
1107
 
1075
1108
    if sys.platform == 'darwin':
1083
1116
        import locale
1084
1117
 
1085
1118
    try:
1086
 
        _cached_user_encoding = locale.getpreferredencoding()
 
1119
        user_encoding = locale.getpreferredencoding()
1087
1120
    except locale.Error, e:
1088
1121
        sys.stderr.write('bzr: warning: %s\n'
1089
1122
                         '  Could not determine what text encoding to use.\n'
1091
1124
                         '  doesn\'t support the locale set by $LANG (%s)\n'
1092
1125
                         "  Continuing with ascii encoding.\n"
1093
1126
                         % (e, os.environ.get('LANG')))
 
1127
        user_encoding = 'ascii'
1094
1128
 
1095
1129
    # Windows returns 'cp0' to indicate there is no code page. So we'll just
1096
1130
    # treat that as ASCII, and not support printing unicode characters to the
1097
1131
    # console.
1098
 
    if _cached_user_encoding in (None, 'cp0'):
1099
 
        _cached_user_encoding = 'ascii'
1100
 
    return _cached_user_encoding
 
1132
    if user_encoding in (None, 'cp0'):
 
1133
        user_encoding = 'ascii'
 
1134
    else:
 
1135
        # check encoding
 
1136
        try:
 
1137
            codecs.lookup(user_encoding)
 
1138
        except LookupError:
 
1139
            sys.stderr.write('bzr: warning:'
 
1140
                             ' unknown encoding %s.'
 
1141
                             ' Continuing with ascii encoding.\n'
 
1142
                             % user_encoding
 
1143
                            )
 
1144
            user_encoding = 'ascii'
 
1145
 
 
1146
    if use_cache:
 
1147
        _cached_user_encoding = user_encoding
 
1148
 
 
1149
    return user_encoding
1101
1150
 
1102
1151
 
1103
1152
def recv_all(socket, bytes):