archiver.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import os
  2. import sys
  3. import hashlib
  4. import zlib
  5. import cPickle
  6. from repository import Repository
  7. CHUNKSIZE = 256 * 1024
  8. class Cache(object):
  9. """Client Side cache
  10. """
  11. def __init__(self, path, repo):
  12. self.repo = repo
  13. self.chunkmap = {}
  14. self.archives = []
  15. self.open(path)
  16. def open(self, path):
  17. for archive in self.repo.listdir('archives'):
  18. self.archives.append(archive)
  19. data = self.repo.get_file(os.path.join('archives', archive))
  20. a = cPickle.loads(zlib.decompress(data))
  21. for item in a['items']:
  22. if item['type'] == 'FILE':
  23. for c in item['chunks']:
  24. print 'adding chunk', c.encode('hex')
  25. self.chunk_incref(c)
  26. def save(self):
  27. assert self.repo.state == Repository.OPEN
  28. def chunk_filename(self, sha):
  29. hex = sha.encode('hex')
  30. return 'chunks/%s/%s/%s' % (hex[:2], hex[2:4], hex[4:])
  31. def add_chunk(self, data):
  32. sha = hashlib.sha1(data).digest()
  33. if not self.seen_chunk(sha):
  34. self.repo.put_file(self.chunk_filename(sha), data)
  35. else:
  36. print 'seen chunk', sha.encode('hex')
  37. self.chunk_incref(sha)
  38. return sha
  39. def seen_chunk(self, sha):
  40. return self.chunkmap.get(sha, 0) > 0
  41. def chunk_incref(self, sha):
  42. self.chunkmap.setdefault(sha, 0)
  43. self.chunkmap[sha] += 1
  44. def chunk_decref(self, sha):
  45. assert self.chunkmap.get(sha, 0) > 0
  46. self.chunkmap[sha] -= 1
  47. return self.chunkmap[sha]
  48. class Archiver(object):
  49. def __init__(self):
  50. self.repo = Repository('/tmp/repo')
  51. self.cache = Cache('/tmp/cache', self.repo)
  52. def create_archive(self, archive_name, path):
  53. if archive_name in self.cache.archives:
  54. raise Exception('Archive "%s" already exists' % archive_name)
  55. items = []
  56. for root, dirs, files in os.walk(path):
  57. for d in dirs:
  58. name = os.path.join(root, d)
  59. items.append(self.process_dir(name, self.cache))
  60. for f in files:
  61. name = os.path.join(root, f)
  62. items.append(self.process_file(name, self.cache))
  63. archive = {'name': name, 'items': items}
  64. zdata = zlib.compress(cPickle.dumps(archive))
  65. self.repo.put_file(os.path.join('archives', archive_name), zdata)
  66. print 'Archive file size: %d' % len(zdata)
  67. self.repo.commit()
  68. self.cache.save()
  69. def process_dir(self, path, cache):
  70. print 'Directory: %s' % (path)
  71. return {'type': 'DIR', 'path': path}
  72. def process_file(self, path, cache):
  73. fd = open(path, 'rb')
  74. size = 0
  75. chunks = []
  76. while True:
  77. data = fd.read(CHUNKSIZE)
  78. if not data:
  79. break
  80. size += len(data)
  81. chunks.append(cache.add_chunk(zlib.compress(data)))
  82. print 'File: %s (%d chunks)' % (path, len(chunks))
  83. return {'type': 'FILE', 'path': path, 'size': size, 'chunks': chunks}
  84. def main():
  85. archiver = Archiver()
  86. archiver.create_archive(sys.argv[1], sys.argv[2])
  87. if __name__ == '__main__':
  88. main()