<commit_before>#
# Copyright:: Copyright 2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# 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 "license_scout/net_fetcher"

require "pathname"

module LicenseScout
  class Overrides

    class OverrideLicenseSet

      attr_reader :license_locations

      def initialize(license_locations)
        @license_locations = license_locations || []
      end

      def empty?
        license_locations.empty?
      end

      def resolve_locations(dependency_root_dir)
        license_locations.map do |license_location|
          if NetFetcher.remote?(license_location)
            NetFetcher.cache(license_location)
          else
            normalize_and_verify_path(license_location, dependency_root_dir)
          end
        end
      end

      def normalize_and_verify_path(license_location, dependency_root_dir)
        full_path = File.expand_path(license_location, dependency_root_dir)
        if File.exists?(full_path)
          full_path
        else
          raise Exceptions::InvalidOverride, "Provided license file path '#{license_location}' can not be found under detected dependency path '#{dependency_root_dir}'."
        end
      end

    end

    attr_reader :override_rules

    def initialize(exclude_default: false, &rules)
      @override_rules = {}
      instance_eval(&rules) if block_given?

      default_overrides unless exclude_default
    end

    def override_license(dependency_manager, dependency_name, &rule)
      override_rules[dependency_manager] ||= {}
      override_rules[dependency_manager][dependency_name] = rule
    end

    def license_for(dependency_manager, dependency_name, dependency_version)
      license_data = license_data_for(dependency_manager, dependency_name, dependency_version)
      license_data && license_data[:license]
    end

    def license_files_for(dependency_manager, dependency_name, dependency_version)
      license_data = license_data_for(dependency_manager, dependency_name, dependency_version)
      OverrideLicenseSet.new(license_data && license_data[:license_files])
    end

    def have_override_for?(dependency_manager, dependency_name, dependency_version)
      override_rules.key?(dependency_manager) && override_rules[dependency_manager].key?(dependency_name)
    end

    private

    def license_data_for(dependency_manager, dependency_name, dependency_version)
      return nil unless have_override_for?(dependency_manager, dependency_name, dependency_version)
      override_rules[dependency_manager][dependency_name].call(dependency_version)
    end

    def canonical(shortname)
      File.expand_path("../canonical_licenses/#{shortname}.txt", __FILE__)
    end

    def default_overrides
      # AWS Ruby SDK
      aws_sdk_gems = %w{
          aws-partitions
          aws-sdk-acm
          aws-sdk-apigateway
          aws-sdk-applicationautoscaling
          aws-sdk-applicationdiscoveryservice
          aws-sdk-appstream
          aws-sdk-athena
          aws-sdk-autoscaling
          aws-sdk-batch
          aws-sdk-budgets
          aws-sdk-clouddirectory
          aws-sdk-cloudformation
          aws-sdk-cloudfront
          aws-sdk-cloudhsm
          aws-sdk-cloudhsmv2
          aws-sdk-cloudsearch
          aws-sdk-cloudsearchdomain
          aws-sdk-cloudtrail
          aws-sdk-cloudwatch
          aws-sdk-cloudwatchevents
          aws-sdk-cloudwatchlogs
          aws-sdk-codebuild
          aws-sdk-codecommit
          aws-sdk-codedeploy
          aws-sdk-codepipeline
          aws-sdk-codestar
          aws-sdk-cognitoidentity
          aws-sdk-cognitoidentityprovider
          aws-sdk-cognitosync
          aws-sdk-configservice
          aws-sdk-costandusagereportservice
          aws-sdk-databasemigrationservice
          aws-sdk-datapipeline
          aws-sdk-dax
          aws-sdk-devicefarm
          aws-sdk-directconnect
          aws-sdk-directoryservice
          aws-sdk-dynamodb
          aws-sdk-dynamodbstreams
          aws-sdk-ec2
          aws-sdk-ecr
          aws-sdk-ecs
          aws-sdk-efs
          aws-sdk-elasticache
          aws-sdk-elasticbeanstalk
          aws-sdk-elasticloadbalancing
          aws-sdk-elasticloadbalancingv2
          aws-sdk-elasticsearchservice
          aws-sdk-elastictranscoder
          aws-sdk-emr
          aws-sdk-firehose
          aws-sdk-gamelift
          aws-sdk-glacier
          aws-sdk-glue
          aws-sdk-greengrass
          aws-sdk-health
          aws-sdk-iam
          aws-sdk-importexport
          aws-sdk-inspector
          aws-sdk-iot
          aws-sdk-iotdataplane
          aws-sdk-kinesis
          aws-sdk-kinesisanalytics
          aws-sdk-kms
          aws-sdk-lambda
          aws-sdk-lambdapreview
          aws-sdk-lex
          aws-sdk-lexmodelbuildingservice
          aws-sdk-lightsail
          aws-sdk-machinelearning
          aws-sdk-marketplacecommerceanalytics
          aws-sdk-marketplaceentitlementservice
          aws-sdk-marketplacemetering
          aws-sdk-migrationhub
          aws-sdk-mturk
          aws-sdk-opsworks
          aws-sdk-opsworkscm
          aws-sdk-organizations
          aws-sdk-pinpoint
          aws-sdk-polly
          aws-sdk-rds
          aws-sdk-redshift
          aws-sdk-rekognition
          aws-sdk-resourcegroupstaggingapi
          aws-sdk-route53
          aws-sdk-route53domains
          aws-sdk-s3
          aws-sdk-servicecatalog
          aws-sdk-ses
          aws-sdk-shield
          aws-sdk-simpledb
          aws-sdk-sms
          aws-sdk-snowball
          aws-sdk-sns
          aws-sdk-sqs
          aws-sdk-ssm aws-sdk-states
          aws-sdk-storagegateway
          aws-sdk-support
          aws-sdk-swf
          aws-sdk-waf
          aws-sdk-wafregional
          aws-sdk-workdocs
          aws-sdk-workspaces
          aws-sdk-xray<commit_msg>add aws-sdk-mobile to override gems<commit_after>#
# Copyright:: Copyright 2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# 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 "license_scout/net_fetcher"

require "pathname"

module LicenseScout
  class Overrides

    class OverrideLicenseSet

      attr_reader :license_locations

      def initialize(license_locations)
        @license_locations = license_locations || []
      end

      def empty?
        license_locations.empty?
      end

      def resolve_locations(dependency_root_dir)
        license_locations.map do |license_location|
          if NetFetcher.remote?(license_location)
            NetFetcher.cache(license_location)
          else
            normalize_and_verify_path(license_location, dependency_root_dir)
          end
        end
      end

      def normalize_and_verify_path(license_location, dependency_root_dir)
        full_path = File.expand_path(license_location, dependency_root_dir)
        if File.exists?(full_path)
          full_path
        else
          raise Exceptions::InvalidOverride, "Provided license file path '#{license_location}' can not be found under detected dependency path '#{dependency_root_dir}'."
        end
      end

    end

    attr_reader :override_rules

    def initialize(exclude_default: false, &rules)
      @override_rules = {}
      instance_eval(&rules) if block_given?

      default_overrides unless exclude_default
    end

    def override_license(dependency_manager, dependency_name, &rule)
      override_rules[dependency_manager] ||= {}
      override_rules[dependency_manager][dependency_name] = rule
    end

    def license_for(dependency_manager, dependency_name, dependency_version)
      license_data = license_data_for(dependency_manager, dependency_name, dependency_version)
      license_data && license_data[:license]
    end

    def license_files_for(dependency_manager, dependency_name, dependency_version)
      license_data = license_data_for(dependency_manager, dependency_name, dependency_version)
      OverrideLicenseSet.new(license_data && license_data[:license_files])
    end

    def have_override_for?(dependency_manager, dependency_name, dependency_version)
      override_rules.key?(dependency_manager) && override_rules[dependency_manager].key?(dependency_name)
    end

    private

    def license_data_for(dependency_manager, dependency_name, dependency_version)
      return nil unless have_override_for?(dependency_manager, dependency_name, dependency_version)
      override_rules[dependency_manager][dependency_name].call(dependency_version)
    end

    def canonical(shortname)
      File.expand_path("../canonical_licenses/#{shortname}.txt", __FILE__)
    end

    def default_overrides
      # AWS Ruby SDK
      aws_sdk_gems = %w{
          aws-partitions
          aws-sdk-acm
          aws-sdk-apigateway
          aws-sdk-applicationautoscaling
          aws-sdk-applicationdiscoveryservice
          aws-sdk-appstream
          aws-sdk-athena
          aws-sdk-autoscaling
          aws-sdk-batch
          aws-sdk-budgets
          aws-sdk-clouddirectory
          aws-sdk-cloudformation
          aws-sdk-cloudfront
          aws-sdk-cloudhsm
          aws-sdk-cloudhsmv2
          aws-sdk-cloudsearch
          aws-sdk-cloudsearchdomain
          aws-sdk-cloudtrail
          aws-sdk-cloudwatch
          aws-sdk-cloudwatchevents
          aws-sdk-cloudwatchlogs
          aws-sdk-codebuild
          aws-sdk-codecommit
          aws-sdk-codedeploy
          aws-sdk-codepipeline
          aws-sdk-codestar
          aws-sdk-cognitoidentity
          aws-sdk-cognitoidentityprovider
          aws-sdk-cognitosync
          aws-sdk-configservice
          aws-sdk-costandusagereportservice
          aws-sdk-databasemigrationservice
          aws-sdk-datapipeline
          aws-sdk-dax
          aws-sdk-devicefarm
          aws-sdk-directconnect
          aws-sdk-directoryservice
          aws-sdk-dynamodb
          aws-sdk-dynamodbstreams
          aws-sdk-ec2
          aws-sdk-ecr
          aws-sdk-ecs
          aws-sdk-efs
          aws-sdk-elasticache
          aws-sdk-elasticbeanstalk
          aws-sdk-elasticloadbalancing
          aws-sdk-elasticloadbalancingv2
          aws-sdk-elasticsearchservice
          aws-sdk-elastictranscoder
          aws-sdk-emr
          aws-sdk-firehose
          aws-sdk-gamelift
          aws-sdk-glacier
          aws-sdk-glue
          aws-sdk-greengrass
          aws-sdk-health
          aws-sdk-iam
          aws-sdk-importexport
          aws-sdk-inspector
          aws-sdk-iot
          aws-sdk-iotdataplane
          aws-sdk-kinesis
          aws-sdk-kinesisanalytics
          aws-sdk-kms
          aws-sdk-lambda
          aws-sdk-lambdapreview
          aws-sdk-lex
          aws-sdk-lexmodelbuildingservice
          aws-sdk-lightsail
          aws-sdk-machinelearning
          aws-sdk-marketplacecommerceanalytics
          aws-sdk-marketplaceentitlementservice
          aws-sdk-marketplacemetering
          aws-sdk-migrationhub
          aws-sdk-mobile
          aws-sdk-mturk
          aws-sdk-opsworks
          aws-sdk-opsworkscm
          aws-sdk-organizations
          aws-sdk-pinpoint
          aws-sdk-polly
          aws-sdk-rds
          aws-sdk-redshift
          aws-sdk-rekognition
          aws-sdk-resourcegroupstaggingapi
          aws-sdk-route53
          aws-sdk-route53domains
          aws-sdk-s3
          aws-sdk-servicecatalog
          aws-sdk-ses
          aws-sdk-shield
          aws-sdk-simpledb
          aws-sdk-sms
          aws-sdk-snowball
          aws-sdk-sns
          aws-sdk-sqs
          aws-sdk-ssm aws-sdk-states
          aws-sdk-storagegateway
          aws-sdk-support
          aws-sdk-swf
          aws-sdk-waf
          aws-sdk-wafregional
          aws-sdk-workdocs
          aws-sdk-workspaces
          aws-sdk-xray
{'fraction_non_alphanumeric': 0.06815877719722374, 'fraction_numerical': 0.0028074553536613896, 'mean_word_length': 2.141597256246938, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '793093', 'n_tokens_mistral': 3725, 'n_tokens_neox': 3582, 'n_words': 674}