Skip to content

Commit c04999c

Browse files
author
Andrei Neagu
committed
removed unused feature
1 parent 46a0c29 commit c04999c

File tree

6 files changed

+37
-186
lines changed

6 files changed

+37
-186
lines changed

packages/service-library/src/servicelib/archiving_utils/_interface_7zip.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ async def read_stream(
7676
raise ArchiveError(msg)
7777

7878

79-
async def print_output_handler(chunk: str) -> None:
80-
print(f"{chunk=}")
81-
82-
8379
_TOTAL_BYTES_RE: Final[str] = r" (\d+)\s*bytes "
8480
_PROGRESS_PERCENT_RE: Final[str] = r" (?:100|\d?\d)% "
8581
_ALL_DONE_RE: Final[str] = r"Everything is Ok"
@@ -151,13 +147,7 @@ async def progress_handler(byte_progress: NonNegativeInt) -> None:
151147
print(f"{byte_progress=}")
152148

153149
parser = ProgressParser(progress_handler)
154-
await _run_cli_command(
155-
command,
156-
[
157-
# print_output_handler,
158-
parser.parse_chunk,
159-
],
160-
)
150+
await _run_cli_command(command, [parser.parse_chunk])
161151

162152

163153
async def unarchive_dir(
@@ -168,11 +158,14 @@ async def unarchive_dir(
168158
progress_bar: ProgressBarData | None = None,
169159
log_cb: Callable[[str], Awaitable[None]] | None = None,
170160
) -> set[Path]:
171-
# NOTE: maintained here conserve the interface
172-
_ = max_workers # no longer used
161+
_ = max_workers # not required here, can be removed from the interface
173162

174163
command = f"7z x -bsp1 {archive_to_extract} -o{destination_folder}"
175164

176-
await _run_cli_command(command, [print_output_handler])
165+
async def progress_handler(byte_progress: NonNegativeInt) -> None:
166+
print(f"{byte_progress=}")
167+
168+
parser = ProgressParser(progress_handler)
169+
await _run_cli_command(command, [parser.parse_chunk])
177170

178171
return set()

packages/service-library/src/servicelib/archiving_utils/_interface_zipfile.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import fnmatch
32
import functools
43
import logging
54
import types
@@ -54,16 +53,11 @@ def _strip_undecodable_in_path(path: Path) -> Path:
5453
return Path(str(path).encode(errors="replace").decode("utf-8"))
5554

5655

57-
def _iter_files_to_compress(
58-
dir_path: Path, exclude_patterns: set[str] | None
59-
) -> Iterator[Path]:
60-
exclude_patterns = exclude_patterns if exclude_patterns else set()
56+
def _iter_files_to_compress(dir_path: Path) -> Iterator[Path]:
6157
# NOTE: make sure to sort paths othrwise between different runs
6258
# the zip will have a different structure and hash
6359
for path in sorted(dir_path.rglob("*")):
64-
if path.is_file() and not any(
65-
fnmatch.fnmatch(f"{path}", x) for x in exclude_patterns
66-
):
60+
if path.is_file():
6761
yield path
6862

6963

@@ -303,12 +297,10 @@ def _add_to_archive(
303297
store_relative_path: bool, # noqa: FBT001
304298
update_progress,
305299
loop,
306-
exclude_patterns: set[str] | None = None,
307300
) -> None:
308301
compression = zipfile.ZIP_DEFLATED if compress else zipfile.ZIP_STORED
309302
folder_size_bytes = sum(
310-
file.stat().st_size
311-
for file in _iter_files_to_compress(dir_to_compress, exclude_patterns)
303+
file.stat().st_size for file in _iter_files_to_compress(dir_to_compress)
312304
)
313305
desc = f"compressing {dir_to_compress} -> {destination}"
314306
with tqdm_logging_redirect(
@@ -320,7 +312,7 @@ def _add_to_archive(
320312
) as progress_bar, _progress_enabled_zip_write_handler(
321313
ReproducibleZipFile(destination, "w", compression=compression), progress_bar
322314
) as zip_file_handler:
323-
for file_to_add in _iter_files_to_compress(dir_to_compress, exclude_patterns):
315+
for file_to_add in _iter_files_to_compress(dir_to_compress):
324316
progress_bar.set_description(f"{desc}/{file_to_add.name}\n")
325317
file_name_in_archive = (
326318
_strip_directory_from_path(file_to_add, dir_to_compress)
@@ -350,7 +342,6 @@ async def archive_dir(
350342
*,
351343
compress: bool,
352344
store_relative_path: bool,
353-
exclude_patterns: set[str] | None = None,
354345
progress_bar: ProgressBarData | None = None,
355346
) -> None:
356347
"""
@@ -359,9 +350,6 @@ async def archive_dir(
359350
When unarchiveing, the **escaped version** of the file names
360351
will be created.
361352
362-
The **exclude_patterns** is a set of patterns created using
363-
Unix shell-style wildcards to exclude files and directories.
364-
365353
destination: Path deleted if errors
366354
367355
::raise ArchiveError
@@ -373,8 +361,7 @@ async def archive_dir(
373361

374362
async with AsyncExitStack() as stack:
375363
folder_size_bytes = sum(
376-
file.stat().st_size
377-
for file in _iter_files_to_compress(dir_to_compress, exclude_patterns)
364+
file.stat().st_size for file in _iter_files_to_compress(dir_to_compress)
378365
)
379366
sub_progress = await stack.enter_async_context(
380367
progress_bar.sub_progress(folder_size_bytes, description=IDStr("..."))
@@ -393,7 +380,6 @@ async def archive_dir(
393380
store_relative_path,
394381
functools.partial(_update_progress, sub_progress),
395382
asyncio.get_event_loop(),
396-
exclude_patterns,
397383
)
398384
except Exception as err:
399385
if destination.is_file():

packages/service-library/tests/archiving_utils/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ async def mixed_file_types(tmp_path: Path, faker: Faker) -> AsyncIterable[Path]:
2929

3030
# images cause issues with zipping, below content produced different
3131
# hashes for zip files
32-
for i in range(4):
32+
for i in range(2):
3333
image_dir = base_dir / f"images{i}"
3434
image_dir.mkdir()
3535
for n in range(50):
36-
a = numpy.random.rand(1900, 1900, 3) * 255 # noqa: NPY002
36+
a = numpy.random.rand(900, 900, 3) * 255 # noqa: NPY002
3737
im_out = Image.fromarray(a.astype("uint8")).convert("RGB")
3838
image_path = image_dir / f"out{n}.jpg"
3939
im_out.save(image_path)

packages/service-library/tests/archiving_utils/test_archiving__interface_7zip.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,46 @@ def unpacked_archive(tmp_path: Path) -> Path:
2626

2727

2828
@pytest.fixture
29-
def compress_stdout(package_tests_dir: Path) -> list[str]:
30-
path = package_tests_dir / "data" / "archive_utils" / "compress_stdout.json"
29+
def data_archive_utils(package_tests_dir: Path) -> Path:
30+
path = package_tests_dir / "data" / "archive_utils"
3131
assert path.exists()
32-
return json.loads(path.read_text())
33-
34-
35-
@pytest.fixture
36-
def decompress_stdout(package_tests_dir: Path) -> list[str]:
37-
path = package_tests_dir / "data" / "archive_utils" / "decompress_stdout.json"
38-
assert path.exists()
39-
return json.loads(path.read_text())
40-
41-
42-
async def test_compress_progress_parser(compress_stdout: list[str]):
43-
detected_entries: list[NonNegativeInt] = []
44-
45-
async def progress_handler(byte_progress: NonNegativeInt) -> None:
46-
detected_entries.append(byte_progress)
47-
48-
parser = ProgressParser(progress_handler)
49-
for chunk in compress_stdout:
50-
await parser.parse_chunk(chunk)
32+
assert path.is_dir()
33+
return path
5134

52-
print(detected_entries)
53-
assert sum(detected_entries) == 434866026
5435

36+
@pytest.mark.parametrize(
37+
"progress_stdout, expected_size",
38+
[
39+
("compress_stdout.json", 434866026),
40+
("decompress_stdout.json", 434902745),
41+
],
42+
)
43+
async def test_compress_progress_parser(
44+
data_archive_utils: Path, progress_stdout: str, expected_size: NonNegativeInt
45+
):
46+
stdout_path = data_archive_utils / progress_stdout
47+
assert stdout_path.exists()
48+
stdout_entries: list[str] = json.loads(stdout_path.read_text())
5549

56-
# TODO: unify these 2 tests since they just use some ["compress_stdout.json", "decompress_stdout.json"] and expected sizes at the end of the day
57-
async def test_decompress_progress_parser(decompress_stdout: list[str]):
5850
detected_entries: list[NonNegativeInt] = []
59-
# TODO: als an expected length of [detected_entries] would be ideal to make sure all 100% entries are found
6051

6152
async def progress_handler(byte_progress: NonNegativeInt) -> None:
6253
detected_entries.append(byte_progress)
6354

6455
parser = ProgressParser(progress_handler)
65-
for chunk in decompress_stdout:
56+
for chunk in stdout_entries:
6657
await parser.parse_chunk(chunk)
6758

6859
print(detected_entries)
69-
assert sum(detected_entries) == 434902745
60+
assert sum(detected_entries) == expected_size
7061

7162

72-
async def test_something(
73-
mixed_file_types: Path, archive_path: Path, unpacked_archive: Path
63+
@pytest.mark.parametrize("compress", [True, False])
64+
async def test_archive_unarchive(
65+
mixed_file_types: Path, archive_path: Path, unpacked_archive: Path, compress: bool
7466
):
7567
await archive_dir(
76-
mixed_file_types, archive_path, compress=True, store_relative_path=True
68+
mixed_file_types, archive_path, compress=compress, store_relative_path=True
7769
)
7870

7971
await unarchive_dir(archive_path, unpacked_archive)

packages/service-library/tests/archiving_utils/test_archiving_utils.py

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212
import tempfile
1313
from collections.abc import Callable, Iterable, Iterator
1414
from concurrent.futures import ProcessPoolExecutor
15-
from dataclasses import dataclass
1615
from pathlib import Path
1716

1817
import pytest
1918
from faker import Faker
20-
from helpers import print_tree
2119
from pydantic import ByteSize, TypeAdapter
2220
from pytest_benchmark.plugin import BenchmarkFixture
2321
from servicelib import archiving_utils
@@ -80,24 +78,6 @@ def get_dirs_and_subdris_in_path(path_to_scan: Path) -> list[Path]:
8078
yield temp_dir_path
8179

8280

83-
@pytest.fixture
84-
def exclude_patterns_validation_dir(tmp_path: Path, faker: Faker) -> Path:
85-
"""Directory with well known structure"""
86-
base_dir = tmp_path / "exclude_patterns_validation_dir"
87-
base_dir.mkdir()
88-
(base_dir / "empty").mkdir()
89-
(base_dir / "d1").mkdir()
90-
(base_dir / "d1" / "f1").write_text(faker.text())
91-
(base_dir / "d1" / "f2.txt").write_text(faker.text())
92-
(base_dir / "d1" / "sd1").mkdir()
93-
(base_dir / "d1" / "sd1" / "f1").write_text(faker.text())
94-
(base_dir / "d1" / "sd1" / "f2.txt").write_text(faker.text())
95-
96-
print("exclude_patterns_validation_dir ---")
97-
print_tree(base_dir)
98-
return base_dir
99-
100-
10181
def __raise_error(*arts, **kwargs) -> None:
10282
msg = "raised as requested"
10383
raise ArchiveError(msg)
@@ -429,101 +409,6 @@ def _create_file(file_name: str, content: str) -> None:
429409
}
430410

431411

432-
@dataclass(frozen=True)
433-
class ExcludeParams:
434-
exclude_patterns: set[str]
435-
expected_result: set[Path]
436-
437-
438-
# + /exclude_patterns_validation_dir
439-
# + empty
440-
# + d1
441-
# - f2.txt
442-
# + sd1
443-
# - f2.txt
444-
# - f1
445-
# - f1
446-
@pytest.mark.parametrize(
447-
"params",
448-
[
449-
ExcludeParams(
450-
exclude_patterns={"/d1*"},
451-
expected_result=EMPTY_SET,
452-
),
453-
ExcludeParams(
454-
exclude_patterns={"/d1/sd1*"},
455-
expected_result={
456-
Path("d1/f2.txt"),
457-
Path("d1/f1"),
458-
},
459-
),
460-
ExcludeParams(
461-
exclude_patterns={"d1*"},
462-
expected_result=EMPTY_SET,
463-
),
464-
ExcludeParams(
465-
exclude_patterns={"*d1*"},
466-
expected_result=EMPTY_SET,
467-
),
468-
ExcludeParams(
469-
exclude_patterns={"*.txt"},
470-
expected_result={
471-
Path("d1/f1"),
472-
Path("d1/sd1/f1"),
473-
},
474-
),
475-
ExcludeParams(
476-
exclude_patterns={"/absolute/path/does/not/exist*"},
477-
expected_result=ALL_ITEMS_SET,
478-
),
479-
ExcludeParams(
480-
exclude_patterns={"/../../this/is/ignored*"},
481-
expected_result=ALL_ITEMS_SET,
482-
),
483-
ExcludeParams(
484-
exclude_patterns={"*relative/path/does/not/exist"},
485-
expected_result=ALL_ITEMS_SET,
486-
),
487-
],
488-
)
489-
async def test_archive_unarchive_check_exclude(
490-
params: ExcludeParams,
491-
exclude_patterns_validation_dir: Path,
492-
tmp_path: Path,
493-
):
494-
temp_dir_one = tmp_path / "one"
495-
temp_dir_two = tmp_path / "two"
496-
497-
temp_dir_one.mkdir()
498-
temp_dir_two.mkdir()
499-
500-
archive_file = temp_dir_one / "archive.zip"
501-
502-
# make exclude_patterns work relative to test directory
503-
exclude_patterns = {
504-
f"{exclude_patterns_validation_dir}/{x.strip('/') if x.startswith('/') else x}"
505-
for x in params.exclude_patterns
506-
}
507-
508-
await archive_dir(
509-
dir_to_compress=exclude_patterns_validation_dir,
510-
destination=archive_file,
511-
store_relative_path=True,
512-
compress=False,
513-
exclude_patterns=exclude_patterns,
514-
)
515-
516-
unarchived_paths: set[Path] = await unarchive_dir(
517-
archive_to_extract=archive_file, destination_folder=temp_dir_two
518-
)
519-
520-
relative_unarchived_paths = {x.relative_to(temp_dir_two) for x in unarchived_paths}
521-
522-
assert (
523-
relative_unarchived_paths == params.expected_result
524-
), f"Exclude rules: {exclude_patterns=}"
525-
526-
527412
async def test_unarchive_dir_raises_error(
528413
zipfile_single_file_extract_worker_raises_error: None,
529414
dir_with_random_content: Path,

packages/service-library/tests/archiving_utils/test_archiving_utils_extra.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ async def test_override_and_prune_from_archive(
133133
compress: bool,
134134
):
135135
download_file = tmp_path / "download.zip"
136-
expected_paths = {
137-
p.relative_to(new_state_dir)
138-
for p in new_state_dir.rglob("*")
139-
if is_leaf_path(p)
140-
}
141136

142137
# archive new_state_dir -> download.zip
143138
await archive_dir(

0 commit comments

Comments
 (0)