~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2005-04-28 07:24:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050428072453-7b99afa993a1e549
todo

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
 
85
85
def uuid():
86
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.
87
91
    try:
88
 
        return file('/proc/sys/kernel/random/uuid').readline().rstrip('\n')
 
92
        return chomp(file('/proc/sys/kernel/random/uuid').readline())
89
93
    except IOError:
90
94
        return chomp(os.popen('uuidgen').readline())
91
95
 
92
96
 
 
97
def chomp(s):
 
98
    if s and (s[-1] == '\n'):
 
99
        return s[:-1]
 
100
    else:
 
101
        return s
 
102
 
 
103
 
93
104
def sha_file(f):
94
105
    import sha
 
106
    ## TODO: Maybe read in chunks to handle big files
95
107
    if hasattr(f, 'tell'):
96
108
        assert f.tell() == 0
97
109
    s = sha.new()
98
 
    BUFSIZE = 128<<10
99
 
    while True:
100
 
        b = f.read(BUFSIZE)
101
 
        if not b:
102
 
            break
103
 
        s.update(b)
 
110
    s.update(f.read())
104
111
    return s.hexdigest()
105
112
 
106
113
 
227
234
 
228
235
def compare_files(a, b):
229
236
    """Returns true if equal in contents"""
 
237
    # TODO: don't read the whole thing in one go.
230
238
    BUFSIZE = 4096
231
239
    while True:
232
240
        ai = a.read(BUFSIZE)