Skip to content

Commit

Permalink
dev(narugo): fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Dec 28, 2023
1 parent 5bf5428 commit 0193df4
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 13 deletions.
14 changes: 11 additions & 3 deletions skpick/archive/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,26 @@ def get_archive_type(archive_file: str) -> str:
raise ValueError(f'Unknown type of archive file {archive_file!r}.')


def archive_unpack(archive_file: str, silent: bool = False):
def archive_unpack(archive_file: str, silent: bool = False, base_dir: str = '.'):
"""
Unpack an archive file into a directory using the specified archive type.
:param archive_file: The filename of the archive.
:type archive_file: str
:param silent: If True, suppress warnings during the unpacking process.
:type silent: bool
:param base_dir: Base directory on relative directory when yielded.
:type base_dir: str
:return: The path to the unpacked directory.
:rtype: str
"""
type_name = get_archive_type(archive_file)
_, fn_unpack = _KNOWN_ARCHIVE_TYPES[type_name]
yield from fn_unpack(archive_file, silent=silent)
for file, relpath in fn_unpack(archive_file, silent=silent):
full_relpath = os.path.join(base_dir, relpath)
try:
get_archive_type(file)
except ValueError:
yield file, full_relpath
else:
yield from archive_unpack(file, silent, full_relpath)
43 changes: 43 additions & 0 deletions test/archive/test_nested.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os.path
import shutil

import pytest
from hbutils.testing import isolated_directory, disable_output

from skpick.archive import archive_unpack
from test.testings import get_testfile


@pytest.fixture()
def raw_nested_zip():
return get_testfile('raw_nested.zip')


@pytest.fixture()
def raw_nested_x_7z():
return get_testfile('raw_nested_x.7z')


@pytest.mark.unittest
class TestArchiveNested:
def test_archive_unpack_nested(self, raw_nested_zip, check_unpack_nested_dir):
with isolated_directory():
with disable_output():
for file, relpath in archive_unpack(raw_nested_zip):
dst_file = relpath
if os.path.dirname(dst_file):
os.makedirs(os.path.dirname(dst_file), exist_ok=True)
shutil.copyfile(file, dst_file)

check_unpack_nested_dir('.')

def test_archive_unpack_nested_x(self, raw_nested_x_7z, check_unpack_nested_x_dir):
with isolated_directory():
with disable_output():
for file, relpath in archive_unpack(raw_nested_x_7z):
dst_file = relpath
if os.path.dirname(dst_file):
os.makedirs(os.path.dirname(dst_file), exist_ok=True)
shutil.copyfile(file, dst_file)

check_unpack_nested_x_dir('.')
37 changes: 27 additions & 10 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import os.path
import pathlib
import re

import pytest
from hbutils.random import random_sha1_with_timestamp

from hfutils.operate.base import get_hf_fs, get_hf_client
from hfutils.utils import walk_files

from test.testings import get_testfile, dir_compare


Expand All @@ -19,12 +16,32 @@ def raw_dir():
def check_unpack_dir(raw_dir):
def _check(directory):
dir_compare(raw_dir, directory)
# for file in walk_files(raw_dir):
# src_file = os.path.join(raw_dir, file)
# dst_file = os.path.join(directory, file)
# assert os.path.exists(dst_file), f'File {dst_file!r} not exists!'
# assert pathlib.Path(src_file).read_text().splitlines(keepends=False) == \
# pathlib.Path(dst_file).read_text().splitlines(keepends=False)

return _check


@pytest.fixture()
def raw_nested_dir():
return get_testfile('raw_nested')


@pytest.fixture()
def check_unpack_nested_dir(raw_nested_dir):
def _check(directory):
dir_compare(raw_nested_dir, directory)

return _check


@pytest.fixture()
def raw_nested_x_dir():
return get_testfile('raw_nested_x')


@pytest.fixture()
def check_unpack_nested_x_dir(raw_nested_x_dir):
def _check(directory):
dir_compare(raw_nested_x_dir, directory)

return _check

Expand Down
Binary file added test/testfile/raw_nested.zip
Binary file not shown.
1 change: 1 addition & 0 deletions test/testfile/raw_nested/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is 1.txt
3 changes: 3 additions & 0 deletions test/testfile/raw_nested/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
These are some files for testing the packages.

lol
1 change: 1 addition & 0 deletions test/testfile/raw_nested/raw.tar.gz/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is 1.txt
3 changes: 3 additions & 0 deletions test/testfile/raw_nested/raw.tar.gz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
These are some files for testing the packages.

lol
3 changes: 3 additions & 0 deletions test/testfile/raw_nested/raw.tar.gz/subdir/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

print(os.cpu_count())
3 changes: 3 additions & 0 deletions test/testfile/raw_nested/subdir/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

print(os.cpu_count())
Binary file added test/testfile/raw_nested_x.7z
Binary file not shown.
1 change: 1 addition & 0 deletions test/testfile/raw_nested_x/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is 1.txt
3 changes: 3 additions & 0 deletions test/testfile/raw_nested_x/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
These are some files for testing the packages.

lol
1 change: 1 addition & 0 deletions test/testfile/raw_nested_x/raw_nested.zip/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is 1.txt
3 changes: 3 additions & 0 deletions test/testfile/raw_nested_x/raw_nested.zip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
These are some files for testing the packages.

lol
1 change: 1 addition & 0 deletions test/testfile/raw_nested_x/raw_nested.zip/raw.tar.gz/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
These are some files for testing the packages.

lol
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

print(os.cpu_count())
3 changes: 3 additions & 0 deletions test/testfile/raw_nested_x/raw_nested.zip/subdir/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

print(os.cpu_count())
3 changes: 3 additions & 0 deletions test/testfile/raw_nested_x/subdir/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

print(os.cpu_count())

0 comments on commit 0193df4

Please sign in to comment.