1 |
#!/usr/bin/env python
|
2 |
#---------------------------------------------------------------------------------------------------
|
3 |
# Script to cleanup the potentially bulky production output
|
4 |
#
|
5 |
# Author: C.Paus (February 8, 2010)
|
6 |
#---------------------------------------------------------------------------------------------------
|
7 |
import os,sys,getopt,re,string
|
8 |
|
9 |
# Make list of files to consider for cleaning
|
10 |
#--------------------------------------------
|
11 |
def makeFileList(crabId):
|
12 |
print ' Make the file list for crab Id '
|
13 |
allFiles = []
|
14 |
cmd = 'find ./' + crabId + '/res \( -name \*.stderr -o -name \*.stdout \)'
|
15 |
for line in os.popen(cmd).readlines(): # run command
|
16 |
line = line[:-1] # strip '\n'
|
17 |
#print ' LINE: ' + line
|
18 |
file = line # splitting every blank
|
19 |
#file = f.pop()
|
20 |
|
21 |
allFiles.append(file)
|
22 |
#crabTask.show()
|
23 |
return allFiles
|
24 |
|
25 |
# Cleanup one given file
|
26 |
#-----------------------
|
27 |
def cleanupFile(file):
|
28 |
print ' Cleaning file: ' + file
|
29 |
|
30 |
cmd = ' gzip ' + file
|
31 |
status = os.system(cmd)
|
32 |
|
33 |
#cmd = ' mv ' + file + ' ' + file + '.original'
|
34 |
#status = os.system(cmd)
|
35 |
#cmd = ' cat ' + file + '.original | grep -v \'Begin processing the\' > ' + file
|
36 |
#status = os.system(cmd)
|
37 |
#cmd = ' rm ' + file + '.original'
|
38 |
#status = os.system(cmd)
|
39 |
|
40 |
#===================================================================================================
|
41 |
# Main starts here
|
42 |
#===================================================================================================
|
43 |
# Define string to explain usage of the script
|
44 |
usage = "Usage: cleanupLog.py --crabId=<name>\n"
|
45 |
usage += " --help\n"
|
46 |
|
47 |
# Define the valid options which can be specified and check out the command line
|
48 |
valid = ['crabId=','help']
|
49 |
try:
|
50 |
opts, args = getopt.getopt(sys.argv[1:], "", valid)
|
51 |
except getopt.GetoptError, ex:
|
52 |
print usage
|
53 |
print str(ex)
|
54 |
sys.exit(1)
|
55 |
|
56 |
# --------------------------------------------------------------------------------------------------
|
57 |
# Get all parameters for the production
|
58 |
# --------------------------------------------------------------------------------------------------
|
59 |
# Set defaults for each option
|
60 |
crabId = None
|
61 |
|
62 |
# Read new values from the command line
|
63 |
for opt, arg in opts:
|
64 |
if opt == "--help":
|
65 |
print usage
|
66 |
sys.exit(0)
|
67 |
if opt == "--crabId":
|
68 |
crabId = arg
|
69 |
|
70 |
# Deal with obvious problems
|
71 |
if crabId == None:
|
72 |
cmd = "--crabId required parameter not provided."
|
73 |
raise RuntimeError, cmd
|
74 |
|
75 |
cmd = 'du -s --block-size=1 ' + crabId + ' | cut -f1'
|
76 |
for line in os.popen(cmd).readlines(): # run command
|
77 |
sizeBefore = line[:-1] # strip '\n'
|
78 |
|
79 |
allFiles = []
|
80 |
allFiles = makeFileList(crabId)
|
81 |
|
82 |
for file in allFiles:
|
83 |
#print ' Clean file: %s'%(file)
|
84 |
cleanupFile(file)
|
85 |
|
86 |
cmd = 'du -s --block-size=1 ' + crabId + ' | cut -f1'
|
87 |
for line in os.popen(cmd).readlines(): # run command
|
88 |
sizeAfter = line[:-1] # strip '\n'
|
89 |
|
90 |
sizeBeforeMb = int(sizeBefore)/1024./1024.
|
91 |
sizeAfterMb = int(sizeAfter)/1024./1024.
|
92 |
deltaMb = (int(sizeBefore)-int(sizeAfter))/1024./1024.
|
93 |
|
94 |
print ' '
|
95 |
print ' Space before: %12.4f MB'%sizeBeforeMb
|
96 |
print ' Space after : %12.4f MB'%sizeAfterMb
|
97 |
print ' ==================================='
|
98 |
print ' Gained : %12.4f MB'%deltaMb
|
99 |
print ' '
|