<commit_before># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import sys

from telemetry import test as test_module
from telemetry.core import browser_options
from telemetry.core import discover
from telemetry.page import page_test as page_test_module
from telemetry.page import page_runner
from telemetry.page import page_set

def Main(env):
  """Turns a PageTest into a command-line program.

  Args:
    base_dir: Path to directory containing tests and ProfileCreators.
  """
  runner = PageTestRunner()
  sys.exit(runner.Run(env))

class PageTestRunner(object):
  def __init__(self):
    self._parser = None
    self._options = None
    self._args = None

  @property
  def test_class(self):
    return page_test_module.PageTest

  @property
  def test_class_name(self):
    return 'test'

  def Run(self, env):
    test, ps, expectations = self.ParseCommandLine(sys.argv, env)
    results = page_runner.Run(test, ps, expectations, self._options)
    results.PrintSummary()
    return min(255, len(results.failures + results.errors))

  def FindTestConstructors(self, base_dirs):
    # Look for both Tests and PageTests, but Tests get priority, because
    test_constructors = {}
    for base_dir in base_dirs:
      test_constructors.update(discover.DiscoverClasses(
          base_dir, base_dir, self.test_class))
    for base_dir in base_dirs:
      test_constructors.update(discover.DiscoverClasses(
          base_dir, base_dir, test_module.Test, index_by_class_name=True))
    return test_constructors

  def FindTestName(self, test_constructors, args):
    """Find the test name in an arbitrary argument list.

    We can't use the optparse parser, because the test may add its own
    command-line options. If the user passed in any of those, the
    optparse parsing will fail.

    Returns:
      test_name or None
    """
    test_name = None
    for arg in [self.GetModernizedTestName(a) for a in args]:
      if arg in test_constructors:
        test_name = arg

    return test_name

  def GetModernizedTestName(self, arg):
    """Sometimes tests change names but buildbots keep calling the old name.

    If arg matches an old test name, return the new test name instead.
    Otherwise, return the arg.
    """
    return arg

  def GetPageSet(self, test, env):
    ps = test.CreatePageSet(self._args, self._options)
    if ps:
      return ps

    if len(self._args) < 2:
      page_set_filenames = []
      for perf_path in env.base_paths:
        # Check if page_sets subfolder is present, and get all page_sets
        # files if present.
        page_set_dir = os.path.join(perf_path, "page_sets")
        if not os.path.exists(page_set_dir):
          continue
        page_set_filenames.append(
            discover.GetAllPageSetFilenames(page_set_dir))
      page_set_list = ',\n'.join(
          sorted([os.path.relpath(f) for f in page_set_filenames]))
      self.PrintParseError(
          'No page set, file, or URL specified.\n'
          'Available page sets:\n'
          '%s' % page_set_list)

    page_set_arg = self._args[1]

    # We've been given a URL. Create a page set with just that URL.
    if (page_set_arg.startswith('http://') or
        page_set_arg.startswith('https://')):<commit_msg>telemetry: deprecate GetModernizedTestName which is not used anymore<commit_after># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import sys

from telemetry import test as test_module
from telemetry.core import browser_options
from telemetry.core import discover
from telemetry.page import page_test as page_test_module
from telemetry.page import page_runner
from telemetry.page import page_set

def Main(env):
  """Turns a PageTest into a command-line program.

  Args:
    base_dir: Path to directory containing tests and ProfileCreators.
  """
  runner = PageTestRunner()
  sys.exit(runner.Run(env))

class PageTestRunner(object):
  def __init__(self):
    self._parser = None
    self._options = None
    self._args = None

  @property
  def test_class(self):
    return page_test_module.PageTest

  @property
  def test_class_name(self):
    return 'test'

  def Run(self, env):
    test, ps, expectations = self.ParseCommandLine(sys.argv, env)
    results = page_runner.Run(test, ps, expectations, self._options)
    results.PrintSummary()
    return min(255, len(results.failures + results.errors))

  def FindTestConstructors(self, base_dirs):
    # Look for both Tests and PageTests, but Tests get priority, because
    test_constructors = {}
    for base_dir in base_dirs:
      test_constructors.update(discover.DiscoverClasses(
          base_dir, base_dir, self.test_class))
    for base_dir in base_dirs:
      test_constructors.update(discover.DiscoverClasses(
          base_dir, base_dir, test_module.Test, index_by_class_name=True))
    return test_constructors

  def FindTestName(self, test_constructors, args):
    """Find the test name in an arbitrary argument list.

    We can't use the optparse parser, because the test may add its own
    command-line options. If the user passed in any of those, the
    optparse parsing will fail.

    Returns:
      test_name or None
    """
    test_name = None
    for arg in args:
      if arg in test_constructors:
        test_name = arg

    return test_name

  def GetPageSet(self, test, env):
    ps = test.CreatePageSet(self._args, self._options)
    if ps:
      return ps

    if len(self._args) < 2:
      page_set_filenames = []
      for perf_path in env.base_paths:
        # Check if page_sets subfolder is present, and get all page_sets
        # files if present.
        page_set_dir = os.path.join(perf_path, "page_sets")
        if not os.path.exists(page_set_dir):
          continue
        page_set_filenames.append(
            discover.GetAllPageSetFilenames(page_set_dir))
      page_set_list = ',\n'.join(
          sorted([os.path.relpath(f) for f in page_set_filenames]))
      self.PrintParseError(
          'No page set, file, or URL specified.\n'
          'Available page sets:\n'
          '%s' % page_set_list)

    page_set_arg = self._args[1]

    # We've been given a URL. Create a page set with just that URL.
    if (page_set_arg.startswith('http://') or
        page_set_arg.startswith('https://')):
{'fraction_non_alphanumeric': 0.08174261083743842, 'fraction_numerical': 0.002770935960591133, 'mean_word_length': 3.1862113402061856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '404307', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1962, 'n_words': 629}