CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
electronStore.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #========================================================================
4 #
5 # This script is used to copy a root file of histograms,
6 # together with its log files, in a destination directory.
7 # Log files will be automatically compressed.
8 #
9 # Command-line options :
10 #
11 # -f : force the copy, even if the destination file already exists.
12 # -r <name> : name of this set of histograms.
13 # -m <message> : specific comment about this set of histograms.
14 # -a <analyzers> : slash separated list of analyzers.
15 # -c <config> : slash separated list of cmsRun configurations.
16 #
17 # Command-line arguments :
18 #
19 # $1 : path of the ROOT file containing the histograms.
20 # $... : path of log files.
21 # $n : destination directory.
22 #
23 #=========================================================================
24 
25 
26 import os, sys, datetime, shutil, optparse
27 
28 
29 #============================================
30 # display a command and eventually executes
31 #============================================
32 
33 def mysystem(command,apply=1):
34  print command
35  if apply==1: return os.system(command)
36  elif apply==0: return 0
37  else:
38  print '[electronStore.py] UNSUPPORTED ARGUMENT VALUE FOR mysystem(,apply):',apply
39  exit(1)
40 
41 
42 #============================================
43 # force immediate flushing of stdout
44 #============================================
45 
46 class flushfile(object):
47  def __init__(self,f):
48  self.f = f
49  def write(self,x):
50  self.f.write(x)
51  self.f.flush()
52 
53 sys.stdout = flushfile(sys.stdout)
54 
55 
56 #===================================================================
57 # when called as an independant executable
58 #===================================================================
59 
60 if __name__ == "__main__":
61 
62 
63  #============================================
64  # command-line arguments
65  #============================================
66 
67  parser = optparse.OptionParser()
68  parser.add_option("-f", "--force", dest="force", action="store_true", default=False,
69  help="force the copy, even if the destination file already exists.")
70  parser.add_option("-r", "--release", dest="release", action="store", default="current",
71  help="release name of this set of histograms.")
72  parser.add_option("-m", "--message", dest="message", action="store", default="",
73  help="specific comment about this set of histograms")
74  parser.add_option("-a", "--analyzers", dest="analyzers", action="store", default="",
75  help="slash separated list of analyzers")
76  parser.add_option("-c", "--configs", dest="configs", action="store", default="",
77  help="slash separated list of cmsRun configurations")
78  (options, args) = parser.parse_args()
79 
80  if len(args)<2:
81  print "[electronStore.py] I NEED AT LEAST TWO ARGUMENTS."
82  exit(2)
83  store_file = args.pop(0)
84  store_dir = args.pop()
85  if len(args)>0: store_logs = ' '.join(args)
86  else: store_logs = ''
87 
88  analyzers = options.analyzers.split('/') ;
89  configs = options.configs.split('/') ;
90 
91 
92  #============================================
93  # prepare output directory
94  #============================================
95 
96  if os.path.exists(store_dir)==False:
97  os.makedirs(store_dir)
98 
99 
100  #============================================
101  # check data files
102  #============================================
103 
104  if os.path.isfile(store_file)==True :
105  print "STORE_FILE =",store_file
106  else :
107  print "[electronStore.py] FILE DOES NOT EXIST :",store_file
108  exit(3)
109 
110  if ( store_logs != '' ) :
111  print "STORE_LOGS =",store_logs
112 
113 
114  #============================================
115  # check if already done
116  #============================================
117 
118  output_file = store_dir+'/'+store_file
119  if ( options.force==False and os.path.isfile(output_file)==True ) :
120  print "[electronStore.py] ERROR: "+store_file+" ALREADY STORED IN "+store_dir+" !"
121  exit(4)
122 
123 
124  #============================================
125  # copy
126  #============================================
127 
128  files = [ store_file ]
129  for analyzer in analyzers:
130  files.append('../plugins/'+analyzer+'.h')
131  files.append('../plugins/'+analyzer+'.cc')
132  for config in configs:
133  files.append(config+'.py')
134 
135  mysystem('cp '+' '.join(files)+' '+store_dir)
136 
137  if ( store_logs != '' ) :
138  mysystem('cp '+store_logs+' '+store_dir)
139  mysystem('cd '+store_dir+' && gzip -f *.olog')
140 
141 
142  #============================================
143  # comment
144  #============================================
145 
146  store_url = store_dir.replace('/afs/cern.ch/cms/','http://cmsdoc.cern.ch/',1)
147 
148  links = []
149  for analyzer in analyzers:
150  links.append('<a href="'+store_url+'/'+analyzer+'.h">'+analyzer+'.h</a>')
151  links.append('<a href="'+store_url+'/'+analyzer+'.cc">'+analyzer+'.cc</a>')
152  for config in configs:
153  links.append('<a href="'+store_url+'/'+config+'.py">'+config+'.py</a>')
154 
155  comment_file = open(store_dir+'/'+store_file+'.comment','w')
156  print >>comment_file, 'The <a href="'+store_url+'/'+store_file+'">'+options.release+' histograms</a>',
157  if (options.message!=''):
158  print >>comment_file, ' ('+options.message+')',
159  print >>comment_file, ' have been prepared with those analyzers and configurations: '+', '.join(links)+'.',
160  print >>comment_file
161  comment_file.close()
162 
163 
164  #============================================
165  # fin
166  #============================================
167 
168  exit(0)
169 
170 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18