Skip to content

Commit

Permalink
Allow adding arbitrary licenses
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Apr 5, 2018
1 parent 6a80fb3 commit ad67762
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 9 deletions.
2 changes: 2 additions & 0 deletions library/packages/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ y2rns_fetchers_DATA = \
y2licenses_fetchersdir = "${yast2dir}/lib/y2packager/licenses_fetchers"
y2licenses_fetchers_DATA = \
lib/y2packager/licenses_fetchers/base.rb \
lib/y2packager/licenses_handlers/dummy.rb \
lib/y2packager/licenses_fetchers/rpm.rb \
lib/y2packager/licenses_fetchers/url.rb

y2licenses_handlersdir = "${yast2dir}/lib/y2packager/licenses_handlers"
y2licenses_handlers_DATA = \
lib/y2packager/licenses_handlers/dummy.rb \
lib/y2packager/licenses_handlers/rpm.rb

EXTRA_DIST = $(module_DATA) $(ynclude_DATA) $(ylib_DATA) $(y2packager_DATA) $(y2rns_fetchers_DATA) \
Expand Down
4 changes: 2 additions & 2 deletions library/packages/src/lib/y2packager/license.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class << self
# @param source [:rpm,:url] Source to get the license from. For the time being,
# only :rpm is really supported.
# @return [License]
def find(product_name, source)
def find(product_name, source, options = {})
return cache[product_name] if cache[product_name]

# This could be done in the constructor.
fetcher = LicensesFetchers.for(source, product_name)
fetcher = LicensesFetchers.for(source, product_name, options)
new_license = License.new(fetcher)
return unless new_license.id

Expand Down
5 changes: 3 additions & 2 deletions library/packages/src/lib/y2packager/licenses_fetchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
require "yast"
require "y2packager/licenses_fetchers/rpm"
require "y2packager/licenses_fetchers/url"
require "y2packager/licenses_fetchers/dummy"

module Y2Packager
module LicensesFetchers
# Return the licenses proper fetcher for a given source
#
# @param source [Symbol] :rpm or :url
def self.for(source, product_name)
def self.for(source, product_name, options = {})
klass = const_get(source.to_s.capitalize)
klass.new(product_name)
klass.new(product_name, options)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Base
# Constructor
#
# @param product_name [String] to get licenses for
def initialize(product_name)
def initialize(product_name, _options = {})
@product_name = product_name
end
end
Expand Down
49 changes: 49 additions & 0 deletions library/packages/src/lib/y2packager/licenses_fetchers/dummy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# ------------------------------------------------------------------------------
# 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 Dummy < Base
def initialize(product_name, options = {})
super
@content = options[:content]
end
# 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)
@content
end

# Return available locales for product's license
#
# @return [Array<String>] Language codes ("de_DE", "en_US", etc.)
def license_locales
[License::DEFAULT_LANG]
end

# Determine whether the license should be accepted or not
#
# @return [Boolean] true if the license acceptance is required
def license_confirmation_required?
false
end
end
end
end
1 change: 1 addition & 0 deletions library/packages/src/lib/y2packager/licenses_handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# ------------------------------------------------------------------------------

require "y2packager/licenses_handlers/rpm"
require "y2packager/licenses_handlers/dummy"

module Y2Packager
module LicensesHandlers
Expand Down
38 changes: 38 additions & 0 deletions library/packages/src/lib/y2packager/licenses_handlers/dummy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ------------------------------------------------------------------------------
# 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.
# ------------------------------------------------------------------------------

module Y2Packager
module LicensesHandlers
class Dummy
attr_reader :product_name

# Constructor
#
# @param product_name [String] Product's name
def initialize(product_name)
@product_name = product_name
end

# Determine whether the license should be accepted or not
#
# @return [Boolean] true if the license acceptance is required
def license_confirmation_required?
end

# Set the license confirmation for the product
#
# @param confirmed [Boolean] true if it should be accepted; false otherwise
def license_confirmation=(confirmed)
end
end
end
end
9 changes: 6 additions & 3 deletions library/packages/src/lib/y2packager/product_license.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ class << self
# @param source [:rpm,:url] Source to get the license from. For the time being,
# only :rpm is really supported.
# @return [ProductLicense]

def find(product_name, source = :rpm)
def find(product_name, source = :rpm, options = {})
return cache[product_name] if cache[product_name]
license = License.find(product_name, source)
license = License.find(product_name, source, options)
return nil unless license
cache[product_name] = ProductLicense.new(product_name, license, source: source)
end

def find_or_create(product_name, content)
find(product_name, :dummy, {content: content})
end

# Clear product licenses cache
def clear_cache
@cache = nil
Expand Down
12 changes: 11 additions & 1 deletion library/packages/test/y2packager/product_license_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
end

it "returns a product license for the given product" do
expect(Y2Packager::License).to receive(:find).with("SLES", :rpm).and_return(license)
expect(Y2Packager::License).to receive(:find).with("SLES", :rpm, {})
.and_return(license)
product_license = described_class.find("SLES", :rpm)
expect(product_license).to be_a(Y2Packager::ProductLicense)
expect(product_license.license).to eq(license)
Expand All @@ -50,6 +51,15 @@
end
end

describe ".find_or_create" do
it "returns a product license with the given content" do
product_license = described_class.find_or_create("SLES", "Some predefined content")
expect(product_license).to be_a(Y2Packager::ProductLicense)
license = product_license.license
expect(license.content_for).to eq("Some predefined content")
end
end

describe "#content_for" do
it "delegates to License#content_for" do
expect(license).to receive(:content_for).with("es_ES").and_return("contenido")
Expand Down

0 comments on commit ad67762

Please sign in to comment.