1 |
#!/usr/bin/env python
|
2 |
#---------------------------------------------------------------------------------------------------
|
3 |
# Script to move a file from one place to another. Restrictions to application of course apply :-)
|
4 |
#
|
5 |
# Author: C.Paus (May 06, 2010)
|
6 |
#---------------------------------------------------------------------------------------------------
|
7 |
import os,sys,getopt,re
|
8 |
|
9 |
def debugPrint(text):
|
10 |
if debug == 1:
|
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 |
else:
|
30 |
debugPrint(' Identified a normal directory: ' + target)
|
31 |
cmd = 'ls -1 ' + target + '>& /dev/null'
|
32 |
|
33 |
status = os.system(cmd)
|
34 |
debugPrint(' Status: %d' % status)
|
35 |
|
36 |
return (status == 0)
|
37 |
|
38 |
def remove(source):
|
39 |
if re.search('/castor/cern.ch/',source):
|
40 |
debugPrint(' Identified castor file')
|
41 |
cmd = "stager_rm -M " + source + "; nsrm " + source
|
42 |
elif re.search('/pnfs/cmsaf.mit.edu/',source):
|
43 |
debugPrint(' Identified tier-2 file')
|
44 |
cmd = 'ssh paus@cgate.mit.edu rm -rf ' + source
|
45 |
else:
|
46 |
debugPrint(' Identified a normal directory')
|
47 |
cmd = 'rm -rf ' + source
|
48 |
|
49 |
# ready to perform the requested operation
|
50 |
debugPrint(' -> removing with: ' + cmd)
|
51 |
status = 0
|
52 |
if exe == 1:
|
53 |
status = os.system(cmd)
|
54 |
|
55 |
return status
|
56 |
|
57 |
#===================================================================================================
|
58 |
# Main starts here
|
59 |
#===================================================================================================
|
60 |
# Define string to explain usage of the script
|
61 |
usage = "Usage: remove <source>"
|
62 |
usage += " --exe\n"
|
63 |
usage += " --debug\n"
|
64 |
usage += " --help\n"
|
65 |
|
66 |
# Define the valid options which can be specified and check out the command line
|
67 |
valid = ['exe','debug','help']
|
68 |
try:
|
69 |
opts, args = getopt.getopt(sys.argv[1:], "", valid)
|
70 |
except getopt.GetoptError, ex:
|
71 |
print usage
|
72 |
print str(ex)
|
73 |
sys.exit(1)
|
74 |
|
75 |
# --------------------------------------------------------------------------------------------------
|
76 |
# Get all parameters for the production
|
77 |
# --------------------------------------------------------------------------------------------------
|
78 |
# Set defaults for each option
|
79 |
debug = 0
|
80 |
exe = 0
|
81 |
|
82 |
# Read new values from the command line
|
83 |
for opt, arg in opts:
|
84 |
#print ' OPT , ARG: ' + opt + ' ' + arg
|
85 |
if opt == '--help':
|
86 |
print usage
|
87 |
sys.exit(0)
|
88 |
elif opt == '--debug':
|
89 |
debug = 1
|
90 |
elif opt == '--exe':
|
91 |
exe = 1
|
92 |
|
93 |
newArgv = []
|
94 |
for arg in sys.argv[1:]:
|
95 |
#print ' ARG: ' + arg
|
96 |
if arg[:2] == "--":
|
97 |
continue
|
98 |
else:
|
99 |
newArgv.append(arg)
|
100 |
|
101 |
# Define source and target
|
102 |
source = clean(newArgv[0])
|
103 |
|
104 |
# Test whether the source exists
|
105 |
if exists(source):
|
106 |
debugPrint("\n Removing: " + source)
|
107 |
remove(source)
|
108 |
elif not exists(source):
|
109 |
print ' ERROR: the source ('+source+') does not exist.'
|