CMS 3D CMS Logo

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