Class: Pmirror::Pmirror

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging, Methadone::Main, Methadone::SH
Defined in:
lib/pmirror.rb

Class Method Summary collapse

Class Method Details

.download_files(local_dir, url_hash = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pmirror.rb', line 79

def self.download_files(local_dir, url_hash={})
  debug "Inside download_files"
  url_hash.each_key do |single_url|
    debug "Working on #{single_url}"
    url_hash[single_url].each do |file|
      local_fn = "#{local_dir}/#{file}"

      unless Dir.exist? options[:localdir]
        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)
        info "Downloading File: #{file}"
        info "#{remote_fn} ==> #{local_fn}"
        http_to_file(local_fn, remote_fn)
        # File.write(local_fn, open(remote_fn).read)
        info "Download Complete for #{file}"
      else
        info "Skipping #{file}, already exists"
      end
    end
  end
end

.execute(cmd) ⇒ Object



124
125
126
127
128
# File 'lib/pmirror.rb', line 124

def self.execute(cmd)
  debug "Inside execute"
  info "Executing: #{cmd}"
  sh("cd #{options[:localdir]} && #{cmd}")
end

.get_download_list(url_list, pattern) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pmirror.rb', line 57

def self.get_download_list(url_list, pattern)
  debug "inside get_download_list"
  downloads = {}
  url_list.each do |single_url|
    downloads[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)
          debug "Found match: #{file_name}"
          downloads[single_url] << file_name
        end
      end
    end
    debug "Returning downloads: #{downloads.inspect}"
  end
  downloads
end

.http_to_file(filename, url) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pmirror.rb', line 106

def self.http_to_file(filename,url)
  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
        pbar = ProgressBar.new("=", t)
        pbar.file_transfer_mode
      end
    },
    :progress_proc => lambda {|s|
      pbar.set s if pbar
    }) {|f| save_file.write(f.read) }
  end
  info ""
end

.parse_config(config_file) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/pmirror.rb', line 46

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

.update_repodata(local_dir) ⇒ Object



130
131
132
133
# File 'lib/pmirror.rb', line 130

def self.update_repodata(local_dir)
  puts "Running createrepo for dir '#{local_dir}'"
  sh("createrepo -c /#{local_dir}/.cache -d #{local_dir}")
end