<commit_before># Copyright 2013 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
from slave import recipe_api


def path_method(api, name, base):
  """Returns a shortcut static method which functions like os.path.join but
  with a fixed first component |base|.
  """
  def path_func_inner(*pieces, **kwargs):
    """Return a path to a file in '%s'.

    It supports the following kwargs:
      wrapper (bool): If true, the path should be considered to be a wrapper
                      script, and will gain the appropriate '.bat' extension
                      on windows.
    """
    use_wrapper = kwargs.get('wrapper') and api.m.platform.is_win
    WRAPPER_EXTENSION = '.bat' if use_wrapper else ''
    assert api.pardir not in pieces
    return api.join(base, *pieces) + WRAPPER_EXTENSION
  path_func_inner.__name__ = name
  path_func_inner.__doc__ = path_func_inner.__doc__ % base
  return path_func_inner


class mock_path(object):
  """Standin for os.path when we're in test mode.

  This class simulates the os.path interface exposed by PathApi, respecting the
  current platform according to the `platform` module. This allows us to
  simulate path functions according to the platform being tested, rather than
  the platform which is currently running.
  """

  def __init__(self, api, _mock_path_exists):
    self._api = api
    self._mock_path_exists = set(_mock_path_exists)
    self._pth = None

  def __getattr__(self, name):
    if not self._pth:
      if self._api.platform.is_win:
        import ntpath as pth
      elif self._api.platform.is_mac or self._api.platform.is_linux:
        import posixpath as pth
      self._pth = pth
    return getattr(self._pth, name)

  def _initialize_exists(self):  # pylint: disable=E0202
    """
    Calculates all the parent paths of the mock'd paths and makes exists()
    read from this new set().
    """
    self._initialize_exists = lambda: None
    for path in list(self._mock_path_exists):
      self.mock_add_paths(path)
    self.exists = lambda path: path in self._mock_path_exists

  def mock_add_paths(self, path):
    """
    Adds a path and all of its parents to the set of existing paths.
    """
    self._initialize_exists()
    while path:
      self._mock_path_exists.add(path)
      path = self.dirname(path)

  def exists(self, path):  # pylint: disable=E0202
    """Return True if path refers to an existing path."""
    self._initialize_exists()
    return self.exists(path)

  def abspath(self, path):
    """Returns the absolute version of path."""
    path = self.normpath(path)
    if path[0] != '[':  # pragma: no cover
      # We should never really hit this, but simulate the effect.
      return self.slave_build(path)
    else:
      return path


class PathApi(recipe_api.RecipeApi):
  """
  PathApi provides common os.path functions as well as convenience functions
  for generating absolute paths to things in a testable way.

  Mocks:
    exists (list): Paths which should exist in the test case. Thes must be paths
      using the [*_ROOT] placeholders. ex. '[BUILD_ROOT]/scripts'.
  """

  OK_METHODS = ('basename', 'abspath', 'join', 'pardir', 'exists', 'splitext',
                'split')

  def __init__(self, **kwargs):
    super(PathApi, self).__init__(**kwargs)

    if self._mock is None:  # pragma: no cover
      self._path_mod = os.path
      # e.g. /b/build/slave/<slavename>/build
      self.slave_build = path_method(
        self, 'slave_build', self.abspath(os.getcwd()))

      # e.g. /b
      r = self.abspath(self.join(self.slave_build(), *([self.pardir]*4)))
      for token in ('build_internal', 'build', 'depot_tools'):
        # e.g. /b/{token}
        setattr(self, token, path_method(self, token, self.join(r, token)))
      self.root = path_method(self, 'root', r)
    else:
      self._path_mod = mock_path(self.m, self._mock.get('exists', []))
      self.slave_build = path_method(self, 'slave_build', '[SLAVE_BUILD_ROOT]')
      self.build_internal = path_method(
        self, 'build_internal', '[BUILD_INTERNAL_ROOT]')
      self.build = path_method(self, 'build', '[BUILD_ROOT]')<commit_msg>Add CHROME_BIN to polymer recipe.<commit_after># Copyright 2013 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
from slave import recipe_api


def path_method(api, name, base):
  """Returns a shortcut static method which functions like os.path.join but
  with a fixed first component |base|.
  """
  def path_func_inner(*pieces, **kwargs):
    """Return a path to a file in '%s'.

    It supports the following kwargs:
      wrapper (bool): If true, the path should be considered to be a wrapper
                      script, and will gain the appropriate '.bat' extension
                      on windows.
    """
    use_wrapper = kwargs.get('wrapper') and api.m.platform.is_win
    WRAPPER_EXTENSION = '.bat' if use_wrapper else ''
    assert api.pardir not in pieces
    return api.join(base, *pieces) + WRAPPER_EXTENSION
  path_func_inner.__name__ = name
  path_func_inner.__doc__ = path_func_inner.__doc__ % base
  return path_func_inner


class mock_path(object):
  """Standin for os.path when we're in test mode.

  This class simulates the os.path interface exposed by PathApi, respecting the
  current platform according to the `platform` module. This allows us to
  simulate path functions according to the platform being tested, rather than
  the platform which is currently running.
  """

  def __init__(self, api, _mock_path_exists):
    self._api = api
    self._mock_path_exists = set(_mock_path_exists)
    self._pth = None

  def __getattr__(self, name):
    if not self._pth:
      if self._api.platform.is_win:
        import ntpath as pth
      elif self._api.platform.is_mac or self._api.platform.is_linux:
        import posixpath as pth
      self._pth = pth
    return getattr(self._pth, name)

  def _initialize_exists(self):  # pylint: disable=E0202
    """
    Calculates all the parent paths of the mock'd paths and makes exists()
    read from this new set().
    """
    self._initialize_exists = lambda: None
    for path in list(self._mock_path_exists):
      self.mock_add_paths(path)
    self.exists = lambda path: path in self._mock_path_exists

  def mock_add_paths(self, path):
    """
    Adds a path and all of its parents to the set of existing paths.
    """
    self._initialize_exists()
    while path:
      self._mock_path_exists.add(path)
      path = self.dirname(path)

  def exists(self, path):  # pylint: disable=E0202
    """Return True if path refers to an existing path."""
    self._initialize_exists()
    return self.exists(path)

  def abspath(self, path):
    """Returns the absolute version of path."""
    path = self.normpath(path)
    if path[0] != '[':  # pragma: no cover
      # We should never really hit this, but simulate the effect.
      return self.slave_build(path)
    else:
      return path


class PathApi(recipe_api.RecipeApi):
  """
  PathApi provides common os.path functions as well as convenience functions
  for generating absolute paths to things in a testable way.

  Mocks:
    exists (list): Paths which should exist in the test case. Thes must be paths
      using the [*_ROOT] placeholders. ex. '[BUILD_ROOT]/scripts'.
  """

  OK_METHODS = ('abspath', 'basename', 'exists', 'join', 'pardir',
                'sep', 'split', 'splitext')

  def __init__(self, **kwargs):
    super(PathApi, self).__init__(**kwargs)

    if self._mock is None:  # pragma: no cover
      self._path_mod = os.path
      # e.g. /b/build/slave/<slavename>/build
      self.slave_build = path_method(
        self, 'slave_build', self.abspath(os.getcwd()))

      # e.g. /b
      r = self.abspath(self.join(self.slave_build(), *([self.pardir]*4)))
      for token in ('build_internal', 'build', 'depot_tools'):
        # e.g. /b/{token}
        setattr(self, token, path_method(self, token, self.join(r, token)))
      self.root = path_method(self, 'root', r)
    else:
      self._path_mod = mock_path(self.m, self._mock.get('exists', []))
      self.slave_build = path_method(self, 'slave_build', '[SLAVE_BUILD_ROOT]')
      self.build_internal = path_method(
        self, 'build_internal', '[BUILD_INTERNAL_ROOT]')
      self.build = path_method(self, 'build', '[BUILD_ROOT]')
{'fraction_non_alphanumeric': 0.10682702511496286, 'fraction_numerical': 0.0033014974649215895, 'mean_word_length': 3.1395802830649098, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '6058620', 'n_tokens_mistral': 2681, 'n_tokens_neox': 2599, 'n_words': 844}