Add py3.2 compatible unhexlify to helpers.py

This commit is contained in:
Jonas Borgström 2013-06-26 22:00:23 +02:00
parent f6e9da9c5a
commit 00bdfa1a8e
5 changed files with 28 additions and 16 deletions

View File

@ -3,10 +3,10 @@ import fcntl
from itertools import zip_longest from itertools import zip_longest
import msgpack import msgpack
import os import os
from binascii import hexlify, unhexlify from binascii import hexlify
import shutil 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 from .hashindex import ChunkIndex
@ -58,7 +58,7 @@ class Cache(object):
if self.config.getint('cache', 'version') != 1: if self.config.getint('cache', 'version') != 1:
raise Exception('%s Does not look like a darc cache') raise Exception('%s Does not look like a darc cache')
self.id = self.config.get('cache', 'repository') 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.chunks = ChunkIndex(os.path.join(self.path, 'chunks').encode('utf-8'))
self.files = None self.files = None

View File

@ -1,7 +1,5 @@
import argparse import argparse
from datetime import datetime, timedelta import binascii
from fnmatch import fnmatchcase
from operator import attrgetter
import grp import grp
import msgpack import msgpack
import os import os
@ -10,10 +8,12 @@ import re
import stat import stat
import sys import sys
import time 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 MANIFEST_ID = b'\0' * 32
@ -62,7 +62,7 @@ def prune_split(archives, pattern, n, skip=[]):
return keep return keep
class Statistics(object): class Statistics:
def __init__(self): def __init__(self):
self.osize = self.csize = self.usize = self.nfiles = 0 self.osize = self.csize = self.usize = self.nfiles = 0
@ -112,7 +112,7 @@ def exclude_path(path, patterns):
return False return False
class IncludePattern(object): class IncludePattern:
"""--include PATTERN """--include PATTERN
""" """
def __init__(self, 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'): def remove_surrogates(s, errors='replace'):
"""Replace surrogates generated by fsdecode with '?'
"""
return s.encode('utf-8', errors).decode('utf-8') return s.encode('utf-8', errors).decode('utf-8')
if sys.version < '3.3': if sys.version < '3.3':
# st_mtime_ns attribute only available in 3.3+
def st_mtime_ns(st): def st_mtime_ns(st):
return int(st.st_mtime * 10**9) 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: else:
def st_mtime_ns(st): def st_mtime_ns(st):
return st.st_mtime_ns return st.st_mtime_ns
unhexlify = binascii.unhexlify

View File

@ -1,5 +1,5 @@
from configparser import RawConfigParser from configparser import RawConfigParser
from binascii import hexlify, unhexlify from binascii import hexlify
import fcntl import fcntl
import os import os
import re import re
@ -10,7 +10,7 @@ import unittest
from zlib import crc32 from zlib import crc32
from .hashindex import NSIndex 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 from .lrucache import LRUCache
MAX_OBJECT_SIZE = 20 * 1024 * 1024 MAX_OBJECT_SIZE = 20 * 1024 * 1024
@ -81,7 +81,7 @@ class Repository(object):
raise Exception('%s Does not look like a darc repository') raise Exception('%s Does not look like a darc repository')
self.max_segment_size = self.config.getint('repository', 'max_segment_size') self.max_segment_size = self.config.getint('repository', 'max_segment_size')
self.segments_per_dir = self.config.getint('repository', 'segments_per_dir') 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() self.rollback()
def close(self): def close(self):

View File

@ -2,11 +2,11 @@ import os
import re import re
import shutil import shutil
import tempfile import tempfile
from binascii import hexlify, unhexlify from binascii import hexlify
from darc.crypto import bytes_to_long from darc.crypto import bytes_to_long
from darc.testsuite import DarcTestCase from darc.testsuite import DarcTestCase
from darc.key import PlaintextKey, PassphraseKey, KeyfileKey from darc.key import PlaintextKey, PassphraseKey, KeyfileKey
from darc.helpers import Location from darc.helpers import Location, unhexlify
class KeyTestCase(DarcTestCase): class KeyTestCase(DarcTestCase):

View File

@ -2,4 +2,5 @@
envlist = py32, py33 envlist = py32, py33
[testenv] [testenv]
commands = fakeroot {envpython} -m darc.testsuite.run [] changedir = docs # Change dir to avoid import problem
commands = fakeroot {envpython} -m darc.testsuite.run -bv []