utils.py 1.97 KB
Newer Older
wuxk1's avatar
wuxk1 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
This module provides helper functions for ComfyUI-nunchaku.
"""

from importlib.metadata import PackageNotFoundError, distribution, metadata
from pathlib import Path

try:
    import tomllib
except ImportError:
    import tomli as tomllib


def get_package_metadata(package_name: str) -> str:
    """
    Retrieve metadata for a given installed package.

    Parameters
    ----------
    package_name : str
        The name of the package to query.

    Returns
    -------
    str
        Formatted metadata string, or an error message if not found.
    """
    try:
        meta = metadata(package_name)
        meta_dict = dict(meta)

        dist = distribution(package_name)
        location = dist.locate_file("").as_posix()  # 转为字符串路径

        lines = [f"{k}: {v}" for k, v in meta_dict.items()]
        lines.append(f"Location: {location}")
        return "\n".join(lines)

    except PackageNotFoundError:
        return f"Package '{package_name}' not found."


def get_package_version(package_name: str) -> str:
    """
    Retrieve the version string for a given installed package.

    Parameters
    ----------
    package_name : str
        The name of the package to query.

    Returns
    -------
    str
        The version string, or an error message if not found.
    """
    try:
        meta = metadata(package_name)
        meta_dict = dict(meta)

        version = meta_dict.get("Version", "Unknown version")
        return version

    except PackageNotFoundError:
        return f"Package '{package_name}' not found."


def get_plugin_version() -> str:
    """
    Retrieve the version of the current plugin from pyproject.toml.

    Returns
    -------
    str
        The plugin version string.
    """
    cur_path = Path(__file__)
    toml_path = cur_path.parent / "pyproject.toml"
    with open(toml_path, "rb") as f:
        data = tomllib.load(f)
        project_version = data["project"]["version"]
        return project_version


supported_versions = ["v1.0.0"]