소스 검색

Add py3.2 compatible unhexlify to helpers.py

Jonas Borgström 12 년 전
부모
커밋
00bdfa1a8e
5개의 변경된 파일28개의 추가작업 그리고 16개의 파일을 삭제
  1. 3 3
      darc/cache.py
  2. 18 7
      darc/helpers.py
  3. 3 3
      darc/repository.py
  4. 2 2
      darc/testsuite/key.py
  5. 2 1
      tox.ini

+ 3 - 3
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
 

+ 18 - 7
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

+ 3 - 3
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):

+ 2 - 2
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):

+ 2 - 1
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 []