Skip to content

Commit

Permalink
Added y2packager license API
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator authored and imobachgs committed Apr 5, 2018
1 parent 2d6a0a1 commit db2b4e2
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 18 deletions.
81 changes: 81 additions & 0 deletions library/packages/src/lib/y2packager/license.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,81 @@
# ------------------------------------------------------------------------------
# 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
# Represent a License which could be the same for multiple products.
#
# This class stores the license ID and the traslated content of the license
# for different languages.
class License
DEFAULT_LANG = "en_US"
# @return [String] License unique identifier
attr_reader :id

# @return [Boolean] whether the license has been accepted or not
attr_reader :accepted

# @retrun [Hash<String, String>] language -> content
attr_reader :translations

alias_method :accepted?, :accepted

# Constructor
#
# @param options [Hash<String, String>]
def initialize(options = {})
@id = id_for(options)
@translations = { DEFAULT_LANG => options[:content] }
end

# Return the license translated content for the given language
#
# @param [String] language
# @return [String,nil] the license translated content or nil if not found
def content_for(lang = DEFAULT_LANG)
translations[lang]
end

# Add the license translated content for the given language
#
# @param lang [String]
# @param content [String]
# @return [String,nil] the license translated content or nil if not found
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

# Generate the license unique identifier based on the given options.
# Currently the id is obtained using the MD5 digest of te license's
# content (in the default language).
#
# @param options [Hash<String,String>] License map options
# @return [String] MD5 digest of the license's content
def id_for(options)
Digest::MD5.hexdigest(options[:content])
end
end
end
106 changes: 106 additions & 0 deletions library/packages/src/lib/y2packager/license_reader.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,106 @@
# ------------------------------------------------------------------------------
# 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/license"
require "y2packager/license_store"
require "y2packager/licenses_fetchers/rpm"
require "y2packager/licenses_fetchers/url"

module Y2Packager
# This class is able to read the licence of a given product
class LicenseReader
include Yast::Logger

attr_reader :product_name
attr_reader :source

# 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, source = :rpm)
@product_name = product_name
@source = source
end

# Get the license for the current product name.
#
# @return [License, nil] License or nil if license was not found
def license
license = store.license_for(product_name)
return license if license

content = fetcher.license_content(License::DEFAULT_LANG)
return unless content

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
def license_confirmation_required?
fetcher.license_confirmation_required?
end

# Fallback language
DEFAULT_LANG = "en".freeze

private

def fetcher
fetchers = {
rpm: LicensesFetchers::Rpm.new(product_name),
url: LicensesFetchers::Url.new(product_name)
}

fetchers[source]
end

# Determine whether the system is registered
#
# @return [Boolean]
def registered?
require "registration/registration"
Registration::Registration.is_registered?
rescue LoadError
false
end

def store
LicenseStore.instance
end
end
end
40 changes: 40 additions & 0 deletions library/packages/src/lib/y2packager/license_store.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,40 @@
# ------------------------------------------------------------------------------
# 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 'singleton'

module Y2Packager
class LicenseStore
include Singleton

attr_reader :product_licenses

def initialize
@product_licenses = {}
end

def license_for(product_name)
@product_licenses[product_name]
end

def add_license_for(product_name, license)
stored_license = license(license.id)

@product_licenses[product_name] = stored_license || license
end
private

def license(id)
@product_licenses.values.find { |l| l.id == id }
end
end
end
26 changes: 26 additions & 0 deletions library/packages/src/lib/y2packager/license_translation.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,26 @@
# ------------------------------------------------------------------------------
# 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
class LicenseTranslation
# @return [String] Content Language
attr_reader :lang
attr_reader :content

def initialize(language:, content:)
@lang = language
@content = content
end
end
end
35 changes: 35 additions & 0 deletions library/packages/src/lib/y2packager/licenses_fetchers/base.rb
Original file line number Original file line 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/license"

Yast.import "Pkg"

module Y2Packager
module LicensesFetchers
# Base class for licences 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)
@product_name = product_name
end
end
end
end
52 changes: 52 additions & 0 deletions library/packages/src/lib/y2packager/licenses_fetchers/rpm.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,52 @@
# ------------------------------------------------------------------------------
# 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
class Rpm < Base
# Return the license text to be confirmed
#
# It will return the empty string ("") if the license does not exist or if
# it was already confirmed.
#
# @param lang [String] Language
# @return [String,nil] Product's license; nil if the product was not found.
def license_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 license_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 license_confirmation_required?
Yast::Pkg.PrdNeedToAcceptLicense(product_name)
end
end
end
end
33 changes: 33 additions & 0 deletions library/packages/src/lib/y2packager/licenses_fetchers/url.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,33 @@
# ------------------------------------------------------------------------------
# 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
# FIXME: Finish implementation
class Url < Base

def license_content(lang)
"Fetching product license"
end

def license_locales
[License::DEFAULT_LANG]
end

def license_confirmation_required?
false
end
end
end
end
Loading

0 comments on commit db2b4e2

Please sign in to comment.