repository.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env python
  2. import fcntl
  3. import tempfile
  4. import logging
  5. import os
  6. import posixpath
  7. import shutil
  8. import unittest
  9. import uuid
  10. log = logging.getLogger('')
  11. class Repository(object):
  12. """
  13. """
  14. IDLE = 'Idle'
  15. OPEN = 'Open'
  16. ACTIVE = 'Active'
  17. VERSION = 'DEDUPSTORE REPOSITORY VERSION 1'
  18. def __init__(self, path):
  19. self.tid = -1
  20. self.state = Repository.IDLE
  21. if not os.path.exists(path):
  22. self.create(path)
  23. self.open(path)
  24. def create(self, path):
  25. log.info('Initializing Repository at "%s"' % path)
  26. os.mkdir(path)
  27. open(os.path.join(path, 'VERSION'), 'wb').write(self.VERSION)
  28. open(os.path.join(path, 'uuid'), 'wb').write(str(uuid.uuid4()))
  29. open(os.path.join(path, 'tid'), 'wb').write('0')
  30. os.mkdir(os.path.join(path, 'data'))
  31. def open(self, path):
  32. self.path = path
  33. if not os.path.isdir(path):
  34. raise Exception('%s Does not look like a repository')
  35. version_path = os.path.join(path, 'version')
  36. if not os.path.exists(version_path) or open(version_path, 'rb').read() != self.VERSION:
  37. raise Exception('%s Does not look like a repository2')
  38. self.uuid = open(os.path.join(path, 'uuid'), 'rb').read()
  39. self.lock_fd = open(os.path.join(path, 'lock'), 'w')
  40. fcntl.flock(self.lock_fd, fcntl.LOCK_EX)
  41. self.tid = int(open(os.path.join(path, 'tid'), 'r').read())
  42. self.recover()
  43. def recover(self):
  44. if os.path.exists(os.path.join(self.path, 'txn-active')):
  45. self.rollback()
  46. if os.path.exists(os.path.join(self.path, 'txn-commit')):
  47. self.apply_txn()
  48. if os.path.exists(os.path.join(self.path, 'txn-applied')):
  49. shutil.rmtree(os.path.join(self.path, 'txn-applied'))
  50. self.state = Repository.OPEN
  51. def close(self):
  52. self.recover()
  53. self.lock_fd.close()
  54. self.state = Repository.IDLE
  55. def commit(self):
  56. """
  57. """
  58. if self.state == Repository.OPEN:
  59. return
  60. assert self.state == Repository.ACTIVE
  61. remove_fd = open(os.path.join(self.path, 'txn-active', 'remove'), 'wb')
  62. remove_fd.write('\n'.join(self.txn_removed))
  63. remove_fd.close()
  64. add_fd = open(os.path.join(self.path, 'txn-active', 'add_index'), 'wb')
  65. add_fd.write('\n'.join(self.txn_added))
  66. add_fd.close()
  67. tid_fd = open(os.path.join(self.path, 'txn-active', 'tid'), 'wb')
  68. tid_fd.write(str(self.tid + 1))
  69. tid_fd.close()
  70. os.rename(os.path.join(self.path, 'txn-active'),
  71. os.path.join(self.path, 'txn-commit'))
  72. self.apply_txn()
  73. def apply_txn(self):
  74. assert os.path.isdir(os.path.join(self.path, 'txn-commit'))
  75. tid = int(open(os.path.join(self.path, 'txn-commit', 'tid'), 'rb').read())
  76. assert tid >= self.tid
  77. remove_list = [line.strip() for line in
  78. open(os.path.join(self.path, 'txn-commit', 'remove'), 'rb').readlines()]
  79. for name in remove_list:
  80. path = os.path.join(self.path, 'data', name)
  81. os.unlink(path)
  82. add_list = [line.strip() for line in
  83. open(os.path.join(self.path, 'txn-commit', 'add_index'), 'rb').readlines()]
  84. for name in add_list:
  85. destname = os.path.join(self.path, 'data', name)
  86. if not os.path.exists(os.path.dirname(destname)):
  87. os.makedirs(os.path.dirname(destname))
  88. shutil.move(os.path.join(self.path, 'txn-commit', 'add', name), destname)
  89. tid_fd = open(os.path.join(self.path, 'tid'), 'wb')
  90. tid_fd.write(str(tid))
  91. tid_fd.close()
  92. os.rename(os.path.join(self.path, 'txn-commit'),
  93. os.path.join(self.path, 'txn-applied'))
  94. shutil.rmtree(os.path.join(self.path, 'txn-applied'))
  95. self.tid = tid
  96. self.state = Repository.OPEN
  97. def rollback(self):
  98. """
  99. """
  100. txn_path = os.path.join(self.path, 'txn-active')
  101. if os.path.exists(txn_path):
  102. shutil.rmtree(txn_path)
  103. self.state = Repository.OPEN
  104. def prepare_txn(self):
  105. if self.state == Repository.ACTIVE:
  106. return os.path.join(self.path, 'txn-active')
  107. elif self.state == Repository.OPEN:
  108. os.mkdir(os.path.join(self.path, 'txn-active'))
  109. os.mkdir(os.path.join(self.path, 'txn-active', 'add'))
  110. self.txn_removed = []
  111. self.txn_added = []
  112. self.state = Repository.ACTIVE
  113. def get_file(self, path):
  114. """
  115. """
  116. if os.path.exists(os.path.join(self.path, 'txn-active', 'add', path)):
  117. return open(os.path.join(self.path, 'txn-active', 'add', path), 'rb').read()
  118. elif os.path.exists(os.path.join(self.path, 'data', path)):
  119. return open(os.path.join(self.path, 'data', path), 'rb').read()
  120. else:
  121. raise Exception('FileNotFound: %s' % path)
  122. def put_file(self, path, data):
  123. """
  124. """
  125. self.prepare_txn()
  126. if (path in self.txn_added or
  127. (path not in self.txn_removed and os.path.exists(os.path.join(self.path, 'data', path)))):
  128. raise Exception('FileAlreadyExists: %s' % path)
  129. if path in self.txn_removed:
  130. self.txn_removed.remove(path)
  131. if path not in self.txn_added:
  132. self.txn_added.append(path)
  133. filename = os.path.join(self.path, 'txn-active', 'add', path)
  134. if not os.path.exists(os.path.dirname(filename)):
  135. os.makedirs(os.path.dirname(filename))
  136. fd = open(filename, 'wb')
  137. try:
  138. fd.write(data)
  139. finally:
  140. fd.close()
  141. def delete(self, path):
  142. """
  143. """
  144. self.prepare_txn()
  145. if os.path.exists(os.path.join(self.path, 'txn-active', 'add', path)):
  146. os.unlink(os.path.join(self.path, 'txn-active', 'add', path))
  147. elif os.path.exists(os.path.join(self.path, 'data', path)):
  148. self.txn_removed.append(path)
  149. else:
  150. raise Exception('FileNotFound: %s' % path)
  151. def listdir(self, path):
  152. """
  153. """
  154. entries = set(os.listdir(os.path.join(self.path, 'data', path)))
  155. if self.state == Repository.ACTIVE:
  156. txn_entries = set(os.listdir(os.path.join(self.path, 'txn-active', 'add', path)))
  157. entries = entries.union(txn_entries)
  158. for e in entries:
  159. if posixpath.join(path, e) in self.txn_removed:
  160. entries.remove(e)
  161. return list(entries)
  162. def mkdir(self, path):
  163. """
  164. """
  165. def rmdir(self, path):
  166. """
  167. """
  168. class RepositoryTestCase(unittest.TestCase):
  169. def setUp(self):
  170. self.tmppath = tempfile.mkdtemp()
  171. self.repo = Repository(os.path.join(self.tmppath, 'repo'))
  172. def tearDown(self):
  173. shutil.rmtree(self.tmppath)
  174. def test1(self):
  175. self.assertEqual(self.repo.tid, 0)
  176. self.assertEqual(self.repo.state, Repository.OPEN)
  177. self.assertEqual(self.repo.listdir(''), [])
  178. self.repo.put_file('foo', 'SOMEDATA')
  179. self.assertRaises(Exception, lambda: self.repo.put_file('foo', 'SOMETHINGELSE'))
  180. self.assertEqual(self.repo.get_file('foo'), 'SOMEDATA')
  181. self.assertEqual(self.repo.listdir(''), ['foo'])
  182. self.repo.rollback()
  183. self.assertEqual(self.repo.listdir(''), [])
  184. def test2(self):
  185. self.repo.put_file('foo', 'SOMEDATA')
  186. self.repo.put_file('bar', 'SOMEDATAbar')
  187. self.assertEqual(self.repo.listdir(''), ['foo', 'bar'])
  188. self.assertEqual(self.repo.get_file('foo'), 'SOMEDATA')
  189. self.repo.delete('foo')
  190. self.assertRaises(Exception, lambda: self.repo.get_file('foo'))
  191. self.assertEqual(self.repo.listdir(''), ['bar'])
  192. self.assertEqual(self.repo.state, Repository.ACTIVE)
  193. self.assertEqual(os.path.exists(os.path.join(self.tmppath, 'repo', 'data', 'bar')), False)
  194. self.repo.commit()
  195. self.assertEqual(os.path.exists(os.path.join(self.tmppath, 'repo', 'data', 'bar')), True)
  196. self.assertEqual(self.repo.listdir(''), ['bar'])
  197. self.assertEqual(self.repo.state, Repository.IDLE)
  198. if __name__ == '__main__':
  199. unittest.main()