From 04ab81357e38515cc5caf5ac9e2468c1d0cede2a Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Thu, 28 Mar 2024 09:26:55 -0500 Subject: [PATCH 1/8] use absolute path for csv file export --- pyiron_base/project/generic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyiron_base/project/generic.py b/pyiron_base/project/generic.py index 0631f5cf8..0544dd584 100644 --- a/pyiron_base/project/generic.py +++ b/pyiron_base/project/generic.py @@ -1835,6 +1835,7 @@ def pack( copy_all_files (bool): """ directory_to_transfer = os.path.basename(self.path[:-1]) + csv_file_path = os.path.join(os.path.dirname(destination_path), csv_file_name) if destination_path == directory_to_transfer: raise ValueError( "The destination_path cannot have the same name as the project to compress." @@ -1848,7 +1849,7 @@ def pack( df = export_archive.export_database( self, directory_to_transfer, destination_path ) - df.to_csv(csv_file_name) + df.to_csv(csv_file_path) def unpack(self, origin_path, csv_file_name="export.csv", compress=True): """ From 9fc913b5d1f46acf5df007e025de36d0cc5efb52 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Thu, 28 Mar 2024 09:30:30 -0500 Subject: [PATCH 2/8] fix docstring --- pyiron_base/project/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyiron_base/project/generic.py b/pyiron_base/project/generic.py index 0544dd584..c722d97a4 100644 --- a/pyiron_base/project/generic.py +++ b/pyiron_base/project/generic.py @@ -1857,7 +1857,7 @@ def unpack(self, origin_path, csv_file_name="export.csv", compress=True): 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 From 522113a2d8ed3e4394b9944e636fa62e67893043 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Thu, 28 Mar 2024 09:40:28 -0500 Subject: [PATCH 3/8] Add an exception if the unpack csv file is not found. --- pyiron_base/project/generic.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pyiron_base/project/generic.py b/pyiron_base/project/generic.py index c722d97a4..75cabf31a 100644 --- a/pyiron_base/project/generic.py +++ b/pyiron_base/project/generic.py @@ -1862,7 +1862,18 @@ def unpack(self, origin_path, csv_file_name="export.csv", compress=True): 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 + 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 From ce122c999bb552706f8a18fe48e5ecdbaf91b4fd Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Thu, 28 Mar 2024 09:42:51 -0500 Subject: [PATCH 4/8] Add unit test --- tests/archiving/test_export.py | 1 - tests/archiving/test_import.py | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/archiving/test_export.py b/tests/archiving/test_export.py index 223ca740f..ef60aa0ed 100644 --- a/tests/archiving/test_export.py +++ b/tests/archiving/test_export.py @@ -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 diff --git a/tests/archiving/test_import.py b/tests/archiving/test_import.py index bc5bf2423..13ea64f10 100644 --- a/tests/archiving/test_import.py +++ b/tests/archiving/test_import.py @@ -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 From 912cf1d3ec32fb488f0f15b914004f8b5790cfdb Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Thu, 28 Mar 2024 09:55:08 -0500 Subject: [PATCH 5/8] Use absolute paths rather than relative paths --- pyiron_base/project/archiving/export_archive.py | 4 ---- pyiron_base/project/generic.py | 11 ++++++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/pyiron_base/project/archiving/export_archive.py b/pyiron_base/project/archiving/export_archive.py index 9a03eacc4..9b5237f09 100644 --- a/pyiron_base/project/archiving/export_archive.py +++ b/pyiron_base/project/archiving/export_archive.py @@ -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) diff --git a/pyiron_base/project/generic.py b/pyiron_base/project/generic.py index 75cabf31a..58acd73ab 100644 --- a/pyiron_base/project/generic.py +++ b/pyiron_base/project/generic.py @@ -1834,20 +1834,21 @@ 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]) - csv_file_path = os.path.join(os.path.dirname(destination_path), csv_file_name) - 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_path) From 3649010edafd86fd541904c7c46d817938e40015 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Thu, 28 Mar 2024 09:58:48 -0500 Subject: [PATCH 6/8] black formatting --- pyiron_base/project/generic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyiron_base/project/generic.py b/pyiron_base/project/generic.py index 58acd73ab..cc30573e6 100644 --- a/pyiron_base/project/generic.py +++ b/pyiron_base/project/generic.py @@ -1836,7 +1836,9 @@ def pack( """ 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) + 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." From 2b538dd4cfb88eeee50685183b4488357536ea4e Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Thu, 28 Mar 2024 10:35:30 -0500 Subject: [PATCH 7/8] test fixes --- pyiron_base/project/generic.py | 2 ++ tests/archiving/test_export.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pyiron_base/project/generic.py b/pyiron_base/project/generic.py index cc30573e6..a17a30bad 100644 --- a/pyiron_base/project/generic.py +++ b/pyiron_base/project/generic.py @@ -1865,6 +1865,8 @@ def unpack(self, origin_path, csv_file_name="export.csv", compress=True): 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 """ + 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): diff --git a/tests/archiving/test_export.py b/tests/archiving/test_export.py index ef60aa0ed..45fe528d0 100644 --- a/tests/archiving/test_export.py +++ b/tests/archiving/test_export.py @@ -75,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) From 203f3409abb280ce888910e63046ebca04f1eac9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 23 May 2024 12:00:41 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/archiving/test_import.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/archiving/test_import.py b/tests/archiving/test_import.py index 886782197..f2faba72b 100644 --- a/tests/archiving/test_import.py +++ b/tests/archiving/test_import.py @@ -60,7 +60,9 @@ def test_import_csv(self): 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") + 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