Created
April 9, 2019 09:44
-
-
Save BoyanHH/9534876045af4d6d2c02357b9fc78a58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import subprocess | |
| from os.path import isfile,join | |
| from os import listdir,environ | |
| import time | |
| import os | |
| import sys | |
| import argparse | |
| ##add exceptions | |
| ##prints are only for debugging | |
| def too_many_files_in_directory(directory): | |
| """Called when a directory is over the maximum allowed files""" | |
| print("Directory "+directory+" has too many files.\nStarting cleanup procedure") | |
| for root, directories, filenames in os.walk(directory): | |
| for file in filenames: | |
| print(file) | |
| ###############################################remove all files? or all non .log? | |
| def check_directory_files(directory,max_files_per_dir,max_age,max_size): | |
| current_time_in_seconds=time.time() | |
| for root, directories, filenames in os.walk(directory): | |
| file_counter = 0 | |
| for file in filenames: | |
| path=root+"/"+file | |
| file_age_in_seconds= current_time_in_seconds - os.path.getctime(path) | |
| file_size = str(os.path.getsize(path)) | |
| file_counter+=1 | |
| print(file+" SIZE = "+file_size+" AGE in seconds = "+str(file_age_in_seconds)) | |
| if(int(file_size)>max_size and int(file_age_in_seconds)>max_age): | |
| print("File: "+file+" is too big AND old enough to be operated on") | |
| #truncate | |
| #fo = open(root+"/"+file, "w") #open and close or echo? | |
| #fo.close() | |
| #print(file+" Truncated") | |
| #file_counter-=1 | |
| print("Directory "+root+"/"+" has "+str(file_counter)+" files") | |
| if file_counter > max_files_per_dir: | |
| too_many_files_in_directory(root) | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Log cleaner') | |
| parser.add_argument("max_size",type=int,help="maximum file size in bytes") | |
| parser.add_argument("directory",type=str,help="root directory from which to begin procedure recursively") | |
| parser.add_argument("file_type",type=str,help="Which files to operate on (.log) (.gz) all") | |
| parser.add_argument("operation",type=str,help="rm/trunc/split/ls") | |
| parser.add_argument("max_age",type=int,help="Maximum age of a file in seconds") | |
| parser.add_argument("max_files_per_dir",type=int,help="Maximum amount of files in a directory") | |
| arg = parser.parse_args() | |
| ##Quick check for accepted arguments | |
| if arg.max_size < 0.1: | |
| sys.stderr.write('Invalid maximum file size argument. Given is:'+str(arg.max_size)+" Required is: SIZE > 0\n") | |
| if not os.access(arg.directory, os.W_OK): | |
| sys.stderr.write("Unable to acces or have no write access to given directory: "+arg.directory+"\n") | |
| if not (arg.file_type == "gz" or arg.file_type == ".gz" or arg.file_type == ".log" or arg.file_type == "log" or arg.file_type == "all"): | |
| sys.stderr.write("Unrecognized file type "+arg.file_type+" .Valid file types are gz/all/log \n") | |
| if arg.max_size < 0.1: | |
| sys.stderr.write('Invalid maximum age argument. Given is:'+str(arg.max_size)+" Required is: SIZE > 0\n") | |
| if arg.max_files_per_dir < 0.1: | |
| sys.stderr.write('Invalid maximum files per dir argument. Given is:'+str(arg.max_files_per_dir)+" Required is: SIZE > 0\n") | |
| check_directory_files(arg.directory,arg.max_files_per_dir,arg.max_age,arg.max_size) | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment