Gaudi Framework, version v23r5

Home   Generated: Wed Nov 28 2012
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
run_qmtest.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Small wrapper script to simplify the execution of QMTest tests.
4 #
5 # @author: Marco Clemencic <marco.clemencic@cern.ch>
6 # @date: 22/10/2007
7 #
8 import os, sys
9 
10 class Options(object):
11  pass
12 
13 def parseOptions(argv = None):
14  if argv is None:
15  argv = sys.argv[1:]
16  # Defaults
17  opts = Options()
18  opts.package = "Unknown"
19  opts.qmtest_args = []
20  opts.have_user_options = False
21  opts.output = os.path.normpath(os.path.expandvars(os.environ["QMTESTRESULTS"]))
22  opts.qmtest_dir = os.path.normpath(os.path.expandvars(os.environ["QMTESTLOCALDIR"]))
23  opts.dry_run = False
24  if "GAUDI_QMTEST_HTML_OUTPUT" in os.environ:
25  opts.html_output = os.path.normpath(os.path.expandvars(os.environ.get("GAUDI_QMTEST_HTML_OUTPUT")))
26  else:
27  opts.html_output = None
28 
29  # First argument is the package name:
30  if argv:
31  opts.package = argv.pop(0)
32 
33  # If the use specifies a directory where to collect all the results
34  # (e.g. because running from a read-only location) we must use it
35  if "QMTESTRESULTSDIR" in os.environ:
36  opts.output = os.path.normpath(os.path.expandvars(os.environ["QMTESTRESULTSDIR"]))
37  opts.output = os.path.join(opts.output,
38  "%s.%s.qmr" % (opts.package, os.environ.get("CMTCONFIG", "noConfig")))
39 
40  # Do we have user options?
41  opts.have_user_options = len(argv)
42  # Scan the user options (if any) to look for options we must intercept
43  while argv:
44  o = argv.pop(0)
45  if o in ['-o','--output']:
46  # make the path absolute
47  opts.output = os.path.realpath(argv.pop(0))
48  opts.have_user_options -= 2
49  elif o in ["--no-output"]:
50  opts.output = None
51  opts.have_user_options -= 1
52  elif o in ["--dry-run"]:
53  opts.dry_run = True
54  opts.have_user_options -= 1
55  elif o in ["--html-output"]:
56  opts.html_output = os.path.realpath(argv.pop(0))
57  opts.have_user_options -= 2
58  else:
59  opts.qmtest_args.append(o)
60  # Add the option for the output to the qmtest_args
61  if opts.output:
62  opts.qmtest_args = ["-o", opts.output] + opts.qmtest_args
63  else:
64  opts.qmtest_args.insert(0, "--no-output")
65  return opts
66 
67 def main(argv = None):
68  opts = parseOptions(argv)
69 
70  print "==========> Running tests for package %s" % opts.package
71 
72  # create the destination directory if necessary
73  if opts.output:
74  results_dest_dir = os.path.realpath(os.path.join(opts.qmtest_dir, os.path.dirname(opts.output)))
75  if not os.path.exists(results_dest_dir):
76  print "==========> Creating '%s'" % results_dest_dir
77  os.makedirs(results_dest_dir, 0755)
78 
79  print "==========> Entering '%s'" % opts.qmtest_dir
80  os.chdir(opts.qmtest_dir)
81 
82  qmtest_cmd = ["qmtest"]
83 
84  if not os.path.isdir("QMTest"):
85  # If the test-db directory is not initialized, we have to initialize it
86  try:
87  os.mkdir("QMTest")
88  except OSError:
89  # we cannot create the directory... we need a temporary test database
90  from tempfile import mkdtemp
91  from shutil import copytree
92  # the extra level is needed because of a limitation in copytree
93  tdb = os.path.join(mkdtemp("GaudiTest"), "qmtest")
94  copytree(".", tdb)
95  os.chmod(tdb, 0700) # This is needed because copytree will copy also the restrictive permissions
96  qmtest_cmd += ["--tdb", tdb]
97  print "==========> Initializing QMTest database"
98  os.system(" ".join(qmtest_cmd + ["create-tdb"]))
99 
100  if opts.html_output:
101  opts.qmtest_args.insert(0, '''--result-stream "GaudiTest.HTMLResultStream(dir='%s')"'''
102  % opts.html_output.replace("\\","\\\\"))
103 
104  # prepare the qmtest command
105  cmd = " ".join(qmtest_cmd + ["run"] + opts.qmtest_args)
106 
107  if not opts.have_user_options:
108  # If there are no user options, we should check for a default test suite
109  # to run.
110  # The suite to run is the first one available in the list passed via
111  # the environment variable GAUDI_QMTEST_DEFAULT_SUITE (comma-separated list)
112  # or the suite with the same name as the package.
113  # If none of them is present, no argument is passed to qmtest, so all the tests
114  # are run.
115  suites = []
116  if "GAUDI_QMTEST_DEFAULT_SUITE" in os.environ:
117  suites.extend([s.strip().lower()
118  for s in os.environ["GAUDI_QMTEST_DEFAULT_SUITE"].split(",")])
119  suites.append(opts.package.lower())
120  for s in suites:
121  if os.path.exists("%s.qms" % s):
122  cmd += " %s" % s
123  break
124 
125  if opts.dry_run:
126  print "==========> Would run '%s'"%cmd
127  else:
128  print "==========> Running '%s'"%cmd
129  os.system(cmd)
130 
131  # drop the temporary test DB if needed
132  if "--tdb" in qmtest_cmd:
133  from shutil import rmtree
134  def onerror(func, path, exc_info):
135  """
136  Change the permissions and try again to remove a file or a dir.
137  Ignore failures.
138  """
139  if func in [os.remove, os.rmdir]:
140  # change permissions and try again
141  try:
142  os.chmod(path, 0600)
143  os.chmod(os.path.dirname(path), 0700)
144  func(path)
145  except os.error:
146  pass
147  rmtree(os.path.dirname(qmtest_cmd[-1]), onerror = onerror)
148 
149 # Note: the return code of qmtest is not propagated to avoid that
150 # CMT stops if we have a non-PASS tests (e.g. UNTESTED).
151 if __name__ == '__main__':
152  main()

Generated at Wed Nov 28 2012 12:17:17 for Gaudi Framework, version v23r5 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004