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 |
import srm
|
9 |
user = os.environ['TICKET_HOLDER']
|
10 |
t2user = os.environ['TIER2_USER']
|
11 |
|
12 |
def debugPrint(text):
|
13 |
if debug ==1:
|
14 |
print ' DEBUG: ' + text
|
15 |
|
16 |
def clean(file):
|
17 |
if re.search('dcap:',file):
|
18 |
g = file.split('/')
|
19 |
file = '/'.join(g[3:])
|
20 |
if re.search('root:',file):
|
21 |
g = file.split('/')
|
22 |
file = '/'.join(g[3:])
|
23 |
file = '/mnt/hadoop/cms' + file
|
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 = 'ssh ' + t2user + '@lxplus.cern.ch rfdir ' + target + '>& /dev/null'
|
33 |
elif re.search('/mnt/hadoop/cms/store',target):
|
34 |
debugPrint(' Identified a tier-2 directory: ' + target)
|
35 |
cmd = 'ssh ' + t2user + '@se01.cmsaf.mit.edu ls -1 ' + target + ' \>\& /dev/null'
|
36 |
else:
|
37 |
debugPrint(' Identified a normal directory: ' + target)
|
38 |
cmd = 'ls -1 ' + target + '>\& /dev/null'
|
39 |
|
40 |
status = os.system(cmd)
|
41 |
debugPrint(' Status: %d' % status)
|
42 |
|
43 |
return (status == 0)
|
44 |
|
45 |
def move(source,target):
|
46 |
if re.search('/castor/cern.ch/',source) and re.search('/castor/cern.ch/',target):
|
47 |
debugPrint(' Identified two castor directories')
|
48 |
cmd = 'ssh ' + user + '@lxplus.cern.ch nsrename ' + source + ' ' + target
|
49 |
elif re.search('/mnt/hadoop/cms/store',target):
|
50 |
urlSource = srm.convertToUrl(source,False)
|
51 |
urlTarget = srm.convertToUrl(target,False)
|
52 |
cmd = 'srmmv ' + urlSource + ' ' + urlTarget + ' >& /dev/null'
|
53 |
cmd = 'glexec mv ' + source + ' ' + target ## + ' >& /dev/null'
|
54 |
#debugPrint(' Identified a tier-2 directory: ' + target)
|
55 |
#cmd = 'ssh ' + t2user + '@se01.cmsaf.mit.edu ls -1 ' + target + ' \>\& /dev/null'
|
56 |
else:
|
57 |
debugPrint(' Identified a normal directory')
|
58 |
cmd = 'mv ' + source + ' ' + target
|
59 |
|
60 |
# ready to perfrom the requested operation
|
61 |
debugPrint(' -> moving with: ' + cmd)
|
62 |
## status = 0
|
63 |
status = os.system(cmd)
|
64 |
|
65 |
return status
|
66 |
|
67 |
#===================================================================================================
|
68 |
# Main starts here
|
69 |
#===================================================================================================
|
70 |
# Define string to explain usage of the script
|
71 |
usage = "Usage: list <source> <target>"
|
72 |
usage += " --debug\n"
|
73 |
usage += " --help\n"
|
74 |
|
75 |
# Define the valid options which can be specified and check out the command line
|
76 |
valid = ['debug','help']
|
77 |
try:
|
78 |
opts, args = getopt.getopt(sys.argv[1:], "", valid)
|
79 |
except getopt.GetoptError, ex:
|
80 |
print usage
|
81 |
print str(ex)
|
82 |
sys.exit(1)
|
83 |
|
84 |
# --------------------------------------------------------------------------------------------------
|
85 |
# Get all parameters for the production
|
86 |
# --------------------------------------------------------------------------------------------------
|
87 |
# Set defaults for each option
|
88 |
debug = 0
|
89 |
|
90 |
# Read new values from the command line
|
91 |
for opt, arg in opts:
|
92 |
#print ' OPT , ARG: ' + opt + ' ' + arg
|
93 |
if opt == '--help':
|
94 |
print usage
|
95 |
sys.exit(0)
|
96 |
elif opt == '--debug':
|
97 |
debug = 1
|
98 |
|
99 |
newArgv = []
|
100 |
for arg in sys.argv[1:]:
|
101 |
#print ' ARG: ' + arg
|
102 |
if arg[:2] == "--":
|
103 |
continue
|
104 |
else:
|
105 |
newArgv.append(arg)
|
106 |
|
107 |
# Define source and target
|
108 |
source = clean(newArgv[0])
|
109 |
target = clean(newArgv[1])
|
110 |
|
111 |
# Test whether the source exists and that the target does not exist
|
112 |
if exists(source) and not exists(target):
|
113 |
debugPrint("\n Moving: " + source + "\n to " + target + "\n")
|
114 |
move(source,target)
|
115 |
elif not exists(source):
|
116 |
print ' ERROR: the source ('+source+') does not exist.'
|
117 |
elif exists(target):
|
118 |
print ' ERROR: the target ('+target+') exists already.'
|
119 |
else:
|
120 |
print ' ERROR: hmmm.... what is wrong here?'
|