Skip to content

Commit

Permalink
Support Windows
Browse files Browse the repository at this point in the history
fix #19
  • Loading branch information
chezou committed May 31, 2023
1 parent a8e927d commit 633afaf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 14 additions & 18 deletions tdworkflow/util.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import tarfile
from pathlib import Path

from tdworkflow.util import archive_files


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)

Expand Down

0 comments on commit 633afaf

Please sign in to comment.