/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: 2005-06-24 10:42:47 UTC
  • Revision ID: mbp@sourcefrog.net-20050624104247-9cab76149a672c5b
- simple revert of text files 

- test cases for this

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
 
40
40
_QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/_~-])')
41
 
 
42
 
_SLASH_RE = re.compile(r'[\\/]+')
43
 
 
44
41
def quotefn(f):
45
 
    """Return a quoted filename filename
46
 
 
47
 
    This previously used backslash quoting, but that works poorly on
48
 
    Windows."""
49
 
    # TODO: I'm not really sure this is the best format either.x
50
 
    if _QUOTE_RE.search(f):
51
 
        return '"' + f + '"'
52
 
    else:
53
 
        return f
 
42
    """Return shell-quoted filename"""
 
43
    ## We could be a bit more terse by using double-quotes etc
 
44
    f = _QUOTE_RE.sub(r'\\\1', f)
 
45
    if f[0] == '~':
 
46
        f[0:1] = r'\~' 
 
47
    return f
54
48
 
55
49
 
56
50
def file_kind(f):
77
71
 
78
72
 
79
73
 
80
 
def backup_file(fn):
81
 
    """Copy a file to a backup.
82
 
 
83
 
    Backups are named in GNU-style, with a ~ suffix.
84
 
 
85
 
    If the file is already a backup, it's not copied.
86
 
    """
87
 
    import os
88
 
    if fn[-1] == '~':
89
 
        return
90
 
    bfn = fn + '~'
91
 
 
92
 
    inf = file(fn, 'rb')
93
 
    try:
94
 
        content = inf.read()
95
 
    finally:
96
 
        inf.close()
97
 
    
98
 
    outf = file(bfn, 'wb')
99
 
    try:
100
 
        outf.write(content)
101
 
    finally:
102
 
        outf.close()
103
 
 
104
 
def rename(path_from, path_to):
105
 
    """Basically the same as os.rename() just special for win32"""
106
 
    if sys.platform == 'win32':
107
 
        try:
108
 
            os.remove(path_to)
109
 
        except OSError, e:
110
 
            if e.errno != e.ENOENT:
111
 
                raise
112
 
    os.rename(path_from, path_to)
113
 
 
114
 
 
115
 
 
116
 
 
117
 
 
118
74
def isdir(f):
119
75
    """True if f is an accessible directory."""
120
76
    try:
134
90
 
135
91
def is_inside(dir, fname):
136
92
    """True if fname is inside dir.
137
 
    
138
 
    The parameters should typically be passed to os.path.normpath first, so
139
 
    that . and .. and repeated slashes are eliminated, and the separators
140
 
    are canonical for the platform.
141
 
    
142
 
    The empty string as a dir name is taken as top-of-tree and matches 
143
 
    everything.
144
 
    
145
 
    >>> is_inside('src', 'src/foo.c')
146
 
    True
147
 
    >>> is_inside('src', 'srccontrol')
148
 
    False
149
 
    >>> is_inside('src', 'src/a/a/a/foo.c')
150
 
    True
151
 
    >>> is_inside('foo.c', 'foo.c')
152
 
    True
153
 
    >>> is_inside('foo.c', '')
154
 
    False
155
 
    >>> is_inside('', 'foo.c')
156
 
    True
157
93
    """
158
 
    # XXX: Most callers of this can actually do something smarter by 
159
 
    # looking at the inventory
160
 
    if dir == fname:
161
 
        return True
162
 
    
163
 
    if dir == '':
164
 
        return True
165
 
    
166
 
    if dir[-1] != os.sep:
167
 
        dir += os.sep
168
 
    
169
 
    return fname.startswith(dir)
 
94
    return os.path.commonprefix([dir, fname]) == dir
170
95
 
171
96
 
172
97
def is_inside_any(dir_list, fname):
173
98
    """True if fname is inside any of given dirs."""
 
99
    # quick scan for perfect match
 
100
    if fname in dir_list:
 
101
        return True
 
102
    
174
103
    for dirname in dir_list:
175
104
        if is_inside(dirname, fname):
176
105
            return True
367
296
        tt = time.localtime(t)
368
297
        offset = local_time_offset(t)
369
298
    else:
370
 
        raise BzrError("unsupported timezone format %r" % timezone,
371
 
                       ['options are "utc", "original", "local"'])
 
299
        raise BzrError("unsupported timezone format %r",
 
300
                ['options are "utc", "original", "local"'])
372
301
 
373
302
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
374
303
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))