extensions.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import os
  2. import threading
  3. from modules import shared, errors, cache, scripts
  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.cmd_opts.disable_all_extensions or shared.opts.disable_all_extensions == "all":
  10. return []
  11. elif shared.cmd_opts.disable_extra_extensions or 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. dirpath = os.path.join(self.path, subdir)
  75. if not os.path.isdir(dirpath):
  76. return []
  77. res = []
  78. for filename in sorted(os.listdir(dirpath)):
  79. res.append(scripts.ScriptFile(self.path, filename, os.path.join(dirpath, filename)))
  80. res = [x for x in res if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
  81. return res
  82. def check_updates(self):
  83. repo = Repo(self.path)
  84. for fetch in repo.remote().fetch(dry_run=True):
  85. if fetch.flags != fetch.HEAD_UPTODATE:
  86. self.can_update = True
  87. self.status = "new commits"
  88. return
  89. try:
  90. origin = repo.rev_parse('origin')
  91. if repo.head.commit != origin:
  92. self.can_update = True
  93. self.status = "behind HEAD"
  94. return
  95. except Exception:
  96. self.can_update = False
  97. self.status = "unknown (remote error)"
  98. return
  99. self.can_update = False
  100. self.status = "latest"
  101. def fetch_and_reset_hard(self, commit='origin'):
  102. repo = Repo(self.path)
  103. # Fix: `error: Your local changes to the following files would be overwritten by merge`,
  104. # because WSL2 Docker set 755 file permissions instead of 644, this results to the error.
  105. repo.git.fetch(all=True)
  106. repo.git.reset(commit, hard=True)
  107. self.have_info_from_repo = False
  108. def list_extensions():
  109. extensions.clear()
  110. if not os.path.isdir(extensions_dir):
  111. return
  112. if shared.cmd_opts.disable_all_extensions:
  113. print("*** \"--disable-all-extensions\" arg was used, will not load any extensions ***")
  114. elif shared.opts.disable_all_extensions == "all":
  115. print("*** \"Disable all extensions\" option was set, will not load any extensions ***")
  116. elif shared.cmd_opts.disable_extra_extensions:
  117. print("*** \"--disable-extra-extensions\" arg was used, will only load built-in extensions ***")
  118. elif shared.opts.disable_all_extensions == "extra":
  119. print("*** \"Disable all extensions\" option was set, will only load built-in extensions ***")
  120. extension_paths = []
  121. for dirname in [extensions_dir, extensions_builtin_dir]:
  122. if not os.path.isdir(dirname):
  123. return
  124. for extension_dirname in sorted(os.listdir(dirname)):
  125. path = os.path.join(dirname, extension_dirname)
  126. if not os.path.isdir(path):
  127. continue
  128. extension_paths.append((extension_dirname, path, dirname == extensions_builtin_dir))
  129. for dirname, path, is_builtin in extension_paths:
  130. extension = Extension(name=dirname, path=path, enabled=dirname not in shared.opts.disabled_extensions, is_builtin=is_builtin)
  131. extensions.append(extension)