Skip to content

Commit

Permalink
Chore: lint fixes and general qa
Browse files Browse the repository at this point in the history
- ran rubocop on the code (with autofix)
- refactored test setup
- refactored code (e.g. request libraries)
- added test coverage for the library code
  • Loading branch information
till committed Feb 11, 2023
1 parent f569b26 commit a107303
Show file tree
Hide file tree
Showing 21 changed files with 294 additions and 180 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ install:
release:
bundle exec rake release

lint:
bundle exec rubocop .

test: install
bundle exec rake test

docker-build:
docker build -t $(IMAGE) .

Expand Down
27 changes: 10 additions & 17 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
require "bundler/gem_tasks"
require "minitest/autorun"
require "simplecov"
require "simplecov-lcov"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

task :test do
Rake::TestTask.new do |t|
ENV['COVERAGE'] = 'true'
t.pattern = 'tests/*_test.rb'

SimpleCov.formatter = SimpleCov::Formatter::LcovFormatter
SimpleCov::Formatter::LcovFormatter.config do |c|
c.output_directory = './coverage'
c.report_with_single_file = true
c.single_report_path = './coverage/lcov.info'
end
SimpleCov.start

$LOAD_PATH.unshift('lib', 'tests')
Dir.glob('./tests/*_test.rb') do |f|
require f
end
# $LOAD_PATH.unshift('lib', 'tests')
# Dir.glob('./tests/*_test.rb').sort.each do |f|
# require f
# end
end
5 changes: 4 additions & 1 deletion apt-spy2.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: true

# require 'English'
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'apt/spy2/version'
Expand All @@ -14,7 +17,7 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.7', '< 3.3'

spec.files = `git ls-files`.split($/)
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
Expand Down
5 changes: 3 additions & 2 deletions bin/apt-spy2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'apt/spy2'
require 'colored'
Expand All @@ -8,7 +9,7 @@ Encoding.default_internal = Encoding::UTF_8

begin
AptSpy2.start
rescue => the_error
puts the_error.to_s.white_on_red
rescue StandardError => e
puts e.to_s.white_on_red
exit 1
end
136 changes: 58 additions & 78 deletions lib/apt/spy2.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'thor'
require 'open-uri'
require 'colored'
Expand All @@ -7,39 +9,39 @@
require 'apt/spy2/downloader'
require 'apt/spy2/ubuntu_mirrors'
require 'apt/spy2/launchpad'
require 'apt/spy2/request'
require 'apt/spy2/url'
require 'net/http'
require 'net/https'
require 'uri'

# apt-spy2 command interface
class AptSpy2 < Thor
package_name "apt-spy2"
package_name 'apt-spy2'

desc "fix", "Set the closest/fastest mirror"
option :country, :default => "mirrors"
option :commit, :type => :boolean
option :launchpad, :type => :boolean, :banner => "Use launchpad's mirror list"
option :strict, :type => :boolean
desc 'fix', 'Set the closest/fastest mirror'
option :country, default: 'mirrors'
option :commit, type: :boolean
option :launchpad, type: :boolean, banner: "Use launchpad's mirror list"
option :strict, type: :boolean
def fix
working = filter(retrieve(options[:country], use_launchpad?(options)), options[:strict], false)
print "The closest mirror is: "
puts "#{working[0]}".bold.magenta
if !options[:commit]
puts "Run with --commit to adjust /etc/apt/sources.list".yellow
else
puts "Updating /etc/apt/sources.list".yellow
update(working[0])
mirrors = retrieve(options[:country], use_launchpad?(options))
working = filter(mirrors, options[:strict], false)
print 'The closest mirror is: '
puts (working[0]).to_s.bold.magenta
unless options[:commit]
puts 'Run with --commit to adjust /etc/apt/sources.list'.yellow
return
end

puts 'Updating /etc/apt/sources.list'.yellow
update(working[0])
end

desc "check", "Evaluate mirrors"
option :country, :default => "mirrors"
option :output, :type => :boolean, :default => true
option :format, :default => "shell"
option :launchpad, :type => :boolean, :banner => "Use launchpad's mirror list"
option :strict, :type => :boolean
desc 'check', 'Evaluate mirrors'
option :country, default: 'mirrors'
option :output, type: :boolean, default: true
option :format, default: 'shell'
option :launchpad, type: :boolean, banner: "Use launchpad's mirror list"
option :strict, type: :boolean
def check

@writer = Apt::Spy2::Writer.new(options[:format])

mirrors = retrieve(options[:country], use_launchpad?(options))
Expand All @@ -48,50 +50,47 @@ def check
puts @writer.to_json if @writer.json?
end

desc "list", "List the currently available mirrors"
option :country, :default => "mirrors"
option :format, :default => "shell"
option :launchpad, :type => :boolean, :banner => "Use launchpad's mirror list"
desc 'list', 'List the currently available mirrors'
option :country, default: 'mirrors'
option :format, default: 'shell'
option :launchpad, type: :boolean, banner: "Use launchpad's mirror list"
def list
mirrors = retrieve(options[:country], use_launchpad?(options))

@writer = Apt::Spy2::Writer.new(options[:format])

@writer.set_complete(mirrors)
@writer.complete(mirrors)

puts @writer.to_json if @writer.json?
puts mirrors if !@writer.json?
puts mirrors unless @writer.json?
end

desc "version", "Show which version of apt-spy2 is installed"
desc 'version', 'Show which version of apt-spy2 is installed'
def version
puts Apt::Spy2::VERSION
exit
end

private
def retrieve(country = "mirrors", launchpad = false)

def retrieve(country = 'mirrors', launchpad = false)
downloader = Apt::Spy2::Downloader.new

if launchpad === true
csv_path = File.expand_path(File.dirname(__FILE__) + "/../../var/country-names.txt")
if launchpad
csv_path = File.expand_path("#{File.dirname(__FILE__)}/../../var/country-names.txt")
country = Apt::Spy2::Country.new(csv_path)
name = country.to_country_name(options[:country])

launchpad = Apt::Spy2::Launchpad.new(downloader.do_download('https://launchpad.net/ubuntu/+archivemirrors'))
return launchpad.get_mirrors(name)
return launchpad.mirrors(name)
end

country.upcase! if country.length == 2

ubuntu_mirrors = Apt::Spy2::UbuntuMirrors.new(downloader.do_download("http://mirrors.ubuntu.com/#{country}.txt"))
mirrors = ubuntu_mirrors.get_mirrors(country)
return mirrors

ubuntu_mirrors.mirrors(country)
end

private
def filter(mirrors, strict = false, output = true)
# f me :)

Expand All @@ -100,56 +99,40 @@ def filter(mirrors, strict = false, output = true)
url = Apt::Spy2::Url.new(strict)

mirrors.each do |mirror|
data = {"mirror" => mirror }
data = { 'mirror' => mirror }

check = url.get_mirror(mirror)
check = url.adjust!(mirror)

status = broken?(check)

data["status"] = status
data['status'] = status

if status == "up"
working_mirrors << mirror
end
working_mirrors << mirror if status == 'up'

@writer.echo(data) if output
end

return working_mirrors
working_mirrors
end

private
def broken?(url)
uri = URI(url)

http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.use_ssl = true
end

begin
response = http.request(Net::HTTP::Head.new(uri.request_uri))
if response.code == "200"
return "up"
end
req = Apt::Spy2::Request.new(url)
response = req.head
return 'up' if response.code == '200'

if response.code == "404"
return "broken"
end

return "down"
rescue Exception => e
return 'broken' if response.code == '404'
rescue StandardError
# connection errors, ssl errors, etc.
return "down"
end

'down'
end

private
def update(mirror)

t = Time.now
r = `lsb_release -c`.split(" ")[1]
sources = "## Updated on #{t.to_s} by apt-spy2\n"
r = `lsb_release -c`.split(' ')[1]
sources = "## Updated on #{t} by apt-spy2\n"
sources << "deb #{mirror} #{r} main restricted universe multiverse\n"
sources << "deb #{mirror} #{r}-updates main restricted universe multiverse\n"
sources << "deb #{mirror} #{r}-backports main restricted universe multiverse\n"
Expand All @@ -162,26 +145,23 @@ def update(mirror)
File.open(apt_sources, 'w') do |f|
f.write(sources)
end
rescue
rescue StandardError
msg = "Failed updating #{apt_sources}!"
msg << "You probably need sudo!"
msg << 'You probably need sudo!'
raise msg
end

puts "Updated '#{apt_sources}' with #{mirror}".green
puts "Run `apt-get update` to update".black_on_yellow
puts 'Run `apt-get update` to update'.black_on_yellow
end

private
def use_launchpad?(options)
if !options[:launchpad]
return false
end
return false unless options[:launchpad]

if options[:country] && options[:country] == 'mirrors'
raise "Please supply a `--country=foo`. Launchpad cannot guess!"
raise 'Please supply a `--country=foo`. Launchpad cannot guess!'
end

return true
true
end
end
17 changes: 8 additions & 9 deletions lib/apt/spy2/country.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
# frozen_string_literal: true

module Apt
module Spy2
# lookup a country based on code
class Country

def initialize(country_database)
@database = country_database
end

def to_country_name(code)
code = code.upcase
return capitalize(code) unless code.length == 2
return capitalize!(code) unless code.length == 2

File.open(@database).each do |line|
country, tld = line.split(';', 2)
tld.gsub!(/\n/, '')

if code == tld
return capitalize(country)
end

return capitalize!(country) if code == tld
end

raise "Could not look up: #{code}"
end

private
def capitalize(str)
return str.split(" ").map(&:capitalize).join(" ")
end

def capitalize!(str)
str.split(' ').map(&:capitalize).join(' ')
end
end
end
end
Loading

0 comments on commit a107303

Please sign in to comment.