⛏️ Initial work on build.py

This commit is contained in:
Odd Stråbø 2020-10-01 05:53:46 +02:00
commit 046bbf325e
5 changed files with 152 additions and 5 deletions

84
build.py Normal file
View file

@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
from __future__ import division, absolute_import, print_function, unicode_literals
import os
import zipfile
from fnmatch import fnmatchcase as fnmatch
import yaml
def zip_items(file_name, items, base_path=None):
if base_path is None:
base_path = os.path.splitext(file_name)[0]
elif not base_path:
base_path = ""
with zipfile.ZipFile(file_name, "w", zipfile.ZIP_DEFLATED) as z:
for item, path, _pattern in items:
print(path)
archive_path = os.path.join(base_path, path)
if path not in ["."]:
z.write(item, archive_path)
return file_name
def get_config(filename='release.yaml'):
with open(filename, 'r') as fh:
return yaml.safe_load(fh)
def match_item(item, include, exclude):
for pattern in exclude:
if fnmatch(item, pattern):
print('-', item, pattern)
return False, pattern
for pattern in include:
if fnmatch(item, pattern):
print('+', item, pattern)
return True, pattern
return None, None
def get_items(include, exclude, basepath='.'):
for base, dirs, files in os.walk(basepath):
for dir_name in dirs:
dir_path = os.path.join(base, dir_name)
normalized_dir_path = os.path.normpath(os.path.relpath(dir_path, basepath))
match, pattern = match_item(normalized_dir_path, include, exclude)
if match:
yield dir_path, normalized_dir_path, pattern
for file_name in files:
file_path = os.path.join(base, file_name)
normalized_file_path = os.path.normpath(os.path.relpath(file_path, basepath))
match, pattern = match_item(normalized_file_path, include, exclude)
if match:
yield file_path, normalized_file_path, pattern
def main():
relpath = os.path.dirname(__file__) or '.'
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)
items = get_items(include, exclude, relpath)
zip_file_name = zip_items('test.zip', items, base_path='plugin.video.jellyfin')
print(zip_file_name)
if __name__ == "__main__":
main()

View file

@ -6,3 +6,30 @@ changelog: |
- #385 Normalize path for os.mkdir - #385 Normalize path for os.mkdir
- #387 Optimize music sync for large libraries - #387 Optimize music sync for large libraries
- #392 Fix the pipeline. Again. - #392 Fix the pipeline. Again.
build:
include:
- jellyfin_kodi/*
- resources/*
- default.py
- service.py
- context.py
- context_play.py
- addon.xml
- .config/*
- tests/*
- build.py
- requirements-dev.txt
- tox.ini
- .editorconfig
- .env
- README.md
- LICENSE.txt
- CONTRIBUTING.md
exclude:
- .gitignore
- "*.py[ocd]"
- "*/__pycache__"

View file

@ -11,3 +11,5 @@ pytest >= 4.6.11
coverage >= 5.2 coverage >= 5.2
flake8 >= 3.8 flake8 >= 3.8
flake8-import-order >= 0.18 flake8-import-order >= 0.18
PyYAML >= 5.3

33
tests/test_build.py Normal file
View file

@ -0,0 +1,33 @@
import pytest
from build import get_config, get_items
config = get_config()
build_config = config.get('build', {})
include = build_config.get('include', [])
exclude = build_config.get('exclude', [])
items = list(get_items(include, exclude))
def test_config():
assert 'version' in config and type(config['version']) is str
assert 'changelog' in config and type(config['changelog']) is str
assert 'build' in config
build = config['build']
assert type(build) is dict
assert 'include' in build and type(build['include']) is list
assert 'exclude' in build and type(build['exclude']) is list
@pytest.mark.parametrize("pattern", include)
def test_include_pattern_matched(pattern):
matched = False
for _fp, _nfp, item_pattern in items:
if pattern == item_pattern:
matched = True
assert matched, "Pattern `{}` matches no files.".format(pattern)

11
tox.ini
View file

@ -15,10 +15,11 @@ testpaths =
[coverage:run] [coverage:run]
source = source =
jellyfin_kodi jellyfin_kodi
context.py context # Clashes with multiprocessing.context?
context_play.py context_play
default.py default
service.py service
.config/generate_xml.py .config/generate_xml.py # Can't import this file.
build
omit = tests/* omit = tests/*
command_line = -m pytest command_line = -m pytest