ComfyUI-Manager/glob/node_package.py
2024-12-20 15:02:58 -08:00

80 lines
2.2 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
import os
import toml
from git_utils import is_git_repo, get_commit_hash
@dataclass
class InstalledNodePackage:
"""Information about an installed node package."""
id: str
fullpath: str
disabled: bool
version: str
@property
def is_unknown(self) -> bool:
return self.version == "unknown"
@property
def is_nightly(self) -> bool:
return self.version == "nightly"
@property
def is_from_cnr(self) -> bool:
return not self.is_unknown and not self.is_nightly
@property
def is_enabled(self) -> bool:
return not self.disabled
@property
def is_disabled(self) -> bool:
return self.disabled
def get_commit_hash(self) -> str:
return get_commit_hash(self.fullpath)
def isValid(self) -> bool:
if self.is_from_cnr:
return os.path.exists(os.path.join(self.fullpath, '.tracking'))
return True
@staticmethod
def from_fullpath(fullpath: str) -> InstalledNodePackage:
parent_folder_name = os.path.split(fullpath)[-2]
module_name = os.path.basename(fullpath)
pyproject_toml_path = os.path.join(fullpath, "pyproject.toml")
if module_name.endswith(".disabled"):
node_id = module_name[:-9]
disabled = True
elif parent_folder_name == ".disabled":
# Nodes under custom_nodes/.disabled/* are disabled
node_id = module_name
disabled = True
else:
node_id = module_name
disabled = False
if is_git_repo(fullpath):
version = "nightly"
elif os.path.exists(pyproject_toml_path):
# Read project.toml to get the version
with open(pyproject_toml_path, "r", encoding="utf-8") as f:
pyproject_toml = toml.load(f)
# Fallback to 'unknown' if project.version doesn't exist
version = pyproject_toml.get("project", {}).get("version", "unknown")
else:
version = "unknown"
return InstalledNodePackage(
id=node_id, fullpath=fullpath, disabled=disabled, version=version
)