From cbdc9aba09b003670a08d1c85a0c9fa1c376ae25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Odd=20Str=C3=A5b=C3=B8?= Date: Fri, 2 Oct 2020 11:01:07 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Use=20click=20for=20commandline?= =?UTF-8?q?=20parameters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.py | 50 ++++++++++++++++++++++++++++++-------------- release.yaml | 5 +++-- requirements-dev.txt | 2 ++ 3 files changed, 39 insertions(+), 18 deletions(-) mode change 100644 => 100755 build.py diff --git a/build.py b/build.py old mode 100644 new mode 100755 index 7b0d794f..92d9d3c6 --- a/build.py +++ b/build.py @@ -4,8 +4,15 @@ from __future__ import division, absolute_import, print_function, unicode_litera import os import zipfile from fnmatch import fnmatchcase as fnmatch +import logging import yaml +import click +import click_log + + +logger = logging.getLogger("build") +click_log.basic_config(logger) def zip_items(file_name, items, base_path=None): @@ -17,7 +24,7 @@ def zip_items(file_name, items, base_path=None): with zipfile.ZipFile(file_name, "w", zipfile.ZIP_DEFLATED) as z: for item, path, _pattern in items: - print(path) + logger.info(path) archive_path = os.path.join(base_path, path) if path not in ["."]: z.write(item, archive_path) @@ -33,12 +40,12 @@ def get_config(filename='release.yaml'): def match_item(item, include, exclude): for pattern in exclude: if fnmatch(item, pattern): - print('-', item, pattern) + logger.debug('Excluded: {!r} {!r}'.format(pattern, item)) return False, pattern for pattern in include: if fnmatch(item, pattern): - print('+', item, pattern) + logger.debug('Included: {!r} {!r}'.format(pattern, item)) return True, pattern return None, None @@ -61,23 +68,34 @@ def get_items(include, exclude, basepath='.'): yield file_path, normalized_file_path, pattern -def main(): - relpath = os.path.dirname(__file__) or '.' +def build_filename(config, py3): + # type: (dict, bool) -> str + return "{}-{}+{}.zip".format( + config.get('id'), + config.get('version', '0.0.0'), + 'py3' if py3 else 'py2' + ) - config = get_config(os.path.join(relpath, 'release.yaml')) - build_config = config.get('build', {}) - include = build_config.get('include', []) - exclude = build_config.get('exclude', []) - print("Relpath:", relpath) - print("Include:", include) - print("Exclude:", exclude) - print("Config:", config) +@click.command() +@click.option('-3/-2', '--py3/--py2', default=True, type=bool, help="Default is Python 3.") +@click.option('--source', default='.', type=click.Path(exists=True, file_okay=False), help="Path to addon sources (current_dir).") +@click.option('--output', default=None, type=click.Path(), help="Output file (current_dir/addon-version+py3.zip).") +@click_log.simple_verbosity_option(logger) +def main(py3, source, output): + config = get_config(os.path.join(source, 'release.yaml')) + config_build = config.get('build', {}) + include = config_build.get('include', []) + exclude = config_build.get('exclude', []) - items = get_items(include, exclude, relpath) + if output is None: + output = build_filename(config, py3) - zip_file_name = zip_items('test.zip', items, base_path='plugin.video.jellyfin') - print(zip_file_name) + items = get_items(include, exclude, source) + + zip_file_name = zip_items(output, items, base_path=config.get('id')) + + click.echo(zip_file_name) if __name__ == "__main__": diff --git a/release.yaml b/release.yaml index 6ab45749..c17492f8 100644 --- a/release.yaml +++ b/release.yaml @@ -1,3 +1,4 @@ +id: 'plugin.video.jellyfin' version: '0.6.2' changelog: | - #373 Use correct filename for kodirepo command @@ -31,5 +32,5 @@ build: exclude: - .gitignore - - "*.py[ocd]" - - "*/__pycache__" + - '*.py[ocd]' + - '*/__pycache__' diff --git a/requirements-dev.txt b/requirements-dev.txt index c9e2473c..b8764897 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -13,3 +13,5 @@ flake8 >= 3.8 flake8-import-order >= 0.18 PyYAML >= 5.3 +click +click-log