diff --git a/library/packages/src/lib/y2packager/license.rb b/library/packages/src/lib/y2packager/license.rb index 75500b7cb..e128d7636 100644 --- a/library/packages/src/lib/y2packager/license.rb +++ b/library/packages/src/lib/y2packager/license.rb @@ -31,21 +31,59 @@ class License alias_method :accepted?, :accepted + class << self + # Find a license for a given product + def find(product_name, source) + return cache[product_name] if cache[product_name] + + # This could be done in the constructor. + new_license = License.new(LicensesFetchers::Rpm.new(product_name)) + return unless new_license.id + + eq_license = cache.values.find { |l| l.id == new_license.id } + license = eq_license || new_license + cache[product_name] = license + end + + private def cache + @cache ||= {} + end + end + # Constructor # # @param options [Hash] - def initialize(options = {}) + def initialize(fetcher) @accepted = false - @id = id_for(options) - @translations = { DEFAULT_LANG => options[:content] } + @fetcher = fetcher + @translations = {} + end + + def id + @id ||= id_for(content_for(DEFAULT_LANG)) end # Return the license translated content for the given language # + # It will return the empty string ("") if the license does not exist + # # @param [String] language # @return [String,nil] the license translated content or nil if not found def content_for(lang = DEFAULT_LANG) - translations[lang] + return @translations[lang] if @translations[lang] + add_content_for(fetcher.license_content(lang)) + end + + # FIXME: Probably the locales should be obtained through the licenses + # translations, and probably could be initialized the first time a license + # is instantiated. + def locales + fetcher.license_locales + end + + def license_confirmation_required? + # FIXME + true end # Add the license translated content for the given language diff --git a/library/packages/src/lib/y2packager/license_reader.rb b/library/packages/src/lib/y2packager/license_reader.rb index 42874cb83..cf4c46c24 100644 --- a/library/packages/src/lib/y2packager/license_reader.rb +++ b/library/packages/src/lib/y2packager/license_reader.rb @@ -1,4 +1,4 @@ -# ------------------------------------------------------------------------------ + # ------------------------------------------------------------------------------ # Copyright (c) 2018 SUSE LLC, All Rights Reserved. # # This program is free software; you can redistribute it and/or modify it under @@ -46,28 +46,6 @@ def license store.add_license_for(product_name, License.new(content: content)) end - # Return the license text - # - # It will return the empty string ("") if the license does not exist - # - # @param lang [String] Language - # @return [String,nil] Product's license; nil if the product was not found. - def license_content(lang) - return nil unless license - content = license.content_for(lang) - return content if content - content = fetcher.license_content(lang) - return unless content - license.add_content_for(lang, content) - end - - # FIXME: Probably the locales should be obtained through the licenses - # translations, and probably could be initialized the first time a license - # is instantiated. - def locales - fetcher.license_locales - end - # Determine whether the license should be accepted or not # # @return [Boolean] true if the license acceptance is required diff --git a/library/packages/src/lib/y2packager/product_license.rb b/library/packages/src/lib/y2packager/product_license.rb new file mode 100644 index 000000000..5531315f2 --- /dev/null +++ b/library/packages/src/lib/y2packager/product_license.rb @@ -0,0 +1,51 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) 2018 SUSE LLC, All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of version 2 of the GNU General Public License as published by the +# Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# ------------------------------------------------------------------------------ + +require "yast" +require "digest" + +module Y2Packager + class ProductLicense + class << self + # Find license for a given product + def find(product_name, source = :rpm) + return cache[product_name] if cache[product_name] + license = License.find(product_name, source) + return nil unless license + cache[product_name] = ProductLicense.new(product_name, license) + end + + private def cache + @cache ||= {} + end + end + + # Constructor + # + # @param product_name [String] Product name to get licenses for + # @param source [Symbol] Source to use for fetching the license from + def initialize(product_name, license) + @product_name = product_name + @license = license + # @adapter = ProductLicenseAdapter.new + end + + def accepted? + license.accepted? + end + + def accept! + license.accept! + # adapter.accept! + end + end +end