ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/makedir
Revision: 1.4
Committed: Thu Aug 9 21:16:03 2012 UTC (12 years, 8 months ago) by paus
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, Mit_029, HEAD
Changes since 1.3: +0 -11 lines
Log Message:
For version 029.

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 paus 1.2 elif re.search('/mnt/hadoop/cms/store/',target):
29     debugPrint(' Identified tier-2 hadoop file')
30     storagePath = '/srm/v2/server?SFN='
31 paus 1.3 storageEle = 'se01.cmsaf.mit.edu'
32 paus 1.2 storageUrl = 'srm://' + storageEle + ':8443' + storagePath + target
33     cmd = 'srmls ' + storageUrl + ' >& /dev/null'
34 paus 1.1 else:
35     debugPrint(' Identified a normal directory')
36     cmd = 'ls -1 ' + target + ' >& /dev/null'
37    
38     status = os.system(cmd)
39 paus 1.3 debugPrint(' Status: %d (for: %s)' %(status,cmd))
40 paus 1.1
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 paus 1.2 elif re.search('/mnt/hadoop/cms/store/',target):
52     debugPrint(' Identified tier-2 hadoop file')
53     storagePath = '/srm/v2/server?SFN='
54 paus 1.3 storageEle = 'se01.cmsaf.mit.edu'
55 paus 1.2 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 paus 1.1 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)