#!/usr/bin/env python
”'
restricts the number of MacroServer core dumps to 10
”'
def restrictTo10Versions( fName):
from stat import S_ISREG, ST_CTIME, ST_MODE
import os, sys, time
dirpath = '/online_dir/Diag'
#
# get all files of a directory with stat
#
entries = (os.path.join(dirpath, fn) for fn in os.listdir(dirpath))
entries = ((os.stat(path), path) for path in entries)
#
# keep regular files only
#
entries = ((stat[ST_CTIME], path) for stat, path in entries if S_ISREG(stat[ST_MODE]))
#
# keep those files only that match fName
#
entries = ((cdate, path) for cdate, path in entries if path.find( fName) > 0)
lst = [ (cdate, path) for cdate, path in sorted( entries)]
nDel = len( lst) - 10
for cdate, path in lst:
if nDel > 0:
# use os.remove( path)
print( "deleting ", time.ctime( cdate), path)
else:
print( "keeping ", time.ctime( cdate), path)
nDel -= 1
if __name__ == "__main__":
restrictTo10Versions( 'MacroServerCore')