From 80afb8dee7dc07f0494d0cb9a206d9fa85ff5082 Mon Sep 17 00:00:00 2001 From: Ting-Jun Wang Date: Fri, 26 Apr 2024 01:20:59 +0800 Subject: [PATCH] feat: get modified date --- clean.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 clean.py diff --git a/clean.py b/clean.py new file mode 100644 index 0000000..5d312bf --- /dev/null +++ b/clean.py @@ -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)