SHARE
TWEET

Untitled

a guest Apr 19th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import subprocess
  2. from os.path import isfile,join
  3. from os import listdir,environ
  4. import time
  5. import os
  6. import sys
  7. import argparse
  8.  
  9. ##add exceptions
  10. ##prints are only for debugging
  11.  
  12.            
  13. def too_many_files_in_directory(directory):
  14.     """Called when a directory is over the maximum allowed files"""
  15.     print("Directory "+directory+" has too many files.\nStarting cleanup procedure")
  16.     for root, directories, filenames in os.walk(directory):
  17.         for file in filenames:
  18.             print(file)
  19.     ###############################################remove all files? or all non .log?
  20.    
  21.  
  22.  
  23. def check_directory_files(directory,max_files_per_dir,max_age,max_size):
  24.     current_time_in_seconds=time.time()
  25.     for root, directories, filenames in os.walk(directory):
  26.             file_counter = 0
  27.             for file in filenames:
  28.                 path=root+"/"+file
  29.                 file_age_in_seconds= current_time_in_seconds - os.path.getctime(path)
  30.                 file_size = str(os.path.getsize(path))
  31.                 file_counter+=1
  32.                
  33.                 print(file+" SIZE = "+file_size+" AGE in seconds = "+str(file_age_in_seconds))
  34.  
  35.                 if(int(file_size)>max_size and int(file_age_in_seconds)>max_age):
  36.                     print("File: "+file+" is too big AND old enough to be operated on")
  37.                         #truncate
  38.                         #fo = open(root+"/"+file, "w")           #open and close or echo?
  39.                         #fo.close()
  40.                         #print(file+" Truncated")
  41.                         #file_counter-=1
  42.                        
  43.             print("Directory "+root+"/"+" has "+str(file_counter)+" files")
  44.            
  45.             if file_counter > max_files_per_dir:
  46.                 too_many_files_in_directory(root)
  47.                    
  48. def main():
  49.     parser = argparse.ArgumentParser(description='Log cleaner')
  50.     parser.add_argument("max_size",type=int,help="maximum file size in bytes")
  51.     parser.add_argument("directory",type=str,help="root directory from which to begin procedure recursively")
  52.     parser.add_argument("file_type",type=str,help="Which files to operate on (.log) (.gz) all")
  53.     parser.add_argument("operation",type=str,help="rm/trunc/split/ls")
  54.     parser.add_argument("max_age",type=int,help="Maximum age of a file in seconds")
  55.     parser.add_argument("max_files_per_dir",type=int,help="Maximum amount of files in a directory")
  56.     arg = parser.parse_args()
  57.  
  58.     ##Quick check for accepted arguments
  59.     if arg.max_size < 0.1:
  60.         sys.stderr.write('Invalid maximum file size argument. Given is:'+str(arg.max_size)+" Required is: SIZE > 0\n")
  61.     if not os.access(arg.directory, os.W_OK):
  62.         sys.stderr.write("Unable to acces or have no write access to given directory: "+arg.directory+"\n")
  63.     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"):
  64.         sys.stderr.write("Unrecognized file type "+arg.file_type+" .Valid file types are gz/all/log \n")
  65.     if arg.max_size < 0.1:
  66.         sys.stderr.write('Invalid maximum age argument. Given is:'+str(arg.max_size)+" Required is: SIZE > 0\n")
  67.     if arg.max_files_per_dir < 0.1:
  68.         sys.stderr.write('Invalid maximum files per dir argument. Given is:'+str(arg.max_files_per_dir)+" Required is: SIZE > 0\n")        
  69.  
  70.    
  71.     check_directory_files(arg.directory,arg.max_files_per_dir,arg.max_age,arg.max_size)
  72.        
  73. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top