extensions.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import os
  2. import threading
  3. from modules import shared, errors, cache
  4. from modules.gitpython_hack import Repo
  5. from modules.paths_internal import extensions_dir, extensions_builtin_dir, script_path # noqa: F401
  6. extensions = []
  7. os.makedirs(extensions_dir, exist_ok=True)
  8. def active():
  9. if shared.opts.disable_all_extensions == "all":
  10. return []
  11. elif shared.opts.disable_all_extensions == "extra":
  12. return [x for x in extensions if x.enabled and x.is_builtin]
  13. else:
  14. return [x for x in extensions if x.enabled]
  15. class Extension:
  16. lock = threading.Lock()
  17. cached_fields = ['remote', 'commit_date', 'branch', 'commit_hash', 'version']
  18. def __init__(self, name, path, enabled=True, is_builtin=False):
  19. self.name = name
  20. self.path = path
  21. self.enabled = enabled
  22. self.status = ''
  23. self.can_update = False
  24. self.is_builtin = is_builtin
  25. self.commit_hash = ''
  26. self.commit_date = None
  27. self.version = ''
  28. self.branch = None
  29. self.remote = None
  30. self.have_info_from_repo = False
  31. def to_dict(self):
  32. return {x: getattr(self, x) for x in self.cached_fields}
  33. def from_dict(self, d):
  34. for field in self.cached_fields:
  35. setattr(self, field, d[field])
  36. def read_info_from_repo(self):
  37. if self.is_builtin or self.have_info_from_repo:
  38. return
  39. def read_from_repo():
  40. with self.lock:
  41. if self.have_info_from_repo:
  42. return
  43. self.do_read_info_from_repo()
  44. return self.to_dict()
  45. try:
  46. d = cache.cached_data_for_file('extensions-git', self.name, os.path.join(self.path, ".git"), read_from_repo)
  47. self.from_dict(d)
  48. except FileNotFoundError:
  49. pass
  50. self.status = 'unknown' if self.status == '' else self.status
  51. def do_read_info_from_repo(self):
  52. repo = None
  53. try:
  54. if os.path.exists(os.path.join(self.path, ".git")):
  55. repo = Repo(self.path)
  56. except Exception:
  57. errors.report(f"Error reading github repository info from {self.path}", exc_info=True)
  58. if repo is None or repo.bare:
  59. self.remote = None
  60. else:
  61. try:
  62. self.remote = next(repo.remote().urls, None)
  63. commit = repo.head.commit
  64. self.commit_date = commit.committed_date
  65. if repo.active_branch:
  66. self.branch = repo.active_branch.name
  67. self.commit_hash = commit.hexsha
  68. self.version = self.commit_hash[:8]
  69. except Exception:
  70. errors.report(f"Failed reading extension data from Git repository ({self.name})", exc_info=True)
  71. self.remote = None
  72. self.have_info_from_repo = True
  73. def list_files(self, subdir, extension):
  74. from modules import scripts
  75. dirpath = os.path.join(self.path, subdir)
  76. if not os.path.isdir(dirpath):
  77. return []
  78. res = []
  79. for filename in sorted(os.listdir(dirpath)):
  80. res.append(scripts.ScriptFile(self.path, filename, os.path.join(dirpath, filename)))
  81. res = [x for x in res if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
  82. return res
  83. def check_updates(self):
  84. repo = Repo(self.path)
  85. for fetch in repo.remote().fetch(dry_run=True):
  86. if fetch.flags != fetch.HEAD_UPTODATE:
  87. self.can_update = True
  88. self.status = "new commits"
  89. return
  90. try:
  91. origin = repo.rev_parse('origin')
  92. if repo.head.commit != origin:
  93. self.can_update = True
  94. self.status = "behind HEAD"
  95. return
  96. except Exception:
  97. self.can_update = False
  98. self.status = "unknown (remote error)"
  99. return
  100. self.can_update = False
  101. self.status = "latest"
  102. def fetch_and_reset_hard(self, commit='origin'):
  103. repo = Repo(self.path)
  104. # Fix: `error: Your local changes to the following files would be overwritten by merge`,
  105. # because WSL2 Docker set 755 file permissions instead of 644, this results to the error.
  106. repo.git.fetch(all=True)
  107. repo.git.reset(commit, hard=True)
  108. self.have_info_from_repo = False
  109. def list_extensions():
  110. extensions.clear()
  111. if not os.path.isdir(extensions_dir):
  112. return
  113. if shared.opts.disable_all_extensions == "all":
  114. print("*** \"Disable all extensions\" option was set, will not load any extensions ***")
  115. elif shared.opts.disable_all_extensions == "extra":
  116. print("*** \"Disable all extensions\" option was set, will only load built-in extensions ***")
  117. extension_paths = []
  118. for dirname in [extensions_dir, extensions_builtin_dir]:
  119. if not os.path.isdir(dirname):
  120. return
  121. for extension_dirname in sorted(os.listdir(dirname)):
  122. path = os.path.join(dirname, extension_dirname)
  123. if not os.path.isdir(path):
  124. continue
  125. extension_paths.append((extension_dirname, path, dirname == extensions_builtin_dir))
  126. for dirname, path, is_builtin in extension_paths:
  127. extension = Extension(name=dirname, path=path, enabled=dirname not in shared.opts.disabled_extensions, is_builtin=is_builtin)
  128. extensions.append(extension)