ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/remove
Revision: 1.8
Committed: Fri Jan 18 22:55:25 2013 UTC (12 years, 3 months ago) by paus
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_030, Mit_029c, Mit_030_pre1, Mit_029a, HEAD
Changes since 1.7: +3 -2 lines
Log Message:
Adjustments.

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 paus 1.8 #source = srm.convertToUrl(source,debug)
53     #cmd = 'srmrm ' + source + ' >& /dev/null'
54     cmd = 'glexec rm ' + source
55 paus 1.1 else:
56     debugPrint(' Identified a normal directory')
57 paus 1.2 cmd = 'rm -rf ' + source
58 paus 1.1
59     # ready to perform the requested operation
60     debugPrint(' -> removing with: ' + cmd)
61 paus 1.2 status = 0
62     if exe == 1:
63     status = os.system(cmd)
64 paus 1.4 if status != 0:
65     print ' ERROR: remove returned error %d (on %s)'%(status,cmd)
66 paus 1.1
67     return status
68    
69 paus 1.5 def removeCatalog(source,debug):
70     tmp = os.getpid()
71     pid = "%d"%tmp
72     # which catalog is this one in?
73     catalogDir = '/home/cmsprod/catalog/local'
74     if re.search('/castor/cern.ch/',source):
75     catalogDir = '/home/cmsprod/catalog/cern'
76 paus 1.7 elif re.search('/mnt/hadoop/cms/store/user/paus',source):
77 paus 1.5 catalogDir = '/home/cmsprod/catalog/t2mit'
78     # now get the dataset and the book
79     f = source.split('/')
80     file = f[-1]
81     dataset = f[-2]
82     book = f[-4] + '/' + f[-3]
83    
84     dir = catalogDir + '/' + book + '/' + dataset
85    
86     # now remove the particular file from the record
87     cmd = 'cat ' + dir + '/RawFiles.?? | sort -u | grep -v ' + file + ' > /tmp/RawFiles.00.' + pid
88     execute(cmd,debug)
89     cmd = 'rm ' + dir + '/RawFiles.??'
90     execute(cmd,debug)
91     cmd = 'mv /tmp/RawFiles.00.' + pid + ' ' + dir + '/RawFiles.00'
92     execute(cmd,debug)
93     cmd = 'cat ' + dir + '/Files | grep -v ' + file + ' > /tmp/Files.' + pid
94     execute(cmd,debug)
95     cmd = 'mv /tmp/Files.' + pid + ' ' + dir + '/Files'
96     execute(cmd,debug)
97    
98     return
99    
100 paus 1.1 #===================================================================================================
101     # Main starts here
102     #===================================================================================================
103     # Define string to explain usage of the script
104     usage = "Usage: remove <source>"
105 paus 1.2 usage += " --exe\n"
106 paus 1.5 usage += " --catalog\n"
107 paus 1.1 usage += " --debug\n"
108     usage += " --help\n"
109    
110     # Define the valid options which can be specified and check out the command line
111 paus 1.5 valid = ['exe','catalog','debug','help']
112 paus 1.1 try:
113     opts, args = getopt.getopt(sys.argv[1:], "", valid)
114     except getopt.GetoptError, ex:
115     print usage
116     print str(ex)
117     sys.exit(1)
118    
119     # --------------------------------------------------------------------------------------------------
120     # Get all parameters for the production
121     # --------------------------------------------------------------------------------------------------
122     # Set defaults for each option
123 paus 1.5 catalog = False
124     debug = False
125     exe = 0
126 paus 1.1
127     # Read new values from the command line
128     for opt, arg in opts:
129     #print ' OPT , ARG: ' + opt + ' ' + arg
130     if opt == '--help':
131     print usage
132     sys.exit(0)
133     elif opt == '--debug':
134 paus 1.5 debug = True
135     elif opt == '--catalog':
136     catalog = True
137 paus 1.2 elif opt == '--exe':
138 paus 1.5 exe = 1
139 paus 1.1
140     newArgv = []
141     for arg in sys.argv[1:]:
142     #print ' ARG: ' + arg
143     if arg[:2] == "--":
144     continue
145     else:
146     newArgv.append(arg)
147    
148     # Define source and target
149     source = clean(newArgv[0])
150    
151     # Test whether the source exists
152     if exists(source):
153     debugPrint("\n Removing: " + source)
154     remove(source)
155     elif not exists(source):
156     print ' ERROR: the source ('+source+') does not exist.'
157 paus 1.5
158     # Remove also the catalog entry
159     if catalog:
160     removeCatalog(source,debug)