Top Level Namespace

Defined Under Namespace

Modules: MergeObservable Classes: Array, MDArray, NegRange, PackageManager, Range, Renjin

Constant Summary collapse

R =

Create a new R interpreter

Renjin.new
NA =

Add some constants to the R interpreter

R.eval("NA")
NaN =
R.eval("NaN")
Inf =
R.eval("Inf")
MInf =
R.eval("-Inf")
NULL =
R.direct_eval("NULL")

Instance Method Summary collapse

Instance Method Details

#create_directory(dirname) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/JRubyR/download_url_to_file.rb', line 17

def create_directory(dirname)
  unless Dir.exists?(dirname)
    Dir.mkdir(dirname)
  else
    puts "Skipping creating directory " + dirname + ". It already exists."
  end
end

#download_resource(resource, filename) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/JRubyR/download_url_to_file.rb', line 37

def download_resource(resource, filename)
  uri = URI.parse(resource)
  case uri.scheme.downcase
  when /http|https/
    http_download_uri(uri, filename)
  when /ftp/
    ftp_download_uri(uri, filename)
  else
    puts "Unsupported URI scheme for resource " + resource + "."
  end
end

#download_resources(pairs) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/JRubyR/download_url_to_file.rb', line 89

def download_resources(pairs)
  pairs.each do |pair|
    filename = pair[:filename].to_s
    resource = pair[:resource].to_s
    unless File.exists?(filename)
      download_resource(resource, filename)
    else
      puts "Skipping download for " + filename + ". It already exists."
    end
  end
end

#ftp_download_uri(uri, filename) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/JRubyR/download_url_to_file.rb', line 72

def ftp_download_uri(uri, filename)
  puts "Starting FTP download for: " + uri.to_s + "."
  dirname = File.dirname(uri.path)
  basename = File.basename(uri.path)
  begin
    Net::FTP.open(uri.host) do |ftp|
      ftp.
      ftp.chdir(dirname)
      ftp.getbinaryfile(basename)
    end
  rescue Exception => e
    puts "=> Exception: '#{e}'. Skipping download."
    return
  end
  puts "Stored download as " + filename + "."
end

#http_download_uri(uri, filename) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/JRubyR/download_url_to_file.rb', line 49

def http_download_uri(uri, filename)
  puts "Starting HTTP download for: " + uri.to_s
  http_object = Net::HTTP.new(uri.host, uri.port)
  http_object.use_ssl = true if uri.scheme == 'https'
  begin
    http_object.start do |http|
      request = Net::HTTP::Get.new uri.request_uri
      http.read_timeout = 500
      http.request request do |response|
        open filename, 'w' do |io|
          response.read_body do |chunk|
            io.write chunk
          end
        end
      end
    end
  rescue Exception => e
    puts "=> Exception: '#{e}'. Skipping download."
    return
  end
  puts "Stored download as " + filename + "."
end

#mainObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/JRubyR/download_url_to_file.rb', line 102

def main
  sources_file = ARGV.first
  uris = read_uris_from_file(sources_file)
 
  target_dir_name = Date.today.strftime('%y%m%d')
  create_directory(target_dir_name)
  Dir.chdir(target_dir_name)
  puts "Changed directory: " + Dir.pwd
 
  download_resources(uris)
end

#read_uris_from_file(file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/JRubyR/download_url_to_file.rb', line 25

def read_uris_from_file(file)
  uris = Array.new
  File.open(file).each do |line|
    line = line.strip
    next if line == nil || line.length == 0
    parts = line.split(' ')
    pair = Hash[ [:resource, :filename].zip(parts) ]
    uris.push(pair)
  end
  uris
end