Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/clean_pass.py
Дата изменения: Fri Feb 28 14:46:09 2014
Дата индексирования: Sat Mar 1 15:08:37 2014
Кодировка:
#
#MODULE clean_pass.py
#
#***********************************************************************
"""

**PURPOSE** --
Manage disk space for specified directories and files.

**DEVELOPER** --
Greg Wenzel

**MODIFICATION HISTORY** --

o Initial implementation 03/16/04
o add more files to cleanup dc 1/29/07
o add avtut directory dc 9/11/08
o don't error out if a directory is undeleteable. drc 11/1/10
"""
#***********************************************************************
import exceptions
import sys
import spss_sys_util
import os
import time_util
import types
import file_util
import spst_getopt
import exceptions
from boolean import *

__version__ = "11/1/10"

PASSOUT = spss_sys_util.resolver("PASSOUT")

#only delete files in specified directories
#define delete characteristics of each managed directory
#
#directory name, **Must be a unix mounted disk. VMS crossmounted disks will
# show modification times as current time for directories.
#subdirectories = TRUE(to delete subdirectories) or FALSE(to manage files only)
#persistence time,
#list of files (globs,minimum persistence #) to manage.
# minimum persistence is the minimum number of files of
# a given glob type to keep
#BE CAREFUL modifying CONTROLLED_DIRECTORIES! Aviod using *.*!

CONTROLLED_DIRECTORIES =[ {
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'ephem')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('stp*.*',16),('*.orb_notif',0),
('*.temp',0)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'ephem')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*52,
'clean_list' : [('150_*.asc', 8),('150_*.dat', 8),('171_*.asc', 8),
('171_*.dat', 8),('172_*.asc', 8),('172_*.dat', 8),
('174_*.asc', 8),('174_*.dat', 8),('275_*.asc', 8),
('275_*.dat', 8),('tde_*.dat', 8),('tde_*.asc', 8),
('tdw_*.dat', 8),('tdw_*.asc', 8),('tds_*.asc', 8),
('tds_*.dat', 8),('tdrs_*', 16)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'def_ephem')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('std*.*',16),('unmerged_*.*',4),
('*.orb_notif',0),('*.temp',0)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'sched')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('*.instruct',0),('gstdn_*.*',16),
('replace_*.*',4),('sch_*.*',16), ('update_*.nml', 16)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'fdf')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('*stp_*.raw',4), ('*std_*.raw',4),
('*hst_linesumm*.*',4), ('*.bin_error', 0),
('*.txt_error', 0)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'fdf')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*52,
'clean_list' : [('*_TDE_ieee*.raw',4), ('*_TDW_ieee*.raw',4),
('*_TDS_ieee*.raw',4), ('*_150_ieee*.raw',4),
('*_171_ieee*.raw',4), ('*_172_ieee*.raw',4),
('*_275_ieee*.raw',4)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'clin')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('sa*_*.dat',4), ('ta*_*.dat',4), ('sb*_*.dat',4),
('sc*_*.dat',4), ('sa*_*.bas',4), ('ta*_*.bas',4),
('sb*_*.bas',4), ('sc*_*.bas',4), ('sd*_*.bas',4)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'msin')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('*.sms',4), ('*.csh',4), ('sa*.cin',4),
('ga*.cin',4), ('ta*.cin',4), ('sb*.cin',4),
('sc*.cin',4), ('sa*.bas',4), ('ga*.bas',4),
('ta*.bas',4), ('sb*.bas',4), ('sc*.bas',4),
('sd*.bas',4), ('*.sms~',0), ('*.csh~',0),
('sa*.cin~',0), ('ga*.cin~',0), ('ta*.cin~',0),
('sb*.cin~',0), ('sc*.cin~',0), ('sa*.bas~',0),
('ga*.bas~',0), ('ta*.bas~',0), ('sb*.bas~',0),
('sc*.bas~',0), ('sd*.bas~',0), ('*.sms.orig*',4)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'ms')),
'is_directory' : True,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('sa*',4), ('ga*',4), ('ta*',4), ('sb*',4), ('sc*',4), ('sd*',4)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'cl')),
'is_directory' : True,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('sa*',4), ('ga*',4), ('ta*',4), ('sb*',4), ('sc*',4), ('sd*',4)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'mosr')),
'is_directory' : True,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('sa*',4), ('ta*',4), ('sb*',4), ('sc*',4), ('sd*',4)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'sars')),
'is_directory' : True,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('sa*',4), ('ga*',4), ('ta*',4), ('sb*',4),
('sc*',4), ('sd*',4)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'scripts')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('*.csh',10)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'pickles')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('mscl_*.pickle',10),
('sch_*.pickle', 10),
('eph_*.pickle', 10),
('pdb_*.pickle', 10),
('cal_*.pickle', 10)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'workspace')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('*.instruct',0), ('*_notif',0)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'tdo')),
'is_directory' : True,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('g*',0),('t*',0),('s*',0),('e*',0)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'avtut')),
'is_directory' : True,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('g*',0),('t*',0),('s*',0),('e*',0)]
},
{
'glob_directory' : os.path.normpath(os.path.join(PASSOUT,'logs')),
'is_directory' : False,
'persistence_time' : time_util.SECONDS_IN_A_WEEK*4,
'clean_list' : [('*.log',20)]
},
]

def run(*args):
"""Cleans directories in a manor defined by the
list of dictionaries CONTROLLED_DIRECTORIES

Options:

-test Does not delete files only reports to screen list of files that
are selected for deletion.
"""
allowed_options = ['test', 'out=']
try:
options, parms = spst_getopt.spst_getopt(args, allowed_options)
except Exception,e:
e = '%s\n%s' %(e,run.__doc__)
raise IOError(e)

if options.has_key('-test'):
test = True
else:
test = False

if options.has_key('-out'):
File = options['-out']
print "Sending output to", File
outfile = open(File, 'w')
else:
File = ''
outfile = sys.stdout

now = time_util.spss_time()
for clean_dict in CONTROLLED_DIRECTORIES:
for file_tuple in clean_dict['clean_list']:
file_glob_path = os.path.join(clean_dict['glob_directory'],file_tuple[0])
delete_list = spss_sys_util.glob(file_glob_path)
delete_list.sort(file_util.cmp_by_mod_time)

#ensure we keep the minimum number of files of this type
if file_tuple[1]:
delete_path_list = delete_list[:-file_tuple[1]]
else:
delete_path_list = delete_list

for file_path in delete_path_list:
#get the modification time of file in spss_time format
file_time = time_util.spss_time(
os.path.getmtime(file_path) - time_util.SYS_TO_SOGSSEC)
#if the file/directory has persisted on disk longer than
#the specified persistence time then delete it.
if file_time < now - clean_dict['persistence_time']:
if clean_dict['is_directory']:
try:
file_util.deltree(file_path, test, outfile)
except Exception,e:
outfile.write('%s \n\n unable to delete directory %s\n' % (e, file_path))
else:
if os.path.islink(file_path):
if not test:
try_chmod(file_path, outfile)
try:
os.unlink(file_path)
except Exception,e:
outfile.write( '%s \n\nfile not unlinked! \nFile path:%s\n' %(e,file_path))
else:
outfile.write('link selected for unlink: %s %s\n' % (file_path,file_time))
else:
if not test:
try_chmod(file_path, outfile)
try:
os.remove(file_path)
except Exception,e:
outfile.write('%s \n\nfile not removed! \nFile path:%s\n' %(e,file_path))
else:
outfile.write('file selected for remove: %s %s\n' % (file_path,file_time))
if File:
outfile.close()

def try_chmod(file_path, outfile):
try:
os.chmod(file_path,0777)
except Exception,e:
outfile.write('%s \n\nCould not chmod! \nFile path:%s\n' %(e,file_path))

if __name__ == '__main__':
run_string = "run("
for i in range(0,len(sys.argv)):
#Wrap single quotes around each input parameter
run_string = run_string + "'" + str(sys.argv[i]) + "'"
if i != len(sys.argv) - 1:
run_string = run_string + ","
run_string = run_string + ")"
exec "status = " + run_string