sysinfo.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import json
  2. import os
  3. import sys
  4. import traceback
  5. import platform
  6. import hashlib
  7. import pkg_resources
  8. import psutil
  9. import re
  10. import launch
  11. from modules import paths_internal, timer
  12. checksum_token = "DontStealMyGamePlz__WINNERS_DONT_USE_DRUGS__DONT_COPY_THAT_FLOPPY"
  13. environment_whitelist = {
  14. "GIT",
  15. "INDEX_URL",
  16. "WEBUI_LAUNCH_LIVE_OUTPUT",
  17. "GRADIO_ANALYTICS_ENABLED",
  18. "PYTHONPATH",
  19. "TORCH_INDEX_URL",
  20. "TORCH_COMMAND",
  21. "REQS_FILE",
  22. "XFORMERS_PACKAGE",
  23. "GFPGAN_PACKAGE",
  24. "CLIP_PACKAGE",
  25. "OPENCLIP_PACKAGE",
  26. "STABLE_DIFFUSION_REPO",
  27. "K_DIFFUSION_REPO",
  28. "CODEFORMER_REPO",
  29. "BLIP_REPO",
  30. "STABLE_DIFFUSION_COMMIT_HASH",
  31. "K_DIFFUSION_COMMIT_HASH",
  32. "CODEFORMER_COMMIT_HASH",
  33. "BLIP_COMMIT_HASH",
  34. "COMMANDLINE_ARGS",
  35. "IGNORE_CMD_ARGS_ERRORS",
  36. }
  37. def pretty_bytes(num, suffix="B"):
  38. for unit in ["", "K", "M", "G", "T", "P", "E", "Z", "Y"]:
  39. if abs(num) < 1024 or unit == 'Y':
  40. return f"{num:.0f}{unit}{suffix}"
  41. num /= 1024
  42. def get():
  43. res = get_dict()
  44. text = json.dumps(res, ensure_ascii=False, indent=4)
  45. h = hashlib.sha256(text.encode("utf8"))
  46. text = text.replace(checksum_token, h.hexdigest())
  47. return text
  48. re_checksum = re.compile(r'"Checksum": "([0-9a-fA-F]{64})"')
  49. def check(x):
  50. m = re.search(re_checksum, x)
  51. if not m:
  52. return False
  53. replaced = re.sub(re_checksum, f'"Checksum": "{checksum_token}"', x)
  54. h = hashlib.sha256(replaced.encode("utf8"))
  55. return h.hexdigest() == m.group(1)
  56. def get_dict():
  57. ram = psutil.virtual_memory()
  58. res = {
  59. "Platform": platform.platform(),
  60. "Python": platform.python_version(),
  61. "Version": launch.git_tag(),
  62. "Commit": launch.commit_hash(),
  63. "Script path": paths_internal.script_path,
  64. "Data path": paths_internal.data_path,
  65. "Extensions dir": paths_internal.extensions_dir,
  66. "Checksum": checksum_token,
  67. "Commandline": sys.argv,
  68. "Torch env info": get_torch_sysinfo(),
  69. "Exceptions": get_exceptions(),
  70. "CPU": {
  71. "model": platform.processor(),
  72. "count logical": psutil.cpu_count(logical=True),
  73. "count physical": psutil.cpu_count(logical=False),
  74. },
  75. "RAM": {
  76. x: pretty_bytes(getattr(ram, x, 0)) for x in ["total", "used", "free", "active", "inactive", "buffers", "cached", "shared"] if getattr(ram, x, 0) != 0
  77. },
  78. "Extensions": get_extensions(enabled=True),
  79. "Inactive extensions": get_extensions(enabled=False),
  80. "Environment": get_environment(),
  81. "Config": get_config(),
  82. "Startup": timer.startup_record,
  83. "Packages": sorted([f"{pkg.key}=={pkg.version}" for pkg in pkg_resources.working_set]),
  84. }
  85. return res
  86. def format_traceback(tb):
  87. return [[f"{x.filename}, line {x.lineno}, {x.name}", x.line] for x in traceback.extract_tb(tb)]
  88. def format_exception(e, tb):
  89. return {"exception": str(e), "traceback": format_traceback(tb)}
  90. def get_exceptions():
  91. try:
  92. from modules import errors
  93. return list(reversed(errors.exception_records))
  94. except Exception as e:
  95. return str(e)
  96. def get_environment():
  97. return {k: os.environ[k] for k in sorted(os.environ) if k in environment_whitelist}
  98. re_newline = re.compile(r"\r*\n")
  99. def get_torch_sysinfo():
  100. try:
  101. import torch.utils.collect_env
  102. info = torch.utils.collect_env.get_env_info()._asdict()
  103. return {k: re.split(re_newline, str(v)) if "\n" in str(v) else v for k, v in info.items()}
  104. except Exception as e:
  105. return str(e)
  106. def get_extensions(*, enabled):
  107. try:
  108. from modules import extensions
  109. def to_json(x: extensions.Extension):
  110. return {
  111. "name": x.name,
  112. "path": x.path,
  113. "version": x.version,
  114. "branch": x.branch,
  115. "remote": x.remote,
  116. }
  117. return [to_json(x) for x in extensions.extensions if not x.is_builtin and x.enabled == enabled]
  118. except Exception as e:
  119. return str(e)
  120. def get_config():
  121. try:
  122. from modules import shared
  123. return shared.opts.data
  124. except Exception as e:
  125. return str(e)