Mini Shell

Direktori : /opt/sharedrads/
Upload File :
Current File : //opt/sharedrads/clean_moveuser

#!/opt/imh-python/bin/python3
"""
Script that attempts to clean up failed/old directories
left over by SNM at /home/moveuser
"""

import os
import time
import shutil


def build_cpmove_targets():
    """
    Return a list of cpmove files at /home/moveuser
    """
    target_cpmoves = []
    for files in next(os.walk("/home/moveuser"))[2]:
        if "cpmove" in files:
            target_cpmoves.append('/home/moveuser/' + files)
    return target_cpmoves


def build_directories():
    """
    Return a list of directories at /home/moveuser
    """
    target_folders = []
    excluded_dirs = ['mail', '.ssh', 'perl5', '.cl.selector', '.cagefs']
    for dirs in next(os.walk("/home/moveuser"))[1]:
        if dirs not in excluded_dirs:
            target_folders.append('/home/moveuser/' + dirs)
    return target_folders


def is_old(target, age=604800):
    """
    Check age of target file. Default for "old" is 7 days
    """
    now_time = time.time()
    return now_time - os.path.getctime(target) > age


def main():
    """
    Build a list of crap to delete, run a few sanity checks, and do the needful
    """
    huuuge_list = build_cpmove_targets() + build_directories()
    for target in huuuge_list:
        if is_old(target):
            if os.path.isfile(target):
                print("Deleting %s" % (target))
                os.remove(target)
            if os.path.isdir(target):
                print("Deleting %s" % (target))
                shutil.rmtree(target)
        else:
            pass


if __name__ == '__main__':
    main()

Zerion Mini Shell 1.0