xref: /petsc/config/testparse.py (revision 53f2a965e47eca011388e913aa305e614e7f0495)
129921a8fSScott Kruger#!/usr/bin/env python
229921a8fSScott Kruger"""
329921a8fSScott KrugerParse the test file and return a dictionary.
429921a8fSScott Kruger
529921a8fSScott KrugerQuick usage::
629921a8fSScott Kruger
729921a8fSScott Kruger  bin/maint/testparse.py -t src/ksp/ksp/examples/tutorials/ex1.c
829921a8fSScott Kruger
929921a8fSScott KrugerFrom the command line, it prints out the dictionary.
1029921a8fSScott KrugerThis is meant to be used by other scripts, but it is
1129921a8fSScott Krugeruseful to debug individual files.
1229921a8fSScott Kruger
1329921a8fSScott Kruger
1429921a8fSScott Kruger
1529921a8fSScott KrugerExample language
1629921a8fSScott Kruger----------------
1729921a8fSScott Kruger/*T
1829921a8fSScott Kruger   Concepts:
1929921a8fSScott Kruger   requires: moab
2029921a8fSScott KrugerT*/
2129921a8fSScott Kruger
2229921a8fSScott Kruger
2329921a8fSScott Kruger
2429921a8fSScott Kruger/*TEST
2529921a8fSScott Kruger
2629921a8fSScott Kruger   test:
2729921a8fSScott Kruger      args: -pc_type mg -ksp_type fgmres -da_refine 2 -ksp_monitor_short -mg_levels_ksp_monitor_short -mg_levels_ksp_norm_type unpreconditioned -ksp_view -pc_mg_type full
2829921a8fSScott Kruger      output_file: output/ex25_1.out
2929921a8fSScott Kruger
3029921a8fSScott Kruger   test:
3129921a8fSScott Kruger      suffix: 2
3229921a8fSScott Kruger      nsize: 2
3329921a8fSScott Kruger      args: -pc_type mg -ksp_type fgmres -da_refine 2 -ksp_monitor_short -mg_levels_ksp_monitor_short -mg_levels_ksp_norm_type unpreconditioned -ksp_view -pc_mg_type full
3429921a8fSScott Kruger
3529921a8fSScott KrugerTEST*/
3629921a8fSScott Kruger
3729921a8fSScott Kruger"""
3829921a8fSScott Kruger
3929921a8fSScott Krugerimport os, re, glob, types
4029921a8fSScott Krugerfrom distutils.sysconfig import parse_makefile
4129921a8fSScott Krugerimport sys
4229921a8fSScott Krugerimport logging
4329921a8fSScott Krugersys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
4429921a8fSScott Kruger
4529921a8fSScott Krugerimport inspect
4629921a8fSScott Krugerthisscriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
4729921a8fSScott Krugermaintdir=os.path.join(os.path.join(os.path.dirname(thisscriptdir),'bin'),'maint')
4829921a8fSScott Krugersys.path.insert(0,maintdir)
4929921a8fSScott Kruger
5029921a8fSScott Kruger# These are special keys describing build
5129921a8fSScott Krugerbuildkeys="requires TODO SKIP depends".split()
5229921a8fSScott Kruger
5329921a8fSScott Krugerdef _stripIndent(block,srcfile):
5429921a8fSScott Kruger  """
5529921a8fSScott Kruger  Go through and remove a level of indentation
5629921a8fSScott Kruger  Also strip of trailing whitespace
5729921a8fSScott Kruger  """
5829921a8fSScott Kruger  # The first entry should be test: but it might be indented.
5929921a8fSScott Kruger  ext=os.path.splitext(srcfile)[1]
608ccd5183SScott Kruger  for lline in block.split("\n"):
618ccd5183SScott Kruger    line=lline[1:] if lline.startswith("!") else lline
6229921a8fSScott Kruger    if not line.strip(): continue
638ccd5183SScott Kruger    stripstr=" "
6429921a8fSScott Kruger    nspace=len(line)-len(line.lstrip(stripstr))
6529921a8fSScott Kruger    newline=line[nspace:]
6629921a8fSScott Kruger    break
6729921a8fSScott Kruger
6829921a8fSScott Kruger  # Strip off any indentation for the whole string and any trailing
6929921a8fSScott Kruger  # whitespace for convenience
7029921a8fSScott Kruger  newTestStr="\n"
718ccd5183SScott Kruger  for lline in block.split("\n"):
728ccd5183SScott Kruger    line=lline[1:] if lline.startswith("!") else lline
7329921a8fSScott Kruger    if not line.strip(): continue
7429921a8fSScott Kruger    newline=line[nspace:]
7529921a8fSScott Kruger    newTestStr=newTestStr+newline.rstrip()+"\n"
7629921a8fSScott Kruger
7729921a8fSScott Kruger  return newTestStr
7829921a8fSScott Kruger
7929921a8fSScott Krugerdef parseTest(testStr,srcfile):
8029921a8fSScott Kruger  """
8129921a8fSScott Kruger  This parses an individual test
8229921a8fSScott Kruger  YAML is hierarchial so should use a state machine in the general case,
83*53f2a965SBarry Smith  but in practice we only support two levels of test:
8429921a8fSScott Kruger  """
8529921a8fSScott Kruger  basename=os.path.basename(srcfile)
8629921a8fSScott Kruger  # Handle the new at the begininng
8729921a8fSScott Kruger  bn=re.sub("new_","",basename)
8829921a8fSScott Kruger  # This is the default
8929921a8fSScott Kruger  testname="run"+os.path.splitext(bn)[0]
9029921a8fSScott Kruger
9129921a8fSScott Kruger  # Tests that have default everything (so empty effectively)
9229921a8fSScott Kruger  if len(testStr)==0: return testname, {}
9329921a8fSScott Kruger
9429921a8fSScott Kruger  striptest=_stripIndent(testStr,srcfile)
9529921a8fSScott Kruger
9629921a8fSScott Kruger  # go through and parse
9729921a8fSScott Kruger  subtestnum=0
9829921a8fSScott Kruger  subdict={}
9929921a8fSScott Kruger  for line in striptest.split("\n"):
10029921a8fSScott Kruger    if not line.strip(): continue
10129921a8fSScott Kruger    var=line.split(":")[0].strip()
10229921a8fSScott Kruger    val=line.split(":")[1].strip()
10329921a8fSScott Kruger    # Start by seeing if we are in a subtest
10429921a8fSScott Kruger    if line.startswith(" "):
10529921a8fSScott Kruger      subdict[subtestname][var]=val
10629921a8fSScott Kruger    # Determine subtest name and make dict
10729921a8fSScott Kruger    elif var=="test":
10829921a8fSScott Kruger      subtestname="test"+str(subtestnum)
10929921a8fSScott Kruger      subdict[subtestname]={}
11029921a8fSScott Kruger      if not subdict.has_key("subtests"): subdict["subtests"]=[]
11129921a8fSScott Kruger      subdict["subtests"].append(subtestname)
11229921a8fSScott Kruger      subtestnum=subtestnum+1
11329921a8fSScott Kruger    # The reset are easy
11429921a8fSScott Kruger    else:
11529921a8fSScott Kruger      subdict[var]=val
11629921a8fSScott Kruger      if var=="suffix":
11729921a8fSScott Kruger        if len(val)>0:
11829921a8fSScott Kruger          testname=testname+"_"+val
11929921a8fSScott Kruger
12029921a8fSScott Kruger  return testname,subdict
12129921a8fSScott Kruger
12229921a8fSScott Krugerdef parseTests(testStr,srcfile):
12329921a8fSScott Kruger  """
12429921a8fSScott Kruger  Parse the yaml string describing tests and return
12529921a8fSScott Kruger  a dictionary with the info in the form of:
12629921a8fSScott Kruger    testDict[test][subtest]
12729921a8fSScott Kruger  This is an inelegant parser as we do not wish to
12829921a8fSScott Kruger  introduce a new dependency by bringing in pyyaml.
12929921a8fSScott Kruger  The advantage is that validation can be done as
13029921a8fSScott Kruger  it is parsed (e.g., 'test' is the only top-level node)
13129921a8fSScott Kruger  """
13229921a8fSScott Kruger
13329921a8fSScott Kruger  testDict={}
13429921a8fSScott Kruger
13529921a8fSScott Kruger  # The first entry should be test: but it might be indented.
13629921a8fSScott Kruger  newTestStr=_stripIndent(testStr,srcfile)
13729921a8fSScott Kruger
13829921a8fSScott Kruger  # Now go through each test.  First elem in split is blank
13929921a8fSScott Kruger  for test in newTestStr.split("\ntest:\n")[1:]:
14029921a8fSScott Kruger    testname,subdict=parseTest(test,srcfile)
14129921a8fSScott Kruger    if testDict.has_key(testname):
14229921a8fSScott Kruger      print "Multiple test names specified: "+testname+" in file: "+srcfile
14329921a8fSScott Kruger    testDict[testname]=subdict
14429921a8fSScott Kruger
14529921a8fSScott Kruger  return testDict
14629921a8fSScott Kruger
14729921a8fSScott Krugerdef parseTestFile(srcfile):
14829921a8fSScott Kruger  """
14929921a8fSScott Kruger  Parse single example files and return dictionary of the form:
15029921a8fSScott Kruger    testDict[srcfile][test][subtest]
15129921a8fSScott Kruger  """
15229921a8fSScott Kruger  debug=False
15329921a8fSScott Kruger  curdir=os.path.realpath(os.path.curdir)
15429921a8fSScott Kruger  basedir=os.path.dirname(os.path.realpath(srcfile))
15529921a8fSScott Kruger  basename=os.path.basename(srcfile)
15629921a8fSScott Kruger  os.chdir(basedir)
15729921a8fSScott Kruger
15829921a8fSScott Kruger  testDict={}
15929921a8fSScott Kruger  sh=open(srcfile,"r"); fileStr=sh.read(); sh.close()
16029921a8fSScott Kruger
16129921a8fSScott Kruger  ## Start with doing the tests
16229921a8fSScott Kruger  #
16329921a8fSScott Kruger  fsplit=fileStr.split("/*TEST\n")[1:]
16429921a8fSScott Kruger  if len(fsplit)==0:
16529921a8fSScott Kruger    if debug: print "No test found in: "+srcfile
16629921a8fSScott Kruger    return {}
16729921a8fSScott Kruger  # Allow for multiple "/*TEST" blocks even though it really should be
16829921a8fSScott Kruger  # on
16929921a8fSScott Kruger  srcTests=[]
17029921a8fSScott Kruger  for t in fsplit: srcTests.append(t.split("TEST*/")[0])
17129921a8fSScott Kruger  testString=" ".join(srcTests)
17229921a8fSScott Kruger  if len(testString.strip())==0:
17329921a8fSScott Kruger    print "No test found in: "+srcfile
17429921a8fSScott Kruger    return {}
17529921a8fSScott Kruger  testDict[basename]=parseTests(testString,srcfile)
17629921a8fSScott Kruger
17729921a8fSScott Kruger  ## Check and see if we have build reuqirements
17829921a8fSScott Kruger  #
17929921a8fSScott Kruger  if "/*T\n" in fileStr or "/*T " in fileStr:
18029921a8fSScott Kruger    # The file info is already here and need to append
18129921a8fSScott Kruger    Part1=fileStr.split("T*/")[0]
18229921a8fSScott Kruger    fileInfo=Part1.split("/*T")[1]
18329921a8fSScott Kruger    for bkey in buildkeys:
18429921a8fSScott Kruger      if bkey+":" in fileInfo:
18529921a8fSScott Kruger        testDict[basename][bkey]=fileInfo.split(bkey+":")[1].split("\n")[0].strip()
18629921a8fSScott Kruger
18729921a8fSScott Kruger  os.chdir(curdir)
18829921a8fSScott Kruger  return testDict
18929921a8fSScott Kruger
19029921a8fSScott Krugerdef parseTestDir(directory):
19129921a8fSScott Kruger  """
19229921a8fSScott Kruger  Parse single example files and return dictionary of the form:
19329921a8fSScott Kruger    testDict[srcfile][test][subtest]
19429921a8fSScott Kruger  """
19529921a8fSScott Kruger  curdir=os.path.realpath(os.path.curdir)
19629921a8fSScott Kruger  basedir=os.path.realpath(directory)
19729921a8fSScott Kruger  os.chdir(basedir)
19829921a8fSScott Kruger
19929921a8fSScott Kruger  tDict={}
20029921a8fSScott Kruger  for test_file in glob.glob("new_ex*.*"):
20129921a8fSScott Kruger    tDict.update(parseTestFile(test_file))
20229921a8fSScott Kruger
20329921a8fSScott Kruger  os.chdir(curdir)
20429921a8fSScott Kruger  return tDict
20529921a8fSScott Kruger
20629921a8fSScott Krugerdef printExParseDict(rDict):
20729921a8fSScott Kruger  """
20829921a8fSScott Kruger  This is useful for debugging
20929921a8fSScott Kruger  """
21029921a8fSScott Kruger  indent="   "
21129921a8fSScott Kruger  for sfile in rDict:
21229921a8fSScott Kruger    print "\n\n"+sfile
21329921a8fSScott Kruger    for runex in rDict[sfile]:
21429921a8fSScott Kruger      print indent+runex
21529921a8fSScott Kruger      if type(rDict[sfile][runex])==types.StringType:
21629921a8fSScott Kruger        print indent*2+rDict[sfile][runex]
21729921a8fSScott Kruger      else:
21829921a8fSScott Kruger        for var in rDict[sfile][runex]:
21929921a8fSScott Kruger          if var.startswith("test"):
22029921a8fSScott Kruger            print indent*2+var
22129921a8fSScott Kruger            for var2 in rDict[sfile][runex][var]:
22229921a8fSScott Kruger              print indent*3+var2+": "+str(rDict[sfile][runex][var][var2])
22329921a8fSScott Kruger          else:
22429921a8fSScott Kruger            print indent*2+var+": "+str(rDict[sfile][runex][var])
22529921a8fSScott Kruger  return
22629921a8fSScott Kruger
22729921a8fSScott Krugerdef main(directory='',test_file='',verbosity=0):
22829921a8fSScott Kruger
22929921a8fSScott Kruger    if directory:
23029921a8fSScott Kruger      tDict=parseTestDir(directory)
23129921a8fSScott Kruger    else:
23229921a8fSScott Kruger      tDict=parseTestFile(test_file)
23329921a8fSScott Kruger    if verbosity>0: printExParseDict(tDict)
23429921a8fSScott Kruger
23529921a8fSScott Kruger    return
23629921a8fSScott Kruger
23729921a8fSScott Krugerif __name__ == '__main__':
23829921a8fSScott Kruger    import optparse
23929921a8fSScott Kruger    parser = optparse.OptionParser()
24029921a8fSScott Kruger    parser.add_option('-d', '--directory', dest='directory',
24129921a8fSScott Kruger                      default="", help='Directory containing files to parse')
24229921a8fSScott Kruger    parser.add_option('-t', '--test_file', dest='test_file',
24329921a8fSScott Kruger                      default="", help='Test file, e.g., ex1.c, to parse')
24429921a8fSScott Kruger    parser.add_option('-v', '--verbosity', dest='verbosity',
24529921a8fSScott Kruger                      help='Verbosity of output by level: 1, 2, or 3', default=0)
24629921a8fSScott Kruger    opts, extra_args = parser.parse_args()
24729921a8fSScott Kruger
24829921a8fSScott Kruger    if extra_args:
24929921a8fSScott Kruger        import sys
25029921a8fSScott Kruger        sys.stderr.write('Unknown arguments: %s\n' % ' '.join(extra_args))
25129921a8fSScott Kruger        exit(1)
25229921a8fSScott Kruger    if not opts.test_file and not opts.directory:
25329921a8fSScott Kruger      print "test file or directory is required"
25429921a8fSScott Kruger      parser.print_usage()
25529921a8fSScott Kruger      sys.exit()
25629921a8fSScott Kruger
25729921a8fSScott Kruger    # Need verbosity to be an integer
25829921a8fSScott Kruger    try:
25929921a8fSScott Kruger      verbosity=int(opts.verbosity)
26029921a8fSScott Kruger    except:
26129921a8fSScott Kruger      raise Exception("Error: Verbosity must be integer")
26229921a8fSScott Kruger
26329921a8fSScott Kruger    main(directory=opts.directory,test_file=opts.test_file,verbosity=verbosity)
264