Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

142 řádky
2.8 KiB

  1. """
  2. Version Control:
  3. Schema:
  4. properties (key, value)
  5. uncommitted (fname, ftype, content, timestamp)
  6. files (fname, ftype, content, timestamp, version)
  7. """
  8. import unittest
  9. import os
  10. root_path = os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', '..'))
  11. testfile = os.path.join(root_path, 'js/core.js')
  12. class TestVC(unittest.TestCase):
  13. def setUp(self):
  14. self.vc = VersionControl(root_path)
  15. self.vc.setup()
  16. def test_add(self):
  17. test_file = {'fname':'test.js', 'ftype':'js', 'content':'test_code', 'timestamp':'1100'}
  18. self.vc.repo.add(**test_file)
  19. ret = self.vc.sql('select * from uncommitted', as_dict=1)[0]
  20. self.assertTrue(ret==test_file)
  21. def tearDown(self):
  22. self.vc.close()
  23. os.remove(os.path.join(root_path, '.wnf'))
  24. class VersionControl:
  25. def __init__(self, root):
  26. self.repo = Repository(self)
  27. self.root(root)
  28. def setup(self):
  29. """
  30. setup the schema
  31. """
  32. self.cur.executescript("""
  33. create table properties(key, value);
  34. create table uncommitted(fname, ftype, content, timestamp);
  35. create table files(fname, ftype, content, timestamp, version);
  36. """)
  37. def root(self, path=None):
  38. """
  39. set / reset root and connect
  40. """
  41. if path:
  42. self.root_path = path
  43. else:
  44. return self.root_path
  45. import sqlite3
  46. self.conn = sqlite3.connect(os.path.join(self.root_path, '.wnf'))
  47. self.cur = self.conn.cursor()
  48. def sql(self, query, values=(), as_dict=None):
  49. """
  50. like webnotes.db.sql
  51. """
  52. self.cur.execute(query, values)
  53. res = self.cur.fetchall()
  54. if as_dict:
  55. out = []
  56. for row in res:
  57. d = {}
  58. for idx, col in enumerate(self.cur.description):
  59. d[col[0]] = row[idx]
  60. out.append(d)
  61. return out
  62. return res
  63. def init(self):
  64. """
  65. crate a .wnf db in the rool path to store the versions
  66. """
  67. pass
  68. def ignore(self, fname):
  69. """
  70. update ignore list
  71. """
  72. pass
  73. def add_all(self):
  74. """
  75. walk the root folder Add all dirty files to the vcs
  76. """
  77. pass
  78. def commit(self, comment=None):
  79. """
  80. commit added files to the repository
  81. """
  82. pass
  83. def close(self):
  84. self.conn.close()
  85. class Repository:
  86. def __init__(self, vc):
  87. self.vc = vc
  88. def add(self, fname, ftype, timestamp, content=None):
  89. """
  90. add to uncommitted
  91. """
  92. self.vc.sql("insert into uncommitted(fname, ftype, timestamp, content) values (?, ?, ?, ?)" \
  93. , (fname, ftype, timestamp, content))
  94. def commit(self, version):
  95. """
  96. copy uncommitted files to repository, update the log and add the change
  97. """
  98. pass
  99. class Log:
  100. def __init__(self, vc):
  101. self.vc = vc
  102. def add_to_log(self, version, fname, ftype):
  103. """
  104. add file to log
  105. """
  106. pass
  107. def get_diff(self, from_version, to_version=None):
  108. """
  109. get list of files changed between versions
  110. """
  111. pass
  112. if __name__=='__main__':
  113. unittest.main()