Skip to content

Commit

Permalink
Make cleanPath work with Windows fast paths (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
youngmit committed Sep 9, 2021
1 parent f3ea290 commit 6efb915
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
22 changes: 15 additions & 7 deletions armi/utils/directoryChangers.py
Expand Up @@ -13,12 +13,14 @@
# limitations under the License.

import os
import pathlib
import random
import shutil
import string
import glob

import armi
from armi import context
from armi import runLog
from armi.utils import pathTools

Expand Down Expand Up @@ -194,14 +196,20 @@ def __init__(
self, root, filesToMove, filesToRetrieve, dumpOnException
)

# If no root dir is given, the default path to grab in context is cwd(), which
# can lead to deleting any directory on the hard drive. So this check is here
# to ensure that if we grab a path from context, it is a proper temp dir.
if not root:
# If no root dir is given, the default path comes from context.getFastPath, which
# *might* be relative to the cwd, making it possible to delete unintended files.
# So this check is here to ensure that if we grab a path from context, it is a
# proper temp dir.
# That said, since the TemporaryDirectoryChanger *always* responsible for
# creating its destination directory, it may always be safe to delete it
# regardless of location.
if root is None:
root = armi.context.getFastPath()
# ARMIs temp dirs are in an /.armi/ directory: validate this is a temp dir.
if ".armi" not in os.path.normpath(root).split(os.path.sep):
raise ValueError("Temporary directory not found.")
# ARMIs temp dirs are in an context.APP_DATA directory: validate this is a temp dir.
if pathlib.Path(context.APP_DATA) not in pathlib.Path(root).parents:
raise ValueError(
"Temporary directory not in a safe location for deletion."
)

# make the tmp dir, if necessary
if not os.path.exists(root):
Expand Down
10 changes: 5 additions & 5 deletions armi/utils/pathTools.py
Expand Up @@ -23,6 +23,7 @@
import pathlib
from time import sleep

from armi import context
from armi import runLog


Expand Down Expand Up @@ -239,14 +240,10 @@ def cleanPath(path):
"""
valid = False
if os.path.exists(path):
runLog.extra("Clearing all files in {}".format(path))
else:
runLog.extra("Nothing to clean in {}. Doing nothing. ".format(path))
if not os.path.exists(path):
return True

for validPath in [
".armi",
"armiruns",
"failedruns",
"mc2run",
Expand All @@ -258,6 +255,9 @@ def cleanPath(path):
if validPath in path.lower():
valid = True

if pathlib.Path(context.APP_DATA) in pathlib.Path(path).parents:
valid = True

if not valid:
raise Exception(
"You tried to delete {0}, but it does not seem safe to do so.".format(path)
Expand Down

0 comments on commit 6efb915

Please sign in to comment.