CMS 3D CMS Logo

relmon_rootfiles_spy.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 ################################################################################
3 # RelMon: a tool for automatic Release Comparison
4 # https://twiki.cern.ch/twiki/bin/view/CMSPublic/RelMon
5 #
6 #
7 #
8 # Danilo Piparo CERN - danilo.piparo@cern.ch
9 #
10 ################################################################################
11 
12 """
13 Just a draft of the real program...It is very ugly still.
14 """
15 from __future__ import print_function
16 
17 
18 from os.path import basename
19 from optparse import OptionParser
20 from re import search
21 from sys import exit
22 from urllib2 import Request,build_opener,urlopen
23 
24 import os
25 if "RELMON_SA" in os.environ:
26  from authentication import X509CertOpen
27  from definitions import server
28  from utils import wget
29 else:
30  from Utilities.RelMon.authentication import X509CertOpen
31  from Utilities.RelMon.definitions import server
32  from Utilities.RelMon.utils import wget
33 
34 def extract_list(page_html,the_server,display_url):
35  contents=[]
36  for line in page_html.split("<tr><td>")[1:]:
37  name=""
38  #link
39  link_start=line.find("href='")+6
40  link_end=line.find("'>")
41  #name
42  name_start=link_end+2
43  name_end=line.find("</a>")
44  if display_url:
45  contents.append(the_server+line[link_start:link_end])
46  else:
47  contents.append(line[name_start:name_end])
48  return contents
49 
50 def get_page(url):
51  """ Get the web page listing the rootfiles. Use the X509 auth.
52  """
53  opener=build_opener(X509CertOpen())
54  datareq = Request(url)
55  datareq.add_header('authenticated_wget', "The ultimate wgetter")
56  filename=basename(url)
57  return opener.open(datareq).read()
58 
59 if __name__=="__main__":
60 
61  parser = OptionParser(usage="usage: %prog [options] dirtolist")
62 
63  parser.add_option("-d","--dev",
64  action="store_true",
65  dest="development",
66  default=False,
67  help="Select the development GUI instance.")
68 
69  parser.add_option("--offline",
70  action="store_true",
71  dest="offline",
72  default=False,
73  help="Select the Offline GUI instance.")
74 
75  parser.add_option("-o","--online",
76  action="store_true",
77  dest="online",
78  default=False,
79  help="Select the Online GUI instance.")
80 
81  parser.add_option("-r","--relval",
82  action="store_true",
83  dest="relval",
84  default=True,
85  help="Select the RelVal GUI instance.")
86 
87  parser.add_option("-u","--show_url",
88  action="store_true",
89  dest="show_url",
90  default=False,
91  help="Show the full URL of the file.")
92 
93  parser.add_option("-g","--get",
94  action="store_true",
95  dest="get",
96  default=False,
97  help="Get the files.")
98 
99  parser.add_option("-p","--path",
100  action="store",
101  dest="path",
102  default="",
103  help="The path to be matched before getting.")
104 
105  (options, args) = parser.parse_args()
106 
107  if not(options.development or options.offline or options.online or options.relval):
108  print("Select development or online instance!")
109  exit(-1)
110 
111  lenargs=len(args)
112  if lenargs>1:
113  print("Please specify only one directory to list!")
114  exit(-1)
115 
116  dirtolist=""
117  if lenargs==1:
118  dirtolist=args[0]
119 
120  mode="relval"
121  if options.online:
122  mode="online"
123  if options.development:
124  mode="dev"
125 
126 
127  directory="%s/dqm/%s/data/browse/%s" %(server,mode,dirtolist)
128  print("peeping ",directory)
129  contents=extract_list(get_page(directory),server,options.show_url)
130 
131  if len(contents)==0:
132  print("No contents found!")
133 
134  for content in contents:
135  if not options.get and search(options.path,content):
136  print(content)
137  if options.get and options.show_url and len(options.path)>0 and search(options.path,content):
138  if not search('pre',options.path) and search('pre',content):
139  continue
140  bcontent=basename(content)
141  print("Getting %s" %bcontent)
142  wget(content)
143  print("Got %s!!" %bcontent)
144 
145 
def wget(url)
Definition: utils.py:457
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:314
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def extract_list(page_html, the_server, display_url)