mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
fixes saltstack/salt#62986 fix file.tidied FileNotFoundError
This commit is contained in:
parent
20bbbf88ba
commit
42e6297433
3 changed files with 72 additions and 40 deletions
1
changelog/62986.fixed
Normal file
1
changelog/62986.fixed
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Fix file.tidied FileNotFoundError
|
|
@ -2102,49 +2102,52 @@ def tidied(
|
||||||
mysize = 0
|
mysize = 0
|
||||||
deleteme = True
|
deleteme = True
|
||||||
path = os.path.join(root, elem)
|
path = os.path.join(root, elem)
|
||||||
if os.path.islink(path):
|
try:
|
||||||
# Get timestamp of symlink (not symlinked file)
|
if os.path.islink(path):
|
||||||
if time_comparison == "ctime":
|
# Get timestamp of symlink (not symlinked file)
|
||||||
mytimestamp = os.lstat(path).st_ctime
|
if time_comparison == "ctime":
|
||||||
elif time_comparison == "mtime":
|
mytimestamp = os.lstat(path).st_ctime
|
||||||
mytimestamp = os.lstat(path).st_mtime
|
elif time_comparison == "mtime":
|
||||||
|
mytimestamp = os.lstat(path).st_mtime
|
||||||
|
else:
|
||||||
|
mytimestamp = os.lstat(path).st_atime
|
||||||
else:
|
else:
|
||||||
mytimestamp = os.lstat(path).st_atime
|
# Get timestamp of file or directory
|
||||||
else:
|
if time_comparison == "ctime":
|
||||||
# Get timestamp of file or directory
|
mytimestamp = os.path.getctime(path)
|
||||||
if time_comparison == "ctime":
|
elif time_comparison == "mtime":
|
||||||
mytimestamp = os.path.getctime(path)
|
mytimestamp = os.path.getmtime(path)
|
||||||
elif time_comparison == "mtime":
|
else:
|
||||||
mytimestamp = os.path.getmtime(path)
|
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
|
||||||
|
else:
|
||||||
|
compare_age_size = mysize >= size
|
||||||
|
elif age_size_logical_operator.upper() == "AND":
|
||||||
|
compare_age_size = mysize >= size and myage.days >= age
|
||||||
else:
|
else:
|
||||||
mytimestamp = os.path.getatime(path)
|
compare_age_size = mysize >= size or myage.days >= age
|
||||||
|
|
||||||
if elem in dirs:
|
if compare_age_size and _matches(name=filename) and deleteme:
|
||||||
# Check if directories should be deleted at all
|
todelete.append(path)
|
||||||
deleteme = rmdirs
|
except FileNotFoundError:
|
||||||
else:
|
continue
|
||||||
# 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 or myage.days >= age
|
|
||||||
|
|
||||||
if compare_age_size and _matches(name=filename) and deleteme:
|
|
||||||
todelete.append(path)
|
|
||||||
|
|
||||||
# Now delete the stuff
|
# Now delete the stuff
|
||||||
if todelete:
|
if todelete:
|
||||||
|
|
|
@ -550,3 +550,31 @@ def test_tidied_age_size_args_AND_operator_size_and_age():
|
||||||
}
|
}
|
||||||
assert ret == exp
|
assert ret == exp
|
||||||
assert remove.call_count == 3
|
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue