archiver.py 3.2 KB

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