From b0bf43e6a272d42a55158e657bd937cb82fc3d8d Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Jun 2023 23:02:34 +0300 Subject: move files, rename home package to homekit --- bin/pio_ini.py | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100755 bin/pio_ini.py (limited to 'bin/pio_ini.py') diff --git a/bin/pio_ini.py b/bin/pio_ini.py new file mode 100755 index 0000000..34ad395 --- /dev/null +++ b/bin/pio_ini.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 +import os +import yaml +import re +import __py_include + +from pprint import pprint +from argparse import ArgumentParser, ArgumentError +from homekit.pio import get_products, platformio_ini +from homekit.pio.exceptions import ProductConfigNotFoundError + + +def get_config(product: str) -> dict: + config_path = os.path.join( + os.getenv('HOME'), '.config', + 'homekit_pio', f'{product}.yaml' + ) + if not os.path.exists(config_path): + raise ProductConfigNotFoundError(f'{config_path}: product config not found') + + with open(config_path, 'r') as f: + return yaml.safe_load(f) + + +def bsd_walk(product_config: dict, + f: callable): + try: + for define_name, define_extra_params in product_config['build_specific_defines'].items(): + define_name = re.sub(r'^CONFIG_', '', define_name) + kwargs = {} + if isinstance(define_extra_params, dict): + kwargs = define_extra_params + f(define_name, **kwargs) + except KeyError: + pass + + +# 'bsd' means 'build_specific_defines' +def bsd_parser(product_config: dict, + parser: ArgumentParser): + def f(define_name, **kwargs): + arg_kwargs = {} + define_name = define_name.lower().replace('_', '-') + + if 'type' in kwargs: + if kwargs['type'] in ('str', 'enum'): + arg_kwargs['type'] = str + if kwargs['type'] == 'enum' and 'list_config_key' in kwargs: + if not isinstance(product_config[kwargs['list_config_key']], list): + raise TypeError(f'product_config[{kwargs["list_config_key"]}] enum is not list') + if not product_config[kwargs['list_config_key']]: + raise ValueError(f'product_config[{kwargs["list_config_key"]}] enum cannot be empty') + arg_kwargs['choices'] = product_config[kwargs['list_config_key']] + if isinstance(product_config[kwargs['list_config_key']][0], int): + arg_kwargs['type'] = int + elif kwargs['type'] == 'int': + arg_kwargs['type'] = int + elif kwargs['type'] == 'bool': + arg_kwargs['action'] = 'store_true' + arg_kwargs['required'] = False + else: + raise TypeError(f'unsupported type {kwargs["type"]} for define {define_name}') + else: + arg_kwargs['action'] = 'store_true' + + if 'required' not in arg_kwargs: + arg_kwargs['required'] = True + parser.add_argument(f'--{define_name}', **arg_kwargs) + + bsd_walk(product_config, f) + + +def bsd_get(product_config: dict, + arg: object): + defines = {} + enums = [] + def f(define_name, **kwargs): + attr_name = define_name.lower() + attr_value = getattr(arg, attr_name) + if 'type' in kwargs: + if kwargs['type'] == 'enum': + enums.append(f'CONFIG_{define_name}') + defines[f'CONFIG_{define_name}'] = f'HOMEKIT_{attr_value.upper()}' + return + if kwargs['type'] == 'bool': + defines[f'CONFIG_{define_name}'] = True + return + defines[f'CONFIG_{define_name}'] = str(attr_value) + bsd_walk(product_config, f) + return defines, enums + + +if __name__ == '__main__': + products = get_products() + + # first, get the product + product_parser = ArgumentParser(add_help=False) + product_parser.add_argument('--product', type=str, choices=products, required=True, + help='PIO product name') + arg, _ = product_parser.parse_known_args() + if not arg.product: + product = os.path.basename(os.path.realpath(os.getcwd())) + if product not in products: + raise ArgumentError(None, 'invalid product') + else: + product = arg.product + + product_config = get_config(product) + + # then everythingm else + parser = ArgumentParser(parents=[product_parser]) + parser.add_argument('--target', type=str, required=True, choices=product_config['targets'], + help='PIO build target') + parser.add_argument('--platform', default='espressif8266', type=str) + parser.add_argument('--framework', default='arduino', type=str) + parser.add_argument('--upload-port', default='/dev/ttyUSB0', type=str) + parser.add_argument('--monitor-speed', default=115200) + parser.add_argument('--debug', action='store_true') + parser.add_argument('--debug-network', action='store_true') + bsd_parser(product_config, parser) + arg = parser.parse_args() + + if arg.target not in product_config['targets']: + raise ArgumentError(None, f'target {arg.target} not found for product {product}') + + bsd, bsd_enums = bsd_get(product_config, arg) + ini = platformio_ini(product_config=product_config, + target=arg.target, + build_specific_defines=bsd, + build_specific_defines_enums=bsd_enums, + platform=arg.platform, + framework=arg.framework, + upload_port=arg.upload_port, + monitor_speed=arg.monitor_speed, + debug=arg.debug, + debug_network=arg.debug_network) + print(ini) -- cgit v1.2.3 From ee0341e137f6a8dcf90d5a744e334f66b9d6d60a Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 11 Jun 2023 01:34:08 +0300 Subject: fix platformio.ini generation --- bin/pio_ini.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'bin/pio_ini.py') diff --git a/bin/pio_ini.py b/bin/pio_ini.py index 34ad395..2926234 100755 --- a/bin/pio_ini.py +++ b/bin/pio_ini.py @@ -8,17 +8,19 @@ from pprint import pprint from argparse import ArgumentParser, ArgumentError from homekit.pio import get_products, platformio_ini from homekit.pio.exceptions import ProductConfigNotFoundError +from homekit.config import CONFIG_DIRECTORIES def get_config(product: str) -> dict: - config_path = os.path.join( - os.getenv('HOME'), '.config', - 'homekit_pio', f'{product}.yaml' - ) - if not os.path.exists(config_path): - raise ProductConfigNotFoundError(f'{config_path}: product config not found') - - with open(config_path, 'r') as f: + path = None + for directory in CONFIG_DIRECTORIES: + config_path = os.path.join(directory, 'pio', f'{product}.yaml') + if os.path.exists(config_path) and os.path.isfile(config_path): + path = config_path + break + if not path: + raise ProductConfigNotFoundError(f'pio/{product}.yaml not found') + with open(path, 'r') as f: return yaml.safe_load(f) @@ -83,7 +85,8 @@ def bsd_get(product_config: dict, defines[f'CONFIG_{define_name}'] = f'HOMEKIT_{attr_value.upper()}' return if kwargs['type'] == 'bool': - defines[f'CONFIG_{define_name}'] = True + if attr_value is True: + defines[f'CONFIG_{define_name}'] = True return defines[f'CONFIG_{define_name}'] = str(attr_value) bsd_walk(product_config, f) @@ -124,6 +127,11 @@ if __name__ == '__main__': raise ArgumentError(None, f'target {arg.target} not found for product {product}') bsd, bsd_enums = bsd_get(product_config, arg) + print('>>> bsd:') + pprint(bsd) + print('>>> bsd_enums:') + pprint(bsd_enums) + ini = platformio_ini(product_config=product_config, target=arg.target, build_specific_defines=bsd, -- cgit v1.2.3 From 1215bbf102498fb585b310ba0b5043df875f71fd Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 11 Jun 2023 01:34:50 +0300 Subject: pio_ini: remove debug code that breaks it :( --- bin/pio_ini.py | 5 ----- 1 file changed, 5 deletions(-) (limited to 'bin/pio_ini.py') diff --git a/bin/pio_ini.py b/bin/pio_ini.py index 2926234..7254eca 100755 --- a/bin/pio_ini.py +++ b/bin/pio_ini.py @@ -4,7 +4,6 @@ import yaml import re import __py_include -from pprint import pprint from argparse import ArgumentParser, ArgumentError from homekit.pio import get_products, platformio_ini from homekit.pio.exceptions import ProductConfigNotFoundError @@ -127,10 +126,6 @@ if __name__ == '__main__': raise ArgumentError(None, f'target {arg.target} not found for product {product}') bsd, bsd_enums = bsd_get(product_config, arg) - print('>>> bsd:') - pprint(bsd) - print('>>> bsd_enums:') - pprint(bsd_enums) ini = platformio_ini(product_config=product_config, target=arg.target, -- cgit v1.2.3 From cbb6ad451749c54039e6d7c7eeeb5e387d95c069 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 11 Jun 2023 05:10:21 +0300 Subject: typo --- bin/pio_ini.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bin/pio_ini.py') diff --git a/bin/pio_ini.py b/bin/pio_ini.py index 7254eca..ee85732 100755 --- a/bin/pio_ini.py +++ b/bin/pio_ini.py @@ -109,7 +109,7 @@ if __name__ == '__main__': product_config = get_config(product) - # then everythingm else + # then everything else parser = ArgumentParser(parents=[product_parser]) parser.add_argument('--target', type=str, required=True, choices=product_config['targets'], help='PIO build target') -- cgit v1.2.3