~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Robert Collins
  • Date: 2007-04-19 02:27:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2426.
  • Revision ID: robertc@robertcollins.net-20070419022744-pfdqz42kp1wizh43
``make docs`` now creates a man page at ``man1/bzr.1`` fixing bug 107388.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
""")
55
55
 
56
56
import bzrlib
 
57
from bzrlib import symbol_versioning
57
58
from bzrlib.symbol_versioning import (
58
59
    deprecated_function,
59
60
    zero_nine,
144
145
    return umask
145
146
 
146
147
 
 
148
_kind_marker_map = {
 
149
    "file": "",
 
150
    _directory_kind: "/",
 
151
    "symlink": "@",
 
152
    'tree-reference': '+',
 
153
}
 
154
 
 
155
 
147
156
def kind_marker(kind):
148
 
    if kind == 'file':
149
 
        return ''
150
 
    elif kind == _directory_kind:
151
 
        return '/'
152
 
    elif kind == 'symlink':
153
 
        return '@'
154
 
    else:
 
157
    try:
 
158
        return _kind_marker_map[kind]
 
159
    except KeyError:
155
160
        raise errors.BzrError('invalid file kind %r' % kind)
156
161
 
 
162
 
157
163
lexists = getattr(os.path, 'lexists', None)
158
164
if lexists is None:
159
165
    def lexists(f):
160
166
        try:
161
 
            if getattr(os, 'lstat') is not None:
162
 
                os.lstat(f)
163
 
            else:
164
 
                os.stat(f)
 
167
            stat = getattr(os, 'lstat', os.stat)
 
168
            stat(f)
165
169
            return True
166
 
        except OSError,e:
 
170
        except OSError, e:
167
171
            if e.errno == errno.ENOENT:
168
172
                return False;
169
173
            else:
517
521
    for dirname in dir_list:
518
522
        if is_inside(dirname, fname):
519
523
            return True
520
 
    else:
521
 
        return False
 
524
    return False
522
525
 
523
526
 
524
527
def is_inside_or_parent_of_any(dir_list, fname):
526
529
    for dirname in dir_list:
527
530
        if is_inside(dirname, fname) or is_inside(fname, dirname):
528
531
            return True
529
 
    else:
530
 
        return False
 
532
    return False
531
533
 
532
534
 
533
535
def pumpfile(fromfile, tofile):
904
906
    return unicode_or_utf8_string.encode('utf-8')
905
907
 
906
908
 
907
 
def safe_revision_id(unicode_or_utf8_string):
 
909
_revision_id_warning = ('Unicode revision ids were deprecated in bzr 0.15.'
 
910
                        ' Revision id generators should be creating utf8'
 
911
                        ' revision ids.')
 
912
 
 
913
 
 
914
def safe_revision_id(unicode_or_utf8_string, warn=True):
908
915
    """Revision ids should now be utf8, but at one point they were unicode.
909
916
 
 
917
    :param unicode_or_utf8_string: A possibly Unicode revision_id. (can also be
 
918
        utf8 or None).
 
919
    :param warn: Functions that are sanitizing user data can set warn=False
 
920
    :return: None or a utf8 revision id.
 
921
    """
 
922
    if (unicode_or_utf8_string is None
 
923
        or unicode_or_utf8_string.__class__ == str):
 
924
        return unicode_or_utf8_string
 
925
    if warn:
 
926
        symbol_versioning.warn(_revision_id_warning, DeprecationWarning,
 
927
                               stacklevel=2)
 
928
    return cache_utf8.encode(unicode_or_utf8_string)
 
929
 
 
930
 
 
931
_file_id_warning = ('Unicode file ids were deprecated in bzr 0.15. File id'
 
932
                    ' generators should be creating utf8 file ids.')
 
933
 
 
934
 
 
935
def safe_file_id(unicode_or_utf8_string, warn=True):
 
936
    """File ids should now be utf8, but at one point they were unicode.
 
937
 
910
938
    This is the same as safe_utf8, except it uses the cached encode functions
911
939
    to save a little bit of performance.
 
940
 
 
941
    :param unicode_or_utf8_string: A possibly Unicode file_id. (can also be
 
942
        utf8 or None).
 
943
    :param warn: Functions that are sanitizing user data can set warn=False
 
944
    :return: None or a utf8 file id.
912
945
    """
913
 
    if unicode_or_utf8_string is None:
914
 
        return None
915
 
    if isinstance(unicode_or_utf8_string, str):
916
 
        # TODO: jam 20070209 Eventually just remove this check.
917
 
        try:
918
 
            utf8_str = cache_utf8.get_cached_utf8(unicode_or_utf8_string)
919
 
        except UnicodeDecodeError:
920
 
            raise errors.BzrBadParameterNotUnicode(unicode_or_utf8_string)
921
 
        return utf8_str
 
946
    if (unicode_or_utf8_string is None
 
947
        or unicode_or_utf8_string.__class__ == str):
 
948
        return unicode_or_utf8_string
 
949
    if warn:
 
950
        symbol_versioning.warn(_file_id_warning, DeprecationWarning,
 
951
                               stacklevel=2)
922
952
    return cache_utf8.encode(unicode_or_utf8_string)
923
953
 
924
954
 
925
 
# TODO: jam 20070217 We start by just re-using safe_revision_id, but ultimately
926
 
#       we want to use a different dictionary cache, because trapping file ids
927
 
#       and revision ids in the same dict seemed to have a noticable effect on
928
 
#       performance.
929
 
safe_file_id = safe_revision_id
930
 
 
931
 
 
932
955
_platform_normalizes_filenames = False
933
956
if sys.platform == 'darwin':
934
957
    _platform_normalizes_filenames = True