~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2005-04-29 00:43:34 UTC
  • Revision ID: mbp@sourcefrog.net-20050429004334-bbb9dc81ce0d9de3
- update install instructions

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
22
from errors import bailout, BzrError
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())
114
84
 
115
85
def uuid():
116
86
    """Return a new UUID"""
 
87
    
 
88
    ## XXX: Could alternatively read /proc/sys/kernel/random/uuid on
 
89
    ## Linux, but we need something portable for other systems;
 
90
    ## preferably an implementation in Python.
117
91
    try:
118
 
        return file('/proc/sys/kernel/random/uuid').readline().rstrip('\n')
 
92
        return chomp(file('/proc/sys/kernel/random/uuid').readline())
119
93
    except IOError:
120
94
        return chomp(os.popen('uuidgen').readline())
121
95
 
122
96
 
 
97
def chomp(s):
 
98
    if s and (s[-1] == '\n'):
 
99
        return s[:-1]
 
100
    else:
 
101
        return s
 
102
 
 
103
 
123
104
def sha_file(f):
124
105
    import sha
 
106
    ## TODO: Maybe read in chunks to handle big files
125
107
    if hasattr(f, 'tell'):
126
108
        assert f.tell() == 0
127
109
    s = sha.new()
128
 
    BUFSIZE = 128<<10
129
 
    while True:
130
 
        b = f.read(BUFSIZE)
131
 
        if not b:
132
 
            break
133
 
        s.update(b)
 
110
    s.update(f.read())
134
111
    return s.hexdigest()
135
112
 
136
113
 
257
234
 
258
235
def compare_files(a, b):
259
236
    """Returns true if equal in contents"""
 
237
    # TODO: don't read the whole thing in one go.
260
238
    BUFSIZE = 4096
261
239
    while True:
262
240
        ai = a.read(BUFSIZE)
315
293
 
316
294
if hasattr(os, 'urandom'): # python 2.4 and later
317
295
    rand_bytes = os.urandom
318
 
elif sys.platform == 'linux2':
319
 
    rand_bytes = file('/dev/urandom', 'rb').read
320
296
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
 
297
    # FIXME: No good on non-Linux
 
298
    _rand_file = file('/dev/urandom', 'rb')
 
299
    rand_bytes = _rand_file.read
329
300
 
330
301
 
331
302
## TODO: We could later have path objects that remember their list