Last active
December 14, 2015 21:08
-
-
Save KellyLSB/5148543 to your computer and use it in GitHub Desktop.
Allows you to include or exclude different repositories and components from your apt-mirror run.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'getoptlong' | |
# Class Extensions | |
class String | |
def first | |
self[0..0] | |
end | |
def last | |
self[-1..-1] | |
end | |
def get(num) | |
self[num..num] | |
end | |
end | |
class GetoptLong | |
def to_h | |
result = {} | |
self.each do |o, a| | |
result[o] = a | |
end | |
result | |
end | |
end | |
# Get Options from CLI | |
opts = GetoptLong.new( | |
[ '--include', '-i', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--exclude', '-e', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--components', '-c', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--exclude-components', '-z', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--apt-mirror', '-m', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--help', '-h', GetoptLong::NO_ARGUMENT ] | |
).to_h | |
if opts['--help'] | |
help = <<-HELP | |
Usage: #{__FILE__} [opts] [/etc/apt/mirror.list] | |
Options: | |
-i / --include | |
Which repositories to include in the update. | |
-e / --exclude | |
Which repositories to exclude in the update. | |
(Overrides items in --include) | |
-c / --components | |
Which components to include in the update. | |
-z / --exclude-components | |
Which components to exclude in the update. | |
(Overrides items in --components) | |
-m / --apt-mirror | |
Specifies where the location of the apt-mirror executable is. | |
-h / --help | |
Shows this help document | |
Usage Example: | |
#{__FILE__} -i http://archive.ubuntu.com/ubuntu | |
This will disable everything but the ubuntu repositories. | |
#{__FILE__} -c precise | |
This will disable everything except repositories tracking the "precise" component. | |
#{__FILE__} -e http://archive.ubuntu.com/ubuntu -c main | |
This will update everything except the official ubuntu mirror as long as it includes the "main" component. | |
HELP | |
puts help.gsub(/^\t/, '') | |
exit | |
end | |
# Remove any excludes from the include list | |
if opts['--include'] && opts['--exclude'] | |
i = opts['--include'].split(',') | |
e = opts['--exclude'].split(',') | |
opts['--include'] = i - e | |
opts['--exclude'] = e | |
elsif opts['--include'] | |
i = opts['--include'].split(',') | |
opts['--include'] = i | |
elsif opts['--exclude'] | |
e = opts['--exclude'].split(',') | |
opts['--exclude'] = e | |
end | |
# Handle Component Splitting | |
if opts['--components'] && opts['--exclude-components'] | |
i = opts['--components'].split(',') | |
e = opts['--exclude-components'].split(',') | |
opts['--components'] = i - e | |
opts['--exclude-components'] = e | |
elsif opts['--components'] | |
i = opts['--components'].split(',') | |
opts['--components'] = i | |
elsif opts['--exclude-components'] | |
e = opts['--exclude-components'].split(',') | |
opts['--exclude-components'] = e | |
end | |
# By default be an array | |
opts['--include'] ||= Array.new | |
opts['--exclude'] ||= Array.new | |
opts['--components'] ||= Array.new | |
opts['--exclude-components'] ||= Array.new | |
mirror_list = File.read(ARGV.shift || '/etc/apt/mirror.list').split("\n") | |
mirror_list.collect! do |line| | |
# next if line.first == '#' | |
# => This is implied | |
unless line.match(/^(deb|deb-src|clean)/).nil? | |
line_a = line.split(' ')[1..-1] | |
source = line_a.shift | |
# Handle Include Exclude Splitting | |
if !opts['--include'].empty? | |
if !opts['--include'].include?(source) | |
line = '#tmp# ' + line | |
end | |
elsif !opts['--exclude'].empty? | |
if opts['--exclude'].include?(source) | |
line = '#tmp# ' + line | |
end | |
end | |
# Handle component splitting | |
if line.match(/^#tmp#/).nil? | |
if !opts['--components'].empty? | |
if (opts['--components'] & line_a).count < 1 | |
line = '#tmp# ' + line | |
end | |
elsif !opts['--exclude-components'].empty? | |
if (opts['--exclude-components'] & line_a).count > 0 | |
line = '#tmp# ' + line | |
end | |
end | |
end | |
end | |
line | |
end | |
# Save the generated file | |
File.open(file = "/tmp/mirror_#{Time.now.to_i}.list", 'w+') do |f| | |
f.write(mirror_list.join("\n")) | |
end | |
# Actual run and deletion of tmp config | |
opts['--apt-mirror'] ||= 'apt-mirror' | |
exec(%{#{opts['--apt-mirror']} #{file} && rm #{file}}) | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment