From 6f4bfb401b3d63602f71fbcedc2148606b436e2a Mon Sep 17 00:00:00 2001 From: till Date: Wed, 23 Nov 2011 20:39:45 +0100 Subject: [PATCH] file-name-convention (or something like that) --- bin/apt-repair-sources | 14 +-- lib/apt/repair/sources.rb | 222 +++++++++++++++++----------------- tests/AptRepairSourcesTest.rb | 14 +-- 3 files changed, 127 insertions(+), 123 deletions(-) diff --git a/bin/apt-repair-sources b/bin/apt-repair-sources index ea4c9a9..65e6163 100755 --- a/bin/apt-repair-sources +++ b/bin/apt-repair-sources @@ -6,6 +6,13 @@ # Copyright: 2011 Till Klampaeckel # +require 'rubygems' +require 'trollop' +require 'net/http' + +$: << File.join(File.dirname(__FILE__), "..", "lib") +require 'apt/repair/sources' + ubuntu = `lsb_release -i|awk '{print $3}'`.gsub(/\s+/, "") if (ubuntu != 'Ubuntu') puts "Ubuntu-only, currently." @@ -27,13 +34,6 @@ if work.length == 0 exit end -require 'rubygems' -require 'trollop' -require 'net/http' - -$: << File.join(File.dirname(__FILE__), "..", "lib") -require 'apt/repair/sources' - opts = Trollop::options do version "apt-repair-sources 0.1.0 (c) 2011 Till Klampaeckel" banner <<-EOS diff --git a/lib/apt/repair/sources.rb b/lib/apt/repair/sources.rb index 57a1ec4..cc51c43 100644 --- a/lib/apt/repair/sources.rb +++ b/lib/apt/repair/sources.rb @@ -5,134 +5,138 @@ # Copyright: 2011 Till Klampaeckel # -# @author Till Klampaeckel -class Apt::Repair::Sources - - # @param [String] line A line from a source.list file. - # @return [AptRepairSources] - def initialize(line) - @l = line - @e = line.split(" ") - end - - # @return [String] The architecture: amd64, i686 - def self.find_platform - return `dpkg --print-architecture`.gsub(/\s+/, "") - end - - # Each 'line' in a sources.list file contains various elements, this is an array - # with them. type (deb, deb-src), url and distribution are stripped. - # @return [Array] - def get_el - el = @l.split(" ") - - el.shift - el.shift - el.shift - - return el - end - - # @return [String] For the URL test! - def get_end - if self.get_type == 'deb' - return "/binary-#{self.class.find_platform}/Packages.gz" - end - return "/source/Sources.gz" - end - - # @return [String] The type: most likely deb or deb-src - def get_type - return @e[0] - end - - # @return [String] Create the base url to test against. - def get_url(base) - if base.nil? - url = @e[1] - else - url = base - end - if url[-1,1] != "/" - url += "/" - end - url += "dists/" + @e[2] + "/" - return url - end +module Apt + module Repair + # @author Till Klampaeckel + class Apt::Repair::Sources + + # @param [String] line A line from a source.list file. + # @return [AptRepairSources] + def initialize(line) + @l = line + @e = line.split(" ") + end - # Tries to fix a line from a sources.list file by correcting the URL or commenting - # it out. When the URL is corrected, we attempt to test if the new URL exists. The - # the failover is always to comment it out. - # @return [String] - def fix_line - el = @l.split(" ") + # @return [String] The architecture: amd64, i686 + def self.find_platform + return `dpkg --print-architecture`.gsub(/\s+/, "") + end - u = URI(el[1]) + # Each 'line' in a sources.list file contains various elements, this is an array + # with them. type (deb, deb-src), url and distribution are stripped. + # @return [Array] + def get_el + el = @l.split(" ") - disable = false + el.shift + el.shift + el.shift - case u.host - when "releases.ubuntu.com" - c = u.host - u.host = "archive.ubuntu.com" - if self.uri_exists(self.get_url(u.to_s)) == false - u.host = c + return el end - when "archive.ubuntu.com", "security.ubuntu.com" - c = u.host - u.host = "old-releases.ubuntu.com" - if self.uri_exists(self.get_url(u.to_s)) == false - u.host = c + + # @return [String] For the URL test! + def get_end + if self.get_type == 'deb' + return "/binary-#{self.class.find_platform}/Packages.gz" + end + return "/source/Sources.gz" end - when "old-releases.ubuntu.com" - raise Exception, "You're fucked." - else - # this is tricky, e.g. is the mirror down or did it move? - c = u.host - - if c =~ /(.*)\.releases\.ubuntu\.com/ - u.host = "archive.ubuntu.com" - elsif c =~ /(.*)\.archive\.ubuntu\.com/ - u.host = "old-releases.ubuntu.com" - else - disable = true + + # @return [String] The type: most likely deb or deb-src + def get_type + return @e[0] end - if disable == false - if self.uri_exists(u.to_s) == false - u.host = c + # @return [String] Create the base url to test against. + def get_url(base = nil) + if base.nil? + url = @e[1] + else + url = base + end + if url[-1,1] != "/" + url += "/" end + url += "dists/" + @e[2] + "/" + return url end - end + # Tries to fix a line from a sources.list file by correcting the URL or commenting + # it out. When the URL is corrected, we attempt to test if the new URL exists. The + # the failover is always to comment it out. + # @return [String] + def fix_line + el = @l.split(" ") + + u = URI(el[1]) + + disable = false + + case u.host + when "releases.ubuntu.com" + c = u.host + u.host = "archive.ubuntu.com" + if self.uri_exists(self.get_url(u.to_s)) == false + u.host = c + end + when "archive.ubuntu.com", "security.ubuntu.com" + c = u.host + u.host = "old-releases.ubuntu.com" + if self.uri_exists(self.get_url(u.to_s)) == false + u.host = c + end + when "old-releases.ubuntu.com" + raise Exception, "You're fucked." + else + # this is tricky, e.g. is the mirror down or did it move? + c = u.host + + if c =~ /(.*)\.releases\.ubuntu\.com/ + u.host = "archive.ubuntu.com" + elsif c =~ /(.*)\.archive\.ubuntu\.com/ + u.host = "old-releases.ubuntu.com" + else + disable = true + end + + if disable == false + if self.uri_exists(u.to_s) == false + u.host = c + end + end + + end - el[1] = u.to_s - line = el.join(" ") + el[1] = u.to_s + line = el.join(" ") - if disable == true - line = '#' + line - end + if disable == true + line = '#' + line + end - return line - end + return line + end - # Check if a URL exists by issueing a HEAD request. - # @param [String] URL - # @return [Boolean] - def uri_exists(url) + # Check if a URL exists by issueing a HEAD request. + # @param [String] URL + # @return [Boolean] + def uri_exists(url) - u = URI(url) + u = URI(url) - Net::HTTP.start(u.host, u.port) do |http| - http.open_timeout = 1 - http.read_timeout = 1 - res = http.head(u.path) + Net::HTTP.start(u.host, u.port) do |http| + http.open_timeout = 1 + http.read_timeout = 1 + res = http.head(u.path) - if res.code == "200" - return true + if res.code == "200" + return true + end + return false + end end - return false + end end - end diff --git a/tests/AptRepairSourcesTest.rb b/tests/AptRepairSourcesTest.rb index cba76ed..d0bc134 100644 --- a/tests/AptRepairSourcesTest.rb +++ b/tests/AptRepairSourcesTest.rb @@ -1,14 +1,14 @@ $: << File.join(File.dirname(__FILE__), "..", "lib") require 'rubygems' require 'net/http' -require 'AptRepairSources' +require 'apt/repair/sources' require 'test/unit' class AptRepairSourcesTest < Test::Unit::TestCase def test_type l = "deb http://archive.ubuntu.com/ubuntu/ lucid main restricted universe multiverse" - helper = AptRepairSources.new(l) + helper = Apt::Repair::Sources.new(l) assert_equal("deb", helper.get_type) end @@ -16,7 +16,7 @@ def test_type def test_url l = "deb http://archive.ubuntu.com/ubuntu/ lucid main restricted universe multiverse" - helper = AptRepairSources.new(l) + helper = Apt::Repair::Sources.new(l) u = helper.get_url(nil) el = helper.get_el @@ -33,15 +33,15 @@ def test_archive l = "deb http://archive.ubuntu.com/ubuntu/ karmic main universe" e = "deb http://old-releases.ubuntu.com/ubuntu/ karmic main universe" - helper = AptRepairSources.new(l) + helper = Apt::Repair::Sources.new(l) assert_equal(e, helper.fix_line) end - # AptRepairSources::get_url - parameter test + # Apt::Repair::Sources::get_url - parameter test def test_get_url l = "deb-src http://security.ubuntu.com/ubuntu karmic-security main universe" - helper = AptRepairSources.new(l) + helper = Apt::Repair::Sources.new(l) assert_equal("http://security.ubuntu.com/ubuntu/dists/karmic-security/", helper.get_url(nil)) assert_equal("http://old-releases.ubuntu.com/ubuntu/dists/karmic-security/", helper.get_url("http://old-releases.ubuntu.com/ubuntu")) @@ -52,7 +52,7 @@ def test_multiverse_security l = "deb-src http://security.ubuntu.com/ubuntu karmic-security main universe" e = "deb-src http://old-releases.ubuntu.com/ubuntu karmic-security main universe" - helper = AptRepairSources.new(l) + helper = Apt::Repair::Sources.new(l) assert_equal(e, helper.fix_line) end end