1 |
|
#!/usr/bin/env python |
2 |
|
#--------------------------------------------------------------------------------------------------- |
3 |
< |
# Script to move a file from one place to another. Restrictions to application of course apply :-) |
3 |
> |
# Script to remove a file. |
4 |
|
# |
5 |
|
# Author: C.Paus (May 06, 2010) |
6 |
|
#--------------------------------------------------------------------------------------------------- |
7 |
< |
import os,sys,getopt,re |
7 |
> |
import os,sys,getopt,re,srm |
8 |
|
|
9 |
|
def debugPrint(text): |
10 |
< |
if debug == 1: |
10 |
> |
if debug: |
11 |
|
print ' DEBUG: ' + text |
12 |
|
|
13 |
+ |
def execute(cmd,debug): |
14 |
+ |
if debug: |
15 |
+ |
print ' DEBUG: ' + cmd |
16 |
+ |
else: |
17 |
+ |
os.system(cmd) |
18 |
+ |
|
19 |
|
def clean(file): |
20 |
|
if re.search('dcap:',file): |
21 |
|
g = file.split('/') |
31 |
|
cmd = 'rfdir ' + target |
32 |
|
elif re.search('/pnfs/cmsaf.mit.edu/',target): |
33 |
|
debugPrint(' Identified a tier-2 directory: ' + target) |
34 |
< |
cmd = 'ssh paus@cgate.mit.edu ls -1 ' + target + '\>\& /dev/null' |
34 |
> |
cmd = 'ssh -x paus@cgate.mit.edu ls -1 ' + target + ' \>\& /dev/null' |
35 |
> |
elif re.search('/mnt/hadoop/cms/store',target): |
36 |
> |
debugPrint(' Identified a tier-2 hadoop directory: ' + target) |
37 |
> |
target = srm.convertToUrl(target,debug) |
38 |
> |
cmd = 'srmls ' + target + ' >& /dev/null' |
39 |
|
else: |
40 |
|
debugPrint(' Identified a normal directory: ' + target) |
41 |
|
cmd = 'ls -1 ' + target + '>& /dev/null' |
42 |
|
|
43 |
|
status = os.system(cmd) |
44 |
< |
debugPrint(' Status: %d' % status) |
44 |
> |
debugPrint(' Status: %d (on %s)' %(status,cmd)) |
45 |
|
|
46 |
|
return (status == 0) |
47 |
|
|
51 |
|
cmd = "stager_rm -M " + source + "; nsrm " + source |
52 |
|
elif re.search('/pnfs/cmsaf.mit.edu/',source): |
53 |
|
debugPrint(' Identified tier-2 file') |
54 |
< |
cmd = 'ssh paus@cgate.mit.edu rm -rf ' + source |
54 |
> |
cmd = 'ssh -x paus@cgate.mit.edu rm -rf ' + source |
55 |
> |
elif re.search('/mnt/hadoop/cms/store/user/paus',source): |
56 |
> |
debugPrint(' Identified a tier-2 hadoop directory: ' + source) |
57 |
> |
source = srm.convertToUrl(source,debug) |
58 |
> |
cmd = 'srmrm ' + source + ' >& /dev/null' |
59 |
|
else: |
60 |
|
debugPrint(' Identified a normal directory') |
61 |
|
cmd = 'rm -rf ' + source |
65 |
|
status = 0 |
66 |
|
if exe == 1: |
67 |
|
status = os.system(cmd) |
68 |
+ |
if status != 0: |
69 |
+ |
print ' ERROR: remove returned error %d (on %s)'%(status,cmd) |
70 |
|
|
71 |
|
return status |
72 |
|
|
73 |
+ |
def removeCatalog(source,debug): |
74 |
+ |
tmp = os.getpid() |
75 |
+ |
pid = "%d"%tmp |
76 |
+ |
# which catalog is this one in? |
77 |
+ |
catalogDir = '/home/cmsprod/catalog/local' |
78 |
+ |
if re.search('/castor/cern.ch/',source): |
79 |
+ |
catalogDir = '/home/cmsprod/catalog/cern' |
80 |
+ |
elif re.search('/pnfs/cmsaf.mit.edu/',source) or \ |
81 |
+ |
re.search('/mnt/hadoop/cms/store/user/paus',source): |
82 |
+ |
catalogDir = '/home/cmsprod/catalog/t2mit' |
83 |
+ |
# now get the dataset and the book |
84 |
+ |
f = source.split('/') |
85 |
+ |
file = f[-1] |
86 |
+ |
dataset = f[-2] |
87 |
+ |
book = f[-4] + '/' + f[-3] |
88 |
+ |
|
89 |
+ |
dir = catalogDir + '/' + book + '/' + dataset |
90 |
+ |
|
91 |
+ |
# now remove the particular file from the record |
92 |
+ |
cmd = 'cat ' + dir + '/RawFiles.?? | sort -u | grep -v ' + file + ' > /tmp/RawFiles.00.' + pid |
93 |
+ |
execute(cmd,debug) |
94 |
+ |
cmd = 'rm ' + dir + '/RawFiles.??' |
95 |
+ |
execute(cmd,debug) |
96 |
+ |
cmd = 'mv /tmp/RawFiles.00.' + pid + ' ' + dir + '/RawFiles.00' |
97 |
+ |
execute(cmd,debug) |
98 |
+ |
cmd = 'cat ' + dir + '/Files | grep -v ' + file + ' > /tmp/Files.' + pid |
99 |
+ |
execute(cmd,debug) |
100 |
+ |
cmd = 'mv /tmp/Files.' + pid + ' ' + dir + '/Files' |
101 |
+ |
execute(cmd,debug) |
102 |
+ |
|
103 |
+ |
return |
104 |
+ |
|
105 |
|
#=================================================================================================== |
106 |
|
# Main starts here |
107 |
|
#=================================================================================================== |
108 |
|
# Define string to explain usage of the script |
109 |
|
usage = "Usage: remove <source>" |
110 |
|
usage += " --exe\n" |
111 |
+ |
usage += " --catalog\n" |
112 |
|
usage += " --debug\n" |
113 |
|
usage += " --help\n" |
114 |
|
|
115 |
|
# Define the valid options which can be specified and check out the command line |
116 |
< |
valid = ['exe','debug','help'] |
116 |
> |
valid = ['exe','catalog','debug','help'] |
117 |
|
try: |
118 |
|
opts, args = getopt.getopt(sys.argv[1:], "", valid) |
119 |
|
except getopt.GetoptError, ex: |
125 |
|
# Get all parameters for the production |
126 |
|
# -------------------------------------------------------------------------------------------------- |
127 |
|
# Set defaults for each option |
128 |
< |
debug = 0 |
129 |
< |
exe = 0 |
128 |
> |
catalog = False |
129 |
> |
debug = False |
130 |
> |
exe = 0 |
131 |
|
|
132 |
|
# Read new values from the command line |
133 |
|
for opt, arg in opts: |
136 |
|
print usage |
137 |
|
sys.exit(0) |
138 |
|
elif opt == '--debug': |
139 |
< |
debug = 1 |
139 |
> |
debug = True |
140 |
> |
elif opt == '--catalog': |
141 |
> |
catalog = True |
142 |
|
elif opt == '--exe': |
143 |
< |
exe = 1 |
143 |
> |
exe = 1 |
144 |
|
|
145 |
|
newArgv = [] |
146 |
|
for arg in sys.argv[1:]: |
159 |
|
remove(source) |
160 |
|
elif not exists(source): |
161 |
|
print ' ERROR: the source ('+source+') does not exist.' |
162 |
+ |
|
163 |
+ |
# Remove also the catalog entry |
164 |
+ |
if catalog: |
165 |
+ |
removeCatalog(source,debug) |