repository.py 8.1 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. class Repository(object):
  11. """
  12. """
  13. IDLE = 'Idle'
  14. OPEN = 'Open'
  15. ACTIVE = 'Active'
  16. VERSION = 'DEDUPSTORE REPOSITORY VERSION 1'
  17. def __init__(self, path):
  18. self.tid = -1
  19. self.state = Repository.IDLE
  20. if not os.path.exists(path):
  21. self.create(path)
  22. self.open(path)
  23. def create(self, path):
  24. log.info('Initializing Repository at "%s"' % path)
  25. os.mkdir(path)
  26. open(os.path.join(path, 'VERSION'), 'wb').write(self.VERSION)
  27. open(os.path.join(path, 'tid'), 'wb').write('0')
  28. os.mkdir(os.path.join(path, 'data'))
  29. def open(self, path):
  30. self.path = path
  31. if not os.path.isdir(path):
  32. raise Exception('%s Does not look like a repository')
  33. version_path = os.path.join(path, 'version')
  34. if not os.path.exists(version_path) or open(version_path, 'rb').read() != self.VERSION:
  35. raise Exception('%s Does not look like a repository2')
  36. self.lock_fd = open(os.path.join(path, 'lock'), 'w')
  37. fcntl.flock(self.lock_fd, fcntl.LOCK_EX)
  38. print 'locked'
  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. add_path = os.path.join(self.path, 'txn-commit', 'add')
  83. for name in os.listdir(add_path):
  84. destname = os.path.join(self.path, 'data', name)
  85. if not os.path.exists(os.path.dirname(destname)):
  86. os.makedirs(os.path.dirname(destname))
  87. shutil.move(os.path.join(add_path, name), destname)
  88. tid_fd = open(os.path.join(self.path, 'tid'), 'wb')
  89. tid_fd.write(str(tid))
  90. tid_fd.close()
  91. os.rename(os.path.join(self.path, 'txn-commit'),
  92. os.path.join(self.path, 'txn-applied'))
  93. shutil.rmtree(os.path.join(self.path, 'txn-applied'))
  94. self.state = Repository.IDLE
  95. def rollback(self):
  96. """
  97. """
  98. txn_path = os.path.join(self.path, 'txn-active')
  99. if os.path.exists(txn_path):
  100. shutil.rmtree(txn_path)
  101. self.state = Repository.OPEN
  102. def prepare_txn(self):
  103. if self.state == Repository.ACTIVE:
  104. return os.path.join(self.path, 'txn-active')
  105. elif self.state == Repository.OPEN:
  106. os.mkdir(os.path.join(self.path, 'txn-active'))
  107. os.mkdir(os.path.join(self.path, 'txn-active', 'add'))
  108. self.txn_removed = []
  109. self.txn_added = []
  110. self.state = Repository.ACTIVE
  111. def get_file(self, path):
  112. """
  113. """
  114. if os.path.exists(os.path.join(self.path, 'txn-active', 'add', path)):
  115. return open(os.path.join(self.path, 'txn-active', 'add', path), 'rb').read()
  116. elif os.path.exists(os.path.join(self.path, 'data', path)):
  117. return open(os.path.join(self.path, 'data', path), 'rb').read()
  118. else:
  119. raise Exception('FileNotFound: %s' % path)
  120. def put_file(self, path, data):
  121. """
  122. """
  123. self.prepare_txn()
  124. if os.path.exists(os.path.join(self.path, 'txn-active', 'add', path)):
  125. raise Exception('FileAlreadyExists: %s' % path)
  126. if path in self.txn_removed:
  127. self.txn_removed.remove(path)
  128. if path not in self.txn_added:
  129. self.txn_added.append(path)
  130. filename = os.path.join(self.path, 'txn-active', 'add', path)
  131. if not os.path.exists(os.path.dirname(filename)):
  132. os.makedirs(os.path.dirname(filename))
  133. fd = open(filename, 'wb')
  134. try:
  135. fd.write(data)
  136. finally:
  137. fd.close()
  138. def delete(self, path):
  139. """
  140. """
  141. self.prepare_txn()
  142. if os.path.exists(os.path.join(self.path, 'txn-active', 'add', path)):
  143. os.unlink(os.path.join(self.path, 'txn-active', 'add', path))
  144. elif os.path.exists(os.path.join(self.path, 'data', path)):
  145. self.txn_removed.append(path)
  146. else:
  147. raise Exception('FileNotFound: %s' % path)
  148. def listdir(self, path):
  149. """
  150. """
  151. entries = set(os.listdir(os.path.join(self.path, 'data', path)))
  152. if self.state == Repository.ACTIVE:
  153. txn_entries = set(os.listdir(os.path.join(self.path, 'txn-active', 'add', path)))
  154. entries = entries.union(txn_entries)
  155. for e in entries:
  156. if posixpath.join(path, e) in self.txn_removed:
  157. entries.remove(e)
  158. return list(entries)
  159. def mkdir(self, path):
  160. """
  161. """
  162. def rmdir(self, path):
  163. """
  164. """
  165. class RepositoryTestCase(unittest.TestCase):
  166. def setUp(self):
  167. self.tmppath = tempfile.mkdtemp()
  168. self.repo = Repository(os.path.join(self.tmppath, 'repo'))
  169. def tearDown(self):
  170. shutil.rmtree(self.tmppath)
  171. def test1(self):
  172. self.assertEqual(self.repo.tid, 0)
  173. self.assertEqual(self.repo.state, Repository.OPEN)
  174. self.assertEqual(self.repo.listdir(''), [])
  175. self.repo.put_file('foo', 'SOMEDATA')
  176. self.assertRaises(Exception, lambda: self.repo.put_file('foo', 'SOMETHINGELSE'))
  177. self.assertEqual(self.repo.get_file('foo'), 'SOMEDATA')
  178. self.assertEqual(self.repo.listdir(''), ['foo'])
  179. self.repo.rollback()
  180. self.assertEqual(self.repo.listdir(''), [])
  181. def test2(self):
  182. self.repo.put_file('foo', 'SOMEDATA')
  183. self.repo.put_file('bar', 'SOMEDATAbar')
  184. self.assertEqual(self.repo.listdir(''), ['foo', 'bar'])
  185. self.assertEqual(self.repo.get_file('foo'), 'SOMEDATA')
  186. self.repo.delete('foo')
  187. self.assertRaises(Exception, lambda: self.repo.get_file('foo'))
  188. self.assertEqual(self.repo.listdir(''), ['bar'])
  189. self.assertEqual(self.repo.state, Repository.ACTIVE)
  190. self.assertEqual(os.path.exists(os.path.join(self.tmppath, 'repo', 'data', 'bar')), False)
  191. self.repo.commit()
  192. self.assertEqual(os.path.exists(os.path.join(self.tmppath, 'repo', 'data', 'bar')), True)
  193. self.assertEqual(self.repo.listdir(''), ['bar'])
  194. self.assertEqual(self.repo.state, Repository.IDLE)
  195. if __name__ == '__main__':
  196. unittest.main()