2022-10-31 08:36:45 -06:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2023-02-13 09:04:34 -07:00
|
|
|
import time
|
2022-10-31 08:36:45 -06:00
|
|
|
import git
|
|
|
|
|
|
|
|
from modules import paths, shared
|
|
|
|
|
|
|
|
extensions = []
|
2023-01-25 09:15:42 -07:00
|
|
|
extensions_dir = os.path.join(paths.data_path, "extensions")
|
2022-12-03 08:06:33 -07:00
|
|
|
extensions_builtin_dir = os.path.join(paths.script_path, "extensions-builtin")
|
2022-10-31 08:36:45 -06:00
|
|
|
|
2023-01-25 10:18:55 -07:00
|
|
|
if not os.path.exists(extensions_dir):
|
|
|
|
os.makedirs(extensions_dir)
|
2022-10-31 08:36:45 -06:00
|
|
|
|
|
|
|
def active():
|
|
|
|
return [x for x in extensions if x.enabled]
|
|
|
|
|
|
|
|
|
|
|
|
class Extension:
|
2022-12-03 08:06:33 -07:00
|
|
|
def __init__(self, name, path, enabled=True, is_builtin=False):
|
2022-10-31 08:36:45 -06:00
|
|
|
self.name = name
|
|
|
|
self.path = path
|
|
|
|
self.enabled = enabled
|
|
|
|
self.status = ''
|
|
|
|
self.can_update = False
|
2022-12-03 08:06:33 -07:00
|
|
|
self.is_builtin = is_builtin
|
2023-02-13 09:04:34 -07:00
|
|
|
self.version = ''
|
2022-10-31 08:36:45 -06:00
|
|
|
|
|
|
|
repo = None
|
|
|
|
try:
|
|
|
|
if os.path.exists(os.path.join(path, ".git")):
|
|
|
|
repo = git.Repo(path)
|
|
|
|
except Exception:
|
|
|
|
print(f"Error reading github repository info from {path}:", file=sys.stderr)
|
|
|
|
print(traceback.format_exc(), file=sys.stderr)
|
|
|
|
|
|
|
|
if repo is None or repo.bare:
|
|
|
|
self.remote = None
|
|
|
|
else:
|
2022-11-05 06:04:48 -06:00
|
|
|
try:
|
|
|
|
self.remote = next(repo.remote().urls, None)
|
|
|
|
self.status = 'unknown'
|
2023-02-13 09:04:34 -07:00
|
|
|
head = repo.head.commit
|
|
|
|
ts = time.asctime(time.gmtime(repo.head.commit.committed_date))
|
2023-02-18 23:54:04 -07:00
|
|
|
self.version = f'{head.hexsha[:8]} ({ts})'
|
2023-02-13 09:04:34 -07:00
|
|
|
|
2022-11-05 06:04:48 -06:00
|
|
|
except Exception:
|
|
|
|
self.remote = None
|
2022-10-31 08:36:45 -06:00
|
|
|
|
|
|
|
def list_files(self, subdir, extension):
|
|
|
|
from modules import scripts
|
|
|
|
|
|
|
|
dirpath = os.path.join(self.path, subdir)
|
|
|
|
if not os.path.isdir(dirpath):
|
|
|
|
return []
|
|
|
|
|
|
|
|
res = []
|
|
|
|
for filename in sorted(os.listdir(dirpath)):
|
2022-10-31 09:40:47 -06:00
|
|
|
res.append(scripts.ScriptFile(self.path, filename, os.path.join(dirpath, filename)))
|
2022-10-31 08:36:45 -06:00
|
|
|
|
|
|
|
res = [x for x in res if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def check_updates(self):
|
|
|
|
repo = git.Repo(self.path)
|
|
|
|
for fetch in repo.remote().fetch("--dry-run"):
|
|
|
|
if fetch.flags != fetch.HEAD_UPTODATE:
|
|
|
|
self.can_update = True
|
|
|
|
self.status = "behind"
|
|
|
|
return
|
|
|
|
|
|
|
|
self.can_update = False
|
|
|
|
self.status = "latest"
|
|
|
|
|
2022-11-12 11:44:42 -07:00
|
|
|
def fetch_and_reset_hard(self):
|
2022-10-31 08:36:45 -06:00
|
|
|
repo = git.Repo(self.path)
|
2022-11-12 11:44:42 -07:00
|
|
|
# Fix: `error: Your local changes to the following files would be overwritten by merge`,
|
|
|
|
# because WSL2 Docker set 755 file permissions instead of 644, this results to the error.
|
|
|
|
repo.git.fetch('--all')
|
|
|
|
repo.git.reset('--hard', 'origin')
|
2022-10-31 08:36:45 -06:00
|
|
|
|
|
|
|
|
|
|
|
def list_extensions():
|
|
|
|
extensions.clear()
|
|
|
|
|
|
|
|
if not os.path.isdir(extensions_dir):
|
|
|
|
return
|
|
|
|
|
2022-12-03 08:06:33 -07:00
|
|
|
paths = []
|
|
|
|
for dirname in [extensions_dir, extensions_builtin_dir]:
|
|
|
|
if not os.path.isdir(dirname):
|
|
|
|
return
|
2022-10-31 08:36:45 -06:00
|
|
|
|
2022-12-03 08:06:33 -07:00
|
|
|
for extension_dirname in sorted(os.listdir(dirname)):
|
|
|
|
path = os.path.join(dirname, extension_dirname)
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
continue
|
|
|
|
|
|
|
|
paths.append((extension_dirname, path, dirname == extensions_builtin_dir))
|
|
|
|
|
|
|
|
for dirname, path, is_builtin in paths:
|
|
|
|
extension = Extension(name=dirname, path=path, enabled=dirname not in shared.opts.disabled_extensions, is_builtin=is_builtin)
|
2022-10-31 08:36:45 -06:00
|
|
|
extensions.append(extension)
|
2022-11-08 09:03:56 -07:00
|
|
|
|