core/script/gen_requirements_all.py

298 lines
7.6 KiB
Python
Raw Normal View History

2015-11-17 08:18:42 +00:00
#!/usr/bin/env python3
2016-03-09 10:15:04 +00:00
"""Generate an updated requirements_all.txt."""
2015-11-17 08:18:42 +00:00
import importlib
import os
import pkgutil
import re
2015-12-18 07:51:34 +00:00
import sys
2015-11-17 08:18:42 +00:00
COMMENT_REQUIREMENTS = (
'RPi.GPIO',
'raspihats',
2016-05-12 05:56:05 +00:00
'rpi-rf',
'Adafruit_Python_DHT',
'Adafruit_BBIO',
2016-01-29 05:37:08 +00:00
'fritzconnection',
'pybluez',
'beacontools',
'bluepy',
'opencv-python',
'python-lirc',
'gattlib',
'pyuserinput',
'evdev',
'pycups',
2017-01-18 05:48:33 +00:00
'python-eq3bt',
'avion',
'decora',
'face_recognition',
'blinkt',
'smbus-cffi',
'envirophat',
'i2csense'
)
TEST_REQUIREMENTS = (
'aioautomatic',
'aiohttp_cors',
'apns2',
'dsmr_parser',
'ephem',
'evohomeclient',
'feedparser',
'forecastio',
'fuzzywuzzy',
'gTTS-token',
'ha-ffmpeg',
'haversine',
'hbmqtt',
'holidays',
'influxdb',
'libpurecoollink',
'libsoundtouch',
'mficlient',
'nx584',
'paho',
'pexpect',
'pilight',
'pmsensor',
'prometheus_client',
'pydispatch',
'PyJWT',
'pylitejet',
'pyunifi',
'pywebpush',
'restrictedpython',
'rflink',
'ring_doorbell',
'rxv',
'sleepyq',
'SoCo',
'somecomfort',
'sqlalchemy',
'statsd',
'uvcclient',
'warrant',
'yahoo-finance',
)
IGNORE_PACKAGES = (
'homeassistant.components.recorder.models',
)
IGNORE_PIN = ('colorlog>2.1,<3', 'keyring>=9.3,<10.0', 'urllib3')
IGNORE_REQ = (
'colorama<=1', # Windows only requirement in check_config
)
2017-01-22 16:34:00 +00:00
URL_PIN = ('https://home-assistant.io/developers/code_review_platform/'
'#1-requirements')
2015-11-17 08:18:42 +00:00
CONSTRAINT_PATH = os.path.join(os.path.dirname(__file__),
'../homeassistant/package_constraints.txt')
CONSTRAINT_BASE = """
# Breaks Python 3.6 and is not needed for our supported Pythons
enum34==1000000000.0.0
"""
2015-11-17 08:18:42 +00:00
def explore_module(package, explore_children):
2016-03-09 10:15:04 +00:00
"""Explore the modules."""
2015-11-17 08:18:42 +00:00
module = importlib.import_module(package)
found = []
if not hasattr(module, '__path__'):
return found
for _, name, _ in pkgutil.iter_modules(module.__path__, package + '.'):
2015-11-17 08:18:42 +00:00
found.append(name)
if explore_children:
found.extend(explore_module(name, False))
return found
def core_requirements():
2016-03-09 10:15:04 +00:00
"""Gather core requirements out of setup.py."""
2015-11-17 08:18:42 +00:00
with open('setup.py') as inp:
reqs_raw = re.search(
r'REQUIRES = \[(.*?)\]', inp.read(), re.S).group(1)
return re.findall(r"'(.*?)'", reqs_raw)
def comment_requirement(req):
2016-03-09 10:15:04 +00:00
"""Some requirements don't install on all systems."""
return any(ign in req for ign in COMMENT_REQUIREMENTS)
2015-11-25 22:31:04 +00:00
def gather_modules():
"""Collect the information."""
2016-02-01 07:52:42 +00:00
reqs = {}
2015-11-17 08:18:42 +00:00
errors = []
2015-11-25 22:31:04 +00:00
for package in sorted(explore_module('homeassistant.components', True) +
explore_module('homeassistant.scripts', True)):
2015-11-17 08:18:42 +00:00
try:
module = importlib.import_module(package)
except ImportError:
if package not in IGNORE_PACKAGES:
errors.append(package)
2015-11-17 08:18:42 +00:00
continue
if not getattr(module, 'REQUIREMENTS', None):
continue
for req in module.REQUIREMENTS:
if req in IGNORE_REQ:
continue
if req.partition('==')[1] == '' and req not in IGNORE_PIN:
2017-01-22 16:34:00 +00:00
errors.append(
"{}[Please pin requirement {}, see {}]".format(
package, req, URL_PIN))
2015-11-17 08:18:42 +00:00
reqs.setdefault(req, []).append(package)
2016-02-01 07:52:42 +00:00
for key in reqs:
reqs[key] = sorted(reqs[key],
key=lambda name: (len(name.split('.')), name))
2015-11-17 08:18:42 +00:00
if errors:
2015-12-18 07:51:34 +00:00
print("******* ERROR")
print("Errors while importing: ", ', '.join(errors))
print("Make sure you import 3rd party libraries inside methods.")
return None
2015-11-17 08:18:42 +00:00
return reqs
def generate_requirements_list(reqs):
"""Generate a pip file based on requirements."""
output = []
2016-02-01 07:52:42 +00:00
for pkg, requirements in sorted(reqs.items(), key=lambda item: item[0]):
2015-11-17 08:18:42 +00:00
for req in sorted(requirements,
key=lambda name: (len(name.split('.')), name)):
2015-11-25 22:31:04 +00:00
output.append('\n# {}'.format(req))
if comment_requirement(pkg):
2015-11-25 22:31:04 +00:00
output.append('\n# {}\n'.format(pkg))
else:
2015-11-25 22:31:04 +00:00
output.append('\n{}\n'.format(pkg))
return ''.join(output)
def requirements_all_output(reqs):
"""Generate output for requirements_all."""
output = []
output.append('# Home Assistant core')
output.append('\n')
output.append('\n'.join(core_requirements()))
output.append('\n')
output.append(generate_requirements_list(reqs))
return ''.join(output)
def requirements_test_output(reqs):
"""Generate output for test_requirements."""
output = []
output.append('# Home Assistant test')
output.append('\n')
with open('requirements_test.txt') as fp:
output.append(fp.read())
output.append('\n')
filtered = {key: value for key, value in reqs.items()
if any(ign in key for ign in TEST_REQUIREMENTS)}
output.append(generate_requirements_list(filtered))
2015-11-25 22:31:04 +00:00
return ''.join(output)
def gather_constraints():
"""Construct output for constraint file."""
return '\n'.join(core_requirements() + [''])
def write_requirements_file(data):
2016-03-09 10:15:04 +00:00
"""Write the modules to the requirements_all.txt."""
with open('requirements_all.txt', 'w+', newline="\n") as req_file:
2015-11-25 22:31:04 +00:00
req_file.write(data)
def write_test_requirements_file(data):
"""Write the modules to the requirements_all.txt."""
with open('requirements_test_all.txt', 'w+', newline="\n") as req_file:
req_file.write(data)
def write_constraints_file(data):
"""Write constraints to a file."""
with open(CONSTRAINT_PATH, 'w+', newline="\n") as req_file:
req_file.write(data + CONSTRAINT_BASE)
def validate_requirements_file(data):
2016-03-09 10:15:04 +00:00
"""Validate if requirements_all.txt is up to date."""
2015-12-18 07:51:34 +00:00
with open('requirements_all.txt', 'r') as req_file:
return data == req_file.read()
2015-12-18 07:51:34 +00:00
def validate_requirements_test_file(data):
"""Validate if requirements_all.txt is up to date."""
with open('requirements_test_all.txt', 'r') as req_file:
return data == req_file.read()
def validate_constraints_file(data):
"""Validate if constraints is up to date."""
with open(CONSTRAINT_PATH, 'r') as req_file:
return data + CONSTRAINT_BASE == req_file.read()
2015-11-25 22:31:04 +00:00
def main():
2016-03-09 10:15:04 +00:00
"""Main section of the script."""
2015-11-25 22:31:04 +00:00
if not os.path.isfile('requirements_all.txt'):
print('Run this from HA root dir')
return
2015-11-25 22:31:04 +00:00
data = gather_modules()
if data is None:
2015-12-18 07:51:34 +00:00
sys.exit(1)
constraints = gather_constraints()
reqs_file = requirements_all_output(data)
reqs_test_file = requirements_test_output(data)
if sys.argv[-1] == 'validate':
errors = []
if not validate_requirements_file(reqs_file):
errors.append("requirements_all.txt is not up to date")
if not validate_requirements_test_file(reqs_test_file):
errors.append("requirements_test_all.txt is not up to date")
if not validate_constraints_file(constraints):
errors.append(
"home-assistant/package_constraints.txt is not up to date")
if errors:
print("******* ERROR")
print('\n'.join(errors))
print("Please run script/gen_requirements_all.py")
sys.exit(1)
sys.exit(0)
write_requirements_file(reqs_file)
write_test_requirements_file(reqs_test_file)
write_constraints_file(constraints)
2015-11-17 08:18:42 +00:00
2016-11-19 05:47:59 +00:00
2015-11-17 08:18:42 +00:00
if __name__ == '__main__':
main()