diff --git a/clean.py b/clean.py index 5d312bf..f75ea6f 100644 --- a/clean.py +++ b/clean.py @@ -31,39 +31,81 @@ CLEAN_PATH ''' import os import subprocess +import smtplib +from email.mime.text import MIMEText -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"} +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"] DATE = '2024-02-01' +GOOGLE_OWNER = "snsd0805@cmlab.csie.ntu.edu.tw" +GOOGLE_CODE = os.environ['GOOGLE_CODE'] -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: +class Mailer(): + def __init__(self, google_owner, google_code): + self.google_owner = google_owner + self.google_code = google_code + + def send(self, sender, receiver, subject, msg): + msg = MIMEText(msg, 'html') + msg['Subject'] = subject + msg['From'] = sender + msg['To'] = receiver + + smtp = smtplib.SMTP('smtp.gmail.com', 587) + smtp.ehlo() + smtp.starttls() + smtp.login(self.google_owner, self.google_code) + + status = smtp.send_message(msg) + smtp.quit() + + if status == {}: + return True + else: + return False + +class Cleaner(): + def __init__(self, root, date, mailer, white_list=WHITE_LIST, keep_files=KEEP_FILES): + self.root = root + 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: continue - obj = obj.replace(' ', '\ ') + for obj in sorted(os.listdir(f'{self.root}/{user}')): + if obj in self.keep_files: + continue - # 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}}'" ) + obj = obj.replace(' ', '\ ') - needRemove = modify_date < DATE - print(f' {obj}') - print(f' {modify_date} {needRemove}') + # 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 < DATE + print(f' {obj}') + print(f' {modify_date} {needRemove}') -clean_with_root_dir(CLEAN_PATH) + + +mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE) +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) +cleaner.run()