diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml index b664360..ed35439 100644 --- a/.github/workflows/pythonapp.yml +++ b/.github/workflows/pythonapp.yml @@ -4,15 +4,15 @@ on: [push] jobs: build: - runs-on: ubuntu-latest strategy: matrix: python-version: ['3.8', '3.9', '3.10', '3.11'] - + os: [ubuntu-latest, windows-latest] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - name: Set up Python - uses: actions/setup-python@v3 + uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies diff --git a/.github/workflows/pythonpublish.yml b/.github/workflows/pythonpublish.yml index 73cb5c7..307a516 100644 --- a/.github/workflows/pythonpublish.yml +++ b/.github/workflows/pythonpublish.yml @@ -15,7 +15,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Set up Python - uses: actions/setup-python@v3 + uses: actions/setup-python@v4 with: python-version: '3.x' - name: Install dependencies diff --git a/tdworkflow/util.py b/tdworkflow/util.py index afb250b..df09ba1 100644 --- a/tdworkflow/util.py +++ b/tdworkflow/util.py @@ -1,38 +1,34 @@ -import glob import io import logging import os import re import tarfile from datetime import datetime, timedelta, timezone +from pathlib import Path from typing import List, Optional logger = logging.getLogger(__name__) -def exclude_files(pattern): - def _filter(tarinfo): - if re.search(pattern, tarinfo.name) or os.path.basename( - tarinfo.name - ).startswith("."): - return None - else: - return tarinfo - - return _filter - - def archive_files(target_dir: str, exclude_patterns: List[str]) -> io.BytesIO: _partial = r")|(".join(exclude_patterns) pattern = rf"({_partial})" + target_dir_path = Path(target_dir) _bytes = io.BytesIO() with tarfile.open(mode="w:gz", fileobj=_bytes) as tar: - for fn in glob.glob(os.path.join(target_dir, "**"), recursive=True): - if not re.search(pattern, fn) and os.path.basename(fn) != "": - write_fn = fn.replace(f"{target_dir}/", "") - logger.info(f"Added {fn} as {write_fn}") - tar.add(fn, write_fn, recursive=False) + for current_dir, directories, files in os.walk(target_dir_path): + for file_or_dir in [*directories, *files]: + file_path = Path(os.path.join(current_dir, file_or_dir)) + if re.search(pattern, str(file_path)) or file_or_dir.startswith("."): + continue + relative_path = file_path.relative_to(target_dir_path) + logger.info(f"Added {file_path} as {relative_path}") + tar.add( + file_path, + relative_path, + recursive=False, + ) _bytes.seek(0) return _bytes diff --git a/tests/test_util.py b/tests/test_util.py index 1acea1c..c5cc3f5 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,4 +1,5 @@ import tarfile +from pathlib import Path from tdworkflow.util import archive_files @@ -6,7 +7,7 @@ def test_archive_files(): expected_files = ["py_scripts", "py_scripts/exec.py", "main.dig"] - target_dir = "tests/resources/sample_project" + target_dir = Path("tests", "resources", "sample_project") excludes = ["ignore_dir", "__ignoredir__"] data = archive_files(target_dir, excludes)