From 00bdfa1a8ea766a7ed5aafcbb4712c2c89e12c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Wed, 26 Jun 2013 22:00:23 +0200 Subject: [PATCH] Add py3.2 compatible unhexlify to helpers.py --- darc/cache.py | 6 +++--- darc/helpers.py | 25 ++++++++++++++++++------- darc/repository.py | 6 +++--- darc/testsuite/key.py | 4 ++-- tox.ini | 3 ++- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/darc/cache.py b/darc/cache.py index b8a6b568f..c871d544b 100644 --- a/darc/cache.py +++ b/darc/cache.py @@ -3,10 +3,10 @@ import fcntl from itertools import zip_longest import msgpack import os -from binascii import hexlify, unhexlify +from binascii import hexlify import shutil -from .helpers import get_cache_dir, decode_dict, st_mtime_ns +from .helpers import get_cache_dir, decode_dict, st_mtime_ns, unhexlify from .hashindex import ChunkIndex @@ -58,7 +58,7 @@ class Cache(object): if self.config.getint('cache', 'version') != 1: raise Exception('%s Does not look like a darc cache') self.id = self.config.get('cache', 'repository') - self.manifest_id = unhexlify(self.config.get('cache', 'manifest').encode('ascii')) # .encode needed for Python 3.[0-2] + self.manifest_id = unhexlify(self.config.get('cache', 'manifest')) self.chunks = ChunkIndex(os.path.join(self.path, 'chunks').encode('utf-8')) self.files = None diff --git a/darc/helpers.py b/darc/helpers.py index a391845f4..67ac3cacc 100644 --- a/darc/helpers.py +++ b/darc/helpers.py @@ -1,7 +1,5 @@ import argparse -from datetime import datetime, timedelta -from fnmatch import fnmatchcase -from operator import attrgetter +import binascii import grp import msgpack import os @@ -10,10 +8,12 @@ import re import stat import sys import time -import urllib +from datetime import datetime, timedelta +from fnmatch import fnmatchcase +from operator import attrgetter -class Manifest(object): +class Manifest: MANIFEST_ID = b'\0' * 32 @@ -62,7 +62,7 @@ def prune_split(archives, pattern, n, skip=[]): return keep -class Statistics(object): +class Statistics: def __init__(self): self.osize = self.csize = self.usize = self.nfiles = 0 @@ -112,7 +112,7 @@ def exclude_path(path, patterns): return False -class IncludePattern(object): +class IncludePattern: """--include PATTERN """ def __init__(self, pattern): @@ -345,12 +345,23 @@ def decode_dict(d, keys, encoding='utf-8', errors='surrogateescape'): def remove_surrogates(s, errors='replace'): + """Replace surrogates generated by fsdecode with '?' + """ return s.encode('utf-8', errors).decode('utf-8') if sys.version < '3.3': + # st_mtime_ns attribute only available in 3.3+ def st_mtime_ns(st): return int(st.st_mtime * 10**9) + + # unhexlify in < 3.3 incorrectly only accepts bytes input + def unhexlify(data): + if isinstance(data, str): + data = data.encode('ascii') + return binascii.unhexlify(data) else: def st_mtime_ns(st): return st.st_mtime_ns + + unhexlify = binascii.unhexlify diff --git a/darc/repository.py b/darc/repository.py index 5fa39f189..28ac7a425 100644 --- a/darc/repository.py +++ b/darc/repository.py @@ -1,5 +1,5 @@ from configparser import RawConfigParser -from binascii import hexlify, unhexlify +from binascii import hexlify import fcntl import os import re @@ -10,7 +10,7 @@ import unittest from zlib import crc32 from .hashindex import NSIndex -from .helpers import IntegrityError, read_msgpack, write_msgpack +from .helpers import IntegrityError, read_msgpack, write_msgpack, unhexlify from .lrucache import LRUCache MAX_OBJECT_SIZE = 20 * 1024 * 1024 @@ -81,7 +81,7 @@ class Repository(object): raise Exception('%s Does not look like a darc repository') self.max_segment_size = self.config.getint('repository', 'max_segment_size') self.segments_per_dir = self.config.getint('repository', 'segments_per_dir') - self.id = unhexlify(self.config.get('repository', 'id').strip().encode('ascii')) # .encode needed for Python 3.[0-2] + self.id = unhexlify(self.config.get('repository', 'id').strip()) self.rollback() def close(self): diff --git a/darc/testsuite/key.py b/darc/testsuite/key.py index 81d8e3bcf..417314def 100644 --- a/darc/testsuite/key.py +++ b/darc/testsuite/key.py @@ -2,11 +2,11 @@ import os import re import shutil import tempfile -from binascii import hexlify, unhexlify +from binascii import hexlify from darc.crypto import bytes_to_long from darc.testsuite import DarcTestCase from darc.key import PlaintextKey, PassphraseKey, KeyfileKey -from darc.helpers import Location +from darc.helpers import Location, unhexlify class KeyTestCase(DarcTestCase): diff --git a/tox.ini b/tox.ini index 128173ee0..c8b60d7a1 100644 --- a/tox.ini +++ b/tox.ini @@ -2,4 +2,5 @@ envlist = py32, py33 [testenv] -commands = fakeroot {envpython} -m darc.testsuite.run [] +changedir = docs # Change dir to avoid import problem +commands = fakeroot {envpython} -m darc.testsuite.run -bv []