/
product.rb
296 lines (267 loc) · 10.6 KB
/
product.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# ------------------------------------------------------------------------------
# Copyright (c) 2017 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.
# ------------------------------------------------------------------------------
Yast.import "Pkg"
require "y2packager/product_reader"
require "y2packager/release_notes_reader"
require "y2packager/product_license_mixin"
require "y2packager/resolvable"
module Y2Packager
# Represent a product which is present in a repository. At this
# time this class is responsible for finding out whether two
# products instances are the same (for example, coming from different
# repositories).
class Product
include Yast::Logger
include ProductLicenseMixin
# @return [String] Name
attr_reader :name
# @return [String] Short name
attr_reader :short_name
# @return [String] Display name
attr_reader :display_name
# @return [String] Version
attr_reader :version
# @return [String] Architecture
attr_reader :arch
# @return [Symbol] Category
attr_reader :category
# @return [String] Vendor
attr_reader :vendor
# @return [Integer] Display order
attr_reader :order
# @return [String] package including installation.xml for install on top of lean os
attr_reader :installation_package
# @return [Integer] repository for the installation_package
attr_reader :installation_package_repo
class << self
PKG_BINDINGS_ATTRS = ["name", "short_name", "display_name", "version", "arch",
"category", "vendor"].freeze
# Resets cached attributes of the class
#
# @return [true]
def reset
@forced_base_product = nil
end
# Create a product from pkg-bindings hash data.
# @param product [Hash] the pkg-bindings product hash
# @return [Y2Packager::Product] converted product
def from_h(product)
params = PKG_BINDINGS_ATTRS.each_with_object({}) { |a, h| h[a.to_sym] = product[a] }
Y2Packager::Product.new(params)
end
# Create a product from Y2Packager::Resolvable
# @param product [Y2Packager::Resolvable] product
# @param installation_package [String] installation package name
# @param displayorder [Integer] display order from the package provides
# @return [Y2Packager::Product] converted product
def from_resolvable(product, installation_package = "",
displayorder = nil)
Y2Packager::Product.new(
name: product.name, short_name: product.short_name,
display_name: product.display_name, version: product.version,
arch: product.arch, category: product.category,
vendor: product.vendor, order: displayorder,
installation_package: installation_package
)
end
# Return all known available products
#
# @return [Array<Product>] Known available products
def all
Y2Packager::ProductReader.new.all_products
end
# Return available base products
#
# @return [Array<Product>] Available base products
def available_base_products
Y2Packager::ProductReader.new.available_base_products
end
# Return the installed base product
#
# @return [Product,nil] Installed base product or nil if not found
def installed_base_product
Y2Packager::ProductReader.new.installed_base_product
end
# Return all installed products (including the base product)
#
# @return [Product,nil] Installed products
def installed_products
Y2Packager::ProductReader.new.all_installed_products
end
# Returns the selected base product
#
# It assumes that at most 1 base product can be selected.
#
# @return [Product] Selected base product
def selected_base
products = Y2Packager::ProductReader.new.available_base_products(force_repos: true)
selected = products.find(&:selected?)
selected ||= products.first
selected
end
# Return the products with a given status
#
# @param statuses [Array<Symbol>] Product status (:available, :installed, :selected, etc.)
# @return [Array<Product>] Products with the given status
def with_status(*statuses)
all.select { |p| p.status?(*statuses) }
end
# Returns, if any, the base product which must be selected
#
# A base product can be forced to be selected through the `select_product`
# element in the software section of the control.xml file (bsc#1124590,
# bsc#1143943).
#
# @return [Y2Packager::Product, nil] the forced base product or nil when
# either, it wasn't selected or the selected wasn't found among the
# available ones.
def forced_base_product
Yast.import "ProductFeatures"
return @forced_base_product if @forced_base_product
forced_product_name = Yast::ProductFeatures.GetStringFeature("software", "select_product")
return if forced_product_name.to_s.empty?
@forced_base_product = available_base_products.find { |p| p.name == forced_product_name }
end
end
# Constructor
#
# @param name [String] Name
# @param short_name [String] Short name
# @param display_name [String] Display name
# @param version [String] Version
# @param arch [String] Architecture
# @param category [Symbol] Category (:base, :addon)
# @param vendor [String] Vendor
# @param order [Integer] Display order
# @param installation_package [String] Installation package name
def initialize(name: nil, short_name: nil, display_name: nil, version: nil, arch: nil,
category: nil, vendor: nil, order: nil, installation_package: nil)
@name = name
@short_name = short_name
@display_name = display_name
@version = version
@arch = arch.to_sym if arch
@category = category.to_sym if category
@vendor = vendor
@order = order
@installation_package = installation_package
end
# Compare two different products
#
# If arch, name, version and vendor match they are considered the
# same product.
#
# @return [Boolean] true if both products are the same; false otherwise
def ==(other)
result = arch == other.arch && name == other.name &&
version == other.version && vendor == other.vendor
log.info("Comparing products: '#{arch}' <=> '#{other.arch}', '#{name}' <=> '#{other.name}', "\
"'#{version}' <=> '#{other.version}', '#{vendor}' <=> '#{other.vendor}' => "\
"result: #{result}")
result
end
# is the product selected to install?
#
# Only the 'name' will be used to find out whether the product is selected,
# ignoring the architecture, version, vendor or any other property. libzypp
# will take care of finding the proper product.
#
# @return [Boolean] true if it is selected
def selected?
status?(:selected)
end
# is the product selected to install?
#
# Only the 'name' will be used to find out whether the product is installed,
# ignoring the architecture, version, vendor or any other property. libzypp
# will take care of finding the proper product.
#
# @see #status?
# @return [Boolean] true if it is installed
def installed?
status?(:installed)
end
# select the product to install
#
# Only the 'name' will be used to select the product, ignoring the
# architecture, version, vendor or any other property. libzypp will take
# care of selecting the proper product.
#
# @return [Boolean] true if the product has been sucessfully selected
def select
log.info "Selecting product #{name} to install"
Yast::Pkg.ResolvableInstall(name, :product, "")
end
# Restore the status of a product
#
# Only the 'name' will be used to restore the product status, ignoring the
# architecture, version, vendor or any other property. libzypp will take
# care of modifying the proper product.
#
def restore
log.info "Restoring product #{name} status"
Yast::Pkg.ResolvableNeutral(name, :product, true)
end
# Return a package label
#
# It will use 'display_name', 'short_name' or 'name'.
#
# @return [String] Package label
def label
display_name || short_name || name
end
# Return product's release notes
#
# @param format [Symbol] Release notes format (use :txt as default)
# @param user_lang [String] Preferred language (use current language as default)
# @return [ReleaseNotes] Release notes for product, language and format
# @see ReleaseNotesReader
# @see ReleaseNotes
def release_notes(user_lang, format = :txt)
ReleaseNotesReader.new(self).release_notes(user_lang: user_lang, format: format)
end
# Return release notes URL
#
# Release notes might not be defined in libzypp and this method returns the URL
# to get release notes from.
#
# @return [String,nil] Release notes URL or nil if it is not defined.
def relnotes_url
return nil unless resolvable_properties
url = resolvable_properties.relnotes_url
url.empty? ? nil : url
end
# Determine whether a product is in a given status
#
# Only the 'name' will be used to find out whether the product status,
# ignoring the architecture, version, vendor or any other property. libzypp
# will take care of finding the proper product.
#
# @param statuses [Array<Symbol>] Status to compare with.
# @return [Boolean] true if it is in the given status
def status?(*statuses)
Y2Packager::Resolvable.find(kind: :product, name: name).any? do |res|
statuses.include?(res.status)
end
end
# Return product's resolvable properties
#
# Only the 'name' and 'version' will be used to find out the product
# properties, ignoring the architecture, vendor or any other property.
# libzypp will take care of finding the proper product.
#
# @return [Hash] properties
def resolvable_properties
@resolvable_properties ||= Y2Packager::Resolvable.find(kind: :product, name: name, version: version).first
end
end
end