repository.py 8.1 KB

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