apt-spy2 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .vagrant
2
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ apt-spy2 (0.2.1)
5
+ colored
6
+ thor
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ colored (1.2)
12
+ rake (10.1.0)
13
+ thor (0.18.1)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ apt-spy2!
20
+ bundler (~> 1.3)
21
+ rake
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # apt-spy2 or, "apt-spy for ubuntu"
2
+
3
+
4
+ ## setup
5
+
6
+ ```
7
+ gem install bundler
8
+ bundle install --dev
9
+ ```
10
+
11
+ or:
12
+
13
+ ```
14
+ gem install apt-spy2
15
+ ```
16
+
17
+ ## usage
18
+
19
+ ```
20
+ $ apt-spy2 [21:03:52]
21
+ apt-spy2 commands:
22
+ apt-spy2 check # Evaluate mirrors
23
+ apt-spy2 fix # Set the closest/fastest mirror
24
+ apt-spy2 help [COMMAND] # Describe available commands or one specific command
25
+ apt-spy2 list # List the currently available mirrors
26
+ ```
27
+
28
+ ### list
29
+
30
+ Displays a list of currently available mirrors. These mirrors are automatically selected via
31
+ [ubuntu-mirrors](http://mirrors.ubuntu.com) using your IP's location.
32
+
33
+ ### check
34
+
35
+ `check` works like `list`, but also determines if the servers returned are working.
36
+
37
+ ### fix
38
+
39
+ `fix` applies the result of `check` and updates `/etc/apt/sources.list`.
40
+
41
+ Please note: Depending on the context, it may require sudo.
42
+
43
+ ### options/switches
44
+
45
+ See `apt-spy2 help list|check|fix` for available options.
46
+
47
+ ### exit codes
48
+
49
+ * 0 - all went well
50
+ * 1 - some kind of error
51
+
52
+ ### output and non-interactiveness
53
+
54
+ See `apt-spy2 help COMMAND` for more information.
55
+
56
+ Generally, this piece of code plays especially nice in a non-interactive environment and won't ask for anything.
57
+
58
+ ## License
59
+
60
+ [New BSD License](http://opensource.org/licenses/BSD-2-Clause)
61
+
62
+ ## Contributions
63
+
64
+ Contributions are welcome!
65
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/Vagrantfile ADDED
@@ -0,0 +1,11 @@
1
+ Vagrant::Config.run do |config|
2
+
3
+ config.vm.box = "precise64"
4
+ config.vm.box_url = "http://dl.dropbox.com/u/1537815/precise64.box"
5
+
6
+ config.vm.customize [
7
+ "modifyvm", :id,
8
+ "--name", "apt-spy2 testbox",
9
+ "--memory", "128"
10
+ ]
11
+ end
data/apt-spy2/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in apt-spy2.gemspec
4
+ gemspec
@@ -0,0 +1,7 @@
1
+ require "apt/spy2/version"
2
+
3
+ module Apt
4
+ module Spy2
5
+ # Your code goes here...
6
+ end
7
+ end
data/apt-spy2.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apt/spy2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "apt-spy2"
8
+ spec.version = Apt::Spy2::VERSION
9
+ spec.authors = ["till"]
10
+ spec.email = ["till@php.net"]
11
+ spec.description = "Keep your /etc/apt/sources.list up to date"
12
+ spec.summary = "apt-spy2, or apt-spy for ubuntu"
13
+ spec.homepage = "https://github.com/lagged/apt-spy2"
14
+ spec.license = "BSD"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'thor'
22
+ spec.add_dependency 'colored'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
data/bin/apt-spy2 ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+
6
+
7
+ require 'thor'
8
+ require 'open-uri'
9
+ require 'colored'
10
+ require 'fileutils'
11
+ require 'apt/spy2'
12
+
13
+ AptSpy2.start
@@ -0,0 +1,5 @@
1
+ module Apt
2
+ module Spy2
3
+ VERSION = "0.2.1"
4
+ end
5
+ end
data/lib/apt/spy2.rb ADDED
@@ -0,0 +1,106 @@
1
+ class AptSpy2 < Thor
2
+ package_name "apt-spy2"
3
+
4
+ desc "fix", "Set the closest/fastest mirror"
5
+ option :country, :default => "mirrors"
6
+ option :commit, :type => :boolean
7
+ def fix
8
+ working = filter(retrieve(), false)
9
+ print "The closest mirror is: "
10
+ puts "#{working[0]}".white_on_green
11
+ if !options[:commit]
12
+ puts "Run with --commit to adjust /etc/apt/sources.list".yellow
13
+ else
14
+ puts "Updating /etc/apt/sources.list".yellow
15
+ update(working[0])
16
+ end
17
+ end
18
+
19
+ desc "check", "Evaluate mirrors"
20
+ option :country, :default => "mirrors"
21
+ option :output, :type => :boolean, :default => true
22
+ def check
23
+ mirrors = retrieve(options[:country])
24
+ return filter(mirrors, options[:output])
25
+ end
26
+
27
+ desc "list", "List the currently available mirrors"
28
+ option :country, :default => "mirrors"
29
+ def list
30
+ mirrors = retrieve(options[:country])
31
+ puts mirrors
32
+ end
33
+
34
+ private
35
+ def retrieve(country = "mirrors")
36
+ begin
37
+ country.upcase! if country.length == 2
38
+ mirrors = open("http://mirrors.ubuntu.com/#{country}.txt") do |list|
39
+ list.read
40
+ end
41
+ rescue OpenURI::HTTPError => the_error
42
+ case the_error.io.status[0]
43
+ when "404"
44
+ puts "The country code '#{country}' is incorrect.".red
45
+ exit 1
46
+ else
47
+ puts "Status: #{the_error.io.status[0]}".red
48
+ exit 1
49
+ end
50
+ end
51
+
52
+ mirrors = mirrors.split(/\n/)
53
+ return mirrors
54
+ end
55
+
56
+ private
57
+ def filter(mirrors, output = true)
58
+ # f me :)
59
+
60
+ working_mirrors = []
61
+
62
+ mirrors.each do |mirror|
63
+ print "Mirror: #{mirror} - " if output
64
+ begin
65
+ mirror_status = open(mirror)
66
+ puts "UP".green if output
67
+ working_mirrors << mirror
68
+ rescue OpenURI::HTTPError => the_error
69
+ puts "BROKEN: #{the_error.io.status[0]}".yellow_on_red if output
70
+ rescue Errno::ECONNREFUSED
71
+ puts "DOWN".red if output
72
+ end
73
+ end
74
+
75
+ return working_mirrors
76
+
77
+ end
78
+
79
+ private
80
+ def update(mirror)
81
+
82
+ t = Time.now
83
+ r = `lsb_release -c`.split(" ")[1]
84
+ sources = "## Updated on #{t.to_s}\n"
85
+ sources << "deb #{mirror} #{r} main restricted universe multiverse\n"
86
+ sources << "deb #{mirror} #{r}-updates main restricted universe multiverse\n"
87
+ sources << "deb #{mirror} #{r}-backports main restricted universe multiverse\n"
88
+ sources << "deb #{mirror} #{r}-security main restricted universe multiverse\n"
89
+
90
+ apt_sources = '/etc/apt/sources.list'
91
+
92
+ begin
93
+ File.rename apt_sources, "#{apt_sources}.#{t.to_i}"
94
+ File.open(apt_sources, 'w') do |f|
95
+ f.write(sources)
96
+ end
97
+ rescue
98
+ puts "Failed updating #{apt_sources}!".red_on_white
99
+ puts "You probably need sudo!".red
100
+ exit 1
101
+ end
102
+
103
+ puts "Updated '#{apt_sources}' with #{mirror}".green
104
+ puts "Run `apt-get update` to update".red_on_yellow
105
+ end
106
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apt-spy2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - till
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: colored
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Keep your /etc/apt/sources.list up to date
79
+ email:
80
+ - till@php.net
81
+ executables:
82
+ - apt-spy2
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - Gemfile.lock
89
+ - README.md
90
+ - Rakefile
91
+ - Vagrantfile
92
+ - apt-spy2.gemspec
93
+ - apt-spy2/Gemfile
94
+ - apt-spy2/lib/apt/spy2.rb
95
+ - bin/apt-spy2
96
+ - lib/apt/spy2.rb
97
+ - lib/apt/spy2/version.rb
98
+ homepage: https://github.com/lagged/apt-spy2
99
+ licenses:
100
+ - BSD
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.23
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: apt-spy2, or apt-spy for ubuntu
123
+ test_files: []