<commit_before>#
# Copyright 2015, <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'shellwords'

require 'chef/mixin/which'
require 'chef/provider/package'
require 'chef/resource/package'
require 'poise'

require 'poise_python/python_command_mixin'


module PoisePython
  module Resources
    # (see PythonPackage::Resource)
    # @since 1.0.0
    module PythonPackage
      # A Python snippet to hack pip a bit so `pip list --outdated` will show
      # only the things we want and will understand version requirements.
      # @api private
      PIP_HACK_SCRIPT = <<-EOH
import sys

import pip
try:
    # >= 6.0
    from pip.utils import get_installed_distributions
except ImportError:
    # <= 1.5.6
    from pip.util import get_installed_distributions

def replacement(*args, **kwargs):
    import copy, sys
    from pip._vendor import pkg_resources
    dists = []
    for raw_req in sys.argv[3:]:
        req = pkg_resources.Requirement.parse(raw_req)
        dist = pkg_resources.working_set.by_key.get(req.key)
        if dist:
          # Don't mutate stuff from the global working set.
          dist = copy.copy(dist)
        else:
          # Make a fake one.
          dist = pkg_resources.Distribution(project_name=req.key, version='0')
        # Fool the .key property into using our string.
        dist._key = raw_req
        dists.append(dist)
    return dists
try:
    # For Python 2.
    get_installed_distributions.func_code = replacement.func_code
except AttributeError:
    # For Python 3.
    get_installed_distributions.__code__ = replacement.__code__

sys.exit(pip.main())
EOH

      # A `python_package` resource to manage Python installations using pip.
      #
      # @provides python_package
      # @action install
      # @action upgrade
      # @action uninstall
      # @example
      #   python_package 'django' do
      #     python '2'
      #     version '1.8.3'
      #   end
      class Resource < Chef::Resource::Package
        include PoisePython::PythonCommandMixin
        provides(:python_package)


        # @!attribute group
        #   System group to install the package.
        #   @return [String, Integer, nil]
        attribute(:group, kind_of: [String, Integer, NilClass])
        # @!attribute user
        #   System user to install the package.
        #   @return [String, Integer, nil]
        attribute(:user, kind_of: [String, Integer, NilClass])

        def initialize(*args)
          super
          # For older Chef.
          @resource_name = :python_package
          # We don't have these actions.
          @allowed_actions.delete(:purge)
          @allowed_actions.delete(:reconfig)
        end<commit_msg>Make sure matchers work with python_package like with node_package.<commit_after>#
# Copyright 2015, <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'shellwords'

require 'chef/mixin/which'
require 'chef/provider/package'
require 'chef/resource/package'
require 'poise'

require 'poise_python/python_command_mixin'


module PoisePython
  module Resources
    # (see PythonPackage::Resource)
    # @since 1.0.0
    module PythonPackage
      # A Python snippet to hack pip a bit so `pip list --outdated` will show
      # only the things we want and will understand version requirements.
      # @api private
      PIP_HACK_SCRIPT = <<-EOH
import sys

import pip
try:
    # >= 6.0
    from pip.utils import get_installed_distributions
except ImportError:
    # <= 1.5.6
    from pip.util import get_installed_distributions

def replacement(*args, **kwargs):
    import copy, sys
    from pip._vendor import pkg_resources
    dists = []
    for raw_req in sys.argv[3:]:
        req = pkg_resources.Requirement.parse(raw_req)
        dist = pkg_resources.working_set.by_key.get(req.key)
        if dist:
          # Don't mutate stuff from the global working set.
          dist = copy.copy(dist)
        else:
          # Make a fake one.
          dist = pkg_resources.Distribution(project_name=req.key, version='0')
        # Fool the .key property into using our string.
        dist._key = raw_req
        dists.append(dist)
    return dists
try:
    # For Python 2.
    get_installed_distributions.func_code = replacement.func_code
except AttributeError:
    # For Python 3.
    get_installed_distributions.__code__ = replacement.__code__

sys.exit(pip.main())
EOH

      # A `python_package` resource to manage Python installations using pip.
      #
      # @provides python_package
      # @action install
      # @action upgrade
      # @action uninstall
      # @example
      #   python_package 'django' do
      #     python '2'
      #     version '1.8.3'
      #   end
      class Resource < Chef::Resource::Package
        include PoisePython::PythonCommandMixin
        provides(:python_package)
        # Manually create matchers because #actions is unreliable.
        %i{install upgrade remove}.each do |action|
          Poise::Helpers::ChefspecMatchers.create_matcher(:python_package, action)
        end


        # @!attribute group
        #   System group to install the package.
        #   @return [String, Integer, nil]
        attribute(:group, kind_of: [String, Integer, NilClass])
        # @!attribute user
        #   System user to install the package.
        #   @return [String, Integer, nil]
        attribute(:user, kind_of: [String, Integer, NilClass])

        def initialize(*args)
          super
          # For older Chef.
          @resource_name = :python_package
          # We don't have these actions.
          @allowed_actions.delete(:purge)
          @allowed_actions.delete(:reconfig)
        end
{'fraction_non_alphanumeric': 0.08493686109440769, 'fraction_numerical': 0.0072158749248346366, 'mean_word_length': 2.706406685236769, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2391916', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1857, 'n_words': 618}