From c06d472d0e884c3db656423628736951b7313ba2 Mon Sep 17 00:00:00 2001 From: TING-JUN WANG Date: Fri, 26 Apr 2024 02:12:37 +0800 Subject: [PATCH] feat: split modules --- clean.py | 111 ----------------------------------------------------- cleaner.py | 36 +++++++++++++++++ mailer.py | 27 +++++++++++++ main.py | 53 +++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 111 deletions(-) delete mode 100644 clean.py create mode 100644 cleaner.py create mode 100644 mailer.py create mode 100644 main.py diff --git a/clean.py b/clean.py deleted file mode 100644 index f75ea6f..0000000 --- a/clean.py +++ /dev/null @@ -1,111 +0,0 @@ -''' - -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 -import smtplib -from email.mime.text import MIMEText - -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'] - - -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 - - for obj in sorted(os.listdir(f'{self.root}/{user}')): - if obj in self.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 {self.root}/{user}/{obj} | grep Modify | awk -F ' ' '{{print $2}}'" ) - - needRemove = modify_date < DATE - print(f' {obj}') - print(f' {modify_date} {needRemove}') - - - - - -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() diff --git a/cleaner.py b/cleaner.py new file mode 100644 index 0000000..e261979 --- /dev/null +++ b/cleaner.py @@ -0,0 +1,36 @@ +import os +import subprocess + +class Cleaner(): + def __init__(self, root, date, mailer, white_list, 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 + + for obj in sorted(os.listdir(f'{self.root}/{user}')): + if obj in self.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 {self.root}/{user}/{obj} | grep Modify | awk -F ' ' '{{print $2}}'" ) + + needRemove = modify_date < self.date + print(f' {obj}') + print(f' {modify_date} {needRemove}') + diff --git a/mailer.py b/mailer.py new file mode 100644 index 0000000..30f7d26 --- /dev/null +++ b/mailer.py @@ -0,0 +1,27 @@ +import smtplib +from email.mime.text import MIMEText + +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 + diff --git a/main.py b/main.py new file mode 100644 index 0000000..ba3e3d3 --- /dev/null +++ b/main.py @@ -0,0 +1,53 @@ +''' + +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) + +''' + +from mailer import Mailer +from cleaner import Cleaner +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"] + +DATE = '2024-02-01' + +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') + + cleaner = Cleaner('/home/master/12', "2024/02/01", mailer, WHITE_LIST, KEEP_FILES) + cleaner.run()