~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2005-05-10 06:21:07 UTC
  • Revision ID: mbp@sourcefrog.net-20050510062107-8b15b4c6b52d0279
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
 
19
 
import os, types, re, time, errno, sys
 
19
import os, types, re, time, errno
20
20
from stat import S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE
21
21
 
22
 
from bzrlib.errors import BzrError
23
 
from bzrlib.trace import mutter
 
22
from errors import bailout, BzrError
 
23
from trace import mutter
24
24
import bzrlib
25
25
 
26
26
def make_readonly(filename):
56
56
    elif S_ISLNK(mode):
57
57
        return 'symlink'
58
58
    else:
59
 
        raise BzrError("can't handle file kind with mode %o of %r" % (mode, f))
60
 
 
61
 
 
62
 
def kind_marker(kind):
63
 
    if kind == 'file':
64
 
        return ''
65
 
    elif kind == 'directory':
66
 
        return '/'
67
 
    elif kind == 'symlink':
68
 
        return '@'
69
 
    else:
70
 
        raise BzrError('invalid file kind %r' % kind)
 
59
        raise BzrError("can't handle file kind with mode %o of %r" % (mode, f)) 
71
60
 
72
61
 
73
62
 
88
77
        return False
89
78
 
90
79
 
91
 
def is_inside(dir, fname):
92
 
    """True if fname is inside dir.
93
 
    """
94
 
    return os.path.commonprefix([dir, fname]) == dir
95
 
 
96
 
 
97
 
def is_inside_any(dir_list, fname):
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
 
    
103
 
    for dirname in dir_list:
104
 
        if is_inside(dirname, fname):
105
 
            return True
106
 
    else:
107
 
        return False
108
 
 
109
 
 
110
80
def pumpfile(fromfile, tofile):
111
81
    """Copy contents of one file to another."""
112
82
    tofile.write(fromfile.read())
248
218
    if e:
249
219
        m = _EMAIL_RE.search(e)
250
220
        if not m:
251
 
            raise BzrError("%r doesn't seem to contain a reasonable email address" % e)
 
221
            bailout("%r doesn't seem to contain a reasonable email address" % e)
252
222
        return m.group(0)
253
223
 
254
224
    return _auto_user_id()[1]
296
266
        tt = time.localtime(t)
297
267
        offset = local_time_offset(t)
298
268
    else:
299
 
        raise BzrError("unsupported timezone format %r",
 
269
        bailout("unsupported timezone format %r",
300
270
                ['options are "utc", "original", "local"'])
301
271
 
302
272
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
315
285
 
316
286
if hasattr(os, 'urandom'): # python 2.4 and later
317
287
    rand_bytes = os.urandom
318
 
elif sys.platform == 'linux2':
319
 
    rand_bytes = file('/dev/urandom', 'rb').read
320
288
else:
321
 
    # not well seeded, but better than nothing
322
 
    def rand_bytes(n):
323
 
        import random
324
 
        s = ''
325
 
        while n:
326
 
            s += chr(random.randint(0, 255))
327
 
            n -= 1
328
 
        return s
 
289
    # FIXME: No good on non-Linux
 
290
    _rand_file = file('/dev/urandom', 'rb')
 
291
    rand_bytes = _rand_file.read
329
292
 
330
293
 
331
294
## TODO: We could later have path objects that remember their list
345
308
    >>> splitpath('a/../b')
346
309
    Traceback (most recent call last):
347
310
    ...
348
 
    BzrError: sorry, '..' not allowed in path
 
311
    BzrError: ("sorry, '..' not allowed in path", [])
349
312
    """
350
313
    assert isinstance(p, types.StringTypes)
351
314
 
356
319
    rps = []
357
320
    for f in ps:
358
321
        if f == '..':
359
 
            raise BzrError("sorry, %r not allowed in path" % f)
 
322
            bailout("sorry, %r not allowed in path" % f)
360
323
        elif (f == '.') or (f == ''):
361
324
            pass
362
325
        else:
367
330
    assert isinstance(p, list)
368
331
    for f in p:
369
332
        if (f == '..') or (f == None) or (f == ''):
370
 
            raise BzrError("sorry, %r not allowed in path" % f)
 
333
            bailout("sorry, %r not allowed in path" % f)
371
334
    return os.path.join(*p)
372
335
 
373
336
 
382
345
    mutter('external command: %s' % `cmd`)
383
346
    if os.system(cmd):
384
347
        if not ignore_errors:
385
 
            raise BzrError('command failed')
 
348
            bailout('command failed')
386
349