Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. import subprocess
  2. from os.path import isfile,join
  3. from os import listdir,environ
  4. from stat import S_ISREG, ST_CTIME, ST_MODE
  5. import time
  6. import os
  7. import glob
  8. import sys
  9. import argparse
  10. from pathlib import Path
  11. import fnmatch
  12. import re
  13.  
  14. #TODO
  15. #signals (import) trqbva da ima nachin da se spre ako zabie
  16. #
  17. #
  18. #
  19.  
  20. def too_many_files_in_directory_ls(directory,max_files,file_counter):
  21. """Called when a directory is over the maximum allowed files. By default it removes the oldest files"""
  22. print("Directory "+directory+" has too many files.("+str(int(file_counter)-int(max_files))+ " over the limit)\nStarting cleanup procedure")
  23.  
  24. date_file_list = []
  25. for folder in glob.glob(directory):
  26. for file in glob.glob(folder + '/*.*'):
  27. stats = os.stat(file)
  28. lastmod_date = time.localtime(stats[8])
  29. date_file_tuple = lastmod_date, file
  30. date_file_list.append(date_file_tuple)
  31.  
  32. date_file_list.sort()
  33. #date_file_list.reverse() # If we want the newest files first
  34. print("%-40s %s" % ("filename:", "last modified:"))
  35. for file in date_file_list:
  36. folder, file_name = os.path.split(file[1]) # just the filename
  37. file_date = time.strftime("%m/%d/%y %H:%M:%S", file[0]) # convert date tuple to MM/DD/YYYY HH:MM:SS format
  38. print("%-40s %s" % (file_name, file_date))
  39. #print("Removing "+file_name)
  40. file_counter-=1;
  41. if(max_files>=file_counter):
  42. exit(1)
  43. exit(1)
  44.  
  45.  
  46.  
  47. ##def too_many_files_in_directory_rm(directory):
  48. ## """Called when a directory is over the maximum allowed files. By default it removes the oldest files"""
  49. ## date_file_list = []
  50. ## for folder in glob.glob(directory):
  51. ## for file in glob.glob(folder + '/*.*'):
  52. ## stats = os.stat(file)
  53. ## lastmod_date = time.localtime(stats[8])
  54. ## date_file_tuple = lastmod_date, file
  55. ## date_file_list.append(date_file_tuple)
  56. ## date_file_list.sort()
  57. ## for file in date_file_list:
  58. ## folder, file_name = os.path.split(file[1])
  59. ## os.remove(folder+"/"+file_name)
  60. ##
  61.  
  62.  
  63.  
  64. def check_directory_files_ls(directory,max_files_per_dir,max_age,max_size,pattern):
  65. """Lists directory files recursively and checks if files match given pattern.
  66. If they do, get their size and ctime, and compare them with given limits .
  67. Will also count files in each directory."""
  68.  
  69. current_time_in_seconds=time.time()
  70. for root, directories, filenames in os.walk(directory):
  71. file_counter = 0
  72. for file in fnmatch.filter(filenames, pattern):
  73. print(file)
  74.  
  75. path=root+"/"+file
  76. file_age_in_seconds= current_time_in_seconds - os.path.getctime(path)
  77. file_size = str(os.path.getsize(path))
  78. file_counter+=1
  79.  
  80. print(file+" SIZE = "+file_size+" AGE in seconds = "+str(file_age_in_seconds))
  81.  
  82. if((int(file_size)>max_size and int(file_age_in_seconds)>max_age) ):
  83. print("==========="+pattern+" is in file "+file)
  84. print("File: "+file+" is too big AND old enough to be operated on")
  85.  
  86. print("Directory "+root+"/"+" has "+str(file_counter)+" files")
  87. if file_counter > max_files_per_dir:
  88. too_many_files_in_directory_ls(root,max_files_per_dir,file_counter)
  89.  
  90.  
  91. ##
  92. ##def check_directory_files_rm(directory,max_files_per_dir,max_age,max_size):
  93. ## current_time_in_seconds=time.time()
  94. ## for root, directories, filenames in os.walk(directory):
  95. ## file_counter = 0
  96. ## for file in filenames:
  97. ## path=root+"/"+file
  98. ## file_age_in_seconds= current_time_in_seconds - os.path.getctime(path)
  99. ## file_size = str(os.path.getsize(path))
  100. ## file_counter+=1
  101. ##
  102. ## if(int(file_size)>max_size and int(file_age_in_seconds)>max_age):
  103. ## rm
  104. ## os.remove(root+"/"+file)
  105. ## file_counter-=1
  106. ##
  107. ## if file_counter > max_files_per_dir:
  108. ## too_many_files_in_directory_rm(root)
  109. ##
  110. ##
  111. ##
  112. ##def check_directory_files_trunc(directory,max_files_per_dir,max_age,max_size):
  113. ## current_time_in_seconds=time.time()
  114. ## for root, directories, filenames in os.walk(directory):
  115. ## file_counter = 0
  116. ## for file in filenames:
  117. ## path=root+"/"+file
  118. ## file_age_in_seconds= current_time_in_seconds - os.path.getctime(path)
  119. ## file_size = str(os.path.getsize(path))
  120. ## file_counter+=1
  121. ##
  122. ## if(int(file_size)>max_size and int(file_age_in_seconds)>max_age):
  123. ## fo = open(root+"/"+file, "w")
  124. ## fo.close()
  125. ## file_counter-=1
  126. ##
  127. ## if file_counter > max_files_per_dir:
  128. ## too_many_files_in_directory_rm(root)
  129.  
  130.  
  131.  
  132. def main():
  133. parser = argparse.ArgumentParser(description='Log cleaner')
  134. parser.add_argument("max_size",type=int,help="maximum file size in bytes")
  135. parser.add_argument("directory",type=str,help="root directory from which to begin procedure recursively")
  136. parser.add_argument("file_type",type=str,help="Which files to operate on (.log) (.gz) all")
  137. parser.add_argument("operation",type=str,help="rm/trunc/split/ls")
  138. parser.add_argument("max_age",type=int,help="Maximum age of a file in seconds")
  139. parser.add_argument("max_files_per_dir",type=int,help="Maximum amount of files in a directory")
  140. arg = parser.parse_args()
  141.  
  142. if arg.max_size < 0.1:
  143. sys.stderr.write('Invalid maximum file size argument. Given is:'+str(arg.max_size)+" Required is: SIZE > 0\n")
  144. if not os.access(arg.directory, os.W_OK):
  145. sys.stderr.write("Unable to acces or have no write access to given directory: "+arg.directory+"\n")
  146. # 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"):
  147. # sys.stderr.write("Unrecognized file type "+arg.file_type+" .Valid file types are gz/all/log \n")
  148. # sys.exit(1)
  149. if arg.max_size < 0.1:
  150. sys.stderr.write('Invalid maximum age argument. Given is:'+str(arg.max_size)+" Required is: SIZE > 0\n")
  151. if arg.max_files_per_dir < 0.1:
  152. sys.stderr.write('Invalid maximum files per dir argument. Given is:'+str(arg.max_files_per_dir)+" Required is: SIZE > 0\n")
  153.  
  154. if(arg.operation=="ls"):
  155. print("Starting with operation ls and pattern "+arg.file_type)
  156. #temp_glob_test(arg.directory,arg.max_files_per_dir,arg.max_age,arg.max_size,arg.file_type)
  157. #exit(1)
  158. check_directory_files_ls(arg.directory,arg.max_files_per_dir,arg.max_age,arg.max_size,arg.file_type )
  159. exit(1)
  160.  
  161.  
  162. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement