ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/removedir
Revision: 1.4
Committed: Thu Aug 9 21:16:03 2012 UTC (12 years, 9 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 -6 lines
Log Message:
For version 029.

File Contents

# Content
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 t2user = os.environ['TIER2_USER']
9
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('/mnt/hadoop/cms/store',target):
28 debugPrint(' Identified a tier-2 hadoop directory: ' + target)
29 target = srm.convertToUrl(target,debug)
30 cmd = 'srmls ' + target + ' >& /dev/null'
31 else:
32 debugPrint(' Identified a normal directory: ' + target)
33 cmd = 'ls -1 ' + target + '>& /dev/null'
34
35 status = os.system(cmd)
36 debugPrint(' Status: %d (on %s)' %(status,cmd))
37
38 return (status == 0)
39
40 def remove(source):
41 if re.search('/castor/cern.ch/',source):
42 debugPrint(' Identified castor file')
43 cmd = "stager_rm -M " + source + "; nsrm " + source
44 elif re.search('/mnt/hadoop/cms/store/user/paus',source):
45 debugPrint(' Identified a tier-2 hadoop directory: ' + source)
46 source = srm.convertToUrl(source,debug)
47 cmd = 'srmrmdir ' + source + ' >& /dev/null'
48 else:
49 debugPrint(' Identified a normal directory')
50 cmd = 'rm -rf ' + source
51
52 # ready to perform the requested operation
53 debugPrint(' -> removing directory with: ' + cmd)
54 status = 0
55 if exe == 1:
56 status = os.system(cmd)
57 if status != 0:
58 print ' ERROR: remove returned error %d (on %s)'%(status,cmd)
59
60 return status
61
62 #===================================================================================================
63 # Main starts here
64 #===================================================================================================
65 # Define string to explain usage of the script
66 usage = "\n Usage: removedir <source>"
67 usage += " --exe\n"
68 usage += " --debug\n"
69 usage += " --help\n\n"
70
71 # Define the valid options which can be specified and check out the command line
72 valid = ['exe','debug','help']
73 try:
74 opts, args = getopt.getopt(sys.argv[1:], "", valid)
75 except getopt.GetoptError, ex:
76 print usage
77 print str(ex)
78 sys.exit(1)
79
80 # --------------------------------------------------------------------------------------------------
81 # Get all parameters for the production
82 # --------------------------------------------------------------------------------------------------
83 # Set defaults for each option
84 debug = False
85 exe = 0
86
87 # Read new values from the command line
88 for opt, arg in opts:
89 #print ' OPT , ARG: ' + opt + ' ' + arg
90 if opt == '--help':
91 print usage
92 sys.exit(0)
93 elif opt == '--debug':
94 debug = True
95 elif opt == '--exe':
96 exe = 1
97
98 newArgv = []
99 for arg in sys.argv[1:]:
100 #print ' ARG: ' + arg
101 if arg[:2] == "--":
102 continue
103 else:
104 newArgv.append(arg)
105
106 # Define source and target
107 source = clean(newArgv[0])
108
109 # Test whether the source exists
110 if exists(source):
111 debugPrint("\n Removing: " + source)
112 remove(source)
113 elif not exists(source):
114 print ' ERROR: the source (' + source + ') does not exist.'