您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

83 行
2.6KB

  1. # Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
  2. #
  3. # This module is part of GitDB and is released under
  4. # the New BSD License: http://www.opensource.org/licenses/bsd-license.php
  5. import codecs
  6. from gitdb.db.base import (
  7. CompoundDB,
  8. )
  9. __all__ = ('ReferenceDB', )
  10. class ReferenceDB(CompoundDB):
  11. """A database consisting of database referred to in a file"""
  12. # Configuration
  13. # Specifies the object database to use for the paths found in the alternates
  14. # file. If None, it defaults to the GitDB
  15. ObjectDBCls = None
  16. def __init__(self, ref_file):
  17. super(ReferenceDB, self).__init__()
  18. self._ref_file = ref_file
  19. def _set_cache_(self, attr):
  20. if attr == '_dbs':
  21. self._dbs = list()
  22. self._update_dbs_from_ref_file()
  23. else:
  24. super(ReferenceDB, self)._set_cache_(attr)
  25. # END handle attrs
  26. def _update_dbs_from_ref_file(self):
  27. dbcls = self.ObjectDBCls
  28. if dbcls is None:
  29. # late import
  30. from gitdb.db.git import GitDB
  31. dbcls = GitDB
  32. # END get db type
  33. # try to get as many as possible, don't fail if some are unavailable
  34. ref_paths = list()
  35. try:
  36. with codecs.open(self._ref_file, 'r', encoding="utf-8") as f:
  37. ref_paths = [l.strip() for l in f]
  38. except (OSError, IOError):
  39. pass
  40. # END handle alternates
  41. ref_paths_set = set(ref_paths)
  42. cur_ref_paths_set = {db.root_path() for db in self._dbs}
  43. # remove existing
  44. for path in (cur_ref_paths_set - ref_paths_set):
  45. for i, db in enumerate(self._dbs[:]):
  46. if db.root_path() == path:
  47. del(self._dbs[i])
  48. continue
  49. # END del matching db
  50. # END for each path to remove
  51. # add new
  52. # sort them to maintain order
  53. added_paths = sorted(ref_paths_set - cur_ref_paths_set, key=lambda p: ref_paths.index(p))
  54. for path in added_paths:
  55. try:
  56. db = dbcls(path)
  57. # force an update to verify path
  58. if isinstance(db, CompoundDB):
  59. db.databases()
  60. # END verification
  61. self._dbs.append(db)
  62. except Exception:
  63. # ignore invalid paths or issues
  64. pass
  65. # END for each path to add
  66. def update_cache(self, force=False):
  67. # re-read alternates and update databases
  68. self._update_dbs_from_ref_file()
  69. return super(ReferenceDB, self).update_cache(force)