Module: Katello::Util::Package

Defined in:
app/lib/katello/util/package.rb

Constant Summary collapse

SUFFIX_RE =
/\.(rpm)$/
ARCH_RE =
/\.([^.\-]*)$/
EPOCH_RE =
/([0-9]+):/
NVRE_RE =
/^(?:([0-9]+):)?(.*)-([^-]*)-([^-]*)$/
SUPPORTED_ARCHS =
%w(noarch i386 i686 ppc64 s390x x86_64 ia64)

Class Method Summary collapse

Class Method Details

.build_nvra(package) ⇒ Object



77
78
79
80
81
82
83
# File 'app/lib/katello/util/package.rb', line 77

def self.build_nvra(package)
  package = package.with_indifferent_access
  nvra = "#{package[:name]}-#{package[:version]}-#{package[:release]}"
  nvra = "#{nvra}.#{package[:arch]}" unless package[:arch].nil?
  nvra = "#{nvra}.#{package[:suffix]}" unless package[:suffix].nil?
  nvra
end

.build_nvrea(package, include_zero_epoch = true) ⇒ Object



63
64
65
# File 'app/lib/katello/util/package.rb', line 63

def self.build_nvrea(package, include_zero_epoch = true)
  [package[:name], build_vrea(package, include_zero_epoch)].compact.reject(&:empty?).join('-')
end

.build_vrea(package, include_zero_epoch = true) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'app/lib/katello/util/package.rb', line 67

def self.build_vrea(package, include_zero_epoch = true)
  vrea =  [package[:version], package[:release]].compact.join('-')
  vrea = vrea + '.' + package[:arch] unless package[:arch].nil?
  vrea = vrea + '.' + package[:suffix] unless package[:suffix].nil?
  unless package[:epoch].nil?
    vrea = package[:epoch] + ':' + vrea if package[:epoch].to_i != 0 || include_zero_epoch
  end
  vrea
end

.extract_arch(name) ⇒ Object



50
51
52
# File 'app/lib/katello/util/package.rb', line 50

def self.extract_arch(name)
  return name.split(ARCH_RE)
end

.extract_suffix(name) ⇒ Object



46
47
48
# File 'app/lib/katello/util/package.rb', line 46

def self.extract_suffix(name)
  return name.split(SUFFIX_RE)
end

.find_latest_packages(packages) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/lib/katello/util/package.rb', line 85

def self.find_latest_packages(packages)
  latest_pack = nil
  selected_packs = []

  packages.each do |pack|
    next if pack.nil?

    pack = pack.with_indifferent_access
    if (latest_pack.nil?) ||
       (pack[:epoch] > latest_pack[:epoch]) ||
       (pack[:epoch] == latest_pack[:epoch] && pack[:release] > latest_pack[:release]) ||
       (pack[:epoch] == latest_pack[:epoch] && pack[:release] == latest_pack[:release] && pack[:version] > latest_pack[:version])
      latest_pack = pack
      selected_packs = [pack]

    elsif (pack[:epoch] == latest_pack[:epoch] && pack[:release] == latest_pack[:release] && pack[:version] == latest_pack[:version])
      selected_packs << pack
    end
  end

  selected_packs
end

.format_requires(requires) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/lib/katello/util/package.rb', line 54

def self.format_requires(requires)
  flags = {'GT' => '>', 'LT' => '>', 'EQ' => '=', 'GE' => '>=', 'LE' => '<='}
  if requires['flags']
    "#{requires['name']} #{flags[requires['flags']]} #{build_vrea(requires, false)}"
  else
    build_nvrea(requires, false)
  end
end

.parse_nvre(name) ⇒ Object

parses package nvre and stores it in a hash epoch:name-ve.rs.ion-rel.e.ase.rpm



24
25
26
27
28
29
30
31
32
33
34
# File 'app/lib/katello/util/package.rb', line 24

def self.parse_nvre(name)
  name, suffix = extract_suffix(name)

  if match = NVRE_RE.match(name)
    {:suffix => suffix,
     :epoch => match[1],
     :name => match[2],
     :version => match[3],
     :release => match[4]}.delete_if { |_k, v| v.nil? }
  end
end

.parse_nvrea(name) ⇒ Object

parses package nvrea and stores it in a hash epoch:name-ve.rs.ion-rel.e.ase.arch.rpm



12
13
14
15
16
17
18
19
20
# File 'app/lib/katello/util/package.rb', line 12

def self.parse_nvrea(name)
  name, suffix = extract_suffix(name)
  name, arch = extract_arch(name)
  return unless arch

  if nvre = parse_nvre(name)
    nvre.merge(:suffix => suffix, :arch => arch).delete_if { |_k, v| v.nil? }
  end
end

.parse_nvrea_nvre(name) ⇒ Object

is able to take both nvre and nvrea and parse it correctly



37
38
39
40
41
42
43
44
# File 'app/lib/katello/util/package.rb', line 37

def self.parse_nvrea_nvre(name)
  package = self.parse_nvrea(name)
  if package && SUPPORTED_ARCHS.include?(package[:arch])
    return package
  else
    return self.parse_nvre(name)
  end
end

.setup_shared_unique_filter(repoids, search_mode, search_results) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/lib/katello/util/package.rb', line 129

def self.setup_shared_unique_filter(repoids, search_mode, search_results)
  repo_filter_ids = repoids.collect do |repo|
    {:term => {:repoids => [repo]}}
  end
  case search_mode
  when :shared
    search_results.filter :and, repo_filter_ids
  when :unique
    search_results.filter :or, repo_filter_ids
    search_results.filter :not, :filter => {:and => repo_filter_ids}
  else
    search_results.filter :or, repo_filter_ids
  end
end

.sortable_version(version) ⇒ String

Converts a package version to a sortable string

See the Fedora docs for what chars are accepted fedoraproject.org/wiki/Archive:Tools/RPM/VersionComparison

See Pulp’s documentation for more info about this algorithm pulp-rpm-dev-guide.readthedocs.org/en/latest/sort-index.html

Parameters:

  • version (String)

    a package version (e.g. “3.9”)

Returns:

  • (String)

    a string that can be sorted (e.g. “01-3.01-9”)



154
155
156
157
158
159
160
# File 'app/lib/katello/util/package.rb', line 154

def self.sortable_version(version)
  return "" if version.blank?
  pieces = version.scan(/([A-Za-z]+|\d+)/).flatten.map do |chunk|
    chunk =~ /\d+/ ? "#{"%02d" % chunk.length}-#{chunk}" : "$#{chunk}"
  end
  pieces.join(".")
end

.valid_package_charactersObject



125
126
127
# File 'app/lib/katello/util/package.rb', line 125

def self.valid_package_characters
  /[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-\.\_\+\,]+/
end

.valid_package_name_format(package) ⇒ Object



121
122
123
# File 'app/lib/katello/util/package.rb', line 121

def self.valid_package_name_format(package)
  return (package =~ valid_package_characters)
end

.validate_package_list_format(packages) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/lib/katello/util/package.rb', line 108

def self.validate_package_list_format(packages)
  # validate the format of the comma-separated package list provided
  packages = packages.split(/ *, */)

  packages.each do |package_name|
    unless valid_package_name_format(package_name).nil?
      return false
    end
  end

  return packages
end