ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/removedir
Revision: 1.3
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.2: +3 -2 lines
Log Message:
Preparing for new tag (Mit_029).

File Contents

# User Rev Content
1 paus 1.1 #!/usr/bin/env python
2     #---------------------------------------------------------------------------------------------------
3     # Script to remove a file.
4     #
5     # Author: C.Paus (May 06, 2010)
6     #---------------------------------------------------------------------------------------------------
7     import os,sys,getopt,re,srm
8 paus 1.3 t2user = os.environ['TIER2_USER']
9 paus 1.1
10     def debugPrint(text):
11     if debug:
12     print ' DEBUG: ' + text
13    
14     def clean(file):
15     if re.search('dcap:',file):
16     g = file.split('/')
17     file = '/'.join(g[3:])
18     debugPrint(' Cleaned: ' + file)
19    
20     return file
21    
22    
23     def exists(target):
24     if re.search('/castor/cern.ch/',target):
25     debugPrint(' Identified a castor directory: ' + target)
26     cmd = 'rfdir ' + target
27     elif re.search('/pnfs/cmsaf.mit.edu/',target):
28     debugPrint(' Identified a tier-2 directory: ' + target)
29 paus 1.3 cmd = 'ssh ' + t2user + '@cgate.mit.edu ls -1 ' + target + ' \>\& /dev/null'
30 paus 1.1 elif re.search('/mnt/hadoop/cms/store',target):
31     debugPrint(' Identified a tier-2 hadoop directory: ' + target)
32     target = srm.convertToUrl(target,debug)
33     cmd = 'srmls ' + target + ' >& /dev/null'
34     else:
35     debugPrint(' Identified a normal directory: ' + target)
36     cmd = 'ls -1 ' + target + '>& /dev/null'
37    
38     status = os.system(cmd)
39     debugPrint(' Status: %d (on %s)' %(status,cmd))
40    
41     return (status == 0)
42    
43     def remove(source):
44     if re.search('/castor/cern.ch/',source):
45     debugPrint(' Identified castor file')
46     cmd = "stager_rm -M " + source + "; nsrm " + source
47     elif re.search('/pnfs/cmsaf.mit.edu/',source):
48     debugPrint(' Identified tier-2 file')
49 paus 1.3 cmd = 'ssh ' + t2user + '@cgate.mit.edu rm -rf ' + source
50 paus 1.1 elif re.search('/mnt/hadoop/cms/store/user/paus',source):
51     debugPrint(' Identified a tier-2 hadoop directory: ' + source)
52     source = srm.convertToUrl(source,debug)
53     cmd = 'srmrmdir ' + source + ' >& /dev/null'
54     else:
55     debugPrint(' Identified a normal directory')
56     cmd = 'rm -rf ' + source
57    
58     # ready to perform the requested operation
59     debugPrint(' -> removing directory with: ' + cmd)
60     status = 0
61     if exe == 1:
62     status = os.system(cmd)
63     if status != 0:
64     print ' ERROR: remove returned error %d (on %s)'%(status,cmd)
65    
66     return status
67    
68     #===================================================================================================
69     # Main starts here
70     #===================================================================================================
71     # Define string to explain usage of the script
72     usage = "\n Usage: removedir <source>"
73     usage += " --exe\n"
74     usage += " --debug\n"
75     usage += " --help\n\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 = False
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 = True
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     # Define source and target
113     source = clean(newArgv[0])
114    
115     # Test whether the source exists
116     if exists(source):
117     debugPrint("\n Removing: " + source)
118     remove(source)
119     elif not exists(source):
120     print ' ERROR: the source (' + source + ') does not exist.'