ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/move
Revision: 1.9
Committed: Wed Jul 25 03:05:45 2012 UTC (12 years, 9 months ago) by paus
Branch: MAIN
CVS Tags: Mit_029_pre1
Changes since 1.8: +8 -6 lines
Log Message:
Preparing for new tag (Mit_029).

File Contents

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