feat: check /tmp2
This commit is contained in:
parent
678564df42
commit
1f692b792a
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
|
||||||
from src.cleaner import Cleaner
|
from src.cleaner import Cleaner
|
||||||
@ -7,5 +7,5 @@ import os
|
|||||||
student_dir = os.environ['HOME']
|
student_dir = os.environ['HOME']
|
||||||
print(student_dir)
|
print(student_dir)
|
||||||
cleaner = Cleaner(student_dir)
|
cleaner = Cleaner(student_dir)
|
||||||
cleaner.run(test=True, notify_stage=False)
|
cleaner.run( remove=False, notify=False)
|
||||||
|
|
||||||
18
check_tmp
Executable file
18
check_tmp
Executable 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
19
main.py
@ -39,7 +39,7 @@ from constant import GOOGLE_OWNER
|
|||||||
GOOGLE_CODE = os.environ['GOOGLE_CODE']
|
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)
|
mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE)
|
||||||
|
|
||||||
if clean_type == 'home':
|
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):
|
for index, student_dir in enumerate(student_list):
|
||||||
print(f'{index+1}/{len(student_list)} {student_dir}')
|
print(f'{index+1}/{len(student_list)} {student_dir}')
|
||||||
cleaner = Cleaner(student_dir, mailer)
|
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__":
|
if __name__ == "__main__":
|
||||||
main('home', test=True, notify_stage=True)
|
main('tmp', remove=False, notify=False)
|
||||||
|
|||||||
@ -7,13 +7,15 @@ class Cleaner():
|
|||||||
self.user_dir = user_dir
|
self.user_dir = user_dir
|
||||||
self.mailer = mailer
|
self.mailer = mailer
|
||||||
|
|
||||||
def run(self, test=True, notify_stage=True):
|
def run(self, remove=False, notify=True):
|
||||||
'''
|
'''
|
||||||
notify_stage=1, test=1: 不會刪,不會寄信(只寄給開發人員)
|
remove=0, notify=0: developer testing
|
||||||
notify_stage=1, test=0: 不會刪,會寄信 (提醒階段)
|
remove=0, notify=1: 寄通知信件通知準備要刪除的資料
|
||||||
notify_stage=0, test=1: 不會刪,不會寄信(確認)
|
remove=1, notify=0: 正式刪除
|
||||||
notify_stage=0, test=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]
|
username = self.user_dir.split('/')[-1]
|
||||||
if username in WHITE_LIST:
|
if username in WHITE_LIST:
|
||||||
print(f' WHITE LIST: {username}')
|
print(f' WHITE LIST: {username}')
|
||||||
@ -35,25 +37,27 @@ class Cleaner():
|
|||||||
# print $2: column 2 (get date)
|
# print $2: column 2 (get date)
|
||||||
modify_date = subprocess.getoutput(f"stat {self.user_dir}/{obj} | grep Modify | awk -F ' ' '{{print $2}}'" )
|
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))
|
remove_objs.append((obj, modify_date))
|
||||||
print(f' {modify_date} {obj}')
|
print(f' {modify_date} {obj}')
|
||||||
|
|
||||||
if notify_stage and len(remove_objs) != 0:
|
if len(remove_objs) != 0:
|
||||||
self.notify(username, test, remove_objs)
|
|
||||||
|
|
||||||
elif not notify_stage and len(remove_objs) != 0:
|
|
||||||
# delete files
|
|
||||||
remove_obj_names = [ name for name, date in remove_objs ]
|
remove_obj_names = [ name for name, date in remove_objs ]
|
||||||
if test:
|
if not remove:
|
||||||
command = 'rm -rf {}'.format(" ".join(remove_obj_names))
|
if notify:
|
||||||
print(f' we will run:\n {command}')
|
self.notify(username, test, remove_objs)
|
||||||
|
else:
|
||||||
|
# testing
|
||||||
|
command = 'rm -rf {}'.format(" ".join(remove_obj_names))
|
||||||
|
print(f' we will run:\n {command}')
|
||||||
else:
|
else:
|
||||||
|
# delete files
|
||||||
if not os.path.isdir(f'/volume1/cmlabhome/home_backup/{username}'):
|
if not os.path.isdir(f'/volume1/cmlabhome/home_backup/{username}'):
|
||||||
os.mkdir(f'/volume1/cmlabhome/home_backup/{username}')
|
os.mkdir(f'/volume1/cmlabhome/home_backup/{username}')
|
||||||
|
|
||||||
msg = ""
|
msg = ""
|
||||||
for name, date in remove_objs:
|
for name, date in remove_objs:
|
||||||
|
print(name)
|
||||||
if name[0] != '.' and 'conda' not in name:
|
if name[0] != '.' and 'conda' not in name:
|
||||||
command = f'mv {self.user_dir}/{name} /volume1/cmlabhome/home_backup/{username}/{name}'
|
command = f'mv {self.user_dir}/{name} /volume1/cmlabhome/home_backup/{username}/{name}'
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user