feat: check /tmp2

This commit is contained in:
TING-JUN WANG 2024-09-03 17:19:09 +08:00
parent 678564df42
commit 1f692b792a
4 changed files with 54 additions and 19 deletions

View File

@ -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)

18
check_tmp Executable file
View File

@ -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")

19
main.py
View File

@ -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)

View File

@ -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: