Skip to content

Commit

Permalink
File refactor (#1)
Browse files Browse the repository at this point in the history
* refactor files

* finalize refactor locally

* fixs issues with refactor, add util test cases

Co-authored-by: Ryan <rmammina@gmail.com>
  • Loading branch information
ryan-avamia and rmammina authored Dec 8, 2021
1 parent 3e90ff3 commit 7bbc621
Show file tree
Hide file tree
Showing 250 changed files with 7,882 additions and 5,711 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Kanrisuru 0.14.0 (December 08, 2021) ##
* Add additional unit test cases for `util` methods.
* Refactor the `core` module commands into smaller files, with each command split up. Also refactor parsing code into separate `Parser` class.

## Kanrisuru 0.13.0 (December 06, 2021) ##
* Add functional test cases for `Kanrisuru::Result` class.
* Refactor integration tests for better parallelization with the `parallel_tests` gem. This dropped overall test time from 35 minutes, to 22 minutes after first integration with 1 processor. After scaling upto 8 core machine, the run time dropped to 16 minutes, but was still sending the entire test file to a processor. By splitting up each host test into a seperate file, the run time dropped to a little over 9 minutes. There's probably a way to optimize which test gets run together, but overall a much better scenario.
Expand Down
288 changes: 4 additions & 284 deletions lib/kanrisuru/core/apt.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
# frozen_string_literal: true

require_relative 'apt/types'
require_relative 'apt/parser'
require_relative 'apt/commands'

module Kanrisuru
module Core
module Apt
extend OsPackage::Define

os_define :debian, :apt

Source = Struct.new(:url, :dist, :architecture)
PackageOverview = Struct.new(:package, :version, :suites, :architecture, :installed, :upgradeable, :automatic)
PackageDetail = Struct.new(
:package,
:version,
:priority,
:section,
:origin,
:maintainer,
:original_maintainer,
:bugs,
:install_size,
:dependencies,
:recommends,
:provides,
:suggests,
:breaks,
:conflicts,
:replaces,
:homepage,
:task,
:supported,
:download_size,
:apt_manual_installed,
:apt_sources,
:description,
:summary
)

def apt(action, opts = {})
case action
when 'list'
Expand Down Expand Up @@ -64,261 +39,6 @@ def apt(action, opts = {})
apt_autoclean(opts)
end
end

private

def apt_autoclean(_opts)
command = Kanrisuru::Command.new('apt-get autoclean')
execute_shell(command)
Kanrisuru::Result.new(command)
end

def apt_clean(_opts)
command = Kanrisuru::Command.new('apt-get clean')
execute_shell(command)
Kanrisuru::Result.new(command)
end

def apt_search(opts)
command = Kanrisuru::Command.new('apt search')
command << opts[:query]

execute_shell(command)

Kanrisuru::Result.new(command) do |cmd|
lines = cmd.to_a
lines.shift
lines.shift

result = []

lines.each do |line|
next unless line.include?('/')

item = parse_apt_line(line)
next unless item

result << item
end

result
end
end

def apt_list(opts)
command = Kanrisuru::Command.new('apt list')
command.append_flag('--installed', opts[:installed])
command.append_flag('--upgradeable', opts[:upgradeable])
command.append_flag('--all-versions', opts[:all_versions])
command.append_arg('-a', opts[:package_name])

execute_shell(command)

Kanrisuru::Result.new(command) do |cmd|
lines = cmd.to_a
lines.shift

result = []
lines.each do |line|
item = parse_apt_line(line)
next unless item

result << item
end

result
end
end

def apt_autoremove(_opts)
command = Kanrisuru::Command.new('apt-get autoremove')
command.append_flag('-y')

execute_shell(command)
Kanrisuru::Result.new(command)
end

def apt_purge(opts)
command = Kanrisuru::Command.new('apt-get purge')
command.append_flag('-y')

packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
command << packages

execute_shell(command)
Kanrisuru::Result.new(command)
end

def apt_remove(opts)
command = Kanrisuru::Command.new('apt-get remove')
command.append_flag('-y')

packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
command << packages

execute_shell(command)
Kanrisuru::Result.new(command)
end

def apt_install(opts)
command = Kanrisuru::Command.new('apt-get install')
command.append_flag('-y')

command.append_flag('--no-upgrade', opts[:no_upgrade])
command.append_flag('--only-upgrade', opts[:only_upgrade])
command.append_flag('--reinstall', opts[:reinstall])

packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
command << packages

execute_shell(command)
Kanrisuru::Result.new(command)
end

def apt_full_upgrade(_opts)
command = Kanrisuru::Command.new('apt full-upgrade')
command.append_flag('-y')
execute_shell(command)
Kanrisuru::Result.new(command)
end

def apt_upgrade(_opts)
command = Kanrisuru::Command.new('apt-get upgrade')
command.append_flag('-y')
execute_shell(command)
Kanrisuru::Result.new(command)
end

def apt_update(_opts)
command = Kanrisuru::Command.new('apt-get update')
command.append_flag('-y')
execute_shell(command)
Kanrisuru::Result.new(command)
end

def apt_show(opts)
command = Kanrisuru::Command.new('apt show')
command.append_flag('-a')

packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
command << packages

execute_shell(command)

Kanrisuru::Result.new(command) do |cmd|
lines = cmd.to_a
rows = []

current_row = nil
description = ''

lines.each do |line|
next if line == 'WARNING: apt does not have a stable CLI interface. Use with caution in scripts.'
next if ['', nil, '.'].include?(line)

case line
when /^Package:/
unless current_row.nil?
current_row.description = description.strip
description = ''
rows << current_row
end

current_row = PackageDetail.new
current_row.package = extract_single_line(line)
when /^Version:/
current_row.version = extract_single_line(line)
when /^Priority:/
current_row.priority = extract_single_line(line)
when /^Section:/
current_row.section = extract_single_line(line)
when /^Origin:/
current_row.origin = extract_single_line(line)
when /^Maintainer:/
current_row.maintainer = extract_single_line(line)
when /^Original-Maintainer:/
current_row.original_maintainer = extract_single_line(line)
when /^Bugs:/
current_row.bugs = extract_single_line(line)
when /^Installed-Size:/
size = Kanrisuru::Util::Bits.normalize_size(extract_single_line(line))
current_row.install_size = size
when /^Download-Size:/
size = Kanrisuru::Util::Bits.normalize_size(extract_single_line(line))
current_row.download_size = size
when /^Depends:/
current_row.dependencies = parse_comma_values(extract_single_line(line))
when /^Provides:/
current_row.provides = parse_comma_values(extract_single_line(line))
when /^Recommends:/
current_row.recommends = parse_comma_values(extract_single_line(line))
when /^Suggests:/
current_row.suggests = parse_comma_values(extract_single_line(line))
when /^Breaks:/
current_row.breaks = parse_comma_values(extract_single_line(line))
when /^Conflicts:/
current_row.conflicts = parse_comma_values(extract_single_line(line))
when /^Replaces:/
current_row.replaces = parse_comma_values(extract_single_line(line))
when /^Homepage:/
current_row.homepage = extract_single_line(line)
when /^Task:/
current_row.task = parse_comma_values(extract_single_line(line))
when /^Supported:/
current_row.supported = extract_single_line(line)
when /^APT-Sources:/
current_row.apt_sources = parse_apt_sources(extract_single_line(line))
when /^APT-Manual-Installed:/
current_row.apt_manual_installed = extract_single_line(line) == 'yes'
when /^Description:/
current_row.summary = extract_single_line(line)
else
description += " #{line.strip}"
end
end

current_row.description = description.strip
rows << current_row
rows
end
end

def extract_single_line(line)
line.split(': ')[1]
end

def parse_comma_values(string)
string.split(', ')
end

def parse_apt_sources(string)
url, dist, architecture, = string.split
Source.new(url, dist, architecture)
end

def parse_apt_line(line)
values = line.split('/')
return if values.length < 2

package = values[0]

values = values[1].split
suites = values[0].split(',')
version = values[1]
architecture = values[2]

installed = false
upgradeable = false
automatic = false

if values.length > 3
installed = values[3].include?('installed')
upgradeable = values[3].include?('upgradeable')
automatic = values[3].include?('automatic')
end

PackageOverview.new(package, version, suites, architecture, installed, upgradeable, automatic)
end
end
end
end
14 changes: 14 additions & 0 deletions lib/kanrisuru/core/apt/commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require_relative 'commands/autoclean'
require_relative 'commands/autoremove'
require_relative 'commands/clean'
require_relative 'commands/full_upgrade'
require_relative 'commands/install'
require_relative 'commands/list'
require_relative 'commands/purge'
require_relative 'commands/remove'
require_relative 'commands/search'
require_relative 'commands/show'
require_relative 'commands/update'
require_relative 'commands/upgrade'
13 changes: 13 additions & 0 deletions lib/kanrisuru/core/apt/commands/autoclean.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Kanrisuru
module Core
module Apt
def apt_autoclean(_opts)
command = Kanrisuru::Command.new('apt-get autoclean')
execute_shell(command)
Kanrisuru::Result.new(command)
end
end
end
end
15 changes: 15 additions & 0 deletions lib/kanrisuru/core/apt/commands/autoremove.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Kanrisuru
module Core
module Apt
def apt_autoremove(_opts)
command = Kanrisuru::Command.new('apt-get autoremove')
command.append_flag('-y')

execute_shell(command)
Kanrisuru::Result.new(command)
end
end
end
end
13 changes: 13 additions & 0 deletions lib/kanrisuru/core/apt/commands/clean.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Kanrisuru
module Core
module Apt
def apt_clean(_opts)
command = Kanrisuru::Command.new('apt-get clean')
execute_shell(command)
Kanrisuru::Result.new(command)
end
end
end
end
14 changes: 14 additions & 0 deletions lib/kanrisuru/core/apt/commands/full_upgrade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Kanrisuru
module Core
module Apt
def apt_full_upgrade(_opts)
command = Kanrisuru::Command.new('apt full-upgrade')
command.append_flag('-y')
execute_shell(command)
Kanrisuru::Result.new(command)
end
end
end
end
Loading

0 comments on commit 7bbc621

Please sign in to comment.