-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fix overwrite directories #86
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
670e9af
Moved copying data to registrar_util. Overwriting directories now works
stuartmcalpine 807c33f
Update the docs for overwriting datasets
stuartmcalpine e2b1752
Add saftey when overwirting datasets, now a backup is created, the co…
stuartmcalpine 74eb768
Make sure the entry in the database is successful before copying any …
stuartmcalpine d8ce32c
Address reviewer comments
stuartmcalpine 080f262
Black formatting
stuartmcalpine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pytest | ||
import os | ||
from dataregistry.registrar_util import _copy_data | ||
|
||
|
||
@pytest.fixture | ||
def dummy_file(tmp_path): | ||
"""Create some dummy (temporary) files and directories""" | ||
|
||
# Root temp source dir for files | ||
tmp_src_dir = tmp_path / "source" | ||
tmp_src_dir.mkdir() | ||
|
||
# Make a dummy file | ||
p = tmp_src_dir / "dummy_standalone_file.txt" | ||
p.write_text("dummy stand alone file") | ||
|
||
# Make a dummy directory with a file in it | ||
p = tmp_src_dir / "tmpdir" / "tmpdir2" | ||
p.mkdir(parents=True) | ||
q = p / "dummy_file_within_folder.txt" | ||
q.write_text("dummy file within folder") | ||
|
||
# Root temp dest dir to copy into | ||
tmp_dest_dir = tmp_path / "dest" | ||
tmp_dest_dir.mkdir() | ||
|
||
return tmp_src_dir, tmp_dest_dir | ||
|
||
|
||
def test_copy_data(dummy_file): | ||
""" | ||
Test copying files and directories | ||
|
||
Each test is looped twice, the 2nd emulating overwriting a dataset. | ||
""" | ||
|
||
tmp_src_dir, tmp_dest_dir = dummy_file | ||
|
||
# Copy a single file from source to destination | ||
for i in range(2): | ||
_copy_data( | ||
"file", | ||
tmp_src_dir / "dummy_standalone_file.txt", | ||
tmp_dest_dir / "dummy_standalone_file.txt", | ||
) | ||
|
||
p = tmp_dest_dir / "dummy_standalone_file.txt" | ||
assert os.path.isfile(p) | ||
assert p.read_text() == "dummy stand alone file" | ||
|
||
# Copy a single directory from source to destination | ||
for i in range(2): | ||
_copy_data("directory", tmp_src_dir / "tmpdir", tmp_dest_dir / "tmpdir") | ||
|
||
assert os.path.isdir(tmp_dest_dir / "tmpdir") | ||
assert os.path.isdir(tmp_dest_dir / "tmpdir" / "tmpdir2") | ||
p = tmp_dest_dir / "tmpdir" / "tmpdir2" / "dummy_file_within_folder.txt" | ||
assert os.path.isfile(p) | ||
assert p.read_text() == "dummy file within folder" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If anything goes wrong with the subsequent copy, the old dataset will be gone and it won't be possible to recover the old state. It would be safer to
mv
the old dataset somewhere (e.g. to something with a path similar to the true destination, but perhaps starting with a.
or with strange characters - emacs uses#
at the beginning and end of the filename - strategically placed), copy in the new data, then if that succeeds do rmtree.If we're feeling really paranoid, the rmtree shouldn't happen until after the db row is successfully created. And for datasets which are just files we should also do just a
mv
initially to a similar name, thenrm
after the database entry is made.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed it as suggested.
When the destination exists, mv it to a backup, copy the data, then delete the backup if successful. If the
try
fails, mv the backup back to the original.If its an individual file being ingested, a checksum is also performed.
I think putting the entire database entry in a try would be a bit cumbersome, to check if the entry is also sucessful before deleting the backup.