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

Move files from temporary directory changer #2022

Merged
merged 16 commits into from
Nov 26, 2024
Merged

Move files from temporary directory changer #2022

merged 16 commits into from
Nov 26, 2024

Conversation

mgjarrett
Copy link
Contributor

@mgjarrett mgjarrett commented Nov 26, 2024

What is the change?

Use shutil.move / mv instead of shutil.copy / cp to move files from temporary directory back to the original working directory on __exit__.

Why is the change being made?

Copying files from one location to another keeps the original file intact, but the original files in a temporary directory do not need to be kept around after the __exit__ from the TemporaryDirectoryChanger. These files are immediately deleted after the copy operation is completed.

Using shutil.move() is significantly faster than shutil.copy() when the source and destination locations are on the same drive, since the data doesn't have to physically be copied to a new place in memory.


Checklist

  • The release notes have been updated if necessary.
  • The documentation is still up-to-date in the doc folder.
  • The dependencies are still up-to-date in pyproject.toml.

@mgjarrett mgjarrett self-assigned this Nov 26, 2024
@mgjarrett mgjarrett added the enhancement New feature or request label Nov 26, 2024
@mgjarrett mgjarrett changed the title Move files temp dir Move files from temporary directory changer Nov 26, 2024
Copy link
Member

@opotowsky opotowsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the move doesn't work with windows/mac.

But I'd like to make a higher level comment. The secret overwriting of shutil.copy is something I want to remove from ARMI (i.e., I want to go change all the instances of shutil.copy to explicitly use safeCopy instead, then remove the overwrite. See #1690.

Maybe for this update it makes sense to keep the same scheme and we just tack this onto that tech debt payoff since it won't really be causing any extra work.

But I'd also be happy if the shutil.move calls were just safeMove instead. :-)

@mgjarrett
Copy link
Contributor Author

mgjarrett commented Nov 26, 2024

I didn't touch safeCopy at all, but I added some assert statements to verify that the safeCopy call is actually copying the files. The unit test for safeCopy passes locally for me, but here it is now failing on those assertions, which is strange.

It's also weird that the existing unit test of safeCopy doesn't actually assert that the files are copied successfully. Makes me wonder if there is a known issue here.

Edit: It looks like the problem was the "windows-like" file path dir1\\file2.txt, which doesn't get picked up by safeCopy. I removed the assertion of this file existing and now the test passes.

armi/utils/__init__.py Outdated Show resolved Hide resolved
Copy link
Member

@opotowsky opotowsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

@john-science john-science marked this pull request as ready for review November 26, 2024 21:27
@john-science
Copy link
Member

The secret overwriting of shutil.copy is something I want to remove from ARMI

I note we already have a ticket for this: #1690

But I think Michael's PR doesn't touch that, because he didn't alter safeCopy(), but added a new saveMove(). Am I wrong?

@opotowsky
Copy link
Member

The secret overwriting of shutil.copy is something I want to remove from ARMI

I note we already have a ticket for this: #1690

But I think Michael's PR doesn't touch that, because he didn't alter safeCopy(), but added a new saveMove(). Am I wrong?

Yep! the first iteration in this PR had a similar setup, (where shutil.move was overwritten with safeMove) but that was undone in favor of not overwriting shutil.move.

@opotowsky opotowsky merged commit 9d90012 into main Nov 26, 2024
22 checks passed
@opotowsky opotowsky deleted the moveFiles_tempDir branch November 26, 2024 23:20
drewj-tp added a commit that referenced this pull request Nov 27, 2024
…pin-dep

* origin/main:
  Moving from shutil.copy to safeCopy (#2024)
  Move files from temporary directory changer (#2022)
  Handling checking for OSs better (#2023)
  Expose some skip inspection options for `armi.init` and `db.loadOperator` (#2005)
  Adding setting to control MCNP / ENDF library (#1989)
  Improving logging on ISOTXS compare  (#2013)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants