Browse Source

resolve_requirements.py - consider extras

ex)
`python resolve_requirements.py inference-gpu[yolo-world]`
feat/pkg_tree
Dr.Lt.Data 7 months ago
parent
commit
f6640453a9
  1. 46
      tools/resolve_requirements.py

46
tools/resolve_requirements.py

@ -6,12 +6,13 @@ from packaging import version, specifiers
import re import re
def parse_pip_spec(spec: str) -> Tuple[str, Optional[str]]: def parse_pip_spec(spec: str) -> Tuple[str, Optional[str], Optional[str]]:
match = re.match(r'([^=<>!~]+)(.*)', spec) match = re.match(r'([^=<>!~\[\]]+)(\[.*\])?(.*)', spec)
if match: if match:
package_name = match.group(1).strip() package_name = match.group(1).strip()
version_spec = match.group(2).strip() or None extras = match.group(2).strip()[1:-1] if match.group(2) else None
return package_name, version_spec version_spec = match.group(3).strip() or None
return package_name, extras, version_spec
else: else:
raise ValueError(f"Invalid package spec: {spec}") raise ValueError(f"Invalid package spec: {spec}")
@ -30,8 +31,8 @@ def fetch_package_versions(package_name):
return [] return []
def fetch_required_dist(package_name, version): def fetch_required_dist(package_name, ver, extras):
url = f"https://pypi.org/pypi/{package_name}/{version}/json" url = f"https://pypi.org/pypi/{package_name}/{ver}/json"
try: try:
response = requests.get(url) response = requests.get(url)
response.raise_for_status() response.raise_for_status()
@ -40,19 +41,34 @@ def fetch_required_dist(package_name, version):
requires_dist = data.get('info', {}).get('requires_dist', []) requires_dist = data.get('info', {}).get('requires_dist', [])
if not requires_dist: if not requires_dist:
print(f"No dependencies found for {package_name} version {version}.") print(f"No dependencies found for {package_name} version {ver}.")
return [] return []
dependencies = [] dependencies = []
for dist in requires_dist: for dist in requires_dist:
dist = dist.split(";")[0].strip() dist = dist.split(";")
match = re.match(r'([^=<>!~]+)(.*)', dist) match = re.match(r'([^=<>!~]+)(.*)', dist[0].strip())
pkg_name = None
ver_spec = None
dist_extra = None
if match: if match:
pkg_name = match.group(1).strip() pkg_name = match.group(1).strip()
version_spec = match.group(2).strip() or None ver_spec = match.group(2).strip() or None
dependencies.append((pkg_name, version_spec))
if len(dist) > 1:
print(f"Processed dependencies for {package_name} version {version}: {dependencies}") match = re.match(r'extra\s*==\s*"([^"]*)"', dist[1].strip())
if match:
dist_extra = match.group(1).strip()
if pkg_name is not None:
if dist_extra is None:
dependencies.append((pkg_name, ver_spec))
elif dist_extra == extras:
dependencies.append((pkg_name, ver_spec))
# print(f"Processed dependencies for {package_name} version {ver}: {dependencies}")
return dependencies return dependencies
except requests.RequestException as e: except requests.RequestException as e:
print(f"Error fetching required distribution info: {e}") print(f"Error fetching required distribution info: {e}")
@ -69,7 +85,7 @@ if __name__ == "__main__":
print("Usage: python script.py <package_name>") print("Usage: python script.py <package_name>")
sys.exit(1) sys.exit(1)
pkg_name, version_spec = parse_pip_spec(sys.argv[1]) pkg_name, extras, version_spec = parse_pip_spec(sys.argv[1])
candidate_versions = fetch_package_versions(pkg_name) candidate_versions = fetch_package_versions(pkg_name)
if version_spec: if version_spec:
@ -78,7 +94,7 @@ if __name__ == "__main__":
if candidate_versions: if candidate_versions:
best_version = max(candidate_versions, key=version.parse) best_version = max(candidate_versions, key=version.parse)
print(f"Best matching version: {best_version}") print(f"Best matching version: {best_version}")
required_dist = fetch_required_dist(pkg_name, best_version) required_dist = fetch_required_dist(pkg_name, best_version, extras)
print(f"Required_dist: {required_dist}") print(f"Required_dist: {required_dist}")
else: else:
print("No matching versions found.") print("No matching versions found.")

Loading…
Cancel
Save