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
17
from cStringIO import StringIO
54
# sha and md5 modules are deprecated in python2.6 but hashlib is available as
56
if sys.version_info < (2, 5):
57
import md5 as _mod_md5
59
import sha as _mod_sha
58
69
from bzrlib import symbol_versioning
59
from bzrlib.symbol_versioning import (
62
from bzrlib.trace import mutter
65
72
# On win32, O_BINARY is used to indicate the file should
174
181
# sftp rename doesn't allow overwriting, so play tricks:
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('\\', '/'))
288
if win32utils.winver == 'Windows 98':
289
_win32_abspath = _win98_abspath
292
295
def _win32_realpath(path):
293
296
# Real _nt_realpath doesn't have a problem with a unicode cwd
354
357
if sys.platform == 'win32':
355
abspath = _win32_abspath
358
if win32utils.winver == 'Windows 98':
359
abspath = _win98_abspath
361
abspath = _win32_abspath
356
362
realpath = _win32_realpath
357
363
pathjoin = _win32_pathjoin
358
364
normpath = _win32_normpath
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
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',
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',
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())
423
output_encoding = bzrlib.user_encoding
432
output_encoding = get_user_encoding()
425
434
return output_encoding
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"""
599
608
map(s.update, strings)
600
609
return s.hexdigest()
603
def sha_string(f, _factory=sha.new):
612
def sha_string(f, _factory=sha):
604
613
return _factory(f).hexdigest()
607
616
def fingerprint_file(f):
609
618
return {'size': len(b),
610
'sha1': sha.new(b).hexdigest()}
619
'sha1': sha(b).hexdigest()}
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
643
:param show_offset: Whether to append the timezone.
644
:param date_fmt: strftime format.
652
:param date_fmt: strftime format.
653
:param show_offset: Whether to append the timezone.
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
661
def format_local_date(t, offset=0, timezone='original', date_fmt=None,
663
"""Return an unicode date string formatted according to the current locale.
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
670
:param date_fmt: strftime format.
671
:param show_offset: Whether to append the timezone.
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
680
def _format_date(t, offset, timezone, date_fmt, show_offset):
646
681
if timezone == 'utc':
647
682
tt = time.gmtime(t)
661
696
offset_str = ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)
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)
669
702
def compact_date(when):
1087
1120
del os.environ[env_variable]
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
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,