Skip to content

Commit

Permalink
Update from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Jan 2, 2019
1 parent ca4665a commit cff1b38
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 24 deletions.
4 changes: 2 additions & 2 deletions library/packages/src/lib/y2packager/license.rb
Expand Up @@ -96,8 +96,8 @@ def cache
# Bear in mind that `fetcher` will be ignored if `content` is specified.
#
# @param product_name [String] Product name to retrieve license information
# @param content [String] License content. If this argument is given, this
# string is used as the license's content (and `product_name` is ignored).
# @param content [String] License content. When given, this string is used as the license's
# content, ignoring the `product_name`
# @param fetcher [LicensesFetchers::Base] The license's fetcher
# @param handler [LicensesHandlers::Base] The license's handler
def initialize(product_name: nil, content: nil, fetcher: nil, handler: nil)
Expand Down
2 changes: 2 additions & 0 deletions library/packages/src/lib/y2packager/licenses_fetchers.rb
Expand Up @@ -22,6 +22,8 @@ module Y2Packager
module LicensesFetchers
include Yast::Logger

# Candidate sources to retrieve the license content. Note that order matters because it will be
# chosen the first source able to fetch the content.
KNOWN_SOURCES = [:libzypp, :rpm].freeze

# Return the proper license fetcher
Expand Down
4 changes: 0 additions & 4 deletions library/packages/src/lib/y2packager/licenses_fetchers/base.rb
Expand Up @@ -40,10 +40,6 @@ def found?
!default_content.empty?
end

def content(lang)
return @default_content if default_lang?(lang) && @default_content
end

private

# Return (and caches) the content found for the default language
Expand Down
Expand Up @@ -23,7 +23,7 @@ class Libzypp < Base
#
# @return [String, nil] Product's license; nil if the product or the license were not found.
def content(lang)
super
return @default_content if default_lang?(lang) && @default_content

Yast::Pkg.PrdGetLicenseToConfirm(product_name, lang)
end
Expand Down
41 changes: 27 additions & 14 deletions library/packages/src/lib/y2packager/licenses_fetchers/rpm.rb
Expand Up @@ -23,7 +23,7 @@ class Rpm < Base
#
# @return [String, nil] Product's license; nil if the product or the license were not found
def content(lang)
super
return @default_content if default_lang?(lang) && @default_content

if package.nil?
log.info("No package found for #{product_name}")
Expand Down Expand Up @@ -88,43 +88,54 @@ def license_content_for(lang)
end
end

# Return license file path for a given package and language
# Return license file path for the given languages
#
# When a license for a language "xx_XX" is not found, it will fallback to "xx".
#
# @param directory [String] Directory where licenses were uncompressed
# @param lang [String] Searched language
# @param lang [String] Searched translation
#
# @return [Array<String, String>] Array containing the path and language code
# @return [String, lang] The first licence path for given languages or nil
def license_path(directory, lang)
candidate_langs = [lang]
candidate_langs << lang.split("_", 2).first if lang
candidate_langs.uniq!

log.info("Searching for a #{candidate_langs.join(",")} license translations in #{directory}")

Dir.glob(
File.join(directory, "**", "LICENSE.{#{candidate_langs.join(",")}}.TXT"),
File::FNM_CASEFOLD
).first
find_path_for(directory, "LICENSE.{#{candidate_langs.join(",")}}.TXT")
end

# Fallback license file
FALLBACK_LICENSE_FILE = "LICENSE.TXT".freeze

# Return the fallback license file path
#
# Looking for a license file without language code
#
# @param directory [String] Directory where licenses were uncompressed
#
# @return [String, nil] The fallback license path
def fallback_path(directory)
log.info("Searching for a fallback #{FALLBACK_LICENSE_FILE} file in #{directory}")

Dir.glob(
File.join(directory, "**", FALLBACK_LICENSE_FILE),
File::FNM_CASEFOLD
).first
find_path_for(directory, FALLBACK_LICENSE_FILE)
end

# Return the path for the given file in specified directory
#
# @param directory [String] Directory where licenses were uncompressed
# @param file [String] Searched file
#
# @return [String, nil] The file path; nil if was not found
def find_path_for(directory, file)
Dir.glob(File.join(directory, "**", file), File::FNM_CASEFOLD).first
end

# Valid statuses for packages containing licenses
AVAILABLE_STATUSES = [:available, :selected].freeze

# Find the latest available/selected product package
# Find the highest version of available/selected product package
#
# @return [Y2Packager::Package, nil] Package containing licenses; nil if not found
def package
Expand All @@ -145,7 +156,9 @@ def package
def package_name
return @package_name if @package_name

package_properties = Yast::Pkg.ResolvableProperties(product_name, :product, "").first || {}
package_properties = Yast::Pkg.ResolvableProperties(product_name, :product, "")
package_properties = package_properties.find { |props| props.key?("product_package") }
package_properties ||= {}

@package_name = package_properties.fetch("product_package", nil)
end
Expand Down
2 changes: 1 addition & 1 deletion library/packages/src/lib/y2packager/licenses_handlers.rb
Expand Up @@ -25,7 +25,7 @@ module LicensesHandlers
# @param fetcher [LicensesFetchers::Base] Fetcher used as source to fetch license
# @param product_name [String] Product's name
#
# @return [Object]
# @return [LicenseHandlers::Base]
def self.for(fetcher, product_name)
type = fetcher.class.name.split("::").last
klass = const_get(type.to_s.capitalize)
Expand Down
6 changes: 4 additions & 2 deletions library/packages/src/lib/y2packager/licenses_handlers/rpm.rb
Expand Up @@ -62,7 +62,7 @@ def ignore_acceptance?
# Valid statuses for packages containing licenses
AVAILABLE_STATUSES = [:available, :selected].freeze

# Find the latest available/selected product package
# Find the highest version of available/selected product package
#
# @return [Y2Packager::Package, nil] Package containing licenses; nil if not found
def find_package
Expand All @@ -81,7 +81,9 @@ def find_package
def package_name
return @package_name if @package_name

package_properties = Yast::Pkg.ResolvableProperties(product_name, :product, "").first || {}
package_properties = Yast::Pkg.ResolvableProperties(product_name, :product, "")
package_properties = package_properties.find { |props| props.key?("product_package") }
package_properties ||= {}

@package_name = package_properties.fetch("product_package", nil)
end
Expand Down

0 comments on commit cff1b38

Please sign in to comment.