Skip to content

Commit

Permalink
file-name-convention (or something like that)
Browse files Browse the repository at this point in the history
  • Loading branch information
till committed Nov 23, 2011
1 parent 309961f commit 6f4bfb4
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 123 deletions.
14 changes: 7 additions & 7 deletions bin/apt-repair-sources
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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
Expand Down
222 changes: 113 additions & 109 deletions lib/apt/repair/sources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions tests/AptRepairSourcesTest.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
$: << 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

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
Expand All @@ -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"))
Expand All @@ -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

0 comments on commit 6f4bfb4

Please sign in to comment.