From 7d7f7f4b49cc4265b48ee08fe13b7e7b03cecf98 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 8 Jul 2024 15:45:45 +0900 Subject: [PATCH 01/10] sysinfo handle psutil not working --- modules/sysinfo.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/modules/sysinfo.py b/modules/sysinfo.py index f336251e4..614334661 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -5,7 +5,6 @@ import sys import platform import hashlib import pkg_resources -import psutil import re import launch @@ -69,9 +68,27 @@ def check(x): return h.hexdigest() == m.group(1) -def get_dict(): - ram = psutil.virtual_memory() +def get_cpu_info(): + cpu_info = {"model": platform.processor()} + try: + import psutil + cpu_info["count logical"] = psutil.cpu_count(logical=True) + cpu_info["count physical"] = psutil.cpu_count(logical=False) + except Exception as e: + cpu_info["error"] = str(e) + return cpu_info + +def get_ram_info(): + try: + import psutil + ram = psutil.virtual_memory() + return {x: pretty_bytes(getattr(ram, x, 0)) for x in ["total", "used", "free", "active", "inactive", "buffers", "cached", "shared"] if getattr(ram, x, 0) != 0} + except Exception as e: + return str(e) + + +def get_dict(): res = { "Platform": platform.platform(), "Python": platform.python_version(), @@ -84,14 +101,8 @@ def get_dict(): "Commandline": get_argv(), "Torch env info": get_torch_sysinfo(), "Exceptions": errors.get_exceptions(), - "CPU": { - "model": platform.processor(), - "count logical": psutil.cpu_count(logical=True), - "count physical": psutil.cpu_count(logical=False), - }, - "RAM": { - x: pretty_bytes(getattr(ram, x, 0)) for x in ["total", "used", "free", "active", "inactive", "buffers", "cached", "shared"] if getattr(ram, x, 0) != 0 - }, + "CPU": get_cpu_info(), + "RAM": get_ram_info(), "Extensions": get_extensions(enabled=True), "Inactive extensions": get_extensions(enabled=False), "Environment": get_environment(), @@ -123,6 +134,7 @@ def get_argv(): return res + re_newline = re.compile(r"\r*\n") From 11f827c58b276dff946dccf4167d8e11159eeba5 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 8 Jul 2024 16:33:02 +0900 Subject: [PATCH 02/10] use pip freeze --all to get packages --- modules/sysinfo.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/sysinfo.py b/modules/sysinfo.py index 614334661..65d4e3c98 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -4,7 +4,6 @@ import sys import platform import hashlib -import pkg_resources import re import launch @@ -88,6 +87,19 @@ def get_ram_info(): return str(e) +def get_packages(): + try: + import subprocess + return subprocess.check_output([sys.executable, '-m', 'pip', 'freeze', '--all']).decode("utf8").splitlines() + except Exception as pip_error: + try: + import importlib.metadata + packages = importlib.metadata.distributions() + return sorted([f"{package.metadata['Name']}=={package.version}" for package in packages]) + except Exception as e2: + return {'error pip': pip_error, 'error importlib': str(e2)} + + def get_dict(): res = { "Platform": platform.platform(), @@ -108,7 +120,7 @@ def get_dict(): "Environment": get_environment(), "Config": get_config(), "Startup": timer.startup_record, - "Packages": sorted([f"{pkg.key}=={pkg.version}" for pkg in pkg_resources.working_set]), + "Packages": get_packages(), } return res From 27947a79d619eac5ce40b3f2db62d422313d12f6 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 8 Jul 2024 16:56:06 +0900 Subject: [PATCH 03/10] git status --- modules/launch_utils.py | 8 ++++++++ modules/sysinfo.py | 11 +++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/modules/launch_utils.py b/modules/launch_utils.py index e22da4ec6..0688f4826 100644 --- a/modules/launch_utils.py +++ b/modules/launch_utils.py @@ -85,6 +85,14 @@ def git_tag(): return "" +@lru_cache() +def git_status(): + try: + return subprocess.check_output([git, "-C", script_path, "status"], shell=False, encoding='utf8').strip() + except Exception as e: + return str(e) + + def run(command, desc=None, errdesc=None, custom_env=None, live: bool = default_command_live) -> str: if desc is not None: print(desc) diff --git a/modules/sysinfo.py b/modules/sysinfo.py index 65d4e3c98..52617573b 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -1,13 +1,12 @@ import json import os import sys - +import subprocess import platform import hashlib import re -import launch -from modules import paths_internal, timer, shared, extensions, errors +from modules import paths_internal, timer, shared, extensions, errors, launch_utils checksum_token = "DontStealMyGamePlz__WINNERS_DONT_USE_DRUGS__DONT_COPY_THAT_FLOPPY" environment_whitelist = { @@ -89,7 +88,6 @@ def get_ram_info(): def get_packages(): try: - import subprocess return subprocess.check_output([sys.executable, '-m', 'pip', 'freeze', '--all']).decode("utf8").splitlines() except Exception as pip_error: try: @@ -104,8 +102,9 @@ def get_dict(): res = { "Platform": platform.platform(), "Python": platform.python_version(), - "Version": launch.git_tag(), - "Commit": launch.commit_hash(), + "Version": launch_utils.git_tag(), + "Commit": launch_utils.commit_hash(), + "Git status": launch_utils.git_status(), "Script path": paths_internal.script_path, "Data path": paths_internal.data_path, "Extensions dir": paths_internal.extensions_dir, From dd4f798b97de3e32d0a6a1a18816b4cc28c6008e Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 8 Jul 2024 19:20:49 +0900 Subject: [PATCH 04/10] fallback get_config() --- modules/sysinfo.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/sysinfo.py b/modules/sysinfo.py index 52617573b..2c08dd226 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -179,5 +179,9 @@ def get_extensions(*, enabled): def get_config(): try: return shared.opts.data - except Exception as e: - return str(e) + except Exception as _: + try: + with open(shared.cmd_opts.ui_settings_file, 'r') as f: + return json.load(f) + except Exception as e: + return str(e) From 27d96fa608da81b334163835fe39c1bb32984a7c Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 8 Jul 2024 20:31:50 +0900 Subject: [PATCH 05/10] fallback Extensions info --- modules/sysinfo.py | 56 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/modules/sysinfo.py b/modules/sysinfo.py index 2c08dd226..c2bb35cdf 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -99,6 +99,7 @@ def get_packages(): def get_dict(): + config = get_config() res = { "Platform": platform.platform(), "Python": platform.python_version(), @@ -114,10 +115,10 @@ def get_dict(): "Exceptions": errors.get_exceptions(), "CPU": get_cpu_info(), "RAM": get_ram_info(), - "Extensions": get_extensions(enabled=True), - "Inactive extensions": get_extensions(enabled=False), + "Extensions": get_extensions(enabled=True, fallback_disabled_extensions=config.get('disabled_extensions', [])), + "Inactive extensions": get_extensions(enabled=False, fallback_disabled_extensions=config.get('disabled_extensions', [])), "Environment": get_environment(), - "Config": get_config(), + "Config": config, "Startup": timer.startup_record, "Packages": get_packages(), } @@ -159,19 +160,46 @@ def get_torch_sysinfo(): return str(e) -def get_extensions(*, enabled): - +def run_git(path, *args): try: - def to_json(x: extensions.Extension): - return { - "name": x.name, - "path": x.path, - "version": x.version, - "branch": x.branch, - "remote": x.remote, - } + if os.path.isdir(os.path.join(path, '.git')): + return subprocess.check_output([launch_utils.git, '-C', path, *args], shell=False, encoding='utf8').strip() + return None + except Exception as e: + return str(e) - return [to_json(x) for x in extensions.extensions if not x.is_builtin and x.enabled == enabled] + +def get_info_from_repo_path(path): + return { + 'name': os.path.basename(path), + 'path': path, + 'version': run_git(path, 'rev-parse', 'HEAD'), + 'branch': run_git(path, 'branch', '--show-current'), + 'remote': run_git(path, 'remote', 'get-url', 'origin') + } + + +def get_extensions(*, enabled, fallback_disabled_extensions=None): + try: + if extensions.extensions: + def to_json(x: extensions.Extension): + return { + "name": x.name, + "path": x.path, + "version": x.version, + "branch": x.branch, + "remote": x.remote, + } + return [to_json(x) for x in extensions.extensions if not x.is_builtin and x.enabled == enabled] + else: + extensions_list = [] + for extension_dirname in sorted(os.listdir(paths_internal.extensions_dir)): + path = os.path.join(paths_internal.extensions_dir, extension_dirname) + if enabled == (extension_dirname in fallback_disabled_extensions): + continue + if os.path.isdir(path): + extensions_list.append(get_info_from_repo_path(path)) + return extensions_list except Exception as e: return str(e) From 3f6dcda3e50594a6581bee68f482901cd9ba5d5b Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 8 Jul 2024 20:33:15 +0900 Subject: [PATCH 06/10] Extensions info full commit hash --- modules/sysinfo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/sysinfo.py b/modules/sysinfo.py index c2bb35cdf..13427af63 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -173,7 +173,7 @@ def get_info_from_repo_path(path): return { 'name': os.path.basename(path), 'path': path, - 'version': run_git(path, 'rev-parse', 'HEAD'), + 'commit': run_git(path, 'rev-parse', 'HEAD'), 'branch': run_git(path, 'branch', '--show-current'), 'remote': run_git(path, 'remote', 'get-url', 'origin') } @@ -186,7 +186,7 @@ def get_extensions(*, enabled, fallback_disabled_extensions=None): return { "name": x.name, "path": x.path, - "version": x.version, + "commit": x.commit_hash, "branch": x.branch, "remote": x.remote, } From 4debd4d3ef62449787d6d02943f82ead356bfe48 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Tue, 9 Jul 2024 01:02:11 +0900 Subject: [PATCH 07/10] compact get_info_from_repo_path --- modules/sysinfo.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/modules/sysinfo.py b/modules/sysinfo.py index 13427af63..aa4328ed1 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -5,6 +5,7 @@ import subprocess import platform import hashlib import re +from pathlib import Path from modules import paths_internal, timer, shared, extensions, errors, launch_utils @@ -162,20 +163,19 @@ def get_torch_sysinfo(): def run_git(path, *args): try: - if os.path.isdir(os.path.join(path, '.git')): - return subprocess.check_output([launch_utils.git, '-C', path, *args], shell=False, encoding='utf8').strip() - return None + return subprocess.check_output([launch_utils.git, '-C', path, *args], shell=False, encoding='utf8').strip() except Exception as e: return str(e) -def get_info_from_repo_path(path): +def get_info_from_repo_path(path: Path): + is_repo = (path / '.git').is_dir() return { - 'name': os.path.basename(path), - 'path': path, - 'commit': run_git(path, 'rev-parse', 'HEAD'), - 'branch': run_git(path, 'branch', '--show-current'), - 'remote': run_git(path, 'remote', 'get-url', 'origin') + 'name': path.name, + 'path': str(path), + 'commit': run_git(path, 'rev-parse', 'HEAD') if is_repo else None, + 'branch': run_git(path, 'branch', '--show-current') if is_repo else None, + 'remote': run_git(path, 'remote', 'get-url', 'origin') if is_repo else None, } @@ -192,14 +192,7 @@ def get_extensions(*, enabled, fallback_disabled_extensions=None): } return [to_json(x) for x in extensions.extensions if not x.is_builtin and x.enabled == enabled] else: - extensions_list = [] - for extension_dirname in sorted(os.listdir(paths_internal.extensions_dir)): - path = os.path.join(paths_internal.extensions_dir, extension_dirname) - if enabled == (extension_dirname in fallback_disabled_extensions): - continue - if os.path.isdir(path): - extensions_list.append(get_info_from_repo_path(path)) - return extensions_list + return [get_info_from_repo_path(d) for d in Path(paths_internal.extensions_dir).iterdir() if d.is_dir() and enabled != (str(d.name) in fallback_disabled_extensions)] except Exception as e: return str(e) From 72cfa2829d9595b6c92d554035aec6e5d92a6602 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Tue, 9 Jul 2024 01:30:55 +0900 Subject: [PATCH 08/10] safer Imports --- modules/sysinfo.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/modules/sysinfo.py b/modules/sysinfo.py index aa4328ed1..2faa50757 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -7,7 +7,7 @@ import hashlib import re from pathlib import Path -from modules import paths_internal, timer, shared, extensions, errors, launch_utils +from modules import paths_internal, timer, shared_cmd_options, errors, launch_utils checksum_token = "DontStealMyGamePlz__WINNERS_DONT_USE_DRUGS__DONT_COPY_THAT_FLOPPY" environment_whitelist = { @@ -135,11 +135,11 @@ def get_argv(): res = [] for v in sys.argv: - if shared.cmd_opts.gradio_auth and shared.cmd_opts.gradio_auth == v: + if shared_cmd_options.cmd_opts.gradio_auth and shared_cmd_options.cmd_opts.gradio_auth == v: res.append("") continue - if shared.cmd_opts.api_auth and shared.cmd_opts.api_auth == v: + if shared_cmd_options.cmd_opts.api_auth and shared_cmd_options.cmd_opts.api_auth == v: res.append("") continue @@ -181,6 +181,7 @@ def get_info_from_repo_path(path: Path): def get_extensions(*, enabled, fallback_disabled_extensions=None): try: + from modules import extensions if extensions.extensions: def to_json(x: extensions.Extension): return { @@ -199,10 +200,11 @@ def get_extensions(*, enabled, fallback_disabled_extensions=None): def get_config(): try: + from modules import shared return shared.opts.data except Exception as _: try: - with open(shared.cmd_opts.ui_settings_file, 'r') as f: + with open(shared_cmd_options.cmd_opts.ui_settings_file, 'r') as f: return json.load(f) except Exception as e: return str(e) From 6a7042fe2fe2974b61e7f6271bd8dad3fedd9dd1 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Tue, 9 Jul 2024 01:51:47 +0900 Subject: [PATCH 09/10] move git_status to sysinfo --- modules/launch_utils.py | 9 --------- modules/sysinfo.py | 7 ++++++- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/modules/launch_utils.py b/modules/launch_utils.py index 0688f4826..b2cc71277 100644 --- a/modules/launch_utils.py +++ b/modules/launch_utils.py @@ -85,14 +85,6 @@ def git_tag(): return "" -@lru_cache() -def git_status(): - try: - return subprocess.check_output([git, "-C", script_path, "status"], shell=False, encoding='utf8').strip() - except Exception as e: - return str(e) - - def run(command, desc=None, errdesc=None, custom_env=None, live: bool = default_command_live) -> str: if desc is not None: print(desc) @@ -453,7 +445,6 @@ def prepare_environment(): exit(0) - def configure_for_tests(): if "--api" not in sys.argv: sys.argv.append("--api") diff --git a/modules/sysinfo.py b/modules/sysinfo.py index 2faa50757..e9a83d74e 100644 --- a/modules/sysinfo.py +++ b/modules/sysinfo.py @@ -106,7 +106,7 @@ def get_dict(): "Python": platform.python_version(), "Version": launch_utils.git_tag(), "Commit": launch_utils.commit_hash(), - "Git status": launch_utils.git_status(), + "Git status": git_status(paths_internal.script_path), "Script path": paths_internal.script_path, "Data path": paths_internal.data_path, "Extensions dir": paths_internal.extensions_dir, @@ -168,6 +168,11 @@ def run_git(path, *args): return str(e) +def git_status(path): + if (Path(path) / '.git').is_dir(): + return run_git(paths_internal.script_path, 'status') + + def get_info_from_repo_path(path: Path): is_repo = (path / '.git').is_dir() return { From 5a5fe7494ad6e4ace6b31bcb00f1e606c5755909 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 8 Jul 2024 16:32:20 +0900 Subject: [PATCH 10/10] .gitignore sysinfo.json --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 96cfe22db..40f659d3c 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ notification.mp3 /test/test_outputs /cache trace.json +/sysinfo-????-??-??-??-??.json