1 |
#!/usr/bin/env python
|
2 |
#---------------------------------------------------------------------------------------------------
|
3 |
# Script to make a directory for the production
|
4 |
#
|
5 |
# Author: C.Paus (Jan 08, 2011)
|
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 |
def exists(target):
|
22 |
storagePath = '/srm/managerv2?SFN='
|
23 |
if re.search('/castor/cern.ch/',target):
|
24 |
debugPrint(' Identified castor file')
|
25 |
storageEle = 'srm-cms.cern.ch'
|
26 |
storageUrl = 'srm://' + storageEle + ':8443' + storagePath + target
|
27 |
cmd = 'srmls ' + storageUrl + ' >& /dev/null'
|
28 |
elif re.search('/mnt/hadoop/cms/store/',target):
|
29 |
debugPrint(' Identified tier-2 hadoop file')
|
30 |
storagePath = '/srm/v2/server?SFN='
|
31 |
storageEle = 'se01.cmsaf.mit.edu'
|
32 |
storageUrl = 'srm://' + storageEle + ':8443' + storagePath + target
|
33 |
cmd = 'srmls ' + storageUrl + ' >& /dev/null'
|
34 |
else:
|
35 |
debugPrint(' Identified a normal directory')
|
36 |
cmd = 'ls -1 ' + target + ' >& /dev/null'
|
37 |
|
38 |
status = os.system(cmd)
|
39 |
debugPrint(' Status: %d (for: %s)' %(status,cmd))
|
40 |
|
41 |
return (status == 0)
|
42 |
|
43 |
def makeDir(target):
|
44 |
storagePath = '/srm/managerv2?SFN='
|
45 |
if re.search('/castor/cern.ch/',target):
|
46 |
debugPrint(' Identified castor file')
|
47 |
storageEle = 'srm-cms.cern.ch'
|
48 |
storageUrl = 'srm://' + storageEle + ':8443' + storagePath + target
|
49 |
cmd = 'srmmkdir ' + storageUrl + ' >& /dev/null' + \
|
50 |
'; srm-set-permissions -type=ADD -owner=RWX -group=RWX -other=RWX ' + storageUrl
|
51 |
elif re.search('/mnt/hadoop/cms/store/',target):
|
52 |
debugPrint(' Identified tier-2 hadoop file')
|
53 |
storagePath = '/srm/v2/server?SFN='
|
54 |
storageEle = 'se01.cmsaf.mit.edu'
|
55 |
storageUrl = 'srm://' + storageEle + ':8443' + storagePath + target
|
56 |
cmd = 'srmmkdir ' + storageUrl + ' >& /dev/null' + \
|
57 |
'; srm-set-permissions -type=ADD -owner=RWX -group=RWX -other=RWX ' + storageUrl
|
58 |
else:
|
59 |
debugPrint(' Identified a normal directory')
|
60 |
cmd = 'mkdir ' + target
|
61 |
|
62 |
# ready to perform the requested operation
|
63 |
debugPrint(' -> makedir with: ' + cmd)
|
64 |
status = 0
|
65 |
if exe == 1:
|
66 |
status = os.system(cmd)
|
67 |
|
68 |
return status
|
69 |
|
70 |
#===================================================================================================
|
71 |
# Main starts here
|
72 |
#===================================================================================================
|
73 |
# Define string to explain usage of the script
|
74 |
usage = "Usage: makedir <target>"
|
75 |
usage += " --exe\n"
|
76 |
usage += " --debug\n"
|
77 |
usage += " --help\n"
|
78 |
|
79 |
# Define the valid options which can be specified and check out the command line
|
80 |
valid = ['exe','debug','help']
|
81 |
try:
|
82 |
opts, args = getopt.getopt(sys.argv[1:], "", valid)
|
83 |
except getopt.GetoptError, ex:
|
84 |
print usage
|
85 |
print str(ex)
|
86 |
sys.exit(1)
|
87 |
|
88 |
# --------------------------------------------------------------------------------------------------
|
89 |
# Get all parameters for the production
|
90 |
# --------------------------------------------------------------------------------------------------
|
91 |
# Set defaults for each option
|
92 |
debug = 0
|
93 |
exe = 0
|
94 |
|
95 |
# Read new values from the command line
|
96 |
for opt, arg in opts:
|
97 |
#print ' OPT , ARG: ' + opt + ' ' + arg
|
98 |
if opt == '--help':
|
99 |
print usage
|
100 |
sys.exit(0)
|
101 |
elif opt == '--debug':
|
102 |
debug = 1
|
103 |
elif opt == '--exe':
|
104 |
exe = 1
|
105 |
|
106 |
newArgv = []
|
107 |
for arg in sys.argv[1:]:
|
108 |
#print ' ARG: ' + arg
|
109 |
if arg[:2] == "--":
|
110 |
continue
|
111 |
else:
|
112 |
newArgv.append(arg)
|
113 |
|
114 |
# cleanup the target
|
115 |
target = clean(newArgv[0])
|
116 |
|
117 |
# Test whether the target exists and make it if required
|
118 |
if exists(target):
|
119 |
debugPrint("\n No action required, target exists already: %s\n"%target)
|
120 |
elif not exists(target):
|
121 |
print ' make directory ('+target+').'
|
122 |
makeDir(target)
|