Skip to content

Commit

Permalink
Merge pull request #718 from yast/license_api_poc
Browse files Browse the repository at this point in the history
License API refactoring
  • Loading branch information
imobachgs committed Apr 5, 2018
2 parents 2d6a0a1 + 4c19686 commit 98c5060
Show file tree
Hide file tree
Showing 16 changed files with 1,029 additions and 108 deletions.
21 changes: 18 additions & 3 deletions library/packages/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ ylib_DATA = \

y2packagerdir = "${yast2dir}/lib/y2packager"
y2packager_DATA = \
lib/y2packager/license.rb \
lib/y2packager/licenses_handlers.rb \
lib/y2packager/licenses_fetchers.rb \
lib/y2packager/package.rb \
lib/y2packager/product.rb \
lib/y2packager/product_license.rb \
lib/y2packager/product_reader.rb \
lib/y2packager/product_sorter.rb \
lib/y2packager/product_upgrade.rb \
Expand All @@ -41,12 +45,23 @@ y2packager_DATA = \
lib/y2packager/release_notes_reader.rb \
lib/y2packager/release_notes_store.rb

y2fetchersdir = "${yast2dir}/lib/y2packager/release_notes_fetchers"
y2fetchers_DATA = \
y2rns_fetchersdir = "${yast2dir}/lib/y2packager/release_notes_fetchers"
y2rns_fetchers_DATA = \
lib/y2packager/release_notes_fetchers/base.rb \
lib/y2packager/release_notes_fetchers/rpm.rb \
lib/y2packager/release_notes_fetchers/url.rb

EXTRA_DIST = $(module_DATA) $(ynclude_DATA) $(ylib_DATA) $(y2packager_DATA) $(y2fetchers_DATA)
y2licenses_fetchersdir = "${yast2dir}/lib/y2packager/licenses_fetchers"
y2licenses_fetchers_DATA = \
lib/y2packager/licenses_fetchers/base.rb \
lib/y2packager/licenses_fetchers/libzypp.rb

y2licenses_handlersdir = "${yast2dir}/lib/y2packager/licenses_handlers"
y2licenses_handlers_DATA = \
lib/y2packager/licenses_handlers/base.rb \
lib/y2packager/licenses_handlers/libzypp.rb

EXTRA_DIST = $(module_DATA) $(ynclude_DATA) $(ylib_DATA) $(y2packager_DATA) $(y2rns_fetchers_DATA) \
$(y2licenses_fetchers_DATA) $(y2licenses_handlers_DATA)

include $(top_srcdir)/Makefile.am.common
158 changes: 158 additions & 0 deletions library/packages/src/lib/y2packager/license.rb
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
31 changes: 31 additions & 0 deletions library/packages/src/lib/y2packager/licenses_fetchers.rb
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 library/packages/src/lib/y2packager/licenses_fetchers/base.rb
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 library/packages/src/lib/y2packager/licenses_fetchers/libzypp.rb
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
32 changes: 32 additions & 0 deletions library/packages/src/lib/y2packager/licenses_handlers.rb
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 library/packages/src/lib/y2packager/licenses_handlers/base.rb
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
Loading

0 comments on commit 98c5060

Please sign in to comment.