Skip to content

Fix copy_to and move_to #1744

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

Merged
merged 10 commits into from
Feb 24, 2025
10 changes: 8 additions & 2 deletions pyiron_base/project/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ def copy_to(
if "_hdf5" not in sub_project_name:
sub_project = self.open(sub_project_name)
destination_sub_project = destination.open(sub_project_name)
sub_project.copy_to(destination_sub_project)
sub_project.copy_to(
destination_sub_project, delete_original_data=delete_original_data
)
for job_id in tqdm(self.get_job_ids(recursive=False), desc="Copying jobs"):
ham = self.load(job_id)
if delete_original_data:
Expand All @@ -275,11 +277,15 @@ def copy_to(
if delete_original_data:
for file in tqdm(self.list_files(), desc="Moving files"):
shutil.move(os.path.join(self.path, file), destination.path)

self.removedirs()
else:
for file in tqdm(self.list_files(), desc="Copying files"):
if ".h5" not in file:
shutil.copy(os.path.join(self.path, file), destination.path)
if self._data is not None:
shutil.copy(
os.path.join(self.path, "project_data.h5"), destination.path
)
return destination

def create_from_job(self, job_old: "GenericJob", new_job_name: str) -> "GenericJob":
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/project/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,48 @@ def test_create_job_name(self):
)


class TestProjectMove(TestWithFilledProject):
def test_copy_to(self):
with self.subTest("copy_to destination project"):
# cp project destination creating a reference_pr
destination_pr = self.project.parent_group.open("destination")
self.assertEqual(len(destination_pr.list_groups()), 0)
self.assertEqual(len(destination_pr.list_nodes()), 0)
self.assertEqual(len(destination_pr.list_files()), 0)
self.project.copy_to(destination_pr)
self.assertEqual(self.project.list_groups(), destination_pr.list_groups())
self.assertEqual(self.project.list_nodes(), destination_pr.list_nodes())
self.assertEqual(self.project.list_files(), destination_pr.list_files())
reference_pr = destination_pr
with self.subTest("copy_to with delete_original_data"):
destination_pr = self.project.parent_group.open("destination2")
self.assertEqual(len(destination_pr.list_groups()), 0)
self.assertEqual(len(destination_pr.list_nodes()), 0)
self.assertEqual(len(destination_pr.list_files()), 0)
self.project.copy_to(destination_pr, delete_original_data=True)
self.assertEqual(reference_pr.list_groups(), destination_pr.list_groups())
self.assertEqual(reference_pr.list_nodes(), destination_pr.list_nodes())
self.assertEqual(reference_pr.list_files(), destination_pr.list_files())
self.assertFalse(os.path.exists(self.project_path))
with self.subTest("destination2 move_to old project"):
destination_pr.move_to(self.project)
self.assertEqual(self.project.list_groups(), reference_pr.list_groups())
self.assertEqual(self.project.list_nodes(), reference_pr.list_nodes())
self.assertEqual(self.project.list_files(), reference_pr.list_files())
self.assertFalse(os.path.exists(destination_pr.path))
with self.subTest("copy_to with project data"):
destination_pr = self.project.parent_group.open("destination3")
self.project.data["foo"] = 42
self.project.data.write()
self.project.copy_to(destination_pr)
self.assertEqual(self.project.list_groups(), destination_pr.list_groups())
self.assertEqual(self.project.list_nodes(), destination_pr.list_nodes())
self.assertEqual(self.project.list_files(), destination_pr.list_files())
destination_pr.data.read()
self.assertEqual(destination_pr.data["foo"], 42)
destination_pr.remove(enable=True)


class TestProjectOperations(TestWithFilledProject):
@unittest.skipIf(
pint_not_available,
Expand Down
Loading