diff --git a/check b/check_home similarity index 66% rename from check rename to check_home index fa8d145..5667626 100755 --- a/check +++ b/check_home @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/python3 from src.cleaner import Cleaner @@ -7,5 +7,5 @@ import os student_dir = os.environ['HOME'] print(student_dir) cleaner = Cleaner(student_dir) -cleaner.run(test=True, notify_stage=False) +cleaner.run( remove=False, notify=False) diff --git a/check_tmp b/check_tmp new file mode 100755 index 0000000..1d29dd2 --- /dev/null +++ b/check_tmp @@ -0,0 +1,18 @@ +#!/usr/bin/python3 + + +from src.cleaner import Cleaner +import os + + +username = os.getlogin() +print(username) +for tmp_main_dir in ['/tmp2', '/tmp3']: + student_dir = f'{tmp_main_dir}/{username}' + if os.path.isdir(student_dir): + print(student_dir) + cleaner = Cleaner(student_dir) + cleaner.run(remove=False, notify=False) + else: + print(f"{student_dir} doesn't exist") + diff --git a/main.py b/main.py index d22cab4..84d97ce 100644 --- a/main.py +++ b/main.py @@ -39,7 +39,7 @@ from constant import GOOGLE_OWNER GOOGLE_CODE = os.environ['GOOGLE_CODE'] -def main(clean_type, test=True, notify_stage=True): +def main(clean_type, remove=False, notify=False): mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE) if clean_type == 'home': @@ -53,7 +53,20 @@ def main(clean_type, test=True, notify_stage=True): for index, student_dir in enumerate(student_list): print(f'{index+1}/{len(student_list)} {student_dir}') cleaner = Cleaner(student_dir, mailer) - cleaner.run(test=test, notify_stage=notify_stage) + cleaner.run(remove=remove, notify=notify) + elif clean_type == 'tmp': + for main_dir in ['/tmp2', '/tmp3']: + student_list = [] + if os.path.isdir(main_dir): + for user in os.listdir(f"{main_dir}/"): + if os.path.isdir(f"{main_dir}/{user}/"): + student_list.append(f"{main_dir}/{user}/") + + for index, student_dir in enumerate(student_list): + print(f'{index+1}/{len(student_list)} {student_dir}') + cleaner = Cleaner(student_dir, mailer) + cleaner.run(remove=remove, notify=notify) + if __name__ == "__main__": - main('home', test=True, notify_stage=True) + main('tmp', remove=False, notify=False) diff --git a/src/cleaner.py b/src/cleaner.py index bb2e31b..fd40076 100644 --- a/src/cleaner.py +++ b/src/cleaner.py @@ -7,13 +7,15 @@ class Cleaner(): self.user_dir = user_dir self.mailer = mailer - def run(self, test=True, notify_stage=True): + def run(self, remove=False, notify=True): ''' - notify_stage=1, test=1: 不會刪,不會寄信(只寄給開發人員) - notify_stage=1, test=0: 不會刪,會寄信 (提醒階段) - notify_stage=0, test=1: 不會刪,不會寄信(確認) - notify_stage=0, test=0: 會刪除,不會寄信(刪除階段) + remove=0, notify=0: developer testing + remove=0, notify=1: 寄通知信件通知準備要刪除的資料 + remove=1, notify=0: 正式刪除 + remove=1, notify=1: invalid ''' + assert not ((remove==1) and (notify==1)), "Invalid action: remove=1 & notify=1" + username = self.user_dir.split('/')[-1] if username in WHITE_LIST: print(f' WHITE LIST: {username}') @@ -35,25 +37,27 @@ class Cleaner(): # print $2: column 2 (get date) modify_date = subprocess.getoutput(f"stat {self.user_dir}/{obj} | grep Modify | awk -F ' ' '{{print $2}}'" ) - if modify_date < DATE: + if modify_date < DATE and 'docker' not in obj: remove_objs.append((obj, modify_date)) print(f' {modify_date} {obj}') - if notify_stage and len(remove_objs) != 0: - self.notify(username, test, remove_objs) - - elif not notify_stage and len(remove_objs) != 0: - # delete files + if len(remove_objs) != 0: remove_obj_names = [ name for name, date in remove_objs ] - if test: - command = 'rm -rf {}'.format(" ".join(remove_obj_names)) - print(f' we will run:\n {command}') + if not remove: + if notify: + self.notify(username, test, remove_objs) + else: + # testing + command = 'rm -rf {}'.format(" ".join(remove_obj_names)) + print(f' we will run:\n {command}') else: + # delete files if not os.path.isdir(f'/volume1/cmlabhome/home_backup/{username}'): os.mkdir(f'/volume1/cmlabhome/home_backup/{username}') msg = "" for name, date in remove_objs: + print(name) if name[0] != '.' and 'conda' not in name: command = f'mv {self.user_dir}/{name} /volume1/cmlabhome/home_backup/{username}/{name}' else: