ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/remove
Revision: 1.7
Committed: Thu Aug 9 21:16:03 2012 UTC (12 years, 9 months ago) by paus
Branch: MAIN
CVS Tags: Mit_029b, Mit_029
Changes since 1.6: +1 -8 lines
Log Message:
For version 029.

File Contents

# User Rev Content
1 paus 1.1 #!/usr/bin/env python
2     #---------------------------------------------------------------------------------------------------
3 paus 1.3 # Script to remove a file.
4 paus 1.1 #
5     # Author: C.Paus (May 06, 2010)
6     #---------------------------------------------------------------------------------------------------
7 paus 1.4 import os,sys,getopt,re,srm
8 paus 1.6 t2user = os.environ['TIER2_USER']
9 paus 1.1
10     def debugPrint(text):
11 paus 1.4 if debug:
12 paus 1.1 print ' DEBUG: ' + text
13    
14 paus 1.5 def execute(cmd,debug):
15     if debug:
16     print ' DEBUG: ' + cmd
17     else:
18     os.system(cmd)
19    
20 paus 1.1 def clean(file):
21     if re.search('dcap:',file):
22     g = file.split('/')
23     file = '/'.join(g[3:])
24     debugPrint(' Cleaned: ' + file)
25    
26     return file
27    
28    
29     def exists(target):
30     if re.search('/castor/cern.ch/',target):
31     debugPrint(' Identified a castor directory: ' + target)
32     cmd = 'rfdir ' + target
33 paus 1.4 elif re.search('/mnt/hadoop/cms/store',target):
34     debugPrint(' Identified a tier-2 hadoop directory: ' + target)
35     target = srm.convertToUrl(target,debug)
36     cmd = 'srmls ' + target + ' >& /dev/null'
37 paus 1.1 else:
38     debugPrint(' Identified a normal directory: ' + target)
39     cmd = 'ls -1 ' + target + '>& /dev/null'
40    
41     status = os.system(cmd)
42 paus 1.4 debugPrint(' Status: %d (on %s)' %(status,cmd))
43 paus 1.1
44     return (status == 0)
45    
46     def remove(source):
47     if re.search('/castor/cern.ch/',source):
48     debugPrint(' Identified castor file')
49     cmd = "stager_rm -M " + source + "; nsrm " + source
50 paus 1.4 elif re.search('/mnt/hadoop/cms/store/user/paus',source):
51     debugPrint(' Identified a tier-2 hadoop directory: ' + source)
52     source = srm.convertToUrl(source,debug)
53     cmd = 'srmrm ' + source + ' >& /dev/null'
54 paus 1.1 else:
55     debugPrint(' Identified a normal directory')
56 paus 1.2 cmd = 'rm -rf ' + source
57 paus 1.1
58     # ready to perform the requested operation
59     debugPrint(' -> removing with: ' + cmd)
60 paus 1.2 status = 0
61     if exe == 1:
62     status = os.system(cmd)
63 paus 1.4 if status != 0:
64     print ' ERROR: remove returned error %d (on %s)'%(status,cmd)
65 paus 1.1
66     return status
67    
68 paus 1.5 def removeCatalog(source,debug):
69     tmp = os.getpid()
70     pid = "%d"%tmp
71     # which catalog is this one in?
72     catalogDir = '/home/cmsprod/catalog/local'
73     if re.search('/castor/cern.ch/',source):
74     catalogDir = '/home/cmsprod/catalog/cern'
75 paus 1.7 elif re.search('/mnt/hadoop/cms/store/user/paus',source):
76 paus 1.5 catalogDir = '/home/cmsprod/catalog/t2mit'
77     # now get the dataset and the book
78     f = source.split('/')
79     file = f[-1]
80     dataset = f[-2]
81     book = f[-4] + '/' + f[-3]
82    
83     dir = catalogDir + '/' + book + '/' + dataset
84    
85     # now remove the particular file from the record
86     cmd = 'cat ' + dir + '/RawFiles.?? | sort -u | grep -v ' + file + ' > /tmp/RawFiles.00.' + pid
87     execute(cmd,debug)
88     cmd = 'rm ' + dir + '/RawFiles.??'
89     execute(cmd,debug)
90     cmd = 'mv /tmp/RawFiles.00.' + pid + ' ' + dir + '/RawFiles.00'
91     execute(cmd,debug)
92     cmd = 'cat ' + dir + '/Files | grep -v ' + file + ' > /tmp/Files.' + pid
93     execute(cmd,debug)
94     cmd = 'mv /tmp/Files.' + pid + ' ' + dir + '/Files'
95     execute(cmd,debug)
96    
97     return
98    
99 paus 1.1 #===================================================================================================
100     # Main starts here
101     #===================================================================================================
102     # Define string to explain usage of the script
103     usage = "Usage: remove <source>"
104 paus 1.2 usage += " --exe\n"
105 paus 1.5 usage += " --catalog\n"
106 paus 1.1 usage += " --debug\n"
107     usage += " --help\n"
108    
109     # Define the valid options which can be specified and check out the command line
110 paus 1.5 valid = ['exe','catalog','debug','help']
111 paus 1.1 try:
112     opts, args = getopt.getopt(sys.argv[1:], "", valid)
113     except getopt.GetoptError, ex:
114     print usage
115     print str(ex)
116     sys.exit(1)
117    
118     # --------------------------------------------------------------------------------------------------
119     # Get all parameters for the production
120     # --------------------------------------------------------------------------------------------------
121     # Set defaults for each option
122 paus 1.5 catalog = False
123     debug = False
124     exe = 0
125 paus 1.1
126     # Read new values from the command line
127     for opt, arg in opts:
128     #print ' OPT , ARG: ' + opt + ' ' + arg
129     if opt == '--help':
130     print usage
131     sys.exit(0)
132     elif opt == '--debug':
133 paus 1.5 debug = True
134     elif opt == '--catalog':
135     catalog = True
136 paus 1.2 elif opt == '--exe':
137 paus 1.5 exe = 1
138 paus 1.1
139     newArgv = []
140     for arg in sys.argv[1:]:
141     #print ' ARG: ' + arg
142     if arg[:2] == "--":
143     continue
144     else:
145     newArgv.append(arg)
146    
147     # Define source and target
148     source = clean(newArgv[0])
149    
150     # Test whether the source exists
151     if exists(source):
152     debugPrint("\n Removing: " + source)
153     remove(source)
154     elif not exists(source):
155     print ' ERROR: the source ('+source+') does not exist.'
156 paus 1.5
157     # Remove also the catalog entry
158     if catalog:
159     removeCatalog(source,debug)