All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
run_qmtest Namespace Reference

Classes

class  Options
 

Functions

def parseOptions
 
def main
 

Function Documentation

def run_qmtest.main (   argv = None)

Definition at line 75 of file run_qmtest.py.

75 
76 def main(argv = None):
77  opts = parseOptions(argv)
78 
79  print "==========> Running tests for package %s" % opts.package
80 
81  if not os.path.isdir(opts.qmtest_dir):
82  print "==========> No test directory, exiting"
83  return
84 
85  # create the destination directory if necessary
86  if opts.output:
87  results_dest_dir = os.path.realpath(os.path.join(opts.qmtest_dir, os.path.dirname(opts.output)))
88  if not os.path.exists(results_dest_dir):
89  print "==========> Creating '%s'" % results_dest_dir
90  os.makedirs(results_dest_dir, 0755)
91 
92  print "==========> Entering '%s'" % opts.qmtest_dir
93  os.chdir(opts.qmtest_dir)
94 
95  qmtest_cmd = ["qmtest"]
96 
97  if not os.path.isdir("QMTest"):
98  # If the test-db directory is not initialized, we have to initialize it
99  try:
100  os.mkdir("QMTest")
101  except OSError:
102  # we cannot create the directory... we need a temporary test database
103  from tempfile import mkdtemp
104  from shutil import copytree
105  # the extra level is needed because of a limitation in copytree
106  tdb = os.path.join(mkdtemp("GaudiTest"), "qmtest")
107  copytree(".", tdb)
108  os.chmod(tdb, 0700) # This is needed because copytree will copy also the restrictive permissions
109  qmtest_cmd += ["--tdb", tdb]
110  print "==========> Initializing QMTest database"
111  os.system(" ".join(qmtest_cmd + ["create-tdb"]))
112 
113  if opts.xml_output:
114  opts.qmtest_args.insert(0, '''--result-stream "GaudiTest.XMLResultStream(dir='%s',prefix='%s_')"'''
115  % (opts.xml_output.replace("\\","\\\\"), opts.package))
116 
117  if opts.html_output:
118  opts.qmtest_args.insert(0, '''--result-stream "GaudiTest.HTMLResultStream(dir='%s')"'''
119  % opts.html_output.replace("\\","\\\\"))
120 
121  # prepare the qmtest command
122  cmd = " ".join(qmtest_cmd + ["run"] + opts.qmtest_args)
123 
124  if not opts.have_user_options:
125  # If there are no user options, we should check for a default test suite
126  # to run.
127  # The suite to run is the first one available in the list passed via
128  # the environment variable GAUDI_QMTEST_DEFAULT_SUITE (comma-separated list)
129  # or the suite with the same name as the package.
130  # If none of them is present, no argument is passed to qmtest, so all the tests
131  # are run.
132  suites = []
133  if "GAUDI_QMTEST_DEFAULT_SUITE" in os.environ:
134  suites.extend([s.strip().lower()
135  for s in os.environ["GAUDI_QMTEST_DEFAULT_SUITE"].split(",")])
136  suites.append(opts.package.lower())
137  for s in suites:
138  real_name = os.path.join(*[x + '.qms' for x in s.split('.')])
139  if os.path.exists(real_name):
140  cmd += " %s" % s
141  break
142 
143  if opts.dry_run:
144  print "==========> Would run '%s'"%cmd
145  else:
146  print "==========> Running '%s'"%cmd
147  os.system(cmd)
148 
149  # drop the temporary test DB if needed
150  if "--tdb" in qmtest_cmd:
151  from shutil import rmtree
152  def onerror(func, path, exc_info):
153  """
154  Change the permissions and try again to remove a file or a dir.
155  Ignore failures.
156  """
157  if func in [os.remove, os.rmdir]:
158  # change permissions and try again
159  try:
160  os.chmod(path, 0600)
161  os.chmod(os.path.dirname(path), 0700)
162  func(path)
163  except os.error:
164  pass
165  rmtree(os.path.dirname(qmtest_cmd[-1]), onerror = onerror)
166 
167 # Note: the return code of qmtest is not propagated to avoid that
# CMT stops if we have a non-PASS tests (e.g. UNTESTED).
def parseOptions
Definition: run_qmtest.py:13
def run_qmtest.parseOptions (   argv = None)

Definition at line 13 of file run_qmtest.py.

13 
14 def parseOptions(argv = None):
15  if argv is None:
16  argv = sys.argv[1:]
17  # Defaults
18  opts = Options()
19  opts.package = "Unknown"
20  opts.qmtest_args = []
21  opts.have_user_options = False
22  opts.output = os.path.normpath(os.path.expandvars(os.environ["QMTESTRESULTS"]))
23  opts.qmtest_dir = os.path.normpath(os.path.expandvars(os.environ["QMTESTLOCALDIR"]))
24  opts.dry_run = False
25  if "GAUDI_QMTEST_HTML_OUTPUT" in os.environ:
26  opts.html_output = os.path.normpath(os.path.expandvars(os.environ.get("GAUDI_QMTEST_HTML_OUTPUT")))
27  else:
28  opts.html_output = None
29  if "GAUDI_QMTEST_XML_OUTPUT" in os.environ:
30  opts.xml_output = os.path.normpath(os.path.expandvars(os.environ.get("GAUDI_QMTEST_XML_OUTPUT")))
31  else:
32  opts.xml_output = None
33 
34  # First argument is the package name:
35  if argv:
36  opts.package = argv.pop(0)
37 
38  # If the use specifies a directory where to collect all the results
39  # (e.g. because running from a read-only location) we must use it
40  if "QMTESTRESULTSDIR" in os.environ:
41  opts.output = os.path.normpath(os.path.expandvars(os.environ["QMTESTRESULTSDIR"]))
42  opts.output = os.path.join(opts.output,
43  "%s.%s.qmr" % (opts.package, os.environ.get("CMTCONFIG", "noConfig")))
44 
45  # Do we have user options?
46  opts.have_user_options = len(argv)
47  # Scan the user options (if any) to look for options we must intercept
48  while argv:
49  o = argv.pop(0)
50  if o in ['-o','--output']:
51  # make the path absolute
52  opts.output = os.path.realpath(argv.pop(0))
53  opts.have_user_options -= 2
54  elif o in ["--no-output"]:
55  opts.output = None
56  opts.have_user_options -= 1
57  elif o in ["--dry-run"]:
58  opts.dry_run = True
59  opts.have_user_options -= 1
60  elif o in ["--html-output"]:
61  opts.html_output = os.path.realpath(argv.pop(0))
62  opts.have_user_options -= 2
63  # Add xml output option
64  elif o in ["--xml-output"]:
65  opts.xml_output = os.path.realpath(argv.pop(0))
66  opts.have_user_options -= 2
67  else:
68  opts.qmtest_args.append(o)
69  # Add the option for the output to the qmtest_args
70  if opts.output:
71  opts.qmtest_args = ["-o", opts.output] + opts.qmtest_args
72  else:
73  opts.qmtest_args.insert(0, "--no-output")
74  return opts
def parseOptions
Definition: run_qmtest.py:13