|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'date' |
| 4 | + |
| 5 | +module Kanrisuru |
| 6 | + module Core |
| 7 | + module Yum |
| 8 | + extend OsPackage::Define |
| 9 | + |
| 10 | + os_define :fedora, :yum |
| 11 | + |
| 12 | + YumPackageOverview = Struct.new(:package, :architecture, :version, :installed) |
| 13 | + YumPackageSearchResult = Struct.new(:package, :architecture, :summary) |
| 14 | + YumPackageDetail = Struct.new( |
| 15 | + :package, |
| 16 | + :version, |
| 17 | + :release, |
| 18 | + :architecture, |
| 19 | + :install_size, |
| 20 | + :source, |
| 21 | + :yum_id, |
| 22 | + :repository, |
| 23 | + :summary, |
| 24 | + :url, |
| 25 | + :license, |
| 26 | + :description |
| 27 | + ) |
| 28 | + |
| 29 | + YumRepolist = Struct.new( |
| 30 | + :id, |
| 31 | + :name, |
| 32 | + :status, |
| 33 | + :revision, |
| 34 | + :packages, |
| 35 | + :available_packages, |
| 36 | + :repo_size, |
| 37 | + :mirrors, |
| 38 | + :metalink, |
| 39 | + :updated, |
| 40 | + :baseurl, |
| 41 | + :expire, |
| 42 | + :filters, |
| 43 | + :filename |
| 44 | + ) |
| 45 | + |
| 46 | + def yum(action, opts = {}) |
| 47 | + case action |
| 48 | + when 'install' |
| 49 | + yum_install(opts) |
| 50 | + when 'localinstall' |
| 51 | + yum_localinstall(opts) |
| 52 | + when 'list' |
| 53 | + yum_list(opts) |
| 54 | + when 'search' |
| 55 | + yum_search(opts) |
| 56 | + when 'info' |
| 57 | + yum_info(opts) |
| 58 | + when 'repolist' |
| 59 | + yum_repolist(opts) |
| 60 | + when 'clean' |
| 61 | + yum_clean(opts) |
| 62 | + when 'remove' |
| 63 | + yum_remove(opts) |
| 64 | + when 'autoremove' |
| 65 | + yum_autoremove(opts) |
| 66 | + when 'erase' |
| 67 | + yum_erase(opts) |
| 68 | + when 'update' |
| 69 | + yum_update(opts) |
| 70 | + when 'upgrade' |
| 71 | + yum_upgrade(opts) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + private |
| 76 | + |
| 77 | + def yum_install(opts) |
| 78 | + command = Kanrisuru::Command.new('yum install') |
| 79 | + command.append_flag('-y') |
| 80 | + |
| 81 | + packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ') |
| 82 | + command << packages |
| 83 | + |
| 84 | + execute_shell(command) |
| 85 | + Kanrisuru::Result.new(command) |
| 86 | + end |
| 87 | + |
| 88 | + def yum_localinstall(opts) |
| 89 | + command = Kanrisuru::Command.new('yum localinstall') |
| 90 | + yum_disable_repo(command, opts[:disable_repo]) |
| 91 | + command.append_flag('-y') |
| 92 | + |
| 93 | + if Kanrisuru::Util.present?(opts[:repos]) |
| 94 | + repos = Kanrisuru::Util.string_join_array(opts[:repos], ' ') |
| 95 | + command << repos |
| 96 | + end |
| 97 | + |
| 98 | + execute_shell(command) |
| 99 | + |
| 100 | + Kanrisuru::Result.new(command) |
| 101 | + end |
| 102 | + |
| 103 | + def yum_list(opts) |
| 104 | + command = Kanrisuru::Command.new('yum list') |
| 105 | + |
| 106 | + yum_disable_repo(command, opts[:disable_repo]) |
| 107 | + |
| 108 | + command.append_flag('all', opts[:all]) |
| 109 | + command.append_flag('available', opts[:available]) |
| 110 | + command.append_flag('updates', opts[:updates]) |
| 111 | + command.append_flag('installed', opts[:installed]) |
| 112 | + command.append_flag('extras', opts[:extras]) |
| 113 | + command.append_flag('obsoletes', opts[:obsoletes]) |
| 114 | + |
| 115 | + command << opts[:query] if Kanrisuru::Util.present?(opts[:query]) |
| 116 | + |
| 117 | + pipe_output_newline(command) |
| 118 | + |
| 119 | + execute_shell(command) |
| 120 | + |
| 121 | + Kanrisuru::Result.new(command) do |cmd| |
| 122 | + lines = cmd.to_a |
| 123 | + result = [] |
| 124 | + lines.each do |line| |
| 125 | + item = parse_yum_line(line) |
| 126 | + next unless item |
| 127 | + |
| 128 | + result << item |
| 129 | + end |
| 130 | + |
| 131 | + result |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + def yum_remove(opts) |
| 136 | + command = Kanrisuru::Command.new('yum remove') |
| 137 | + command.append_flag('-y') |
| 138 | + |
| 139 | + packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ') |
| 140 | + raise ArugmentError, "can't remove yum" if packages.include?('yum') |
| 141 | + |
| 142 | + command << packages |
| 143 | + |
| 144 | + execute_shell(command) |
| 145 | + Kanrisuru::Result.new(command) |
| 146 | + end |
| 147 | + |
| 148 | + def yum_clean(opts) |
| 149 | + command = Kanrisuru::Command.new('yum clean') |
| 150 | + |
| 151 | + command.append_flag('dbcache', opts[:dbcache]) |
| 152 | + command.append_flag('expire-cache', opts[:expire_cache]) |
| 153 | + command.append_flag('metadata', opts[:metadata]) |
| 154 | + command.append_flag('packages', opts[:packages]) |
| 155 | + command.append_flag('headers', opts[:headers]) |
| 156 | + command.append_flag('rpmdb', opts[:rpmdb]) |
| 157 | + command.append_flag('all', opts[:all]) |
| 158 | + |
| 159 | + execute_shell(command) |
| 160 | + |
| 161 | + Kanrisuru::Result.new(command) |
| 162 | + end |
| 163 | + |
| 164 | + def yum_autoremove(_opts) |
| 165 | + command = Kanrisuru::Command.new('yum autoremove') |
| 166 | + command.append_flag('-y') |
| 167 | + execute_shell(command) |
| 168 | + Kanrisuru::Result.new(command) |
| 169 | + end |
| 170 | + |
| 171 | + def yum_erase(opts) |
| 172 | + command = Kanrisuru::Command.new('yum erase') |
| 173 | + command.append_flag('-y') |
| 174 | + |
| 175 | + packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ') |
| 176 | + raise ArugmentError, "can't erase yum" if packages.include?('yum') |
| 177 | + |
| 178 | + command << packages |
| 179 | + |
| 180 | + execute_shell(command) |
| 181 | + Kanrisuru::Result.new(command) |
| 182 | + end |
| 183 | + |
| 184 | + def yum_update(_opts) |
| 185 | + command = Kanrisuru::Command.new('yum update') |
| 186 | + command.append_flag('-y') |
| 187 | + execute_shell(command) |
| 188 | + Kanrisuru::Result.new(command) |
| 189 | + end |
| 190 | + |
| 191 | + def yum_upgrade(_opts) |
| 192 | + command = Kanrisuru::Command.new('yum upgrade') |
| 193 | + command.append_flag('-y') |
| 194 | + execute_shell(command) |
| 195 | + Kanrisuru::Result.new(command) |
| 196 | + end |
| 197 | + |
| 198 | + def yum_search(opts) |
| 199 | + command = Kanrisuru::Command.new('yum search') |
| 200 | + command.append_flag('all', opts[:all]) |
| 201 | + command << Kanrisuru::Util.string_join_array(opts[:packages], ' ') |
| 202 | + |
| 203 | + pipe_output_newline(command) |
| 204 | + |
| 205 | + execute_shell(command) |
| 206 | + |
| 207 | + Kanrisuru::Result.new(command) do |cmd| |
| 208 | + lines = cmd.to_a |
| 209 | + |
| 210 | + result = [] |
| 211 | + lines.each do |line| |
| 212 | + line = line.gsub(/\s{2}/, '') |
| 213 | + values = line.split(' : ') |
| 214 | + next if values.length != 2 |
| 215 | + |
| 216 | + full_name = values[0] |
| 217 | + name, architecture = full_name.split('.') |
| 218 | + summary = values[1] |
| 219 | + |
| 220 | + result << YumPackageSearchResult.new(name, architecture, summary) |
| 221 | + end |
| 222 | + |
| 223 | + result |
| 224 | + end |
| 225 | + end |
| 226 | + |
| 227 | + def yum_repolist(opts) |
| 228 | + command = Kanrisuru::Command.new('yum repolist') |
| 229 | + command.append_flag('--verbose') |
| 230 | + |
| 231 | + command << Kanrisuru::Util.string_join_array(opts[:repos], ' ') if opts[:repos] |
| 232 | + |
| 233 | + execute_shell(command) |
| 234 | + |
| 235 | + Kanrisuru::Result.new(command) do |cmd| |
| 236 | + lines = cmd.to_a |
| 237 | + |
| 238 | + rows = [] |
| 239 | + current_row = nil |
| 240 | + |
| 241 | + lines.each do |line| |
| 242 | + case line |
| 243 | + when /^Repo-id/ |
| 244 | + if current_row |
| 245 | + rows << current_row |
| 246 | + current_row = nil |
| 247 | + end |
| 248 | + |
| 249 | + current_row = YumRepolist.new |
| 250 | + current_row.id = extract_single_yum_line(line) |
| 251 | + when /^Repo-name/ |
| 252 | + current_row.name = extract_single_yum_line(line) |
| 253 | + when /^Repo-revision/ |
| 254 | + current_row.revision = extract_single_yum_line(line).to_i |
| 255 | + when /^Repo-updated/, /^Updated/ |
| 256 | + text = extract_single_yum_line(line) |
| 257 | + current_row.updated = DateTime.parse(text) |
| 258 | + when /^Repo-pkgs/ |
| 259 | + current_row.packages = extract_single_yum_line(line).to_i |
| 260 | + when /^Repo-size/ |
| 261 | + size = Kanrisuru::Util::Bits.normalize_size(extract_single_yum_line(line)) |
| 262 | + current_row.repo_size = size |
| 263 | + when /^Repo-mirrors/ |
| 264 | + current_row.mirrors = extract_single_yum_line(line) |
| 265 | + when /^Repo-metalink/ |
| 266 | + current_row.metalink = extract_single_yum_line(line) |
| 267 | + when /^Repo-baseurl/ |
| 268 | + current_row.baseurl = extract_single_yum_line(line).split[0] |
| 269 | + when /^Repo-expire/ |
| 270 | + current_row.expire = extract_single_yum_line(line) |
| 271 | + when /^Repo-filename/ |
| 272 | + current_row.filename = extract_single_yum_line(line) |
| 273 | + when /^Repo-available-pkgs/ |
| 274 | + current_row.available_packages = extract_single_yum_line(line).to_i |
| 275 | + when /^Filter/ |
| 276 | + current_row.filters = extract_single_yum_line(line) |
| 277 | + end |
| 278 | + end |
| 279 | + |
| 280 | + rows << current_row |
| 281 | + rows |
| 282 | + end |
| 283 | + end |
| 284 | + |
| 285 | + def yum_info(opts) |
| 286 | + command = Kanrisuru::Command.new('yum info') |
| 287 | + command.append_flag('--quiet') |
| 288 | + command.append_flag('installed', opts[:installed]) |
| 289 | + |
| 290 | + command << Kanrisuru::Util.string_join_array(opts[:packages], ' ') if opts[:packages] |
| 291 | + |
| 292 | + execute_shell(command) |
| 293 | + |
| 294 | + Kanrisuru::Result.new(command) do |cmd| |
| 295 | + lines = cmd.to_a |
| 296 | + |
| 297 | + rows = [] |
| 298 | + current_row = nil |
| 299 | + description = '' |
| 300 | + |
| 301 | + lines.each do |line| |
| 302 | + next unless line.include?(': ') |
| 303 | + |
| 304 | + case line |
| 305 | + when /^Name/ |
| 306 | + unless current_row.nil? |
| 307 | + current_row.description = description.strip |
| 308 | + description = '' |
| 309 | + rows << current_row |
| 310 | + end |
| 311 | + |
| 312 | + current_row = YumPackageDetail.new |
| 313 | + current_row.package = extract_single_yum_line(line) |
| 314 | + when /^Arch/, /^Architecture/ |
| 315 | + current_row.architecture = extract_single_yum_line(line) |
| 316 | + when /^Version/ |
| 317 | + current_row.version = extract_single_yum_line(line) |
| 318 | + when /^Release/ |
| 319 | + current_row.release = extract_single_yum_line(line) |
| 320 | + when /^Source/ |
| 321 | + current_row.source = extract_single_yum_line(line) |
| 322 | + when /^Repository/ |
| 323 | + current_row.repository = extract_single_yum_line(line) |
| 324 | + when /^Summary/ |
| 325 | + current_row.summary = extract_single_yum_line(line) |
| 326 | + when /^URL/, /^Url/ |
| 327 | + current_row.url = extract_single_yum_line(line) |
| 328 | + when /^License/ |
| 329 | + current_row.license = extract_single_yum_line(line) |
| 330 | + when /^From repo/ |
| 331 | + current_row.yum_id = extract_single_yum_line(line) |
| 332 | + when /^Size/ |
| 333 | + size = Kanrisuru::Util::Bits.normalize_size(extract_single_yum_line(line)) |
| 334 | + current_row.install_size = size |
| 335 | + when /^Description/ |
| 336 | + description = extract_single_yum_line(line) |
| 337 | + else |
| 338 | + next if line == '' |
| 339 | + |
| 340 | + description += " #{extract_single_yum_line(line).strip}" |
| 341 | + end |
| 342 | + end |
| 343 | + |
| 344 | + current_row.description = description.strip |
| 345 | + rows << current_row |
| 346 | + rows |
| 347 | + end |
| 348 | + end |
| 349 | + |
| 350 | + def extract_single_yum_line(line) |
| 351 | + values = line.split(': ', 2) |
| 352 | + values.length == 2 ? values[1] : '' |
| 353 | + end |
| 354 | + |
| 355 | + def parse_yum_line(line) |
| 356 | + values = line.split |
| 357 | + return if values.length != 3 |
| 358 | + return unless /^\w+\.\w+$/i.match(values[0]) |
| 359 | + |
| 360 | + full_name = values[0] |
| 361 | + version = values[1] |
| 362 | + |
| 363 | + name, architecture = full_name.split('.') |
| 364 | + |
| 365 | + YumPackageOverview.new(name, architecture, version) |
| 366 | + end |
| 367 | + |
| 368 | + ## Bug reported on the output of the yum command |
| 369 | + ## https://bugzilla.redhat.com/show_bug.cgi?id=584525 |
| 370 | + ## that autowraps text when used in a script beyond 80 chars wide. |
| 371 | + ## Work-around with formatting by |
| 372 | + ## piping through trimming extra newline chars. |
| 373 | + def pipe_output_newline(command) |
| 374 | + command | "tr '\\n' '#'" |
| 375 | + command | "sed -e 's/# / /g'" |
| 376 | + command | "tr '#' '\\n'" |
| 377 | + end |
| 378 | + |
| 379 | + def yum_disable_repo(command, repo) |
| 380 | + return unless Kanrisuru::Util.present?(repo) |
| 381 | + |
| 382 | + command.append_flag("--disablerepo=#{repo}") |
| 383 | + end |
| 384 | + end |
| 385 | + end |
| 386 | +end |
0 commit comments