/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Vincent Ladeuil
  • Date: 2008-10-02 13:25:47 UTC
  • mfrom: (3759 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3760.
  • Revision ID: v.ladeuil+lp@free.fr-20081002132547-txs4fs006e9p0gt1
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
from cStringIO import StringIO
18
17
import os
19
18
import re
20
19
import stat
35
34
                    splitdrive as _nt_splitdrive,
36
35
                    )
37
36
import posixpath
38
 
import sha
39
37
import shutil
40
38
from shutil import (
41
39
    rmtree,
53
51
    )
54
52
""")
55
53
 
 
54
# sha and md5 modules are deprecated in python2.6 but hashlib is available as
 
55
# of 2.5
 
56
if sys.version_info < (2, 5):
 
57
    import md5 as _mod_md5
 
58
    md5 = _mod_md5.new
 
59
    import sha as _mod_sha
 
60
    sha = _mod_sha.new
 
61
else:
 
62
    from hashlib import (
 
63
        md5,
 
64
        sha1 as sha,
 
65
        )
 
66
 
56
67
 
57
68
import bzrlib
58
69
from bzrlib import symbol_versioning
59
 
from bzrlib.symbol_versioning import (
60
 
    deprecated_function,
61
 
    )
62
 
from bzrlib.trace import mutter
63
70
 
64
71
 
65
72
# On win32, O_BINARY is used to indicate the file should
172
179
    """
173
180
 
174
181
    # sftp rename doesn't allow overwriting, so play tricks:
175
 
    import random
176
182
    base = os.path.basename(new)
177
183
    dirname = os.path.dirname(new)
178
184
    tmp_name = u'tmp.%s.%.9f.%d.%s' % (base, time.time(), os.getpid(), rand_chars(10))
285
291
        path = cwd + '\\' + path
286
292
    return _win32_fixdrive(_nt_normpath(path).replace('\\', '/'))
287
293
 
288
 
if win32utils.winver == 'Windows 98':
289
 
    _win32_abspath = _win98_abspath
290
 
 
291
294
 
292
295
def _win32_realpath(path):
293
296
    # Real _nt_realpath doesn't have a problem with a unicode cwd
352
355
 
353
356
 
354
357
if sys.platform == 'win32':
355
 
    abspath = _win32_abspath
 
358
    if win32utils.winver == 'Windows 98':
 
359
        abspath = _win98_abspath
 
360
    else:
 
361
        abspath = _win32_abspath
356
362
    realpath = _win32_realpath
357
363
    pathjoin = _win32_pathjoin
358
364
    normpath = _win32_normpath
387
393
 
388
394
    This attempts to check both sys.stdout and sys.stdin to see
389
395
    what encoding they are in, and if that fails it falls back to
390
 
    bzrlib.user_encoding.
 
396
    osutils.get_user_encoding().
391
397
    The problem is that on Windows, locale.getpreferredencoding()
392
398
    is not the same encoding as that used by the console:
393
399
    http://mail.python.org/pipermail/python-list/2003-May/162357.html
395
401
    On my standard US Windows XP, the preferred encoding is
396
402
    cp1252, but the console is cp437
397
403
    """
 
404
    from bzrlib.trace import mutter
398
405
    output_encoding = getattr(sys.stdout, 'encoding', None)
399
406
    if not output_encoding:
400
407
        input_encoding = getattr(sys.stdin, 'encoding', None)
401
408
        if not input_encoding:
402
 
            output_encoding = bzrlib.user_encoding
403
 
            mutter('encoding stdout as bzrlib.user_encoding %r', output_encoding)
 
409
            output_encoding = get_user_encoding()
 
410
            mutter('encoding stdout as osutils.get_user_encoding() %r',
 
411
                   output_encoding)
404
412
        else:
405
413
            output_encoding = input_encoding
406
414
            mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
408
416
        mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
409
417
    if output_encoding == 'cp0':
410
418
        # invalid encoding (cp0 means 'no codepage' on Windows)
411
 
        output_encoding = bzrlib.user_encoding
 
419
        output_encoding = get_user_encoding()
412
420
        mutter('cp0 is invalid encoding.'
413
 
               ' encoding stdout as bzrlib.user_encoding %r', output_encoding)
 
421
               ' encoding stdout as osutils.get_user_encoding() %r',
 
422
               output_encoding)
414
423
    # check encoding
415
424
    try:
416
425
        codecs.lookup(output_encoding)
418
427
        sys.stderr.write('bzr: warning:'
419
428
                         ' unknown terminal encoding %s.\n'
420
429
                         '  Using encoding %s instead.\n'
421
 
                         % (output_encoding, bzrlib.user_encoding)
 
430
                         % (output_encoding, get_user_encoding())
422
431
                        )
423
 
        output_encoding = bzrlib.user_encoding
 
432
        output_encoding = get_user_encoding()
424
433
 
425
434
    return output_encoding
426
435
 
569
578
 
570
579
    The file cursor should be already at the start.
571
580
    """
572
 
    s = sha.new()
 
581
    s = sha()
573
582
    BUFSIZE = 128<<10
574
583
    while True:
575
584
        b = f.read(BUFSIZE)
581
590
 
582
591
def sha_file_by_name(fname):
583
592
    """Calculate the SHA1 of a file by reading the full text"""
584
 
    s = sha.new()
 
593
    s = sha()
585
594
    f = os.open(fname, os.O_RDONLY | O_BINARY)
586
595
    try:
587
596
        while True:
593
602
        os.close(f)
594
603
 
595
604
 
596
 
def sha_strings(strings, _factory=sha.new):
 
605
def sha_strings(strings, _factory=sha):
597
606
    """Return the sha-1 of concatenation of strings"""
598
607
    s = _factory()
599
608
    map(s.update, strings)
600
609
    return s.hexdigest()
601
610
 
602
611
 
603
 
def sha_string(f, _factory=sha.new):
 
612
def sha_string(f, _factory=sha):
604
613
    return _factory(f).hexdigest()
605
614
 
606
615
 
607
616
def fingerprint_file(f):
608
617
    b = f.read()
609
618
    return {'size': len(b),
610
 
            'sha1': sha.new(b).hexdigest()}
 
619
            'sha1': sha(b).hexdigest()}
611
620
 
612
621
 
613
622
def compare_files(a, b):
640
649
    :param timezone: How to display the time: 'utc', 'original' for the
641
650
         timezone specified by offset, or 'local' for the process's current
642
651
         timezone.
643
 
    :param show_offset: Whether to append the timezone.
644
 
    :param date_fmt: strftime format.
645
 
    """
 
652
    :param date_fmt: strftime format.
 
653
    :param show_offset: Whether to append the timezone.
 
654
    """
 
655
    (date_fmt, tt, offset_str) = \
 
656
               _format_date(t, offset, timezone, date_fmt, show_offset)
 
657
    date_fmt = date_fmt.replace('%a', weekdays[tt[6]])
 
658
    date_str = time.strftime(date_fmt, tt)
 
659
    return date_str + offset_str
 
660
 
 
661
def format_local_date(t, offset=0, timezone='original', date_fmt=None,
 
662
                      show_offset=True):
 
663
    """Return an unicode date string formatted according to the current locale.
 
664
 
 
665
    :param t: Seconds since the epoch.
 
666
    :param offset: Timezone offset in seconds east of utc.
 
667
    :param timezone: How to display the time: 'utc', 'original' for the
 
668
         timezone specified by offset, or 'local' for the process's current
 
669
         timezone.
 
670
    :param date_fmt: strftime format.
 
671
    :param show_offset: Whether to append the timezone.
 
672
    """
 
673
    (date_fmt, tt, offset_str) = \
 
674
               _format_date(t, offset, timezone, date_fmt, show_offset)
 
675
    date_str = time.strftime(date_fmt, tt)
 
676
    if not isinstance(date_str, unicode):
 
677
        date_str = date_str.decode(bzrlib.user_encoding, 'replace')
 
678
    return date_str + offset_str
 
679
 
 
680
def _format_date(t, offset, timezone, date_fmt, show_offset):
646
681
    if timezone == 'utc':
647
682
        tt = time.gmtime(t)
648
683
        offset = 0
661
696
        offset_str = ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)
662
697
    else:
663
698
        offset_str = ''
664
 
    # day of week depends on locale, so we do this ourself
665
 
    date_fmt = date_fmt.replace('%a', weekdays[tt[6]])
666
 
    return (time.strftime(date_fmt, tt) +  offset_str)
 
699
    return (date_fmt, tt, offset_str)
667
700
 
668
701
 
669
702
def compact_date(when):
1087
1120
            del os.environ[env_variable]
1088
1121
    else:
1089
1122
        if isinstance(value, unicode):
1090
 
            value = value.encode(bzrlib.user_encoding)
 
1123
            value = value.encode(get_user_encoding())
1091
1124
        os.environ[env_variable] = value
1092
1125
    return orig_val
1093
1126
 
1245
1278
    global _selected_dir_reader
1246
1279
    if _selected_dir_reader is None:
1247
1280
        fs_encoding = _fs_enc.upper()
1248
 
        if win32utils.winver == 'Windows NT':
 
1281
        if sys.platform == "win32" and win32utils.winver == 'Windows NT':
1249
1282
            # Win98 doesn't have unicode apis like FindFirstFileW
1250
1283
            # TODO: We possibly could support Win98 by falling back to the
1251
1284
            #       original FindFirstFile, and using TCHAR instead of WCHAR,