lib/pmirror.rb in pmirror-0.0.4 vs lib/pmirror.rb in pmirror-0.1.1
- old
+ new
@@ -9,16 +9,23 @@
include Methadone::CLILogging
include Methadone::SH
version(::Pmirror::VERSION)
main do
- d "Inside main"
+ debug "Inside main"
+ parse_config(options[:config]) if options[:config]
+ normalize_defaults
+
if options[:url] && options[:pattern] && options[:localdir]
download_list = get_download_list(options[:url], options[:pattern])
- d "download_list: #{download_list.inspect}"
- download_files(options[:localdir], download_list)
+ debug "download_list: #{download_list.inspect}"
+ if download_list
+ download_files(options[:localdir], download_list)
+ else
+ info "No files to download"
+ end
execute(options[:exec]) if options[:exec]
else
help_now!("Missing arguments")
end
@@ -29,71 +36,77 @@
on("-p", "--pattern PAT,PAT", Array,
"Regex to match files in remote dir, may specify multiple patterns"
)
on("-l", "--localdir DIR", "Local directory to mirror files to")
on("-e", "--exec CMD", "Execute command after completion")
- on("-d", "--debug", "Enable debugging")
on("-u", "--url URL,URL", Array, "Url or remote site")
+ on("-c", "--config FILE", "Config file (yaml) to use instead of command line options")
- def self.d(msg)
- if options[:debug]
- puts "[DEBUG]: #{msg}"
+ use_log_level_option
+
+ def self.parse_config(config_file)
+ debug "In parse_config"
+ parsed = YAML::load_file(config_file)
+ if parsed.kind_of? Hash
+ parsed.each do |option,value|
+ debug "Storing option '#{option}' with value '#{value.inspect}'"
+ options[option] = value
+ end
end
end
def self.get_download_list(url_list, pattern)
- d "inside get_download_list"
+ debug "inside get_download_list"
downloads = {}
url_list.each do |single_url|
downloads[single_url] = []
- d "Getting download list for url: #{single_url}"
+ info "Getting download list for url: #{single_url}"
page = Nokogiri::HTML(open(single_url))
page.css("a").each do |link|
file_name = link.attributes['href'].value
pattern.each do |matcher|
if /#{matcher}/.match(file_name)
- d "Found match: #{file_name}"
+ debug "Found match: #{file_name}"
downloads[single_url] << file_name
end
end
end
- d "Returning downloads: #{downloads.inspect}"
+ debug "Returning downloads: #{downloads.inspect}"
end
downloads
end
def self.download_files(local_dir, url_hash={})
- d "Inside download_files"
+ debug "Inside download_files"
url_hash.each_key do |single_url|
- d "Working on #{single_url}"
+ debug "Working on #{single_url}"
url_hash[single_url].each do |file|
local_fn = "#{local_dir}/#{file}"
unless Dir.exist? options[:localdir]
- d "PWD: #{Dir.pwd}"
- puts Dir.open(Dir.pwd).read
- puts "Destination directory '#{options[:localdir]}' does not exist!"
+ debug "PWD: #{Dir.pwd}"
+ info "Destination directory '#{options[:localdir]}' does not exist!"
exit 1
end
remote_fn = "#{single_url}/#{file}"
unless File.exist?(local_fn)
- puts "Downloading File: #{file}"
- puts "#{remote_fn} ==> #{local_fn}"
+ info "Downloading File: #{file}"
+ info "#{remote_fn} ==> #{local_fn}"
http_to_file(local_fn, remote_fn)
# File.write(local_fn, open(remote_fn).read)
- puts "Download Complete for #{file}"
+ info "Download Complete for #{file}"
else
- puts "Skipping #{file}, already exists"
+ info "Skipping #{file}, already exists"
end
end
end
end
def self.http_to_file(filename,url)
- d "Inside http_to_file"
+ debug "Inside http_to_file"
pbar = nil
File.open(filename, 'wb') do |save_file|
open(url, 'rb',
:content_length_proc => lambda {|t|
if t && 0 < t
@@ -103,15 +116,15 @@
},
:progress_proc => lambda {|s|
pbar.set s if pbar
}) {|f| save_file.write(f.read) }
end
- puts
+ info ""
end
def self.execute(cmd)
- d "Inside execute"
- puts "Executing: #{cmd}"
+ debug "Inside execute"
+ info "Executing: #{cmd}"
sh("cd #{options[:localdir]} && #{cmd}")
end
def self.update_repodata(local_dir)
puts "Running createrepo for dir '#{local_dir}'"