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

Update Readme #2

Merged
merged 1 commit into from
Jun 26, 2024
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
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
# infra_file_auto_expiry
### Requirement
This project requires sudo priveleges to run

## Purpose
Relating to issue: https://github.com/WATonomous/infra-config/issues/1143

This project is meant to help automatically expire and delete files. It's currently at the stage of gathering all necessary information about file deletion easier. In the future, it is required to add a notification system for users whose files are to be deleted, and an actual deletion system.

Currently it moves through every single top level folder in a directory, and checks whether it is expired or not. This means that every single file in that directory tree must be expired. As it does this, it gathers all the users who created files in that directory, and the days since the most RECENT atime, ctime, and mtime of ANY file in that directory. It only collects these for folders which have been confirmed to be expired.

To collect the expiry information of all top level directories in a given path:
sudo $(which python3) /path_to_directory/infra_file_auto_expiry/infra_file_auto_expiry/source/main.py collect-file-info path_to_check_expiry_of
## Usage
There are two commands currently, which are associated with the first two stages of the project.
1. collect-file-info
2. collect-creator-info

This will return a jsonl file. You can then use this in the following command to tabulate all expired paths that are associated with a particular user.
### To collect the expiry information of all top level directories in a given path:
```
sudo $(which python3) /path_to_directory/infra_file_auto_expiry/infra_file_auto_expiry/source/main.py collect-file-info folder_path --days-for-expiry=5
```
Required flags:
folder_path : STRING | Path to folder to check expiry of
Optional flags:
--days-for-expiry=x : INT | The amount of days of non-usage that indicates any file is expired.

sudo $(which python3) /path_to_directory/infra_file_auto_expiry/infra_file_auto_expiry/source/main.py collect-creator-info path_to_jsonl_file
This will return a jsonl file of the following form. After the first two dictionary type objects, each dictionary contains the information of ONE top level folder inside of the folder_path flag. AKA. This program reports information by iterating through the top level folders, but checks expiry by entering recursively through each of those folders. Essentially, each dictionary is associated with a PATH, and all creators who are associated with said path.
```
{"scrape_time:": x, "scrape_time_datetime": "a datetime"}
{"time_for_scrape_sec": x, "time_for_scrape_min": x}
{"path": "",
"creators": [list of people who created any file in the folder],
"expired": Boolean whether the file is expired or not,
"time_variables": {"atime_datetime": "most recent access datetime",
"ctime_datetime": "most recent change datetime",
"mtime_datetime": "most recent modification datetime"}}
...
```

### You can then use this in the following command to tabulate all expired paths that are associated with a particular user.
```
sudo $(which python3) /path_to_directory/infra_file_auto_expiry/infra_file_auto_expiry/source/main.py collect-creator-info path_to_jsonl_file
```
Required flags:
path_to_jsonl : STRING | Should be a file that is from calling collect-file-info.

Optional flags:
--save-file="" : SRING | A path to a jsonl file to save the collected information to. If this is undefined, then the program will just generate a filename and save it to the working directory.

This command will return a file of the following form. Essentially, each dictionary is associated with a USER, and all paths that are associated with said user.
```
{"scrape_time:": x, "scrape_time_datetime": "a datetime"}
{"paths": {"path1": {dictionary of it's atime, ctime, mtime datetimes},
"path2": {dictionary of it's atime, ctime, mtime datetimes},
"pathx": {dictionary of it's atime, ctime, mtime datetimes}}, "name": "username", "uid": "user id", "gid": "user global id"}
...
```
3 changes: 1 addition & 2 deletions source/utils/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def scan_folder_for_expired(folder_path, expiry_threshold):
for entry in os.scandir(folder_path):
if os.path.exists(entry.path):
expiry_result = is_expired(entry.path, expiry_threshold)
print(entry.path)
# 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
Expand Down Expand Up @@ -90,7 +89,7 @@ def write_jsonl_information(dict_info, file_path, scrape_time):
file.write(json.dumps({"scrape_time:": scrape_time,
"scrape_time_datetime": str(datetime.datetime.fromtimestamp(scrape_time))}) + "\n")
file.write(json.dumps({"time_for_scrape_sec": current_time - scrape_time,
"time_for_scrape_min": (current_time - scrape_time) / 60}))
"time_for_scrape_min": (current_time - scrape_time) / 60}) + "\n")

for key in dict_info:
file.write(json.dumps(dict_info[key]) + "\n")
Expand Down