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

Added option to use or disuse atime for directories #14

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "file_auto_expiry"
version = "0.0.8"
version = "0.0.9"
description = "WATCloud project containing scripts to check if directories / files are expired"
readme = "README.md"
requires-python = ">=3.7, <4"
Expand Down
5 changes: 3 additions & 2 deletions src/file_auto_expiry/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
app = typer.Typer()

@app.command()
def collect_file_info(path: str, save_file: str = "", days_for_expiry: int = 10):
def collect_file_info(path: str, save_file: str = "", days_for_expiry: int = 10, check_folder_atime: bool = False):
"""
Collects information about the top level paths within a given folder path
And dumps it into a json file, specified by the save_file flag
Expand All @@ -16,7 +16,8 @@ def collect_file_info(path: str, save_file: str = "", days_for_expiry: int = 10)
collect_expired_file_information(folder_path=path,
save_file=save_file,
scrape_time=scrape_time,
expiry_threshold=expiry_threshold)
expiry_threshold=expiry_threshold,
check_folder_atime=check_folder_atime)

@app.command()
def collect_creator_info(file_info: str, save_file: str = ""):
Expand Down
14 changes: 9 additions & 5 deletions src/file_auto_expiry/utils/expiry_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .file_creator import *
import datetime

def is_expired(path, expiry_threshold):
def is_expired(path, expiry_threshold, check_folder_atime):
""" Interface function to return if a file-structure is expired or not.
TODO: Provide implementation for character device files, blocks, sockets.
"""
Expand All @@ -16,7 +16,7 @@ def is_expired(path, expiry_threshold):
return is_expired_filepath(path, path_stat, expiry_threshold)

elif stat.S_ISDIR(path_stat.st_mode): # folder
return is_expired_folder(path, path_stat, expiry_threshold)
return is_expired_folder(path, path_stat, expiry_threshold, check_folder_atime)

elif stat.S_ISLNK(path_stat.st_mode): # symlink
return is_expired_link(path, path_stat, expiry_threshold)
Expand Down Expand Up @@ -82,7 +82,7 @@ def is_expired_link(path, file_stat, expiry_threshold):
expiry_threshold=expiry_threshold)


def is_expired_folder(folder_path, folder_stat, expiry_threshold):
def is_expired_folder(folder_path, folder_stat, expiry_threshold, check_folder_atime):
"""
Goes through all files in a folder. Returns true if ALL files in directory
are expire.
Expand All @@ -92,7 +92,10 @@ def is_expired_folder(folder_path, folder_stat, expiry_threshold):
"""
file_creators = set()
# timestamps for the folder itself
recent_atime = folder_stat.st_atime
recent_atime = 0
if check_folder_atime:
recent_atime = folder_stat.st_atime

recent_ctime = folder_stat.st_ctime
recent_mtime = folder_stat.st_mtime
folder_creator = get_file_creator(folder_path)
Expand All @@ -112,7 +115,8 @@ def is_expired_folder(folder_path, folder_stat, expiry_threshold):
continue

file_expiry_information = is_expired(path=str(member_file_path),
expiry_threshold=expiry_threshold)
expiry_threshold=expiry_threshold,
check_folder_atime=check_folder_atime)

if file_expiry_information.is_expired is False:
# if any file is not expired, then set the expiry flag to false
Expand Down
10 changes: 5 additions & 5 deletions src/file_auto_expiry/utils/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def notify_file_creators():
Currently is just the code to print information to a text file
"""

def scan_folder_for_expired(folder_path, expiry_threshold):
def scan_folder_for_expired(folder_path, expiry_threshold, check_folder_atime):
"""Generator function which iterates the expired top level folders
in a given directory.

Expand All @@ -44,13 +44,13 @@ def scan_folder_for_expired(folder_path, expiry_threshold):
for entry in os.scandir(dirfd):
entry_path = os.path.join(folder_path, entry.path)
if os.path.exists(entry_path):
expiry_result = is_expired(entry_path, expiry_threshold)
expiry_result = is_expired(entry_path, expiry_threshold, check_folder_atime)
# path, creator tuple (name, uid, gid), atime, ctime, mtime
yield entry_path, expiry_result.is_expired, expiry_result.creators, \
expiry_result.atime, expiry_result.ctime, expiry_result.mtime
os.close(dirfd)

def collect_expired_file_information(folder_path, save_file, scrape_time, expiry_threshold):
def collect_expired_file_information(folder_path, save_file, scrape_time, expiry_threshold, check_folder_atime):
"""
Interface function which collects which directories are 'expired'

Expand All @@ -70,12 +70,12 @@ def collect_expired_file_information(folder_path, save_file, scrape_time, expiry

path_info = dict()
for path, is_expired, creators, atime, ctime, mtime in scan_folder_for_expired(
folder_path, expiry_threshold):
folder_path, expiry_threshold, check_folder_atime):
# handles generating the dictionary

path_info[path] = {
"path": path, # storing pathname so we keep it when we transfer the dictionary to jsonl
"creators": [creator for creator in creators],
"creators": [creator for creator in creators if isinstance(creator[1], int) and creator[1] > 0 and creator[1] == creator[2]],
"expired": is_expired,
"time_variables": {
"atime_datetime": str(datetime.datetime.fromtimestamp(atime)),
Expand Down
Loading