Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Apr 4, 2018
1 parent 8535b00 commit 98da5f7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 27 deletions.
46 changes: 42 additions & 4 deletions library/packages/src/lib/y2packager/license.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>]
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
Expand Down
24 changes: 1 addition & 23 deletions library/packages/src/lib/y2packager/license_reader.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions library/packages/src/lib/y2packager/product_license.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 "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

0 comments on commit 98da5f7

Please sign in to comment.