Skip to content

Commit fd58947

Browse files
author
Andrei Neagu
committed
fixed issue with file listing
1 parent 805bd9e commit fd58947

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class CouldNotRunCommandError(ArchiveError):
1616
class TableHeaderNotFoundError(ArchiveError):
1717
msg_template = (
1818
"Excepted to detect a table header since files were detected "
19-
"lines_with_file_name='{lines_with_file_name}'. "
19+
"file_lines='{file_lines}'. "
2020
"Command output:\n{command_output}"
2121
)

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
_ALL_DONE_RE: Final[re.Pattern] = re.compile(r"Everything is Ok", re.IGNORECASE)
3939

4040
_TOKEN_TABLE_HEADER_START: Final[str] = "------------------- "
41-
_TOKEN_FILE_PERMISSIONS: Final[str] = " ..... "
4241

4342
_7ZIP_EXECUTABLE: Final[Path] = Path("/usr/bin/7z")
4443

@@ -250,24 +249,35 @@ async def _compressed_bytes(byte_progress: NonNegativeInt) -> None:
250249
await shutil_move(f"{destination}.zip", destination)
251250

252251

252+
def _is_folder(line: str) -> bool:
253+
folder_attribute = line[20]
254+
return folder_attribute == "D"
255+
256+
253257
def _extract_file_names_from_archive(command_output: str) -> set[str]:
254258
file_name_start: NonNegativeInt | None = None
255259

260+
entries_lines: list[str] = []
261+
can_add_to_entries: bool = False
262+
263+
# extract all lines containing files or folders
256264
for line in command_output.splitlines():
257265
if line.startswith(_TOKEN_TABLE_HEADER_START):
258266
file_name_start = line.rfind(" ") + 1
259-
break
267+
can_add_to_entries = not can_add_to_entries
268+
continue
269+
270+
if can_add_to_entries:
271+
entries_lines.append(line)
260272

261-
lines_with_file_name: list[str] = [
262-
line for line in command_output.splitlines() if _TOKEN_FILE_PERMISSIONS in line
263-
]
273+
file_lines: list[str] = [line for line in entries_lines if not _is_folder(line)]
264274

265-
if lines_with_file_name and file_name_start is None:
275+
if file_lines and file_name_start is None:
266276
raise TableHeaderNotFoundError(
267-
lines_with_file_name=lines_with_file_name, command_output=command_output
277+
file_lines=file_lines, command_output=command_output
268278
)
269279

270-
return {line[file_name_start:] for line in lines_with_file_name}
280+
return {line[file_name_start:] for line in file_lines}
271281

272282

273283
async def unarchive_dir(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,21 @@ async def test_archive_unarchive(
6666
mixed_file_types: Path, archive_path: Path, unpacked_archive: Path, compress: bool
6767
):
6868
await archive_dir(mixed_file_types, archive_path, compress=compress)
69+
await unarchive_dir(archive_path, unpacked_archive)
70+
6971

72+
@pytest.fixture
73+
def empty_folder(tmp_path: Path) -> Path:
74+
path = tmp_path / "empty_folder"
75+
path.mkdir()
76+
return path
77+
78+
79+
@pytest.mark.parametrize("compress", [True, False])
80+
async def test_archive_unarchive_empty_folder(
81+
empty_folder: Path, archive_path: Path, unpacked_archive: Path, compress: bool
82+
):
83+
await archive_dir(empty_folder, archive_path, compress=compress)
7084
await unarchive_dir(archive_path, unpacked_archive)
7185

7286

@@ -75,6 +89,8 @@ async def test_archive_unarchive(
7589
[
7690
("list_edge_case.txt", 3),
7791
("list_stdout.txt", 674),
92+
("list_broken_format.txt", 22),
93+
("list_empty_archive.txt", 0),
7894
],
7995
)
8096
def test__extract_file_names_from_archive(
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
7-Zip (z) 24.09 (x64) : Copyright (c) 1999-2024 Igor Pavlov : 2024-11-28
2+
64-bit locale=C.UTF-8 Threads:12 OPEN_MAX:1048576, ASM
3+
Scanning the drive for archives:
4+
1 file, 2089391770 bytes (1993 MiB)
5+
Listing archive: /tmp/simcorefiles/3f8abbcc-923b-429b-953f-827df68b715f/input_1/PromlematicZipFile.zip
6+
--
7+
Path = /tmp/simcorefiles/3f8abbcc-923b-429b-953f-827df68b715f/input_1/PromlematicZipFile.zip
8+
Type = zip
9+
Physical Size = 2089391770
10+
Date Time Attr Size Compressed Name
11+
------------------- ----- ------------ ------------ ------------------------
12+
2024-12-11 15:31:40 ....A 45388221 45392151 b0_pair.nii.gz
13+
2024-12-11 15:31:32 ....A 326 14 bvals
14+
2024-12-11 15:31:32 ....A 2610 1228 bvecs
15+
2024-12-11 15:31:52 ....A 192 111 diff2struct_fsl.mat
16+
2024-12-11 15:31:52 ....A 191 110 diff2struct_fsl_inverse.mat
17+
2024-12-11 15:31:52 ....A 848 367 diff2struct_mrtrix.txt
18+
2024-12-11 15:31:46 ....A 1124081680 1009101417 dwi_preproc.mif
19+
2024-12-11 15:31:52 ....A 1008525390 1008612664 dwi_preproc.nii.gz
20+
2024-12-11 15:31:52 ....A 154 118 electrode_coords_coreg.txt
21+
2024-12-11 15:31:52 ....A 57871 36566 mask_brain_coreg.nii.gz
22+
2024-12-11 15:31:52 ....A 16811 703 mask_lh_hippo_body_coreg.nii.gz
23+
2024-12-11 15:31:52 ....A 17304 1324 mask_lh_hippo_coreg.nii.gz
24+
2024-12-11 15:31:52 ....A 16903 821 mask_lh_hippo_head_coreg.nii.gz
25+
2024-12-11 15:31:52 ....A 16679 540 mask_lh_hippo_tail_coreg.nii.gz
26+
2024-12-11 15:31:52 ....A 16779 670 mask_rh_hippo_body_coreg.nii.gz
27+
2024-12-11 15:31:52 ....A 17232 1246 mask_rh_hippo_coreg.nii.gz
28+
2024-12-11 15:31:52 ....A 16840 752 mask_rh_hippo_head_coreg.nii.gz
29+
2024-12-11 15:31:52 ....A 16699 582 mask_rh_hippo_tail_coreg.nii.gz
30+
2024-12-11 15:31:38 ....A 15068179 15069543 mean_b0.nii.gz
31+
2024-12-11 11:49:22 ....A 54 54 simulation_parameters.txt
32+
2024-11-22 12:21:58 ....A 94 80 stimulation_parameters.txt
33+
2024-12-11 15:31:40 ....A 11166326 11168031 T1_coreg.nii.gz
34+
------------------- ----- ------------ ------------ ------------------------
35+
2024-12-11 15:31:52 2204427383 2089389092 22 files
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
7-Zip (z) 24.09 (x64) : Copyright (c) 1999-2024 Igor Pavlov : 2024-11-28
2+
64-bit locale=en_US.UTF-8 Threads:12 OPEN_MAX:1024, ASM
3+
4+
Scanning the drive for archives:
5+
1 file, 22 bytes (1 KiB)
6+
7+
Listing archive: /tmp/pytest-of-silenthk/pytest-692/test_archive_unarchive_empty_f1/mixed_types_dir.zip
8+
9+
--
10+
Path = /tmp/pytest-of-silenthk/pytest-692/test_archive_unarchive_empty_f1/mixed_types_dir.zip
11+
Type = zip
12+
Physical Size = 22
13+
14+
Date Time Attr Size Compressed Name
15+
------------------- ----- ------------ ------------ ------------------------
16+
------------------- ----- ------------ ------------ ------------------------
17+
0 0 0 files

0 commit comments

Comments
 (0)