refdriver.py
author Joel Maher <joel.maher@gmail.com>
Tue, 06 Oct 2009 13:54:42 -0400
changeset 8 7b4193f6c7101594740b5709d676568f868d77bb
parent 3 e244a72c094a9121bccacb3393ad66d103785b7a
child 20 ba8623e3b1812ebed2c683f08fe527dd54b3fdb2
permissions -rwxr-xr-x
removed xda for winmo and added test-agent for winmo

import os, re, maemkit

class RefDriver(maemkit.MaemKit):
  options = {}
  directories = []

  def addDir(self,targetDir):
    try:
      if (self.directories.index(targetDir) >= 0):
        return
    except ValueError: self.directories.append(targetDir)

  def parseLogs(self):
    self.stitchLogs()

  def parseManifest(self, manifest):
    p = re.compile("^include ([a-zA-Z0-9\\\/\-\.]+)")

    rootdir = os.path.dirname(manifest)

    #backup manifest, use backup incase we are running again
    self.move(manifest + ".bak", manifest + "")
    self.move(manifest, manifest + ".bak")

    fHandle = open(manifest + ".bak", "r")
    lines = fHandle.readlines()
    fHandle.close()

    self.addDir(rootdir)

    #rewrite manifest with include lines # out
    fHandle = open(manifest, "w")
    for line in lines:
      test = p.match(line)
      if (test):
        self.addDir(os.path.normpath(os.path.join(rootdir, test.group(1))))
        line = "#" + line
      fHandle.write(line)
    fHandle.close()

  #parse out manifest for main reftest.list, and any splitdirs
  def findDirs(self, rootfile):
    self.parseManifest(os.path.normpath(rootfile))
    for splitdir in self.options["split-directories"]:
      try:
        if (self.directories.index(os.path.normpath(splitdir.strip())) >= 0):
          self.parseManifest(os.path.normpath(os.path.join(RefDriver.options["testroot"], splitdir)))
      except: continue

  def addOptions(self, input):
    for option in input:
      if (option == "split-directories"):
        self.options[option] = []
        for part in input[option].split(','): self.options[option].append(os.path.normpath(part.strip('\"')))
      else:
        self.options[option] = input[option]

  #split directories, do parallel
  def prepTests(self):

    for dir in self.directories:
      test = dir.split(os.path.normpath(RefDriver.options["testroot"]))
      test = test[1].lstrip(self.dirtype)
      try:
        if (self.options["split-directories"].index(test) >= 0): self.splitManifest(dir)
      except ValueError: continue

    RefDriver.directories = RefDriver.splitListParallel(self, self.directories, self.options)

  #split a manifest info many parts defined by split-percentage as the maximum # of items
  def splitManifest(self, dir):
    fHandle = open(dir, "r")
    lines = fHandle.readlines()
    fHandle.close()

    iter = 0
    counter = 0
    max = int(self.options["split-percentage"])
    if (max*2 >= len(lines)): return

    self.directories.remove(dir)
    while (counter < len(lines)):
      fHandle = open(dir + str(iter), "w")
      for line in lines[counter:counter+max]: fHandle.write(line)
      fHandle.close()
      self.addDir(dir + str(iter))
      iter += 1
      counter += max

  def runTests(self):
    self.mkLogDir(self.options["logdir"])

    for dir in RefDriver.directories:
      logfile = os.path.join(self.options["logdir"],  self.getLogFileName(dir))
      logHandle = open(logfile, "w")
      utilname = os.path.normpath(os.path.join(self.options["utility-path"], "runreftest.py"))
      output = RefDriver.addCommand(self, "python " + utilname + " --appname=" + os.path.normpath(RefDriver.options["appname"]) + " " + dir, True)
      logHandle.write(output)
      logHandle.close()
      if self.options['verbose'] and os.path.exists(logfile):
          print ''.join(open(logfile).readlines())
      RefDriver.cleanup(self)

class Reftest(RefDriver):

  def __init__(self, mkit):
    self.dirtype = mkit.dirtype
    self.addOptions(mkit.config_options["reftest"])

  def addOptions(self, options):
    RefDriver.addOptions(self, options)

  def getTests(self):
    RefDriver.findDirs(self, os.path.normpath(self.options["manifest"]))

  def prepTests(self):
    RefDriver.prepTests(self)

  def runTests(self):
    RefDriver.runTests(self)

  def parseLogs(self):
    RefDriver.parseLogs(self)

class Crashtest(RefDriver):

  def __init__(self, mkit):
    self.dirtype = mkit.dirtype
    self.addOptions(mkit.config_options["crashtest"])

  def addOptions(self, options):
    RefDriver.addOptions(self, options)

  def getTests(self):
    RefDriver.findDirs(self, os.path.normpath(self.options["manifest"]))

  def prepTests(self):
    RefDriver.prepTests(self)

  def runTests(self):
    RefDriver.runTests(self)

  def parseLogs(self):
    RefDriver.parseLogs(self)