ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/makedir
Revision: 1.1
Committed: Thu Mar 24 15:48:11 2011 UTC (14 years, 1 month ago) by paus
Branch: MAIN
CVS Tags: Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a
Log Message:
Make directories also remotely.

File Contents

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