~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-08-14 21:14:26 UTC
  • mfrom: (3634.1.1 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080814211426-i0rmbyhjxf4hi7pt
(jam)(trivial) NEWS fix

Show diffs side-by-side

added added

removed removed

Lines of Context:
569
569
    return length
570
570
 
571
571
 
572
 
def pump_string_file(bytes, file_handle, segment_size=None):
573
 
    """Write bytes to file_handle in many smaller writes.
574
 
 
575
 
    :param bytes: The string to write.
576
 
    :param file_handle: The file to write to.
577
 
    """
578
 
    # Write data in chunks rather than all at once, because very large
579
 
    # writes fail on some platforms (e.g. Windows with SMB  mounted
580
 
    # drives).
581
 
    if not segment_size:
582
 
        segment_size = 5242880 # 5MB
583
 
    segments = range(len(bytes) / segment_size + 1)
584
 
    write = file_handle.write
585
 
    for segment_index in segments:
586
 
        segment = buffer(bytes, segment_index * segment_size, segment_size)
587
 
        write(segment)
588
 
 
589
 
 
590
572
def file_iterator(input_file, readsize=32768):
591
573
    while True:
592
574
        b = input_file.read(readsize)
1278
1260
    """
1279
1261
    _lstat = os.lstat
1280
1262
    _directory = _directory_kind
1281
 
    # Use C accelerated directory listing.
1282
 
    _listdir = _read_dir
 
1263
    _listdir = os.listdir
1283
1264
    _kind_from_mode = _formats.get
1284
1265
 
1285
1266
    # 0 - relpath, 1- basename, 2- kind, 3- stat, 4-toppath
1295
1276
 
1296
1277
        dirblock = []
1297
1278
        append = dirblock.append
1298
 
        # read_dir supplies in should-stat order.
1299
 
        for _, name in sorted(_listdir(top)):
 
1279
        for name in sorted(_listdir(top)):
1300
1280
            abspath = top_slash + name
1301
1281
            statvalue = _lstat(abspath)
1302
1282
            kind = _kind_from_mode(statvalue.st_mode & 0170000, 'unknown')
1303
1283
            append((relprefix + name, name, kind, statvalue, abspath))
1304
 
        dirblock.sort()
1305
1284
        yield (relroot, top), dirblock
1306
1285
 
1307
1286
        # push the user specified dirs from dirblock
1425
1404
        return _cached_user_encoding
1426
1405
 
1427
1406
    if sys.platform == 'darwin':
1428
 
        # python locale.getpreferredencoding() always return
1429
 
        # 'mac-roman' on darwin. That's a lie.
 
1407
        # work around egregious python 2.4 bug
1430
1408
        sys.platform = 'posix'
1431
1409
        try:
1432
 
            if os.environ.get('LANG', None) is None:
1433
 
                # If LANG is not set, we end up with 'ascii', which is bad
1434
 
                # ('mac-roman' is more than ascii), so we set a default which
1435
 
                # will give us UTF-8 (which appears to work in all cases on
1436
 
                # OSX). Users are still free to override LANG of course, as
1437
 
                # long as it give us something meaningful. This work-around
1438
 
                # *may* not be needed with python 3k and/or OSX 10.5, but will
1439
 
                # work with them too -- vila 20080908
1440
 
                os.environ['LANG'] = 'en_US.UTF-8'
1441
1410
            import locale
1442
1411
        finally:
1443
1412
            sys.platform = 'darwin'
1480
1449
    return user_encoding
1481
1450
 
1482
1451
 
1483
 
def get_host_name():
1484
 
    """Return the current unicode host name.
1485
 
 
1486
 
    This is meant to be used in place of socket.gethostname() because that
1487
 
    behaves inconsistently on different platforms.
1488
 
    """
1489
 
    if sys.platform == "win32":
1490
 
        import win32utils
1491
 
        return win32utils.get_host_name()
1492
 
    else:
1493
 
        import socket
1494
 
        return socket.gethostname().decode(get_user_encoding())
1495
 
 
1496
 
 
1497
1452
def recv_all(socket, bytes):
1498
1453
    """Receive an exact number of bytes.
1499
1454
 
1570
1525
        base = abspath(pathjoin(base, '..', '..'))
1571
1526
    filename = pathjoin(base, resource_relpath)
1572
1527
    return open(filename, 'rU').read()
1573
 
 
1574
 
 
1575
 
try:
1576
 
    from bzrlib._readdir_pyx import read_dir as _read_dir
1577
 
except ImportError:
1578
 
    from bzrlib._readdir_py import read_dir as _read_dir