Skip to content

Commit

Permalink
fixes #62986 fix file.tidied FileNotFoundError
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasmhughes authored and Megan Wilhite committed Nov 1, 2022
1 parent 20bbbf8 commit 42e6297
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 39 deletions.
1 change: 1 addition & 0 deletions changelog/62986.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix file.tidied FileNotFoundError
81 changes: 42 additions & 39 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2102,49 +2102,52 @@ def _matches(name):
mysize = 0
deleteme = True
path = os.path.join(root, elem)
if os.path.islink(path):
# Get timestamp of symlink (not symlinked file)
if time_comparison == "ctime":
mytimestamp = os.lstat(path).st_ctime
elif time_comparison == "mtime":
mytimestamp = os.lstat(path).st_mtime
else:
mytimestamp = os.lstat(path).st_atime
else:
# Get timestamp of file or directory
if time_comparison == "ctime":
mytimestamp = os.path.getctime(path)
elif time_comparison == "mtime":
mytimestamp = os.path.getmtime(path)
try:
if os.path.islink(path):
# Get timestamp of symlink (not symlinked file)
if time_comparison == "ctime":
mytimestamp = os.lstat(path).st_ctime
elif time_comparison == "mtime":
mytimestamp = os.lstat(path).st_mtime
else:
mytimestamp = os.lstat(path).st_atime
else:
mytimestamp = os.path.getatime(path)
# Get timestamp of file or directory
if time_comparison == "ctime":
mytimestamp = os.path.getctime(path)
elif time_comparison == "mtime":
mytimestamp = os.path.getmtime(path)
else:
mytimestamp = os.path.getatime(path)

if elem in dirs:
# Check if directories should be deleted at all
deleteme = rmdirs
else:
# Get size of regular file
mysize = os.path.getsize(path)

# Calculate the age and set the name to match
myage = abs(today - date.fromtimestamp(mytimestamp))
filename = elem
if full_path_match:
filename = path

# Verify against given criteria, collect all elements that should be removed
if age_size_only and age_size_only.lower() in ["age", "size"]:
if age_size_only.lower() == "age":
compare_age_size = myage.days >= age
if elem in dirs:
# Check if directories should be deleted at all
deleteme = rmdirs
else:
# Get size of regular file
mysize = os.path.getsize(path)

# Calculate the age and set the name to match
myage = abs(today - date.fromtimestamp(mytimestamp))
filename = elem
if full_path_match:
filename = path

# Verify against given criteria, collect all elements that should be removed
if age_size_only and age_size_only.lower() in ["age", "size"]:
if age_size_only.lower() == "age":
compare_age_size = myage.days >= age
else:
compare_age_size = mysize >= size
elif age_size_logical_operator.upper() == "AND":
compare_age_size = mysize >= size and myage.days >= age
else:
compare_age_size = mysize >= size
elif age_size_logical_operator.upper() == "AND":
compare_age_size = mysize >= size and myage.days >= age
else:
compare_age_size = mysize >= size or myage.days >= age
compare_age_size = mysize >= size or myage.days >= age

if compare_age_size and _matches(name=filename) and deleteme:
todelete.append(path)
if compare_age_size and _matches(name=filename) and deleteme:
todelete.append(path)
except FileNotFoundError:
continue

# Now delete the stuff
if todelete:
Expand Down
28 changes: 28 additions & 0 deletions tests/pytests/unit/states/file/test_tidied.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,31 @@ def test_tidied_age_size_args_AND_operator_size_and_age():
}
assert ret == exp
assert remove.call_count == 3


def test_tidied_filenotfound(tmp_path):
name = tmp_path / "not_found_test"
name.mkdir(parents=True, exist_ok=True)
name = str(tmp_path / "not_found_test")
walker = [
(os.path.join(name, "test1"), [], ["file1"]),
(os.path.join(name, "test2", "test3"), [], []),
(os.path.join(name, "test2"), ["test3"], ["file2"]),
(name, ["test1", "test2"], ["file3"]),
]
# mock the walk, but files aren't there
with patch("os.walk", return_value=walker), patch(
"os.path.islink", return_value=False
):
ret = filestate.tidied(
name=name,
age=1,
size=9,
)
exp = {
"name": name,
"changes": {},
"result": True,
"comment": "Nothing to remove from directory {}".format(name),
}
assert ret == exp

0 comments on commit 42e6297

Please sign in to comment.