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

.d(msg) ⇒ Object



32
33
34
35
36
# File 'lib/pmirror.rb', line 32

def self.d(msg)
  if options[:debug]
    puts "[DEBUG]: #{msg}"
  end
end

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pmirror.rb', line 60

def self.download_files(local_dir, url_hash={})
  d "Inside download_files"
  url_hash.each_key do |single_url|
    d "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!"
        exit 1
      end

      remote_fn = "#{single_url}/#{file}"
      unless File.exist?(local_fn)
        puts "Downloading File: #{file}"
        puts "#{remote_fn} ==> #{local_fn}"
        http_to_file(local_fn, remote_fn)
        # File.write(local_fn, open(remote_fn).read)
        puts "Download Complete for #{file}"
      else
        puts "Skipping #{file}, already exists"
      end
    end
  end
end

.execute(cmd) ⇒ Object



106
107
108
109
110
# File 'lib/pmirror.rb', line 106

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

.get_download_list(url_list, pattern) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pmirror.rb', line 38

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

.http_to_file(filename, url) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pmirror.rb', line 88

def self.http_to_file(filename,url)
  d "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
  puts
end

.update_repodata(local_dir) ⇒ Object



112
113
114
115
# File 'lib/pmirror.rb', line 112

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