Skip to content

Commit

Permalink
Merge b8bbf87 into e50f885
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-janssen committed Apr 19, 2024
2 parents e50f885 + b8bbf87 commit e4b3106
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
4 changes: 0 additions & 4 deletions pyiron_base/project/archiving/export_archive.py
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
Expand Up @@ -1834,34 +1834,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
3 changes: 1 addition & 2 deletions tests/archiving/test_export.py
Expand Up @@ -35,7 +35,6 @@ def tearDownClass(cls) -> None:
uncompressed_pr.remove(enable=True, enforce=True)
os.remove('export.csv')


def test_exportedCSV(self):
# in the first test, the csv file from the packing function is read
# and is compared with the return dataframe from export_database
Expand Down Expand Up @@ -76,7 +75,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),
csv_file_name=os.path.join(tmp_path, 'exported.csv'), compress=True)
Expand Down
4 changes: 4 additions & 0 deletions tests/archiving/test_import.py
Expand Up @@ -59,6 +59,10 @@ 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

0 comments on commit e4b3106

Please sign in to comment.