ATLAS Offline Software
query.py
Go to the documentation of this file.
1 #!/bin/env python2.5
2 #$Name: not supported by cvs2svn $
3 
4 """ Publisher example """
5 import sys, re, time, datetime, os
6 from random import choice
7 
8 def index(q=''):
9 
10  if q.startswith('arq_'):
11  (page, fullpath) = _cache_request(q)
12  else:
13  (page, fullpath) = _new_request(q)
14 
15  _touch(fullpath)
16 
17  return page
18 
19 def _new_request(q=''):
20  installpath = os.path.dirname(__file__)
21 
22  timeofrequest = time.gmtime()
23 
24  # data stream uses current time
25  queryday = time.strftime("%y%m%d",timeofrequest)
26  queryid = time.strftime("%y%m%d%H%M%S",timeofrequest)
27 
28  # add random string to avoid conflict from coincident query
29  queryid += "".join([choice('abcdefghijklmnopqrstyz') for x in xrange(4)])
30 
31  # the query datapath
32  datapath = 'data/arq_%s/arq_%s' % (queryday,queryid)
33 
34  # where to store the result
35  #fulldatapath = '%s/%s' % (installpath,datapath)
36  fulldatapath = '/%s' % datapath
37  os.makedirs(fulldatapath)
38 
39  # the query that should be run
40  queryfile = '%s/query.txt' % fulldatapath
41  fh = open(queryfile,"w")
42  print >> fh, "%s" % q
43  fh.close()
44 
45  # global log file
46  logpath = fulldatapath
47  logfile = '%s/log.txt' % logpath
48  fh = open(logfile,"a")
49  print >> fh, "%s / [id %s] - received query: %s" % (timeofrequest, queryid, q if q else "none" )
50  fh.close()
51 
52  com = "cd %s; ./CoolRunQueryWrapper.sh fileindex %s" % (installpath,queryid)
53 
54  # run the query
55  from commands import getoutput
56  log = getoutput(com)
57  logfile = '%s/log.txt' % fulldatapath
58  fh = open(logfile,"w")
59  print >> fh, log
60  fh.close()
61 
62 
63  # forward file
64  outputfile = '%s/index.html' % fulldatapath
65  try:
66  fh = open(outputfile,"r")
67  page = fh.read()
68  fh.close()
69  if not page.rstrip().endswith("</html>"):
70  page = _error_page(datapath)
71  except IOError:
72  page = "<html><body>No web page created! Here the log file:<pre><br><br><br>%s</pre></body></html>" % (log.replace("<","&lt;").replace(">","&gt;"))
73 
74  return (page,fulldatapath)
75 
76 
77 
78 def _error_page(datapath):
79 
80  s = """<html>
81  <head><title>Error</title></head>
82  <body>
83  Found incomplete web page! Would you like to see the
84  <a target="_blank" href="query.py?q=%s">web page fragment</a> or the
85  <a target="_blank" href="%s/log.txt">log file</a> ?
86  </body>
87  </html>
88  """ % (datapath.split('/')[-1], datapath)
89  return s
90 
91 
92 
94  installpath = os.path.dirname(__file__)
95  fulldatapath = '%s/data/%s/%s' % (installpath,q[:10],q)
96  try:
97  # open cache
98  fh = open('%s/index.html' % fulldatapath,"r")
99  page = fh.read()
100  fh.close()
101  return (page,fulldatapath)
102  except IOError:
103  return ("Could not find cache %s" % q, None)
104 
105 def _touch(fullpath):
106  if fullpath==None: return
107  installpath = os.path.dirname(__file__)
108  try:
109  # open cache
110  fh = open('%s/access.log' % fullpath,"a")
111  timeofaccess = time.gmtime()
112  querytime = time.strftime("%y%m%d",timeofaccess)
113  print >> fh, querytime
114  fh.close()
115  except IOError:
116  pass
xrange
void xrange(TH1 *h, bool symmetric)
Definition: computils.cxx:515
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
dso-stats.getoutput
def getoutput(cmd)
Definition: dso-stats.py:22
query._new_request
def _new_request(q='')
Definition: query.py:19
query.index
def index(q='')
Definition: query.py:8
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Trk::open
@ open
Definition: BinningType.h:40
query._touch
def _touch(fullpath)
Definition: query.py:105
query._error_page
def _error_page(datapath)
Definition: query.py:78
python.AtlRunQueryAMI.choice
int choice
Definition: AtlRunQueryAMI.py:210
query._cache_request
def _cache_request(q)
Definition: query.py:93