home-assistant: include requirements of dependencies

This commit is contained in:
Robert Schütz 2018-05-25 17:14:30 +02:00
parent 1c39035cad
commit b1aa2b7890
2 changed files with 761 additions and 56 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
#! /usr/bin/env nix-shell #! /usr/bin/env nix-shell
#! nix-shell -i python3 -p "python3.withPackages (ps: with ps; [ ])" #! nix-shell -i python3 -p "python3.withPackages (ps: with ps; [ requests pyyaml pytz pip jinja2 voluptuous typing aiohttp async-timeout astral certifi attrs ])"
# #
# This script downloads https://github.com/home-assistant/home-assistant/blob/master/requirements_all.txt. # This script downloads https://github.com/home-assistant/home-assistant/blob/master/requirements_all.txt.
# This file contains lines of the form # This file contains lines of the form
@ -14,15 +14,17 @@
# Then, a Nix attribute set mapping component name to dependencies is created. # Then, a Nix attribute set mapping component name to dependencies is created.
from urllib.request import urlopen from urllib.request import urlopen
from collections import OrderedDict import tempfile
from io import BytesIO
import tarfile
import importlib
import subprocess import subprocess
import os import os
import sys import sys
import json import json
import re import re
GENERAL_PREFIX = '# homeassistant.' COMPONENT_PREFIX = 'homeassistant.components'
COMPONENT_PREFIX = GENERAL_PREFIX + 'components.'
PKG_SET = 'python3Packages' PKG_SET = 'python3Packages'
# If some requirements are matched by multiple python packages, # If some requirements are matched by multiple python packages,
@ -37,28 +39,32 @@ def get_version():
m = re.search('hassVersion = "([\\d\\.]+)";', f.read()) m = re.search('hassVersion = "([\\d\\.]+)";', f.read())
return m.group(1) return m.group(1)
def fetch_reqs(version='master'): def parse_components(version='master'):
requirements = {} components = {}
with urlopen('https://github.com/home-assistant/home-assistant/raw/{}/requirements_all.txt'.format(version)) as response: with tempfile.TemporaryDirectory() as tmp:
components = [] with urlopen('https://github.com/home-assistant/home-assistant/archive/{}.tar.gz'.format(version)) as response:
for line in response.read().decode().splitlines(): tarfile.open(fileobj=BytesIO(response.read())).extractall(tmp)
if line == '': # Use part of a script from the Home Assistant codebase
components = [] sys.path.append(tmp + '/home-assistant-{}'.format(version))
elif line[:len(COMPONENT_PREFIX)] == COMPONENT_PREFIX: from script.gen_requirements_all import explore_module
component = line[len(COMPONENT_PREFIX):] for package in explore_module(COMPONENT_PREFIX, True):
components.append(component) # Remove 'homeassistant.components.' prefix
if component not in requirements: component = package[len(COMPONENT_PREFIX + '.'):]
requirements[component] = [] try:
elif line[:len(GENERAL_PREFIX)] != GENERAL_PREFIX: # skip lines like "# homeassistant.scripts.xyz" module = importlib.import_module(package)
# Some dependencies are commented out because they don't build on all platforms components[component] = {}
# Since they are still required for running the component, don't skip them components[component]['requirements'] = getattr(module, 'REQUIREMENTS', [])
if line[:2] == '# ': components[component]['dependencies'] = getattr(module, 'DEPENDENCIES', [])
line = line[2:] # If there is an ImportError, the imported file is not the main file of the component
# Some requirements are specified by url, e.g. https://example.org/foobar#xyz==1.0.0 except ImportError:
# Therefore, if there's a "#" in the line, only take the part after it continue
line = line[line.find('#') + 1:] return components
for component in components:
requirements[component].append(line) # Recursively get the requirements of a component and its dependencies
def get_reqs(components, component):
requirements = set(components[component]['requirements'])
for dependency in components[component]['dependencies']:
requirements.update(get_reqs(components, dependency))
return requirements return requirements
# Store a JSON dump of Nixpkgs' python3Packages # Store a JSON dump of Nixpkgs' python3Packages
@ -95,11 +101,11 @@ def name_to_attr_path(req):
version = get_version() version = get_version()
print('Generating component-packages.nix for version {}'.format(version)) print('Generating component-packages.nix for version {}'.format(version))
requirements = fetch_reqs(version=version) components = parse_components(version=version)
build_inputs = {} build_inputs = {}
for component, reqs in OrderedDict(sorted(requirements.items())).items(): for component in sorted(components.keys()):
attr_paths = [] attr_paths = []
for req in reqs: for req in get_reqs(components, component):
name = req.split('==')[0] name = req.split('==')[0]
attr_path = name_to_attr_path(name) attr_path = name_to_attr_path(name)
if attr_path is not None: if attr_path is not None:
@ -108,11 +114,8 @@ for component, reqs in OrderedDict(sorted(requirements.items())).items():
else: else:
build_inputs[component] = attr_paths build_inputs[component] = attr_paths
# Only select components which have any dependency
#build_inputs = {k: v for k, v in build_inputs.items() if len(v) > 0}
with open(os.path.dirname(sys.argv[0]) + '/component-packages.nix', 'w') as f: with open(os.path.dirname(sys.argv[0]) + '/component-packages.nix', 'w') as f:
f.write('# Generated from parse-requirements.py\n') f.write('# Generated by parse-requirements.py\n')
f.write('# Do not edit!\n\n') f.write('# Do not edit!\n\n')
f.write('{\n') f.write('{\n')
f.write(' version = "{}";\n'.format(version)) f.write(' version = "{}";\n'.format(version))