2020-04-24 23:36:52 +00:00
|
|
|
#! /usr/bin/env nix-shell
|
2023-03-27 19:17:25 +00:00
|
|
|
#! nix-shell -i python3 -p "python3.withPackages (ps: with ps; [ packaging rich ])" -p nodePackages.pyright ruff isort"
|
2020-04-24 23:36:52 +00:00
|
|
|
#
|
|
|
|
# This script downloads Home Assistant's source tarball.
|
|
|
|
# Inside the homeassistant/components directory, each integration has an associated manifest.json,
|
|
|
|
# specifying required packages and other integrations it depends on:
|
|
|
|
#
|
|
|
|
# {
|
|
|
|
# "requirements": [ "package==1.2.3" ],
|
|
|
|
# "dependencies": [ "component" ]
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# By parsing the files, a dictionary mapping integrations to requirements and dependencies is created.
|
|
|
|
# For all of these requirements and the dependencies' requirements,
|
|
|
|
# nixpkgs' python3Packages are searched for appropriate names.
|
|
|
|
# Then, a Nix attribute set mapping integration name to dependencies is created.
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tarfile
|
|
|
|
import tempfile
|
2021-06-28 23:13:55 +00:00
|
|
|
from functools import reduce
|
2020-04-24 23:36:52 +00:00
|
|
|
from io import BytesIO
|
2023-03-15 16:39:30 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Set
|
2020-04-24 23:36:52 +00:00
|
|
|
from urllib.request import urlopen
|
2023-03-15 16:39:30 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
from packaging import version as Version
|
2023-04-29 16:46:19 +00:00
|
|
|
from packaging.version import InvalidVersion
|
2021-02-05 17:12:51 +00:00
|
|
|
from rich.console import Console
|
|
|
|
from rich.table import Table
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
COMPONENT_PREFIX = "homeassistant.components"
|
2021-06-28 23:13:55 +00:00
|
|
|
PKG_SET = "home-assistant.python.pkgs"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-03-05 16:20:37 +00:00
|
|
|
# If some requirements are matched by multiple or no Python packages, the
|
|
|
|
# following can be used to choose the correct one
|
2020-04-24 23:36:52 +00:00
|
|
|
PKG_PREFERENCES = {
|
2022-10-21 18:38:19 +00:00
|
|
|
"fiblary3": "fiblary3-fork", # https://github.com/home-assistant/core/issues/66466
|
2023-01-11 07:51:40 +00:00
|
|
|
"HAP-python": "hap-python",
|
2022-03-05 16:20:37 +00:00
|
|
|
"tensorflow": "tensorflow",
|
2023-07-15 17:15:38 +00:00
|
|
|
"yt-dlp": "yt-dlp",
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
# Some dependencies are loaded dynamically at runtime, and are not
|
|
|
|
# mentioned in the manifest files.
|
|
|
|
EXTRA_COMPONENT_DEPS = {
|
|
|
|
"conversation": [
|
|
|
|
"intent"
|
|
|
|
],
|
|
|
|
"default_config": [
|
|
|
|
"backup",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-03-15 16:39:30 +00:00
|
|
|
|
|
|
|
def run_sync(cmd: List[str]) -> None:
|
2020-04-24 23:36:52 +00:00
|
|
|
print(f"$ {' '.join(cmd)}")
|
2023-03-15 16:39:30 +00:00
|
|
|
process = subprocess.run(cmd)
|
|
|
|
|
|
|
|
if process.returncode != 0:
|
|
|
|
sys.exit(1)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
2023-03-15 16:39:30 +00:00
|
|
|
def get_version() -> str:
|
2020-04-24 23:36:52 +00:00
|
|
|
with open(os.path.dirname(sys.argv[0]) + "/default.nix") as f:
|
|
|
|
# A version consists of digits, dots, and possibly a "b" (for beta)
|
2023-03-15 16:39:30 +00:00
|
|
|
if match := re.search('hassVersion = "([\\d\\.b]+)";', f.read()):
|
|
|
|
return match.group(1)
|
|
|
|
raise RuntimeError("hassVersion not in default.nix")
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse_components(version: str = "master"):
|
|
|
|
components = {}
|
2022-01-19 23:45:15 +00:00
|
|
|
components_with_tests = []
|
2020-04-24 23:36:52 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
|
|
with urlopen(
|
|
|
|
f"https://github.com/home-assistant/home-assistant/archive/{version}.tar.gz"
|
|
|
|
) as response:
|
|
|
|
tarfile.open(fileobj=BytesIO(response.read())).extractall(tmp)
|
|
|
|
# Use part of a script from the Home Assistant codebase
|
|
|
|
core_path = os.path.join(tmp, f"core-{version}")
|
2022-01-19 23:45:15 +00:00
|
|
|
|
|
|
|
for entry in os.scandir(os.path.join(core_path, "tests/components")):
|
|
|
|
if entry.is_dir():
|
|
|
|
components_with_tests.append(entry.name)
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
sys.path.append(core_path)
|
2023-03-15 16:39:30 +00:00
|
|
|
from script.hassfest.model import Integration # type: ignore
|
2020-04-24 23:36:52 +00:00
|
|
|
integrations = Integration.load_dir(
|
|
|
|
pathlib.Path(
|
|
|
|
os.path.join(core_path, "homeassistant/components")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
for domain in sorted(integrations):
|
|
|
|
integration = integrations[domain]
|
2023-04-29 16:46:19 +00:00
|
|
|
if extra_deps := EXTRA_COMPONENT_DEPS.get(integration.domain):
|
|
|
|
integration.dependencies.extend(extra_deps)
|
2022-01-13 20:06:32 +00:00
|
|
|
if not integration.disabled:
|
|
|
|
components[domain] = integration.manifest
|
2022-01-19 23:45:15 +00:00
|
|
|
|
|
|
|
return components, components_with_tests
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Recursively get the requirements of a component and its dependencies
|
2020-06-15 15:56:04 +00:00
|
|
|
def get_reqs(components: Dict[str, Dict[str, Any]], component: str, processed: Set[str]) -> Set[str]:
|
2020-05-03 17:38:23 +00:00
|
|
|
requirements = set(components[component].get("requirements", []))
|
2020-04-24 23:36:52 +00:00
|
|
|
deps = components[component].get("dependencies", [])
|
2020-06-15 15:56:04 +00:00
|
|
|
deps.extend(components[component].get("after_dependencies", []))
|
|
|
|
processed.add(component)
|
2020-04-24 23:36:52 +00:00
|
|
|
for dependency in deps:
|
2020-06-15 15:56:04 +00:00
|
|
|
if dependency not in processed:
|
|
|
|
requirements.update(get_reqs(components, dependency, processed))
|
2020-04-24 23:36:52 +00:00
|
|
|
return requirements
|
|
|
|
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
def repository_root() -> str:
|
|
|
|
return os.path.abspath(sys.argv[0] + "/../../../..")
|
|
|
|
|
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
# For a package attribute and and an extra, check if the package exposes it via passthru.optional-dependencies
|
2022-04-15 01:41:22 +00:00
|
|
|
def has_extra(package: str, extra: str):
|
|
|
|
cmd = [
|
|
|
|
"nix-instantiate",
|
|
|
|
repository_root(),
|
|
|
|
"-A",
|
2022-06-16 17:23:12 +00:00
|
|
|
f"{package}.optional-dependencies.{extra}",
|
2022-04-15 01:41:22 +00:00
|
|
|
]
|
|
|
|
try:
|
|
|
|
subprocess.run(
|
|
|
|
cmd,
|
|
|
|
check=True,
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
def dump_packages() -> Dict[str, Dict[str, str]]:
|
|
|
|
# Store a JSON dump of Nixpkgs' python3Packages
|
|
|
|
output = subprocess.check_output(
|
|
|
|
[
|
|
|
|
"nix-env",
|
|
|
|
"-f",
|
2022-04-15 01:41:22 +00:00
|
|
|
repository_root(),
|
2020-04-24 23:36:52 +00:00
|
|
|
"-qa",
|
|
|
|
"-A",
|
|
|
|
PKG_SET,
|
2021-06-28 23:13:55 +00:00
|
|
|
"--arg", "config", "{ allowAliases = false; }",
|
2020-04-24 23:36:52 +00:00
|
|
|
"--json",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return json.loads(output)
|
|
|
|
|
|
|
|
|
|
|
|
def name_to_attr_path(req: str, packages: Dict[str, Dict[str, str]]) -> Optional[str]:
|
2022-03-05 16:20:37 +00:00
|
|
|
if req in PKG_PREFERENCES:
|
|
|
|
return f"{PKG_SET}.{PKG_PREFERENCES[req]}"
|
|
|
|
attr_paths = []
|
2020-04-24 23:36:52 +00:00
|
|
|
names = [req]
|
|
|
|
# E.g. python-mpd2 is actually called python3.6-mpd2
|
|
|
|
# instead of python-3.6-python-mpd2 inside Nixpkgs
|
|
|
|
if req.startswith("python-") or req.startswith("python_"):
|
|
|
|
names.append(req[len("python-") :])
|
|
|
|
for name in names:
|
|
|
|
# treat "-" and "_" equally
|
|
|
|
name = re.sub("[-_]", "[-_]", name)
|
2021-04-25 03:57:28 +00:00
|
|
|
# python(minor).(major)-(pname)-(version or unstable-date)
|
|
|
|
# we need the version qualifier, or we'll have multiple matches
|
|
|
|
# (e.g. pyserial and pyserial-asyncio when looking for pyserial)
|
2022-06-26 10:26:21 +00:00
|
|
|
pattern = re.compile(f"^python\\d+\\.\\d+-{name}-(?:\\d|unstable-.*)", re.I)
|
2020-04-24 23:36:52 +00:00
|
|
|
for attr_path, package in packages.items():
|
|
|
|
if pattern.match(package["name"]):
|
2022-03-05 16:20:37 +00:00
|
|
|
attr_paths.append(attr_path)
|
2020-04-24 23:36:52 +00:00
|
|
|
# Let's hope there's only one derivation with a matching name
|
2022-03-05 16:20:37 +00:00
|
|
|
assert len(attr_paths) <= 1, f"{req} matches more than one derivation: {attr_paths}"
|
|
|
|
if attr_paths:
|
|
|
|
return attr_paths[0]
|
2020-04-24 23:36:52 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2022-11-04 12:27:35 +00:00
|
|
|
def get_pkg_version(attr_path: str, packages: Dict[str, Dict[str, str]]) -> Optional[str]:
|
|
|
|
pkg = packages.get(attr_path, None)
|
2021-02-05 17:12:51 +00:00
|
|
|
if not pkg:
|
|
|
|
return None
|
|
|
|
return pkg["version"]
|
|
|
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
def main() -> None:
|
|
|
|
packages = dump_packages()
|
|
|
|
version = get_version()
|
|
|
|
print("Generating component-packages.nix for version {}".format(version))
|
2022-01-19 23:45:15 +00:00
|
|
|
components, components_with_tests = parse_components(version=version)
|
2020-04-24 23:36:52 +00:00
|
|
|
build_inputs = {}
|
2021-02-05 17:12:51 +00:00
|
|
|
outdated = {}
|
2020-04-24 23:36:52 +00:00
|
|
|
for component in sorted(components.keys()):
|
|
|
|
attr_paths = []
|
2022-04-15 01:41:22 +00:00
|
|
|
extra_attrs = []
|
2020-04-24 23:36:52 +00:00
|
|
|
missing_reqs = []
|
2020-06-15 15:56:04 +00:00
|
|
|
reqs = sorted(get_reqs(components, component, set()))
|
2020-04-24 23:36:52 +00:00
|
|
|
for req in reqs:
|
|
|
|
# Some requirements are specified by url, e.g. https://example.org/foobar#xyz==1.0.0
|
|
|
|
# Therefore, if there's a "#" in the line, only take the part after it
|
|
|
|
req = req[req.find("#") + 1 :]
|
2021-02-05 17:12:51 +00:00
|
|
|
name, required_version = req.split("==", maxsplit=1)
|
2023-04-29 16:46:19 +00:00
|
|
|
# Strip conditions off version constraints e.g. "1.0; python<3.11"
|
|
|
|
required_version = required_version.split(";").pop(0)
|
2022-04-15 01:41:22 +00:00
|
|
|
# Split package name and extra requires
|
|
|
|
extras = []
|
2022-03-05 16:20:37 +00:00
|
|
|
if name.endswith("]"):
|
2022-04-15 01:41:22 +00:00
|
|
|
extras = name[name.find("[")+1:name.find("]")].split(",")
|
2022-03-05 16:20:37 +00:00
|
|
|
name = name[:name.find("[")]
|
2020-04-24 23:36:52 +00:00
|
|
|
attr_path = name_to_attr_path(name, packages)
|
2022-11-04 12:27:35 +00:00
|
|
|
if attr_path:
|
|
|
|
if our_version := get_pkg_version(attr_path, packages):
|
|
|
|
attr_name = attr_path.split(".")[-1]
|
2023-04-29 16:46:19 +00:00
|
|
|
attr_outdated = False
|
|
|
|
try:
|
|
|
|
Version.parse(our_version)
|
|
|
|
except InvalidVersion:
|
|
|
|
print(f"Attribute {attr_name} has invalid version specifier {our_version}", file=sys.stderr)
|
|
|
|
attr_outdated = True
|
|
|
|
else:
|
|
|
|
attr_outdated = Version.parse(our_version) < Version.parse(required_version)
|
|
|
|
finally:
|
|
|
|
if attr_outdated:
|
|
|
|
outdated[attr_name] = {
|
|
|
|
'wanted': required_version,
|
|
|
|
'current': our_version
|
|
|
|
}
|
2020-04-24 23:36:52 +00:00
|
|
|
if attr_path is not None:
|
|
|
|
# Add attribute path without "python3Packages." prefix
|
2022-04-15 01:41:22 +00:00
|
|
|
pname = attr_path[len(PKG_SET + "."):]
|
|
|
|
attr_paths.append(pname)
|
|
|
|
for extra in extras:
|
|
|
|
# Check if package advertises extra requirements
|
2022-06-16 17:23:12 +00:00
|
|
|
extra_attr = f"{pname}.optional-dependencies.{extra}"
|
2022-04-15 01:41:22 +00:00
|
|
|
if has_extra(attr_path, extra):
|
|
|
|
extra_attrs.append(extra_attr)
|
|
|
|
else:
|
|
|
|
missing_reqs.append(extra_attr)
|
|
|
|
|
2021-06-28 23:13:55 +00:00
|
|
|
else:
|
2020-04-24 23:36:52 +00:00
|
|
|
missing_reqs.append(name)
|
|
|
|
else:
|
2022-04-15 01:41:22 +00:00
|
|
|
build_inputs[component] = (attr_paths, extra_attrs, missing_reqs)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with open(os.path.dirname(sys.argv[0]) + "/component-packages.nix", "w") as f:
|
|
|
|
f.write("# Generated by parse-requirements.py\n")
|
|
|
|
f.write("# Do not edit!\n\n")
|
|
|
|
f.write("{\n")
|
|
|
|
f.write(f' version = "{version}";\n')
|
|
|
|
f.write(" components = {\n")
|
|
|
|
for component, deps in build_inputs.items():
|
2022-04-15 01:41:22 +00:00
|
|
|
available, extras, missing = deps
|
2020-09-25 04:45:31 +00:00
|
|
|
f.write(f' "{component}" = ps: with ps; [')
|
|
|
|
if available:
|
2023-07-15 17:15:38 +00:00
|
|
|
f.write("\n " + "\n ".join(sorted(available)))
|
2022-04-15 01:41:22 +00:00
|
|
|
f.write("\n ]")
|
|
|
|
if extras:
|
2023-07-15 17:15:38 +00:00
|
|
|
f.write("\n ++ " + "\n ++ ".join(sorted(extras)))
|
2022-04-15 01:41:22 +00:00
|
|
|
f.write(";")
|
2020-04-24 23:36:52 +00:00
|
|
|
if len(missing) > 0:
|
2023-07-15 17:15:38 +00:00
|
|
|
f.write(f" # missing inputs: {' '.join(sorted(missing))}")
|
2020-04-24 23:36:52 +00:00
|
|
|
f.write("\n")
|
|
|
|
f.write(" };\n")
|
2022-01-19 23:45:15 +00:00
|
|
|
f.write(" # components listed in tests/components for which all dependencies are packaged\n")
|
|
|
|
f.write(" supportedComponentsWithTests = [\n")
|
|
|
|
for component, deps in build_inputs.items():
|
2022-04-15 01:41:22 +00:00
|
|
|
available, extras, missing = deps
|
2022-01-19 23:45:15 +00:00
|
|
|
if len(missing) == 0 and component in components_with_tests:
|
|
|
|
f.write(f' "{component}"' + "\n")
|
|
|
|
f.write(" ];\n")
|
2020-04-24 23:36:52 +00:00
|
|
|
f.write("}\n")
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
supported_components = reduce(lambda n, c: n + (build_inputs[c][2] == []),
|
2021-06-28 23:13:55 +00:00
|
|
|
components.keys(), 0)
|
|
|
|
total_components = len(components)
|
|
|
|
print(f"{supported_components} / {total_components} components supported, "
|
|
|
|
f"i.e. {supported_components / total_components:.2%}")
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
if outdated:
|
|
|
|
table = Table(title="Outdated dependencies")
|
|
|
|
table.add_column("Package")
|
|
|
|
table.add_column("Current")
|
|
|
|
table.add_column("Wanted")
|
|
|
|
for package, version in sorted(outdated.items()):
|
|
|
|
table.add_row(package, version['current'], version['wanted'])
|
|
|
|
|
|
|
|
console = Console()
|
|
|
|
console.print(table)
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-03-15 16:39:30 +00:00
|
|
|
run_sync(["pyright", __file__])
|
|
|
|
run_sync(["ruff", "--ignore=E501", __file__])
|
|
|
|
run_sync(["isort", __file__])
|
2020-04-24 23:36:52 +00:00
|
|
|
main()
|