File size: 2,837 Bytes
0bd62e5 |
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 |
import argparse
import importlib
import inspect
import os
from pathlib import Path
import sys
from tomlkit import dumps, parse
from gradio.blocks import BlockContext
from gradio.components import Component
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Description of your program")
parser.add_argument("-m", "--mode", help="Build mode or dev mode")
args = parser.parse_args()
try:
with open("../pyproject.toml") as f:
pyproject_source = f.read()
pyproject_toml = parse(pyproject_source)
keywords = pyproject_toml["project"]["keywords"]
custom_component = ("gradio-custom-component" in keywords or
"gradio custom component" in keywords)
if not custom_component:
sys.exit(0)
module_name = pyproject_toml["project"]["name"]
module = importlib.import_module(module_name)
artifacts: list[str] = pyproject_toml["tool"]["hatch"]["build"]["artifacts"]
def get_relative_path(path):
return (
os.path.abspath(Path(__file__).parent / path)
.replace(os.path.abspath(os.getcwd()), "")
.lstrip("/")
)
for name in dir(module):
value = getattr(module, name)
if name.startswith("__"):
continue
if inspect.isclass(value) and (
issubclass(value, BlockContext) or issubclass(value, Component)
):
file_location = Path(inspect.getfile(value)).parent
found = [
x
for x in artifacts
if get_relative_path(Path("..") / x)
== get_relative_path(file_location / value.TEMPLATE_DIR)
]
if len(found) == 0:
artifact = "/" + (os.path.abspath(file_location / value.TEMPLATE_DIR)
.replace(os.path.abspath(Path("..")), "")
.lstrip("/"))
if artifact not in artifacts:
artifacts.append(artifact)
print(
f"{name}~|~|~|~{os.path.abspath(file_location / value.TEMPLATE_DIR)}~|~|~|~{os.path.abspath(file_location / value.FRONTEND_DIR)}~|~|~|~{value.get_component_class_id()}"
)
continue
if args.mode == "build":
pyproject_toml["tool"]["hatch"]["build"]["artifacts"] = artifacts
with open("../pyproject.toml", "w") as f:
f.write(dumps(pyproject_toml))
except Exception as e:
_, _, exc_tb = sys.exc_info()
print(f"|EXCEPTION|:{str(e)}, examine.py line {exc_tb.tb_lineno}") |