~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

Magnus Thernings patch to remove status --revision, adjusted to update help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
 
19
19
import os, types, re, time, errno, sys
20
 
from stat import (S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE,
21
 
        S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
 
20
from stat import S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE
22
21
 
23
22
from bzrlib.errors import BzrError
24
23
from bzrlib.trace import mutter
65
64
        return 'directory'
66
65
    elif S_ISLNK(mode):
67
66
        return 'symlink'
68
 
    elif S_ISCHR(mode):
69
 
        return 'chardev'
70
 
    elif S_ISBLK(mode):
71
 
        return 'block'
72
 
    elif S_ISFIFO(mode):
73
 
        return 'fifo'
74
 
    elif S_ISSOCK(mode):
75
 
        return 'socket'
76
67
    else:
77
 
        return 'unknown'
 
68
        raise BzrError("can't handle file kind with mode %o of %r" % (mode, f))
78
69
 
79
70
 
80
71
def kind_marker(kind):
416
407
    """Return size of given open file."""
417
408
    return os.fstat(f.fileno())[ST_SIZE]
418
409
 
419
 
# Define rand_bytes based on platform.
420
 
try:
421
 
    # Python 2.4 and later have os.urandom,
422
 
    # but it doesn't work on some arches
423
 
    os.urandom(1)
 
410
 
 
411
if hasattr(os, 'urandom'): # python 2.4 and later
424
412
    rand_bytes = os.urandom
425
 
except (NotImplementedError, AttributeError):
426
 
    # If python doesn't have os.urandom, or it doesn't work,
427
 
    # then try to first pull random data from /dev/urandom
428
 
    if os.path.exists("/dev/urandom"):
429
 
        rand_bytes = file('/dev/urandom', 'rb').read
430
 
    # Otherwise, use this hack as a last resort
431
 
    else:
432
 
        # not well seeded, but better than nothing
433
 
        def rand_bytes(n):
434
 
            import random
435
 
            s = ''
436
 
            while n:
437
 
                s += chr(random.randint(0, 255))
438
 
                n -= 1
439
 
            return s
 
413
elif sys.platform == 'linux2':
 
414
    rand_bytes = file('/dev/urandom', 'rb').read
 
415
else:
 
416
    # not well seeded, but better than nothing
 
417
    def rand_bytes(n):
 
418
        import random
 
419
        s = ''
 
420
        while n:
 
421
            s += chr(random.randint(0, 255))
 
422
            n -= 1
 
423
        return s
 
424
 
440
425
 
441
426
## TODO: We could later have path objects that remember their list
442
427
## decomposition (might be too tricksy though.)