ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/Processing/bin/remove
Revision: 1.1
Committed: Fri Jul 30 18:41:11 2010 UTC (14 years, 9 months ago) by paus
Branch: MAIN
Log Message:
Cleaned up and updated version.

File Contents

# User Rev Content
1 paus 1.1 #!/usr/bin/env python
2     #---------------------------------------------------------------------------------------------------
3     # Script to move a file from one place to another. Restrictions to application of course apply :-)
4     #
5     # Author: C.Paus (May 06, 2010)
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    
22     def exists(target):
23     if re.search('/castor/cern.ch/',target):
24     debugPrint(' Identified a castor directory: ' + target)
25     cmd = 'rfdir ' + target
26     elif re.search('/pnfs/cmsaf.mit.edu/',target):
27     debugPrint(' Identified a tier-2 directory: ' + target)
28     cmd = 'ssh paus@cgate.mit.edu ls -1 ' + target + '\>\& /dev/null'
29     else:
30     debugPrint(' Identified a normal directory: ' + target)
31     cmd = 'ls -1 ' + target + '>& /dev/null'
32    
33     status = os.system(cmd)
34     debugPrint(' Status: %d' % status)
35    
36     return (status == 0)
37    
38     def remove(source):
39     if re.search('/castor/cern.ch/',source):
40     debugPrint(' Identified castor file')
41     cmd = "stager_rm -M " + source + "; nsrm " + source
42     elif re.search('/pnfs/cmsaf.mit.edu/',source):
43     debugPrint(' Identified tier-2 file')
44     cmd = 'ssh paus@cgate.mit.edu rm ' + source
45     else:
46     debugPrint(' Identified a normal directory')
47     cmd = 'rm ' + source
48    
49     # ready to perform the requested operation
50     debugPrint(' -> removing with: ' + cmd)
51     ## status = 0
52     status = os.system(cmd)
53    
54     return status
55    
56     #===================================================================================================
57     # Main starts here
58     #===================================================================================================
59     # Define string to explain usage of the script
60     usage = "Usage: remove <source>"
61     usage += " --debug\n"
62     usage += " --help\n"
63    
64     # Define the valid options which can be specified and check out the command line
65     valid = ['debug','help']
66     try:
67     opts, args = getopt.getopt(sys.argv[1:], "", valid)
68     except getopt.GetoptError, ex:
69     print usage
70     print str(ex)
71     sys.exit(1)
72    
73     # --------------------------------------------------------------------------------------------------
74     # Get all parameters for the production
75     # --------------------------------------------------------------------------------------------------
76     # Set defaults for each option
77     debug = 0
78    
79     # Read new values from the command line
80     for opt, arg in opts:
81     #print ' OPT , ARG: ' + opt + ' ' + arg
82     if opt == '--help':
83     print usage
84     sys.exit(0)
85     elif opt == '--debug':
86     debug = 1
87    
88     newArgv = []
89     for arg in sys.argv[1:]:
90     #print ' ARG: ' + arg
91     if arg[:2] == "--":
92     continue
93     else:
94     newArgv.append(arg)
95    
96     # Define source and target
97     source = clean(newArgv[0])
98    
99     # Test whether the source exists
100     if exists(source):
101     debugPrint("\n Removing: " + source)
102     remove(source)
103     elif not exists(source):
104     print ' ERROR: the source ('+source+') does not exist.'