feat: get modified date

This commit is contained in:
Ting-Jun Wang 2024-04-26 01:20:59 +08:00
commit 80afb8dee7
Signed by: snsd0805
GPG Key ID: 48D331A3D6160354

69
clean.py Normal file
View File

@ -0,0 +1,69 @@
'''
Clean the direction in CML Server (tmp2 or home directory)
Suppose that the CLEAN_PATH may seems like this.
And we will only check the directories & files under the user directory with "depth=1"
CLEAN_PATH
|
+---- user1 (user directory)
| |
| +--- file1 (depth=1, will be check)
| |
| +--- Project1 (depth=1, will be check)
| | |
| | +--- subset1-1 (depth=2, won't be check)
| |
| +--- Project2 (depth=1)
| |
| +--- subset2-1 (depth=2)
| |
| +--- subset2-2 (depth=2)
|
+---- user2 (user directory)
|
+--- Project1 (depth=1)
|
+--- subset1-1 (depth=2)
'''
import os
import subprocess
CLEAN_PATH = "/home"
KEEP_FILES = {".bashrc", ".bash_logout", ".bash_profile", "public-html", ".ssh", ".profile", "examples.desktop", ".bash_history", ".config"}
WHITE_LIST = {"mysql", "unix_manager", "unixmanager215", "gangxing", "how083012", "yulunliu", "amie12349", "minliwu", "chenyuyang"}
DATE = '2024-02-01'
def clean_with_root_dir(root_dir):
for user in sorted(os.listdir(CLEAN_PATH)):
print(user)
if user in WHITE_LIST:
continue
for obj in sorted(os.listdir(f'{root_dir}/{user}')):
if obj in KEEP_FILES:
continue
obj = obj.replace(' ', '\ ')
# stat {file or dir}:
# get Create, modified information
# grep Modify:
# get Modified time
# awk:
# -F ' ': use space to split the data
# print $2: column 2 (get date)
modify_date = subprocess.getoutput(f"stat {root_dir}/{user}/{obj} | grep Modify | awk -F ' ' '{{print $2}}'" )
needRemove = modify_date < DATE
print(f' {obj}')
print(f' {modify_date} {needRemove}')
clean_with_root_dir(CLEAN_PATH)