Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- from os.path import isfile,join
- from os import listdir,environ
- from stat import S_ISREG, ST_CTIME, ST_MODE
- import time
- import os
- import glob
- import sys
- import argparse
- from pathlib import Path
- import fnmatch
- import re
- #TODO
- #signals (import) trqbva da ima nachin da se spre ako zabie
- #
- #
- #
- def too_many_files_in_directory_ls(directory,max_files,file_counter):
- """Called when a directory is over the maximum allowed files. By default it removes the oldest files"""
- print("Directory "+directory+" has too many files.("+str(int(file_counter)-int(max_files))+ " over the limit)\nStarting cleanup procedure")
- date_file_list = []
- for folder in glob.glob(directory):
- for file in glob.glob(folder + '/*.*'):
- stats = os.stat(file)
- lastmod_date = time.localtime(stats[8])
- date_file_tuple = lastmod_date, file
- date_file_list.append(date_file_tuple)
- date_file_list.sort()
- #date_file_list.reverse() # If we want the newest files first
- print("%-40s %s" % ("filename:", "last modified:"))
- for file in date_file_list:
- folder, file_name = os.path.split(file[1]) # just the filename
- file_date = time.strftime("%m/%d/%y %H:%M:%S", file[0]) # convert date tuple to MM/DD/YYYY HH:MM:SS format
- print("%-40s %s" % (file_name, file_date))
- #print("Removing "+file_name)
- file_counter-=1;
- if(max_files>=file_counter):
- exit(1)
- exit(1)
- ##def too_many_files_in_directory_rm(directory):
- ## """Called when a directory is over the maximum allowed files. By default it removes the oldest files"""
- ## date_file_list = []
- ## for folder in glob.glob(directory):
- ## for file in glob.glob(folder + '/*.*'):
- ## stats = os.stat(file)
- ## lastmod_date = time.localtime(stats[8])
- ## date_file_tuple = lastmod_date, file
- ## date_file_list.append(date_file_tuple)
- ## date_file_list.sort()
- ## for file in date_file_list:
- ## folder, file_name = os.path.split(file[1])
- ## os.remove(folder+"/"+file_name)
- ##
- def check_directory_files_ls(directory,max_files_per_dir,max_age,max_size,pattern):
- """Lists directory files recursively and checks if files match given pattern.
- If they do, get their size and ctime, and compare them with given limits .
- Will also count files in each directory."""
- current_time_in_seconds=time.time()
- for root, directories, filenames in os.walk(directory):
- file_counter = 0
- for file in fnmatch.filter(filenames, pattern):
- print(file)
- 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("==========="+pattern+" is in file "+file)
- print("File: "+file+" is too big AND old enough to be operated on")
- print("Directory "+root+"/"+" has "+str(file_counter)+" files")
- if file_counter > max_files_per_dir:
- too_many_files_in_directory_ls(root,max_files_per_dir,file_counter)
- ##
- ##def check_directory_files_rm(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
- ##
- ## if(int(file_size)>max_size and int(file_age_in_seconds)>max_age):
- ## rm
- ## os.remove(root+"/"+file)
- ## file_counter-=1
- ##
- ## if file_counter > max_files_per_dir:
- ## too_many_files_in_directory_rm(root)
- ##
- ##
- ##
- ##def check_directory_files_trunc(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
- ##
- ## if(int(file_size)>max_size and int(file_age_in_seconds)>max_age):
- ## fo = open(root+"/"+file, "w")
- ## fo.close()
- ## file_counter-=1
- ##
- ## if file_counter > max_files_per_dir:
- ## too_many_files_in_directory_rm(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()
- 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")
- # sys.exit(1)
- 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")
- if(arg.operation=="ls"):
- print("Starting with operation ls and pattern "+arg.file_type)
- #temp_glob_test(arg.directory,arg.max_files_per_dir,arg.max_age,arg.max_size,arg.file_type)
- #exit(1)
- check_directory_files_ls(arg.directory,arg.max_files_per_dir,arg.max_age,arg.max_size,arg.file_type )
- exit(1)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement