|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Kanrisuru |
| 4 | + module Core |
| 5 | + module Apt |
| 6 | + extend OsPackage::Define |
| 7 | + |
| 8 | + os_define :debian, :apt |
| 9 | + |
| 10 | + AptSource = Struct.new(:url, :dist, :architecture) |
| 11 | + AptPackageOverview = Struct.new(:package, :version, :suites, :architecture, :installed, :upgradeable, :automatic) |
| 12 | + AptPackageDetail = Struct.new( |
| 13 | + :package, |
| 14 | + :version, |
| 15 | + :priority, |
| 16 | + :section, |
| 17 | + :origin, |
| 18 | + :maintainer, |
| 19 | + :original_maintainer, |
| 20 | + :bugs, |
| 21 | + :install_size, |
| 22 | + :dependencies, |
| 23 | + :recommends, |
| 24 | + :provides, |
| 25 | + :suggests, |
| 26 | + :breaks, |
| 27 | + :conflicts, |
| 28 | + :replaces, |
| 29 | + :homepage, |
| 30 | + :task, |
| 31 | + :supported, |
| 32 | + :download_size, |
| 33 | + :apt_manual_installed, |
| 34 | + :apt_sources, |
| 35 | + :description, |
| 36 | + :summary |
| 37 | + ) |
| 38 | + |
| 39 | + def apt(action, opts = {}) |
| 40 | + case action |
| 41 | + when 'list' |
| 42 | + apt_list(opts) |
| 43 | + when 'update' |
| 44 | + apt_update(opts) |
| 45 | + when 'upgrade' |
| 46 | + apt_upgrade(opts) |
| 47 | + when 'full-upgrade', 'full_upgrade' |
| 48 | + apt_full_upgrade(opts) |
| 49 | + when 'install' |
| 50 | + apt_install(opts) |
| 51 | + when 'remove' |
| 52 | + apt_remove(opts) |
| 53 | + when 'purge' |
| 54 | + apt_purge(opts) |
| 55 | + when 'autoremove' |
| 56 | + apt_autoremove(opts) |
| 57 | + when 'search' |
| 58 | + apt_search(opts) |
| 59 | + when 'show' |
| 60 | + apt_show(opts) |
| 61 | + when 'clean' |
| 62 | + apt_clean(opts) |
| 63 | + when 'autoclean' |
| 64 | + apt_autoclean(opts) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + private |
| 69 | + |
| 70 | + def apt_autoclean(_opts) |
| 71 | + command = Kanrisuru::Command.new('apt-get autoclean') |
| 72 | + execute_shell(command) |
| 73 | + Kanrisuru::Result.new(command) |
| 74 | + end |
| 75 | + |
| 76 | + def apt_clean(_opts) |
| 77 | + command = Kanrisuru::Command.new('apt-get clean') |
| 78 | + execute_shell(command) |
| 79 | + Kanrisuru::Result.new(command) |
| 80 | + end |
| 81 | + |
| 82 | + def apt_search(opts) |
| 83 | + command = Kanrisuru::Command.new('apt search') |
| 84 | + command << opts[:query] |
| 85 | + |
| 86 | + execute_shell(command) |
| 87 | + |
| 88 | + Kanrisuru::Result.new(command) do |cmd| |
| 89 | + lines = cmd.to_a |
| 90 | + lines.shift |
| 91 | + lines.shift |
| 92 | + |
| 93 | + result = [] |
| 94 | + |
| 95 | + lines.each do |line| |
| 96 | + next unless line.include?('/') |
| 97 | + |
| 98 | + item = parse_apt_line(line) |
| 99 | + next unless item |
| 100 | + |
| 101 | + result << item |
| 102 | + end |
| 103 | + |
| 104 | + result |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + def apt_list(opts) |
| 109 | + command = Kanrisuru::Command.new('apt list') |
| 110 | + command.append_flag('--installed', opts[:installed]) |
| 111 | + command.append_flag('--upgradeable', opts[:upgradeable]) |
| 112 | + command.append_flag('--all-versions', opts[:all_versions]) |
| 113 | + command.append_arg('-a', opts[:package_name]) |
| 114 | + |
| 115 | + execute_shell(command) |
| 116 | + |
| 117 | + Kanrisuru::Result.new(command) do |cmd| |
| 118 | + lines = cmd.to_a |
| 119 | + lines.shift |
| 120 | + |
| 121 | + result = [] |
| 122 | + lines.each.with_index do |line, _index| |
| 123 | + item = parse_apt_line(line) |
| 124 | + next unless item |
| 125 | + |
| 126 | + result << item |
| 127 | + end |
| 128 | + |
| 129 | + result |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + def apt_autoremove(_opts) |
| 134 | + command = Kanrisuru::Command.new('apt-get autoremove') |
| 135 | + command.append_flag('-y') |
| 136 | + |
| 137 | + execute_shell(command) |
| 138 | + Kanrisuru::Result.new(command) |
| 139 | + end |
| 140 | + |
| 141 | + def apt_purge(opts) |
| 142 | + command = Kanrisuru::Command.new('apt-get purge') |
| 143 | + command.append_flag('-y') |
| 144 | + |
| 145 | + packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ') |
| 146 | + command << packages |
| 147 | + |
| 148 | + execute_shell(command) |
| 149 | + Kanrisuru::Result.new(command) |
| 150 | + end |
| 151 | + |
| 152 | + def apt_remove(opts) |
| 153 | + command = Kanrisuru::Command.new('apt-get remove') |
| 154 | + command.append_flag('-y') |
| 155 | + |
| 156 | + packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ') |
| 157 | + command << packages |
| 158 | + |
| 159 | + execute_shell(command) |
| 160 | + Kanrisuru::Result.new(command) |
| 161 | + end |
| 162 | + |
| 163 | + def apt_install(opts) |
| 164 | + command = Kanrisuru::Command.new('apt-get install') |
| 165 | + command.append_flag('-y') |
| 166 | + |
| 167 | + command.append_flag('--no-upgrade', opts[:no_upgrade]) |
| 168 | + command.append_flag('--only-upgrade', opts[:only_upgrade]) |
| 169 | + command.append_flag('--reinstall', opts[:reinstall]) |
| 170 | + |
| 171 | + packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ') |
| 172 | + command << packages |
| 173 | + |
| 174 | + execute_shell(command) |
| 175 | + Kanrisuru::Result.new(command) |
| 176 | + end |
| 177 | + |
| 178 | + def apt_full_upgrade(_opts) |
| 179 | + command = Kanrisuru::Command.new('apt full-upgrade') |
| 180 | + command.append_flag('-y') |
| 181 | + execute_shell(command) |
| 182 | + Kanrisuru::Result.new(command) |
| 183 | + end |
| 184 | + |
| 185 | + def apt_upgrade(_opts) |
| 186 | + command = Kanrisuru::Command.new('apt-get upgrade') |
| 187 | + command.append_flag('-y') |
| 188 | + execute_shell(command) |
| 189 | + Kanrisuru::Result.new(command) |
| 190 | + end |
| 191 | + |
| 192 | + def apt_update(_opts) |
| 193 | + command = Kanrisuru::Command.new('apt-get update') |
| 194 | + command.append_flag('-y') |
| 195 | + execute_shell(command) |
| 196 | + Kanrisuru::Result.new(command) |
| 197 | + end |
| 198 | + |
| 199 | + def apt_show(opts) |
| 200 | + command = Kanrisuru::Command.new('apt show') |
| 201 | + command.append_flag('-a') |
| 202 | + |
| 203 | + packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ') |
| 204 | + command << packages |
| 205 | + |
| 206 | + execute_shell(command) |
| 207 | + |
| 208 | + Kanrisuru::Result.new(command) do |cmd| |
| 209 | + lines = cmd.to_a |
| 210 | + rows = [] |
| 211 | + |
| 212 | + current_row = nil |
| 213 | + summary = '' |
| 214 | + |
| 215 | + lines.each do |line| |
| 216 | + next if line == 'WARNING: apt does not have a stable CLI interface. Use with caution in scripts.' |
| 217 | + next if [] ['', nil, '.'].include?(line) |
| 218 | + |
| 219 | + case line |
| 220 | + when /^Package:/ |
| 221 | + unless current_row.nil? |
| 222 | + current_row.summary = summary.strip |
| 223 | + summary = '' |
| 224 | + rows << current_row |
| 225 | + end |
| 226 | + |
| 227 | + current_row = AptPackageDetail.new |
| 228 | + current_row.package = extract_single_line(line) |
| 229 | + when /^Version:/ |
| 230 | + current_row.version = extract_single_line(line) |
| 231 | + when /^Priority:/ |
| 232 | + current_row.priority = extract_single_line(line) |
| 233 | + when /^Section:/ |
| 234 | + current_row.section = extract_single_line(line) |
| 235 | + when /^Origin:/ |
| 236 | + current_row.origin = extract_single_line(line) |
| 237 | + when /^Maintainer:/ |
| 238 | + current_row.maintainer = extract_single_line(line) |
| 239 | + when /^Original-Maintainer:/ |
| 240 | + current_row.original_maintainer = extract_single_line(line) |
| 241 | + when /^Bugs:/ |
| 242 | + current_row.bugs = extract_single_line(line) |
| 243 | + when /^Installed-Size:/ |
| 244 | + current_row.install_size = normalize_size(extract_single_line(line)) |
| 245 | + when /^Download-Size:/ |
| 246 | + current_row.download_size = normalize_size(extract_single_line(line)) |
| 247 | + when /^Depends:/ |
| 248 | + current_row.dependencies = parse_comma_values(extract_single_line(line)) |
| 249 | + when /^Provides:/ |
| 250 | + current_row.provides = parse_comma_values(extract_single_line(line)) |
| 251 | + when /^Recommends:/ |
| 252 | + current_row.recommends = parse_comma_values(extract_single_line(line)) |
| 253 | + when /^Suggests:/ |
| 254 | + current_row.suggests = parse_comma_values(extract_single_line(line)) |
| 255 | + when /^Breaks:/ |
| 256 | + current_row.breaks = parse_comma_values(extract_single_line(line)) |
| 257 | + when /^Conflicts:/ |
| 258 | + current_row.conflicts = parse_comma_values(extract_single_line(line)) |
| 259 | + when /^Replaces:/ |
| 260 | + current_row.replaces = parse_comma_values(extract_single_line(line)) |
| 261 | + when /^Homepage:/ |
| 262 | + current_row.homepage = extract_single_line(line) |
| 263 | + when /^Task:/ |
| 264 | + current_row.task = parse_comma_values(extract_single_line(line)) |
| 265 | + when /^Supported:/ |
| 266 | + current_row.supported = extract_single_line(line) |
| 267 | + when /^APT-Sources:/ |
| 268 | + current_row.apt_sources = parse_apt_sources(extract_single_line(line)) |
| 269 | + when /^APT-Manual-Installed:/ |
| 270 | + current_row.apt_manual_installed = extract_single_line(line) == 'yes' |
| 271 | + when /^Description:/ |
| 272 | + current_row.description = extract_single_line(line) |
| 273 | + else |
| 274 | + summary += " #{line.strip}" |
| 275 | + end |
| 276 | + end |
| 277 | + |
| 278 | + current_row.summary = summary.strip |
| 279 | + rows << current_row |
| 280 | + rows |
| 281 | + end |
| 282 | + end |
| 283 | + |
| 284 | + def extract_single_line(line) |
| 285 | + line.split(': ')[1] |
| 286 | + end |
| 287 | + |
| 288 | + def parse_comma_values(string) |
| 289 | + string.split(', ') |
| 290 | + end |
| 291 | + |
| 292 | + def parse_apt_sources(string) |
| 293 | + url, dist, architecture, = string.split |
| 294 | + AptSource.new(url, dist, architecture) |
| 295 | + end |
| 296 | + |
| 297 | + def normalize_size(string) |
| 298 | + size, unit = string.split |
| 299 | + size = size.to_i |
| 300 | + case unit.downcase |
| 301 | + when 'b' |
| 302 | + Kanrisuru::Util::Bits.convert_bytes(size, :byte, :kilobyte) |
| 303 | + when 'kb' |
| 304 | + size |
| 305 | + when 'mb' |
| 306 | + Kanrisuru::Util::Bits.convert_from_mb(size, :kilobyte).to_i |
| 307 | + when 'gb' |
| 308 | + Kanrisuru::Util::Bits.convert_from_gb(size, :kilobyte).to_i |
| 309 | + end |
| 310 | + end |
| 311 | + |
| 312 | + def parse_apt_line(line) |
| 313 | + values = line.split('/') |
| 314 | + return if values.length < 2 |
| 315 | + |
| 316 | + package = values[0] |
| 317 | + |
| 318 | + values = values[1].split |
| 319 | + suites = values[0].split(',') |
| 320 | + version = values[1] |
| 321 | + architecture = values[2] |
| 322 | + |
| 323 | + installed = false |
| 324 | + upgradeable = false |
| 325 | + automatic = false |
| 326 | + |
| 327 | + if values.length > 3 |
| 328 | + installed = values[3].include?('installed') |
| 329 | + upgradeable = values[3].include?('upgradeable') |
| 330 | + automatic = values[3].include?('automatic') |
| 331 | + end |
| 332 | + |
| 333 | + AptPackageOverview.new(package, version, suites, architecture, installed, upgradeable, automatic) |
| 334 | + end |
| 335 | + end |
| 336 | + end |
| 337 | +end |
0 commit comments