mvndownload 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mvndownload.rb +136 -0
  3. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c731f66506bec5b680df5a375c7c75a68557bfb0
4
+ data.tar.gz: f9b07a18fe5cb520e078371c0fa77c15ba6bb09b
5
+ SHA512:
6
+ metadata.gz: 3e2b53d9d1a9d259ee39bacc891db816ecfe0fe8f55fc45e421b8453d11c8b01f80ac3bc298145df00e6eef8a08a6a48a4f4923577c766ce316480c98ba25817
7
+ data.tar.gz: 17706c617b11cd61e2f9db29b2e28ffd41ff00b4ce517552039ed63d2ff6440d68e9df943b37dc3babbbfd406d663e4fa00539eb2883a263d0fb3dfeabb55866
@@ -0,0 +1,136 @@
1
+ require 'net/http'
2
+ require 'fileutils'
3
+ require 'digest/md5'
4
+ require 'sys/filesystem'
5
+
6
+ # Maven download class
7
+ class Mvndownload
8
+ # Perform a download from a maven type repository.
9
+ # Allows for configuration of basic auth credentials and proxy
10
+ # settings. Will also perform pre-emptive free space checks to
11
+ # ensure enough space to complete the download.
12
+ #
13
+ # Example:
14
+ # Mvndownload.download(:source => "http://my.repo.com/repo/myfile.war",
15
+ # :destination => "/path/to/download",
16
+ # :filename => "myfile.war",
17
+ # :username => "myrepouser",
18
+ # :password => "myrepopassword",
19
+ # :proxyhost => "proxy.mynet.net",
20
+ # :proxyport => "8080",
21
+ # :proxyuser => "myproxyuser",
22
+ # :proxypass => "myproxypass")
23
+ #
24
+ # Arguments:
25
+ # source: (Uri)
26
+ # destination: (Path)
27
+ # filename: (String) Optional, defaults to the source filename
28
+ # username: (String) Optional, user for authenticating with source
29
+ # password: (String) Optional, password for authenticating with source
30
+ # proxyhost: (String) Optional, hostname for proxy connection
31
+ # proxyport: (Integer) Optional, port for proxy connection
32
+ # proxyuser: (String) Optional, username for proxy authentication
33
+ # proxypass: (String) Optional, password for proxy authentication
34
+
35
+ def self.md5_check(source, destination, sourcemd5 = false, destinationmd5 = false)
36
+ smd5 = nil
37
+ dmd5 = nil
38
+ if sourcemd5
39
+ File.open(source, 'rb') { |h| smd5 = h.read } ; nil
40
+ else
41
+ smd5 = Digest::MD5.file(source).hexdigest
42
+ end
43
+
44
+ if destinationmd5
45
+ File.open(destination, 'rb') { |h| dmd5 = h.read } ; nil
46
+ else
47
+ dmd5 = Digest::MD5.file(destination).hexdigest
48
+ end
49
+ s5 = ""
50
+ d5 = ""
51
+ smd5.each_byte { |c|
52
+ s5 << c.chr if c==9 || c==10 || c==13 || (c > 31 && c < 127)
53
+ }
54
+ dmd5.each_byte { |c|
55
+ d5 << c.chr if c==9 || c==10 || c==13 || (c > 31 && c < 127)
56
+ }
57
+ s5 = s5.chomp.strip
58
+ d5 = d5.chomp.strip
59
+ return s5 == d5
60
+ end
61
+ private_class_method :md5_check
62
+
63
+ def self.dodownload(source, dest, user, pass, p_addr, p_port, p_user, p_pass)
64
+ rc = nil
65
+ uri = nil
66
+ uri = URI(source)
67
+ req = Net::HTTP::Get.new(uri.request_uri)
68
+ unless user == nil
69
+ req.basic_auth(user, pass)
70
+ end
71
+ begin
72
+ Net::HTTP.new(uri.host, uri.port, p_addr, p_port, p_user, p_pass).start do |http|
73
+ http.use_ssl = uri.scheme == 'https'
74
+ http.request req do |response|
75
+ parent = File.dirname dest
76
+ stat = Sys::Filesystem.stat(parent)
77
+ bytes_available = stat.block_size * stat.blocks_available
78
+
79
+ if ( response.content_length() != nil )
80
+ if ((response['content-length'].to_i *2) > bytes_available)
81
+ raise RuntimeError,"Not enough disk space to download the file to " + parent
82
+ end
83
+ end
84
+
85
+ rc = response.code
86
+ if rc == '200'
87
+ unless File.directory? parent
88
+ FileUtils.rm_f(parent) if File.exist?(parent)
89
+ FileUtils.mkdir_p parent
90
+ end
91
+ open(dest, 'w') do |io|
92
+ response.read_body do |chunk|
93
+ io.write chunk
94
+ end
95
+ end
96
+ else
97
+ puts "Download failed, return code from download: #{rc}"
98
+ end
99
+ end
100
+ end
101
+ rescue => downloadException
102
+ abort "The file #{source} failed to be downloaded due to: " + downloadException.message
103
+ end
104
+ return rc == '200'
105
+ end
106
+ private_class_method :dodownload
107
+
108
+ def self.download( vars )
109
+ getfile = true
110
+ vars.each do |key, val|
111
+ if val == ""
112
+ vars[key] = nil
113
+ end
114
+ end
115
+ if vars[:filename] == nil
116
+ filename = vars[:source][/([^\/]+)$/]
117
+ else
118
+ filename = vars[:filename]
119
+ end
120
+ dest = File.join vars[:destination], filename
121
+ if File.file?(dest)
122
+ if dodownload("#{vars[:source]}.md5", "#{dest}.md5", vars[:username], vars[:password], vars[:proxyhost], vars[:proxyport], vars[:proxyuser], vars[:proxypass])
123
+ getfile = !md5_check("#{dest}.md5", "#{dest}", true, false)
124
+ end
125
+ end
126
+ if getfile
127
+ FileUtils.rm_f dest
128
+ unless dodownload(vars[:source], dest, vars[:username], vars[:password], vars[:proxyhost], vars[:proxyport], vars[:proxyuser], vars[:proxypass])
129
+ abort "Download failed, you may want to check your remote server for issues or the correct file name"
130
+ end
131
+ if dodownload("#{vars[:source]}.md5", "#{dest}.md5", vars[:username], vars[:password], vars[:proxyhost], vars[:proxyport], vars[:proxyuser], vars[:proxypass])
132
+ abort "MD5 check failed, potentially corrupt download" unless md5_check("#{dest}.md5", "#{dest}", true, false)
133
+ end
134
+ end
135
+ end
136
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mvndownload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nigel Foucha
8
+ - Agustin Caraballo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-10-07 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This gem allows you to download a file. However, it will also check
15
+ to see if a MD5 file exists at the source and compare against the destination for
16
+ validation
17
+ email: nigel.foucha@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/mvndownload.rb
23
+ homepage: http://rubygems.org/gems/mvndownload
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.2.2
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Gem to download files from maven type repositories
47
+ test_files: []