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 + '>& /dev/null'
|
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 move(source,target):
|
39 |
if re.search('/castor/cern.ch/',source) and re.search('/castor/cern.ch/',target):
|
40 |
debugPrint(' Identified two castor directories')
|
41 |
cmd = 'nsrename ' + source + ' ' + target
|
42 |
elif re.search('/pnfs/cmsaf.mit.edu/',source) and re.search('/pnfs/cmsaf.mit.edu/',target):
|
43 |
debugPrint(' Identified two tier-2 directories')
|
44 |
cmd = 'ssh paus@cgate.mit.edu mv ' + source + ' ' + target
|
45 |
else:
|
46 |
debugPrint(' Identified a normal directory')
|
47 |
cmd = 'mv ' + source + ' ' + target
|
48 |
|
49 |
# ready to perfrom the requested operation
|
50 |
debugPrint(' -> moving with: ' + cmd)
|
51 |
## status = 0
|
52 |
status = os.system(cmd)
|
53 |
|
54 |
return status
|
55 |
|
56 |
#===================================================================================================
|
57 |
# Main starts here
|
58 |
#===================================================================================================
|
59 |
# Define string to explain usage of the script
|
60 |
usage = "Usage: list <source> <target>"
|
61 |
usage += " --debug\n"
|
62 |
usage += " --help\n"
|
63 |
|
64 |
# Define the valid options which can be specified and check out the command line
|
65 |
valid = ['debug','help']
|
66 |
try:
|
67 |
opts, args = getopt.getopt(sys.argv[1:], "", valid)
|
68 |
except getopt.GetoptError, ex:
|
69 |
print usage
|
70 |
print str(ex)
|
71 |
sys.exit(1)
|
72 |
|
73 |
# --------------------------------------------------------------------------------------------------
|
74 |
# Get all parameters for the production
|
75 |
# --------------------------------------------------------------------------------------------------
|
76 |
# Set defaults for each option
|
77 |
debug = 0
|
78 |
|
79 |
# Read new values from the command line
|
80 |
for opt, arg in opts:
|
81 |
#print ' OPT , ARG: ' + opt + ' ' + arg
|
82 |
if opt == '--help':
|
83 |
print usage
|
84 |
sys.exit(0)
|
85 |
elif opt == '--debug':
|
86 |
debug = 1
|
87 |
|
88 |
newArgv = []
|
89 |
for arg in sys.argv[1:]:
|
90 |
#print ' ARG: ' + arg
|
91 |
if arg[:2] == "--":
|
92 |
continue
|
93 |
else:
|
94 |
newArgv.append(arg)
|
95 |
|
96 |
# Define source and target
|
97 |
source = clean(newArgv[0])
|
98 |
target = clean(newArgv[1])
|
99 |
|
100 |
# Test whether the source exists and that the target does not exist
|
101 |
if exists(source) and not exists(target):
|
102 |
debugPrint("\n Moving: " + source + "\n to " + target + "\n")
|
103 |
move(source,target)
|
104 |
elif not exists(source):
|
105 |
print ' ERROR: the source ('+source+') does not exist.'
|
106 |
elif exists(target):
|
107 |
print ' ERROR: the target ('+target+') exists already.'
|
108 |
else:
|
109 |
print ' ERROR: hmmm.... what is wrong here?'
|