diff --git a/cleaner.py b/cleaner.py index e261979..0a7f9d4 100644 --- a/cleaner.py +++ b/cleaner.py @@ -1,36 +1,82 @@ import os import subprocess +MAIL_MESSAGE = ''' +Hi, {}
+
+因 CML Server 掛載家目錄(~)的 NAS 空間即將不足,我們將於 4/28(日) 00:00 開始清除長時間沒有修改紀錄的資料夾
+
+本次清理,我們將移除 2024/02/01 以後沒有修改紀錄的資料夾、檔案
+以下是您家目錄中可能被移除的內容:
+ +
+若是仍要保留的資料請隨意對其做修改,以更新修改紀錄的時間。
+造成不便,敬請見諒!

+ +
+* 信件由網管自動化工具寄出,若為錯誤寄送請忽略此信件。
+
+Best,
+CMLab Unix Manager, Ting-Jun Wang
+CMLab, National Taiwan University
+Email: unix_manager@cmlab.csie.ntu.edu.tw
+
+            __  __     _
+           / / / /__  (_)_ __
+   __  ___/ /_/ / _ \/ /\ \ /
+  /  |/  /\____/_//_/_//_\_\____ ____
+ / /|_/ / _ `/ _ \/ _ `/ _ `/ -_) __/
+/_/  /_/\_,_/_//_/\_,_/\_, /\__/_/
+                      /___/
+
+''' + class Cleaner(): - def __init__(self, root, date, mailer, white_list, keep_files): - self.root = root + def __init__(self, user_dir, date, mailer, white_list, keep_files): + self.user_dir = user_dir self.date = date self.white_list = white_list self.keep_files = keep_files self.mailer = mailer def run(self, test=True, send_mail=False): - for user in sorted(os.listdir(self.root)): - print(user) - if user in self.white_list: + username = self.user_dir.split('/')[-1] + if username in self.white_list: + print(f' WHITE LIST: {username}') + return + + remove_objs = [] + for obj in sorted(os.listdir(self.user_dir)): + if obj in self.keep_files: continue - for obj in sorted(os.listdir(f'{self.root}/{user}')): - if obj in self.keep_files: - continue + obj = obj.replace(' ', '\ ') - 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 {self.user_dir}/{obj} | grep Modify | awk -F ' ' '{{print $2}}'" ) - # 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 {self.root}/{user}/{obj} | grep Modify | awk -F ' ' '{{print $2}}'" ) - - needRemove = modify_date < self.date - print(f' {obj}') - print(f' {modify_date} {needRemove}') + if modify_date < self.date: + remove_objs.append((obj, modify_date)) + print(f' {modify_date} {obj}') + + if send_mail and len(remove_objs) != 0: + if test: + receiver = 'snsd0805@cmlab.csie.ntu.edu.tw' + else: + receiver = f'{username}@cmlab.csie.ntu.edu.tw' + sender = 'unix_manager@cmlab.csie.ntu.edu.tw' + subject = f'[網管通知] CML Server 清理預計被移除的內容({username})' + msg = '' + for obj, date in remove_objs: + msg += f'
  • {date} - {obj}
  • ' + msg = MAIL_MESSAGE.format(username, msg) + self.mailer.send(sender, receiver, subject, msg) diff --git a/main.py b/main.py index ba3e3d3..6ead01b 100644 --- a/main.py +++ b/main.py @@ -36,8 +36,8 @@ import os CLEAN_PATH = "/home/master/12" -KEEP_FILES = [".bashrc", ".bash_logout", ".bash_profile", "public-html", ".ssh", ".profile", "examples.desktop", ".bash_history", ".config", ".zshrc", ".vimrc", ".gnupg", ".oh-my-zsh", ".vim", ".viminfo", ".vscode-server", ".gitconfig", ".local"] -WHITE_LIST = ["mysql", "unix_manager", "unixmanager215", "gangxing", "how083012", "yulunliu", "amie12349", "minliwu", "chenyuyang"] +KEEP_FILES = [".bashrc", ".bash_logout", ".bash_profile", "public-html", ".ssh", ".profile", "examples.desktop", ".bash_history", ".config", ".zshrc", ".vimrc", ".gnupg", ".oh-my-zsh", ".vim", ".viminfo", ".vscode-server", ".gitconfig", ".local", ".zsh_history", ".zsh_profile"] +WHITE_LIST = ["mysql", "unix_manager", "unixmanager215", "gangxing", "how083012", "yulunliu", "amie12349", "minliwu", "chenyuyang", "unixmanager5xx"] DATE = '2024-02-01' @@ -45,9 +45,57 @@ GOOGLE_OWNER = "snsd0805@cmlab.csie.ntu.edu.tw" GOOGLE_CODE = os.environ['GOOGLE_CODE'] -if __name__ == "__main__": - mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE) - mailer.send('unix_manager@cmlab.csie.ntu.edu.tw', 'snsd0805@cmlab.csie.ntu.edu.tw', '[測試郵件] test', 'Test mail') +def get_master(): + ans = [] + for year in os.listdir(f'/home/master/'): + if os.path.isdir(f'/home/master/{year}'): + for user in os.listdir(f'/home/master/{year}'): + ans.append(f'/home/master/{year}/{user}') + return ans - cleaner = Cleaner('/home/master/12', "2024/02/01", mailer, WHITE_LIST, KEEP_FILES) - cleaner.run() +def get_phd(): + ans = [] + for dir_name in os.listdir(f'/home/phd/'): + if dir_name.isnumeric(): + year = dir_name + for user in os.listdir(f'/home/phd/{year}'): + ans.append(f'/home/phd/{year}/{user}') + else: + ans.append(f'/home/phd/{dir_name}') + return ans + +def get_ra(): + ans = [] + for user in os.listdir(f'/home/ra/'): + ans.append(f'/home/ra/{user}') + return ans + +def get_extra(): + ans = [] + for user in os.listdir(f'/home/extra/'): + ans.append(f'/home/extra/{user}') + return ans + + +def main(clean_type, test=True, send_mail=False): + mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE) + + if clean_type == 'home': + student_list = [] + for student_type in ['master', 'phd', 'ra', 'extra']: + func = globals()[f'get_{student_type}'] + student_list += func() + + for index, student_dir in enumerate(student_list): + print(f'{index+1}/{len(student_list)} {student_dir}') + cleaner = Cleaner(student_dir, "2024-02-01", mailer, WHITE_LIST, KEEP_FILES) + cleaner.run(test=test, send_mail=send_mail) + + # mailer.send('unix_manager@cmlab.csie.ntu.edu.tw', 'snsd0805@cmlab.csie.ntu.edu.tw', '[測試郵件] test', 'Test mail') + + # cleaner = Cleaner('/home/master/12', "2024/02/01", mailer, WHITE_LIST, KEEP_FILES) + # cleaner.run(test=test, send_mail=send_mail) + + +if __name__ == "__main__": + main('home', test=False, send_mail=True)