Class: Yast2::ArchFilter

Inherits:
Object
  • Object
show all
Defined in:
library/general/src/lib/yast2/arch_filter.rb

Overview

Represents filtering based on hardware architecture like x86_64 or ppc64. Original code lived in Y2Storage::SubvolSpecification

Examples:

Yast2::ArchFilter.from_string("x86_64,aarch64").match?

Defined Under Namespace

Classes: Invalid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specs) ⇒ ArchFilter

creates new architecture filter from passed list of individual specifications

Parameters:

  • specs (Array<String>)

Raises:

  • Invalid when architecture specification is invalid



46
47
48
49
50
51
52
53
54
55
56
# File 'library/general/src/lib/yast2/arch_filter.rb', line 46

def initialize(specs)
  @specifications = []
  specs.each do |spec|
    method = spec.downcase
    negate = spec.start_with?("!")
    method = spec[1..-1] if negate
    raise Invalid, spec unless valid_method?(method)

    @specifications << { method: method.to_sym, negate: negate }
  end
end

Instance Attribute Details

#specificationsArray<Hash> (readonly)

Returns list of specifications where each entry is hash with key :method and :negate, where method is Yast::Arch method and negate specify if method have to return false. There is one specific method :all that is not in Yast::Arch, but can be used to always return true.

Returns:

  • (Array<Hash>)

    list of specifications where each entry is hash with key :method and :negate, where method is Yast::Arch method and negate specify if method have to return false. There is one specific method :all that is not in Yast::Arch, but can be used to always return true.



41
42
43
# File 'library/general/src/lib/yast2/arch_filter.rb', line 41

def specifications
  @specifications
end

Class Method Details

.from_string(value) ⇒ Object

Parses architecture filter specification from a string.

Supported values are methods from Arch with possible ! in front of it.

When a ! is used it is called a negative method and without it is a positive one.

A list of methods is separated with commas ,. To match, at least one positive specified method has to return true and all negative methods have to return false. If there are no positive methods then only negatives are evaluated.

Whitespaces are allowed. Only ! and method has to be without space. Also it is case insensitive, so acronyms can be in upper case.

Examples:

various format and how it behave in given situations

"x86_64,ppc64" # returns true on either x86_64 or ppc64
"ppc,!board_powernv" # returns false on powernv_board or non-ppc
"ppc, !board_powernv" # spaces are allowed
"!ppc64,!aarch64" # returns false on ppc64 and arm and true for others
"s390, !is_zKVM" # return true on s390 when not running in zKVM hypervisor
"" # always return true
"all" # always return true
"invalid" # raises ArchFilter::Invalid exception


82
83
84
# File 'library/general/src/lib/yast2/arch_filter.rb', line 82

def self.from_string(value)
  new(value.split(",").map(&:strip))
end

Instance Method Details

#match?Boolean

checks if filter match current hardware

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'library/general/src/lib/yast2/arch_filter.rb', line 88

def match?
  negative, positive = @specifications.partition { |s| s[:negate] }
  return false if negative.any? { |s| invoke_method(s[:method]) }

  # handle case when there is only negative conditions
  return true if positive.empty?

  positive.any? { |s| invoke_method(s[:method]) }
end