2
0

repository.py 8.0 KB

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