-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #718 from yast/license_api_poc
License API refactoring
- Loading branch information
Showing
16 changed files
with
1,029 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# ------------------------------------------------------------------------------ | ||
# 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" | ||
require "y2packager/licenses_fetchers" | ||
|
||
module Y2Packager | ||
# Represent a License which could be the same for multiple products. | ||
# | ||
# This class represents a license. | ||
class License | ||
include Yast::Logger | ||
|
||
# Default language for licenses. | ||
DEFAULT_LANG = "en_US".freeze | ||
|
||
# @return [Boolean] whether the license has been accepted or not | ||
attr_reader :accepted | ||
|
||
# @return [Hash<String, String>] language -> content | ||
attr_reader :translations | ||
|
||
alias_method :accepted?, :accepted | ||
|
||
class << self | ||
# Find a license for a given product | ||
# | ||
# This method uses a cache to return the same license if it was already | ||
# used for another product. | ||
# | ||
# @param product_name [String] Product's name | ||
# @param source [:libzypp,nil] Source to get the license from. For the time being, | ||
# only :libzypp is supported. | ||
# @param content [String] License content. If this argument is given, this | ||
# string is used as the license's content (and `source` is ignored). | ||
# @return [License] | ||
def find(product_name, source: nil, content: nil) | ||
log.info "Searching for a license for product #{product_name}" | ||
return cache[product_name] if cache[product_name] | ||
|
||
fetcher = source ? LicensesFetchers.for(source, product_name) : nil | ||
new_license = License.new(fetcher: fetcher, content: content) | ||
return unless new_license.id | ||
|
||
eq_license = cache.values.find { |l| l.id == new_license.id } | ||
if eq_license | ||
log.info "Found cached license: #{eq_license.id}" | ||
else | ||
log.info "Caching license: #{new_license.id}" | ||
end | ||
|
||
cache[product_name] = eq_license || new_license | ||
end | ||
|
||
# Clean licenses cache | ||
def clear_cache | ||
@cache = nil | ||
end | ||
|
||
private | ||
|
||
# Licenses cache | ||
# | ||
# @return [Hash<String,License>] | ||
def cache | ||
@cache ||= {} | ||
end | ||
end | ||
|
||
# Constructor | ||
# | ||
# This class should be able to use the proper fetcher (see Y2Packager::LicensesFetchers) | ||
# in order to retrieve license content (including translations). However, for compatibility | ||
# reasons, the constructor can receive a `content` that will be used as licence's | ||
# content. The reason is that, in some parts of YaST, the license content/translations | ||
# is retrieved in different ways. We might need to unify them. | ||
# | ||
# Bear in mind that `fetcher` will be ignored if `content` is specified. | ||
# | ||
# @param fetcher [:libzypp] Fetcher to retrieve licenses information. For the time | ||
# being, only :libzypp is supported. | ||
# @param content [String] License content. If this argument is given, this | ||
# string is used as the license's content (and `source` is ignored). | ||
def initialize(fetcher: nil, content: nil) | ||
@accepted = false | ||
@translations = {} | ||
if content | ||
add_content_for(DEFAULT_LANG, content) | ||
else | ||
@fetcher = fetcher | ||
end | ||
end | ||
|
||
# License unique identifier | ||
# | ||
# This identifier is based on the given default language translation. | ||
# | ||
# @return [String,nil] Unique identifier; nil if the license was not found. | ||
def id | ||
return @id if @id | ||
content = content_for(DEFAULT_LANG) | ||
return unless content | ||
@id = Digest::MD5.hexdigest(content) | ||
end | ||
|
||
# Return the license translated content for the given language | ||
# | ||
# @param lang [String] Contents' language | ||
# @return [String,nil] the license translated content or nil if not found | ||
def content_for(lang = DEFAULT_LANG) | ||
return @translations[lang] if @translations[lang] | ||
return nil unless fetcher | ||
content = fetcher.content(lang) | ||
return add_content_for(lang, content) if content | ||
end | ||
|
||
# Return license's available locales | ||
# | ||
# @return [String] List of available locales | ||
def locales | ||
fetcher.locales | ||
end | ||
|
||
# Add the license translated content for the given language | ||
# | ||
# @param lang [String] Language to add the translation to | ||
# @param content [String] Content to add | ||
# @return [String] the license translated content | ||
def add_content_for(lang, content) | ||
@translations[lang] = content | ||
end | ||
|
||
# Set the license as accepted | ||
def accept! | ||
@accepted = true | ||
end | ||
|
||
# Set the license as rejected | ||
def reject! | ||
@accepted = false | ||
end | ||
|
||
private | ||
|
||
# @return [Y2Packager::LicensesFetchers::Base] License fetcher object | ||
attr_reader :fetcher | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# ------------------------------------------------------------------------------ | ||
# 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 "y2packager/licenses_fetchers/libzypp" | ||
|
||
module Y2Packager | ||
# This module contains licenses fetchers | ||
# | ||
# Licenses can be retrieved from different places (libzypp, URLs, etc.). The classes | ||
# defined in this module are able to retrieve licenses contents. | ||
module LicensesFetchers | ||
# Return the licenses proper fetcher for a given source | ||
# | ||
# @param source [:libzypp,nil] Source to fetch license from (only :rpm is supported) | ||
# @param product_name [String] Product's name | ||
def self.for(source, product_name) | ||
klass = const_get(source.to_s.capitalize) | ||
klass.new(product_name) | ||
end | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
library/packages/src/lib/y2packager/licenses_fetchers/base.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# ------------------------------------------------------------------------------ | ||
# 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 "y2packager/licenses_fetchers/base" | ||
|
||
Yast.import "Pkg" | ||
|
||
module Y2Packager | ||
module LicensesFetchers | ||
# Base class for licenses fetchers | ||
class Base | ||
include Yast::Logger | ||
|
||
# @return [String] Product name to get licenses for | ||
attr_reader :product_name | ||
|
||
# Constructor | ||
# | ||
# @param product_name [String] to get licenses for | ||
def initialize(product_name, _options = {}) | ||
@product_name = product_name | ||
end | ||
end | ||
end | ||
end |
51 changes: 51 additions & 0 deletions
51
library/packages/src/lib/y2packager/licenses_fetchers/libzypp.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "y2packager/licenses_fetchers/base" | ||
|
||
module Y2Packager | ||
module LicensesFetchers | ||
# This class is responsible for obtaining the license and license content | ||
# of a given product from libzypp. | ||
class Libzypp < Base | ||
# Return the license text to be confirmed | ||
# | ||
# @param lang [String] Language | ||
# @return [String,nil] Product's license; nil if the product or the license were not found. | ||
def content(lang) | ||
Yast::Pkg.PrdGetLicenseToConfirm(product_name, lang) | ||
end | ||
|
||
# Return available locales for product's license | ||
# | ||
# @return [Array<String>] Language codes ("de_DE", "en_US", etc.) | ||
def locales | ||
locales = Yast::Pkg.PrdLicenseLocales(product_name) | ||
if locales.nil? | ||
log.error "Error getting the list of available license translations for '#{product_name}'" | ||
return [] | ||
end | ||
|
||
empty_idx = locales.index("") | ||
locales[empty_idx] = License::DEFAULT_LANG if empty_idx | ||
locales | ||
end | ||
|
||
# Determine whether the license should be accepted or not | ||
# | ||
# @return [Boolean] true if the license acceptance is required | ||
def confirmation_required? | ||
Yast::Pkg.PrdNeedToAcceptLicense(product_name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# ------------------------------------------------------------------------------ | ||
# 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 "y2packager/licenses_handlers/libzypp" | ||
|
||
module Y2Packager | ||
# This module contains licenses handlers | ||
# | ||
# Licenses can be retrieved from different places (libzypp, URLs, etc.). The | ||
# classes defined in this module are able to interact with these sources in | ||
# order to find out, for instance, whether a license must be accepted. | ||
module LicensesHandlers | ||
# Return the licenses proper fetcher for a given source | ||
# | ||
# @param source [:libzypp,nil] Source to fetch license from (only :libzypp is supported) | ||
# @param product_name [String] Product's name | ||
# @return [Object] | ||
def self.for(source, product_name) | ||
klass = const_get(source.to_s.capitalize) | ||
klass.new(product_name) | ||
end | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
library/packages/src/lib/y2packager/licenses_handlers/base.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# ------------------------------------------------------------------------------ | ||
# 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" | ||
|
||
module Y2Packager | ||
module LicensesHandlers | ||
# Base class for licenses handlers | ||
class Base | ||
include Yast::Logger | ||
|
||
# @return [String] Product name to handle license status | ||
attr_reader :product_name | ||
|
||
# Constructor | ||
# | ||
# @param product_name [String] Product name to handle license status | ||
def initialize(product_name) | ||
@product_name = product_name | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.