-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathsetup.py
More file actions
144 lines (120 loc) · 4.69 KB
/
Copy pathsetup.py
File metadata and controls
144 lines (120 loc) · 4.69 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# ---------------------------------------------------------------------
# Copyright (c) 2025 Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
# ---------------------------------------------------------------------
import functools
import os
import pathlib
import re
from typing import Literal
from setuptools import find_packages, setup
from setuptools_scm import get_version
PACKAGE_ROOT = (pathlib.Path(__file__).parent / "qai_hub_models").absolute()
MODELS_ROOT = PACKAGE_ROOT / "models"
REQS_FILENAME = "requirements.txt"
# Packages under qai_hub_models that are excluded from release builds
RELEASE_EXCLUDED_PACKAGES = ["scripts"]
# Dirs under models/ that are not model packages but should be kept.
RELEASE_WHITELISTED_MODELS_PACKAGES = ["_shared", "utils"]
# When QAIHM_RELEASE_BUILD=1, exclude unpublished models and development files.
IS_RELEASE_BUILD = os.environ.get("QAIHM_RELEASE_BUILD", "0").lower() in [
"1",
"true",
"yes",
]
def _get_model_status(
model_dir: pathlib.Path,
) -> Literal["published", "unpublished", "pending"] | None:
manifest_yaml = model_dir / "manifest.yaml"
if not manifest_yaml.exists():
return None
with open(manifest_yaml) as f:
for line in f:
if m := re.match(r"^status:\s*(\S+)", line):
out = m.group(1)
assert out in {"published", "unpublished", "pending"}, (
f"Unknown model status: {out}"
)
return out # type: ignore[return-type]
return None
@functools.cache
def _get_unpublished_models() -> list[str]:
"""Return package patterns for unpublished models and non-model dirs."""
return (
[
f"{model_dir.name}"
for model_dir in MODELS_ROOT.iterdir()
if model_dir.is_dir()
and model_dir.name not in RELEASE_WHITELISTED_MODELS_PACKAGES
and _get_model_status(model_dir) != "published"
]
if IS_RELEASE_BUILD
else []
)
def _load_requirements(path: str | os.PathLike) -> list[str]:
"""
Read requirements from the given path, return a list of pip-parseable requirements.
Ignore / remove comments.
"""
with open(path) as file:
return [
line.split("#")[0].strip()
for line in file
if line.strip()
and not line.startswith("#")
and not line.lstrip().startswith(
"-"
) # pip options (e.g. --extra-index-url) are not valid PEP 508 specifiers
]
def _get_extras() -> dict[str, list[str]]:
"""Generate the valid extras for this version of AI Hub Models."""
with open(PACKAGE_ROOT / "requirements-dev.txt") as reqf:
extras_require = {"dev": [line.split("#")[0].strip() for line in reqf]}
# Create extra for every model that requires one.
for model_dir in MODELS_ROOT.iterdir():
if (
not model_dir.is_file()
and (model_dir / REQS_FILENAME).exists()
and model_dir.name not in _get_unpublished_models()
):
extra_with_dash = model_dir.name.replace("_", "-")
reqs = _load_requirements(model_dir / REQS_FILENAME)
extras_require[model_dir.name] = reqs
extras_require[extra_with_dash] = reqs
return extras_require
def _get_excluded_package_data() -> dict[str, list[str]]:
if not IS_RELEASE_BUILD:
return {}
# Exclude data files from unpublished models. Setuptools treats files in
# excluded sub-packages as data files of ancestor packages, so patterns
# must be added at every level that has package_data globs.
return {
"qai_hub_models": [
*[f"models/{model}/**" for model in _get_unpublished_models()],
*[f"{package}/**" for package in RELEASE_EXCLUDED_PACKAGES],
"models/**/release-assets.yaml",
],
}
def _get_excluded_packages() -> list[str]:
excluded = ["qai_hub_models.*.external_repos.*"]
if IS_RELEASE_BUILD:
excluded += [
f"qai_hub_models.{package}*" for package in RELEASE_EXCLUDED_PACKAGES
]
excluded += [
f"qai_hub_models.models.{model}*" for model in _get_unpublished_models()
]
return excluded
def _get_install_requires() -> list[str]:
version = get_version(root="..")
reqs = _load_requirements(PACKAGE_ROOT / REQS_FILENAME)
reqs.append(f"qai_hub_models_cli=={version}")
return reqs
setup(
packages=find_packages(
include=["qai_hub_models*"], exclude=_get_excluded_packages()
),
install_requires=_get_install_requires(),
extras_require=_get_extras(),
exclude_package_data=_get_excluded_package_data(),
)