1 |
#!/usr/bin/env python
|
2 |
#---------------------------------------------------------------------------------------------------
|
3 |
# Script to remove a file.
|
4 |
#
|
5 |
# Author: C.Paus (May 06, 2010)
|
6 |
#---------------------------------------------------------------------------------------------------
|
7 |
import os,sys,getopt,re,srm
|
8 |
|
9 |
def debugPrint(text):
|
10 |
if debug:
|
11 |
print ' DEBUG: ' + text
|
12 |
|
13 |
def clean(file):
|
14 |
if re.search('dcap:',file):
|
15 |
g = file.split('/')
|
16 |
file = '/'.join(g[3:])
|
17 |
debugPrint(' Cleaned: ' + file)
|
18 |
|
19 |
return file
|
20 |
|
21 |
|
22 |
def exists(target):
|
23 |
if re.search('/castor/cern.ch/',target):
|
24 |
debugPrint(' Identified a castor directory: ' + target)
|
25 |
cmd = 'rfdir ' + target
|
26 |
elif re.search('/pnfs/cmsaf.mit.edu/',target):
|
27 |
debugPrint(' Identified a tier-2 directory: ' + target)
|
28 |
cmd = 'ssh paus@cgate.mit.edu ls -1 ' + target + ' \>\& /dev/null'
|
29 |
elif re.search('/mnt/hadoop/cms/store',target):
|
30 |
debugPrint(' Identified a tier-2 hadoop directory: ' + target)
|
31 |
target = srm.convertToUrl(target,debug)
|
32 |
cmd = 'srmls ' + target + ' >& /dev/null'
|
33 |
else:
|
34 |
debugPrint(' Identified a normal directory: ' + target)
|
35 |
cmd = 'ls -1 ' + target + '>& /dev/null'
|
36 |
|
37 |
status = os.system(cmd)
|
38 |
debugPrint(' Status: %d (on %s)' %(status,cmd))
|
39 |
|
40 |
return (status == 0)
|
41 |
|
42 |
def remove(source):
|
43 |
if re.search('/castor/cern.ch/',source):
|
44 |
debugPrint(' Identified castor file')
|
45 |
cmd = "stager_rm -M " + source + "; nsrm " + source
|
46 |
elif re.search('/pnfs/cmsaf.mit.edu/',source):
|
47 |
debugPrint(' Identified tier-2 file')
|
48 |
cmd = 'ssh paus@cgate.mit.edu rm -rf ' + source
|
49 |
elif re.search('/mnt/hadoop/cms/store/user/paus',source):
|
50 |
debugPrint(' Identified a tier-2 hadoop directory: ' + source)
|
51 |
source = srm.convertToUrl(source,debug)
|
52 |
cmd = 'srmrm ' + source + ' >& /dev/null'
|
53 |
else:
|
54 |
debugPrint(' Identified a normal directory')
|
55 |
cmd = 'rm -rf ' + source
|
56 |
|
57 |
# ready to perform the requested operation
|
58 |
debugPrint(' -> removing with: ' + cmd)
|
59 |
status = 0
|
60 |
if exe == 1:
|
61 |
status = os.system(cmd)
|
62 |
if status != 0:
|
63 |
print ' ERROR: remove returned error %d (on %s)'%(status,cmd)
|
64 |
|
65 |
return status
|
66 |
|
67 |
#===================================================================================================
|
68 |
# Main starts here
|
69 |
#===================================================================================================
|
70 |
# Define string to explain usage of the script
|
71 |
usage = "Usage: remove <source>"
|
72 |
usage += " --exe\n"
|
73 |
usage += " --debug\n"
|
74 |
usage += " --help\n"
|
75 |
|
76 |
# Define the valid options which can be specified and check out the command line
|
77 |
valid = ['exe','debug','help']
|
78 |
try:
|
79 |
opts, args = getopt.getopt(sys.argv[1:], "", valid)
|
80 |
except getopt.GetoptError, ex:
|
81 |
print usage
|
82 |
print str(ex)
|
83 |
sys.exit(1)
|
84 |
|
85 |
# --------------------------------------------------------------------------------------------------
|
86 |
# Get all parameters for the production
|
87 |
# --------------------------------------------------------------------------------------------------
|
88 |
# Set defaults for each option
|
89 |
debug = False
|
90 |
exe = 0
|
91 |
|
92 |
# Read new values from the command line
|
93 |
for opt, arg in opts:
|
94 |
#print ' OPT , ARG: ' + opt + ' ' + arg
|
95 |
if opt == '--help':
|
96 |
print usage
|
97 |
sys.exit(0)
|
98 |
elif opt == '--debug':
|
99 |
debug = True
|
100 |
elif opt == '--exe':
|
101 |
exe = 1
|
102 |
|
103 |
newArgv = []
|
104 |
for arg in sys.argv[1:]:
|
105 |
#print ' ARG: ' + arg
|
106 |
if arg[:2] == "--":
|
107 |
continue
|
108 |
else:
|
109 |
newArgv.append(arg)
|
110 |
|
111 |
# Define source and target
|
112 |
source = clean(newArgv[0])
|
113 |
|
114 |
# Test whether the source exists
|
115 |
if exists(source):
|
116 |
debugPrint("\n Removing: " + source)
|
117 |
remove(source)
|
118 |
elif not exists(source):
|
119 |
print ' ERROR: the source ('+source+') does not exist.'
|