|
@@ -15,15 +15,28 @@ class Archive(object):
|
|
|
def __init__(self, store, name=None):
|
|
|
self.store = store
|
|
|
self.items = []
|
|
|
+ self.chunks = []
|
|
|
+ self.chunk_idx = {}
|
|
|
if name:
|
|
|
self.open(name)
|
|
|
|
|
|
+ def add_chunk(self, id, sum, csize, osize):
|
|
|
+ try:
|
|
|
+ return self.chunk_idx[id]
|
|
|
+ except KeyError:
|
|
|
+ idx = len(self.chunks)
|
|
|
+ self.chunks.append((id, sum, csize, osize))
|
|
|
+ self.chunk_idx[id] = idx
|
|
|
+ return idx
|
|
|
+
|
|
|
def open(self, name):
|
|
|
archive = cPickle.loads(zlib.decompress(self.store.get(NS_ARCHIVES, name)))
|
|
|
self.items = archive['items']
|
|
|
+ for i, (id, sum, csize, osize) in enumerate(archive['chunks']):
|
|
|
+ self.chunk_idx[i] = id
|
|
|
|
|
|
def save(self, name):
|
|
|
- archive = {'name': name, 'items': self.items}
|
|
|
+ archive = {'name': name, 'items': self.items, 'chunks': self.chunks}
|
|
|
self.store.put(NS_ARCHIVES, name, zlib.compress(cPickle.dumps(archive)))
|
|
|
self.store.commit()
|
|
|
|
|
@@ -39,10 +52,14 @@ class Archive(object):
|
|
|
if not os.path.exists(item['path']):
|
|
|
os.makedirs(item['path'])
|
|
|
if item['type'] == 'FILE':
|
|
|
+ path = item['path']
|
|
|
+ if not os.path.exists(os.path.dirname(path)):
|
|
|
+ os.makedirs(os.path.dirname(path))
|
|
|
with open(item['path'], 'wb') as fd:
|
|
|
for chunk in item['chunks']:
|
|
|
- data = self.store.get(NS_CHUNKS, chunk)
|
|
|
- if hashlib.sha1(data).digest() != chunk[4:]:
|
|
|
+ id = self.chunk_idx[chunk]
|
|
|
+ data = self.store.get(NS_CHUNKS, id)
|
|
|
+ if hashlib.sha1(data).digest() != id:
|
|
|
raise Exception('Invalid chunk checksum')
|
|
|
fd.write(zlib.decompress(data))
|
|
|
|
|
@@ -50,9 +67,11 @@ class Archive(object):
|
|
|
for item in self.items:
|
|
|
if item['type'] == 'FILE':
|
|
|
print item['path'], '...',
|
|
|
+ print self.chunk_idx[0].encode('hex')
|
|
|
for chunk in item['chunks']:
|
|
|
- data = self.store.get(NS_CHUNKS, chunk)
|
|
|
- if hashlib.sha1(data).digest() != chunk[4:]:
|
|
|
+ id = self.chunk_idx[chunk]
|
|
|
+ data = self.store.get(NS_CHUNKS, id)
|
|
|
+ if hashlib.sha1(data).digest() != id:
|
|
|
print 'ERROR'
|
|
|
break
|
|
|
else:
|
|
@@ -68,28 +87,39 @@ class Archive(object):
|
|
|
cache.archives.remove(self.name)
|
|
|
cache.save()
|
|
|
|
|
|
-
|
|
|
-class Archiver(object):
|
|
|
-
|
|
|
- def create_archive(self, archive_name, paths):
|
|
|
- try:
|
|
|
- self.store.get(NS_ARCHIVES, archive_name)
|
|
|
- except Store.DoesNotExist:
|
|
|
- pass
|
|
|
- else:
|
|
|
- raise Exception('Archive "%s" already exists' % archive_name)
|
|
|
- archive = Archive(self.store)
|
|
|
+ def create(self, name, paths, cache):
|
|
|
for path in paths:
|
|
|
for root, dirs, files in os.walk(path):
|
|
|
for d in dirs:
|
|
|
- name = os.path.join(root, d)
|
|
|
- archive.items.append(self.process_dir(name, self.cache))
|
|
|
+ p = os.path.join(root, d)
|
|
|
+ self.items.append(self.process_dir(p, cache))
|
|
|
for f in files:
|
|
|
- name = os.path.join(root, f)
|
|
|
- archive.items.append(self.process_file(name, self.cache))
|
|
|
- archive.save(archive_name)
|
|
|
- self.cache.archives.append(archive_name)
|
|
|
- self.cache.save()
|
|
|
+ p = os.path.join(root, f)
|
|
|
+ self.items.append(self.process_file(p, cache))
|
|
|
+ self.save(name)
|
|
|
+ cache.archives.append(name)
|
|
|
+ cache.save()
|
|
|
+
|
|
|
+ def process_dir(self, path, cache):
|
|
|
+ path = path.lstrip('/\\:')
|
|
|
+ print 'Directory: %s' % (path)
|
|
|
+ return {'type': 'DIR', 'path': path}
|
|
|
+
|
|
|
+ def process_file(self, path, cache):
|
|
|
+ with open(path, 'rb') as fd:
|
|
|
+ path = path.lstrip('/\\:')
|
|
|
+ print 'Adding: %s...' % path
|
|
|
+ chunks = []
|
|
|
+ for chunk in chunkify(fd, CHUNK_SIZE, cache.summap):
|
|
|
+ chunks.append(self.add_chunk(*cache.add_chunk(chunk)))
|
|
|
+ return {'type': 'FILE', 'path': path, 'chunks': chunks}
|
|
|
+
|
|
|
+
|
|
|
+class Archiver(object):
|
|
|
+
|
|
|
+ def create_archive(self, name, paths):
|
|
|
+ archive = Archive(self.store)
|
|
|
+ archive.create(name, paths, self.cache)
|
|
|
|
|
|
def delete_archive(self, archive_name):
|
|
|
archive = Archive(self.store, archive_name)
|
|
@@ -112,28 +142,6 @@ class Archiver(object):
|
|
|
archive = Archive(self.store, archive_name)
|
|
|
archive.extract()
|
|
|
|
|
|
- def process_dir(self, path, cache):
|
|
|
- path = path.lstrip('/\\:')
|
|
|
- print 'Directory: %s' % (path)
|
|
|
- return {'type': 'DIR', 'path': path}
|
|
|
-
|
|
|
- def process_file(self, path, cache):
|
|
|
- with open(path, 'rb') as fd:
|
|
|
- path = path.lstrip('/\\:')
|
|
|
- print 'Adding: %s...' % path,
|
|
|
- sys.stdout.flush()
|
|
|
- origsize = 0
|
|
|
- compsize = 0
|
|
|
- chunks = []
|
|
|
- for chunk in chunkify(fd, CHUNK_SIZE, self.cache.summap):
|
|
|
- origsize += len(chunk)
|
|
|
- id, size = cache.add_chunk(chunk)
|
|
|
- compsize += size
|
|
|
- chunks.append(id)
|
|
|
- ratio = origsize and compsize * 100 / origsize or 0
|
|
|
- print '(%d chunks: %d%%)' % (len(chunks), ratio)
|
|
|
- return {'type': 'FILE', 'path': path, 'size': origsize, 'chunks': chunks}
|
|
|
-
|
|
|
def run(self):
|
|
|
parser = OptionParser()
|
|
|
parser.add_option("-s", "--store", dest="store",
|