Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for the pack() and unpack() method #1401

Merged
merged 11 commits into from
May 23, 2024
4 changes: 0 additions & 4 deletions pyiron_base/project/archiving/export_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ def copy_files_to_archive(
if not compressed:
compressed = True

if directory_to_transfer[-1] != "/":
directory_to_transfer = os.path.basename(directory_to_transfer)
else:
directory_to_transfer = os.path.basename(directory_to_transfer[:-1])
# print("directory to transfer: "+directory_to_transfer)
if not copy_all_files:
pfi = PyFileIndex(path=directory_to_transfer, filter_function=filter_function)
Expand Down
31 changes: 24 additions & 7 deletions pyiron_base/project/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,34 +1833,51 @@ def pack(
compress (bool): if true, the function will compress the destination_path to a tar.gz file.
copy_all_files (bool):
"""
directory_to_transfer = os.path.basename(self.path[:-1])
if destination_path == directory_to_transfer:
destination_path_abs = os.path.abspath(destination_path)
directory_to_transfer = os.path.dirname(self.path)
csv_file_path = os.path.join(
os.path.dirname(destination_path_abs), csv_file_name
)
if destination_path_abs == directory_to_transfer:
raise ValueError(
"The destination_path cannot have the same name as the project to compress."
)
export_archive.copy_files_to_archive(
directory_to_transfer,
destination_path,
destination_path_abs,
compressed=compress,
copy_all_files=copy_all_files,
)
df = export_archive.export_database(
self, directory_to_transfer, destination_path
self, directory_to_transfer, destination_path_abs
)
df.to_csv(csv_file_name)
df.to_csv(csv_file_path)

def unpack(self, origin_path, csv_file_name="export.csv", compress=True):
"""
by this function, job table is imported from a given csv file,
and also the content of project directory is copied from a given path

Args:
origin_path (str): the relative path of a directory (or a compressed file without the tar.gz exention)
origin_path (str): the relative path of a directory (or a compressed file without the tar.gz extension)
from which the project directory is copied.
csv_file_name (str): the csv file from which the job_table is copied to the current project
compress (bool): if True, it looks for a compressed file
"""
csv_path = csv_file_name
if isinstance(origin_path, Project):
origin_path = origin_path.path
csv_path_origin = os.path.join(os.path.dirname(origin_path), csv_file_name)
csv_path_project = os.path.join(self.path, csv_file_name)
if os.path.exists(csv_file_name):
csv_path = os.path.abspath(csv_file_name)
elif os.path.exists(csv_path_origin):
csv_path = csv_path_origin
elif os.path.exists(csv_path_project):
csv_path = csv_path_project
else:
raise FileNotFoundError(
f"File: {csv_file_name} was not found. Looked for {os.path.abspath(csv_file_name)}, {csv_path_origin} and {csv_path_project}."
)
df = pandas.read_csv(csv_path, index_col=0)
import_archive.import_jobs(
self, archive_directory=origin_path, df=df, compressed=compress
Expand Down
2 changes: 1 addition & 1 deletion tests/archiving/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_content(self):

def test_export_with_targz_extension(self):
os.makedirs(os.path.join(os.curdir, "tmp"))
tmp_path = os.path.join(os.curdir, "tmp")
tmp_path = os.path.abspath(os.path.join(os.curdir, "tmp"))
tar_arch = self.arch_dir_comp + ".tar.gz"
self.pr.pack(
destination_path=os.path.join(tmp_path, tar_arch),
Expand Down
6 changes: 6 additions & 0 deletions tests/archiving/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def test_import_csv(self):
df_original["hamversion"] = float(df_original["hamversion"])
assert_frame_equal(df_original, df_import)

def test_non_existing_unpack(self):
with self.assertRaises(FileNotFoundError):
self.imp_pr.unpack(
origin_path=self.arch_dir_comp, csv_file_name="nofile.csv"
)

def test_import_compressed(self):
path_original = self.pr.path
path_import = self.imp_pr.path
Expand Down
Loading