Exemplo n.º 1
0
def Compare(base, compare, ops, root_path=None, out_path=None):
  """Compares a series of scrapes using a series of operators.

  Args:
    base: (browser, version) tuple of version to consider the baseline
    compare: (browser, version) tuple of version to compare to
    ops: list of operators plus operator arguments
    root_path: root of the scrapes
    out_path: place to put any output from the operators

  Returns:
    None

  @TODO(jhaas): this method will likely change, to provide a robust and
  well-defined way of chaining operators, applying operators conditionally,
  and full-featured scripting of the operator chain. There also needs
  to be better definition of the output; right now it's to stdout and
  a log.txt file, with operator-dependent images saved for error output
  """
  if root_path is None:
    # default save path is "scrapes" off the current root
    root_path = os.path.join(os.path.split(__file__)[0], "Scrapes")

  if out_path is None:
    out_path = os.path.join(os.path.split(__file__)[0], "Compares")

  if type(base) in types.StringTypes: base = (base, None)
  if type(compare) in types.StringTypes: compare = (compare, None)
  if type(ops) in types.StringTypes: ops = [ops]

  base_dir = os.path.join(root_path, base[0])
  compare_dir = os.path.join(root_path, compare[0])

  if base[1] is None:
    # base defaults to earliest capture
    base = (base[0], max(os.listdir(base_dir)))

  if compare[1] is None:
    # compare defaults to latest capture
    compare = (compare[0], min(os.listdir(compare_dir)))

  out_path = os.path.join(out_path, base[0], base[1], compare[0], compare[1])
  drivers.windowing.PreparePath(out_path)

  # TODO(jhaas): right now we're just dumping output to a log file
  # (and the console), which works as far as it goes but isn't nearly
  # robust enough. Change this after deciding exactly what we want to
  # change it to.
  out_file = open(os.path.join(out_path, "log.txt"), "w")
  description_string = ("Comparing %s %s to %s %s" %
                        (base[0], base[1], compare[0], compare[1]))
  out_file.write(description_string)
  print description_string

  base_dir = os.path.join(base_dir, base[1])
  compare_dir = os.path.join(compare_dir, compare[1])

  for filename in os.listdir(base_dir):
    out_file.write("%s: " % filename)

    if not os.path.isfile(os.path.join(compare_dir, filename)):
      out_file.write("Does not exist in target directory\n")
      print "File %s does not exist in target directory" % filename
      continue

    base_filename = os.path.join(base_dir, filename)
    compare_filename = os.path.join(compare_dir, filename)

    for op in ops:
      if type(op) in types.StringTypes: op = (op, None)

      module = operators.GetOperator(op[0])

      ret = module.Compare(base_filename, compare_filename)
      if ret is None:
        print "%s: OK" % (filename,)
        out_file.write("OK\n")
      else:
        print "%s: %s" % (filename, ret[0])
        out_file.write("%s\n" % (ret[0]))
        ret[1].save(os.path.join(out_path, filename))

  out_file.close()
Exemplo n.º 2
0
def ExecuteCompare2(command):
  """Executes the Compare2 command."""
  if command["--url"]:
    url_list = [command["--url"]]
  else:
    startline = command["--startline"]
    if command["--count"]:
      endline = startline+command["--count"]
    else:
      endline = command["--endline"]
    url_list = [url.strip() for url in
                open(command["--list"], "r").readlines()[startline:endline]]

  log_file = open(command["--logfile"], "w")

  outdir = command["--outdir"]
  if not outdir: outdir = tempfile.gettempdir()

  scrape_info_list = []

  class ScrapeInfo(object):
    """Helper class to hold information about a scrape."""
    __slots__ = ["browser_path", "scraper", "outdir", "result"]

  for index in xrange(1, 3):
    scrape_info = ScrapeInfo()
    scrape_info.browser_path = command["--browser%d" % index]
    scrape_info.scraper = scrapers.GetScraper(
      (command["--browser"], command["--browser%dver" % index]))

    if command["--browser%dname" % index]:
      scrape_info.outdir = os.path.join(outdir,
                                        command["--browser%dname" % index])
    else:
      scrape_info.outdir = os.path.join(outdir, str(index))

    drivers.windowing.PreparePath(scrape_info.outdir)
    scrape_info_list.append(scrape_info)

  compare = operators.GetOperator("equals_with_mask")

  for url in url_list:
    success = True

    for scrape_info in scrape_info_list:
      scrape_info.result = scrape_info.scraper.Scrape(
        [url], scrape_info.outdir, command["--size"], (0, 0),
        command["--timeout"], path=scrape_info.browser_path)

      if not scrape_info.result:
        scrape_info.result = "success"
      else:
        success = False

    result = "unknown"

    if success:
      result = "equal"

      file1 = drivers.windowing.URLtoFilename(
        url, scrape_info_list[0].outdir, ".bmp")
      file2 = drivers.windowing.URLtoFilename(
        url, scrape_info_list[1].outdir, ".bmp")

      comparison_result = compare.Compare(file1, file2,
                                          maskdir=command["--maskdir"])

      if comparison_result is not None:
        result = "not-equal"

        if command["--diffdir"]:
          comparison_result[1].save(
            drivers.windowing.URLtoFilename(url, command["--diffdir"], ".bmp"))

    # TODO(jhaas): maybe use the logging module rather than raw file writes
    log_file.write("%s %s %s %s\n" % (url,
                                      scrape_info_list[0].result,
                                      scrape_info_list[1].result,
                                      result))