🔨 Use click for commandline parameters

This commit is contained in:
Odd Stråbø 2020-10-02 11:01:07 +02:00
commit cbdc9aba09
3 changed files with 39 additions and 18 deletions

50
build.py Normal file → Executable file
View file

@ -4,8 +4,15 @@ from __future__ import division, absolute_import, print_function, unicode_litera
import os import os
import zipfile import zipfile
from fnmatch import fnmatchcase as fnmatch from fnmatch import fnmatchcase as fnmatch
import logging
import yaml 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): 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: with zipfile.ZipFile(file_name, "w", zipfile.ZIP_DEFLATED) as z:
for item, path, _pattern in items: for item, path, _pattern in items:
print(path) logger.info(path)
archive_path = os.path.join(base_path, path) archive_path = os.path.join(base_path, path)
if path not in ["."]: if path not in ["."]:
z.write(item, archive_path) z.write(item, archive_path)
@ -33,12 +40,12 @@ def get_config(filename='release.yaml'):
def match_item(item, include, exclude): def match_item(item, include, exclude):
for pattern in exclude: for pattern in exclude:
if fnmatch(item, pattern): if fnmatch(item, pattern):
print('-', item, pattern) logger.debug('Excluded: {!r} {!r}'.format(pattern, item))
return False, pattern return False, pattern
for pattern in include: for pattern in include:
if fnmatch(item, pattern): if fnmatch(item, pattern):
print('+', item, pattern) logger.debug('Included: {!r} {!r}'.format(pattern, item))
return True, pattern return True, pattern
return None, None return None, None
@ -61,23 +68,34 @@ def get_items(include, exclude, basepath='.'):
yield file_path, normalized_file_path, pattern yield file_path, normalized_file_path, pattern
def main(): def build_filename(config, py3):
relpath = os.path.dirname(__file__) or '.' # 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) @click.command()
print("Include:", include) @click.option('-3/-2', '--py3/--py2', default=True, type=bool, help="Default is Python 3.")
print("Exclude:", exclude) @click.option('--source', default='.', type=click.Path(exists=True, file_okay=False), help="Path to addon sources (current_dir).")
print("Config:", config) @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') items = get_items(include, exclude, source)
print(zip_file_name)
zip_file_name = zip_items(output, items, base_path=config.get('id'))
click.echo(zip_file_name)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,3 +1,4 @@
id: 'plugin.video.jellyfin'
version: '0.6.2' version: '0.6.2'
changelog: | changelog: |
- #373 Use correct filename for kodirepo command - #373 Use correct filename for kodirepo command
@ -31,5 +32,5 @@ build:
exclude: exclude:
- .gitignore - .gitignore
- "*.py[ocd]" - '*.py[ocd]'
- "*/__pycache__" - '*/__pycache__'

View file

@ -13,3 +13,5 @@ flake8 >= 3.8
flake8-import-order >= 0.18 flake8-import-order >= 0.18
PyYAML >= 5.3 PyYAML >= 5.3
click
click-log