Class Raven::GemSearchInstaller
In: lib/raven/search_install.rb
Parent: Object

Methods

Public Class methods

[Source]

    # File lib/raven/search_install.rb, line 39
39:     def initialize(server, port, base_url, proxy_info)
40:       @server, @base_url, @port, @proxy_info = server, base_url, port, proxy_info
41:     end

Public Instance methods

[Source]

     # File lib/raven/search_install.rb, line 190
190:     def filtrate_latest(artifacts)
191:       # Create a hash with all versions of each artifact
192:       arth = {}
193:       artifacts.each do |art| 
194:         key = "#{art.groupId}%#{art.artifactId}"
195:         arth[key] ||= []
196:         arth[key] << Version.new(art.versionId)
197:       end
198:       
199:       arth.keys.collect do |key|
200:         last_ver = arth[key].sort[-1].version
201:         find_artifact(artifacts, *(key.split('%') << last_ver))
202:       end
203:     end

[Source]

     # File lib/raven/search_install.rb, line 205
205:     def find_artifact(artifacts, groupId, artifactId, versionId)
206:       artifacts.select do |art|
207:         art if art.artifactId == artifactId && 
208:                 art.groupId == groupId && art.versionId == versionId
209:       end[0]
210:     end

[Source]

    # File lib/raven/search_install.rb, line 64
64:     def get_indices
65:       require 'stringio'
66:       if @proxy_info
67:         http = Net::HTTP::Proxy(*@proxy_info).new(@server, @port)
68:       else
69:         http = Net::HTTP.new(@server, @port)
70:       end
71:       
72:       list_indices.each do |file|
73:         http_fn, fs_fn = @base_url + file, File.join(RAVEN_HOME, file[0..-4])
74:         
75:         # Checking file size to see if it changed
76:         header = http.head(http_fn)
77:         zipsize = header["content-length"]
78:         oldsize = File.exist?(fs_fn) ? File.new(fs_fn).readline.chomp : '0'
79:         
80:         unless (zipsize.to_i == oldsize.to_i)
81:           puts "Refreshing index file #{file[0..-4]}"
82:           response = StringIO.new(http.get(@base_url + file, nil).body)
83:           zlib = Zlib::GzipReader.new(response)
84:           file = File.new(File.join(RAVEN_HOME, file[0..-4]), 'wb')
85:           file << zipsize + "\n"
86:           file.write(zlib.read)
87:           file.close
88:           zlib.close
89:         end
90:       end
91:     end

[Source]

     # File lib/raven/search_install.rb, line 152
152:     def install(artifactId, groupId, versionId, all=false, all_versions=false)
153:       artifacts = search(artifactId, groupId, versionId).uniq
154:       
155:       if (versionId.nil? && !all_versions)
156:         artifacts = filtrate_latest(artifacts)
157:       end
158:       
159:       # Selected several artifacts but supposed to have only one
160:       if (artifacts.nil?)
161:         puts "Gem indices couldn't be found properly (either you provided a wrong URL or the main site is down)."
162:         return
163:       end
164:       raise GemTooManyInstallError.new(artifacts) if (!all && artifacts.size > 1)
165:       
166:       if (artifacts.empty?)
167:         puts "Nothing found."
168:       else
169:         # TODO install from the servers returned by search
170:         # Proceeding with installation
171:         FileUtils.mkdir('ext') unless File.exist?('ext')
172:         puts "Installing Gems:"
173:         artifacts.each do |artifact|
174:           if @proxy_info
175:             http = Net::HTTP::Proxy(*@proxy_info).new(artifact.server[0], artifact.server[1])
176:           else
177:             http = Net::HTTP.new(artifact.server[0], Integer(artifact.server[1]))
178:           end        
179:           puts "  - #{artifact.to_s}"
180:           gemname = Raven.install_remote_gem(artifact, http, artifact.server[2], true)
181:           params = [false]
182:           params << RAVEN_HOME if defined?(GEMS_IN_HOME)
183:           Gem::Installer.new(gemname).install(*params)      
184:           FileUtils.rm(gemname)
185:         end
186:         FileUtils.rm_rf('ext')
187:       end
188:     end

[Source]

    # File lib/raven/search_install.rb, line 43
43:     def list_indices
44:       indices = []
45:       if @proxy_info
46:         http = Net::HTTP::Proxy(*@proxy_info).new(@server, @port)
47:       else
48:         http = Net::HTTP.new(@server, @port)
49:       end
50:       response = http.get(@base_url, nil)
51:       expr = /href="([^"]*idx.gz)"|HREF="([^"]*idx.gz)"/
52:       if response.message.strip == "OK"
53:         response.body.each_line do |line|
54:           if expr =~ line
55:             indices << expr.match(line)[1]
56:           end
57:         end
58:       else
59:         puts "Got a bad response from server http://#{@server}:#{@port}/#{@base_url}, couldn't update local index"
60:       end
61:       indices
62:     end

[Source]

     # File lib/raven/search_install.rb, line 145
145:     def read_lines(file, size)
146:       lines = []
147:       0.upto(size-1) { |m| lines[m] = file.gets }
148:       return nil if lines[size-1].nil?
149:       lines
150:     end

[Source]

     # File lib/raven/search_install.rb, line 93
 93:     def search(artifactId, groupId=nil, versionId=nil)
 94:       idx_files = Dir[File.join(RAVEN_HOME, '*.mvnidx')]
 95:       found = []
 96:       idx_files.each do |idx_file|
 97:         puts "Searching in #{idx_file}..."
 98:         f = File.new(idx_file)
 99:         found.concat(search_index(f, artifactId, groupId, versionId))
100:       end
101:       found
102:     end

[Source]

     # File lib/raven/search_install.rb, line 104
104:     def search_index(idx_file, artifactId, groupId, versionId)
105:       # Removing gz file size header
106:       idx_file.gets
107:       # Getting server name and gz file size header
108:       server_addr = idx_file.gets.chomp.split("#")
109:       aid_line = /artifactId: (.*#{artifactId}.*)/
110:       aidx_line = /artifactId: (#{artifactId})$/
111:       gid_line = /groupId: (.*#{groupId}.*)/
112:       vid_line = /versionId: "?(#{versionId})"?/
113: 
114:       # a, a+v, a+g, g, v+g, a+v+g
115:       found = []
116:       exact_match = false
117:       while (lines = read_lines(idx_file, 5))
118:         ma, mg, mv = false, false, false
119:         ma = lines[2].strip =~ aid_line if artifactId
120:         mg = lines[1].strip =~ gid_line if groupId
121:         mv = lines[3].strip =~ vid_line if versionId
122:         if ((ma && groupId.nil? && versionId.nil?) ||
123:             (ma && mg && versionId.nil?) ||
124:             (ma && mv && groupId.nil?) ||
125:             (ma && mg && mv) ||
126:             (mg && mv && artifactId.nil?) ||
127:             (mg && artifactId.nil? && versionId.nil?))
128:           artifact = YAML.load(lines.join("\n"))
129:           artifact.server = server_addr
130:           # We have an exact match, the rules change, only other exact matches 
131:           # get selected
132:           if lines[2].strip =~ aidx_line
133:             unless exact_match
134:              exact_match = true
135:              found = []
136:             end
137:             found << artifact
138:           end           
139:           found << artifact unless exact_match
140:         end
141:       end
142:       found
143:     end

[Validate]