Class: Gem::Indexer
- Inherits:
-
Object
- Object
- Gem::Indexer
- Includes:
- UserInteraction
- Defined in:
- lib/rubygems/indexer.rb
Overview
Top level class for building the gem repository index.
Instance Attribute Summary collapse
-
#build_legacy ⇒ Object
Build indexes for RubyGems older than 1.2.0 when true.
-
#build_modern ⇒ Object
Build indexes for RubyGems 1.2.0 and newer when true.
-
#dest_directory ⇒ Object
readonly
Index install location.
-
#dest_latest_specs_index ⇒ Object
readonly
Latest specs index install location.
-
#dest_prerelease_specs_index ⇒ Object
readonly
Prerelease specs index install location.
-
#dest_specs_index ⇒ Object
readonly
Specs index install location.
-
#directory ⇒ Object
readonly
Index build directory.
Instance Method Summary collapse
-
#abbreviate(spec) ⇒ Object
Abbreviate the spec for downloading.
-
#build_indicies ⇒ Object
Build various indicies.
-
#build_legacy_indicies ⇒ Object
Builds indicies for RubyGems older than 1.2.x.
-
#build_marshal_gemspecs ⇒ Object
Builds Marshal quick index gemspecs.
-
#build_modern_index(index, file, name) ⇒ Object
Build a single index for RubyGems 1.2 and newer.
-
#build_modern_indicies ⇒ Object
Builds indicies for RubyGems 1.2 and newer.
-
#build_rss ⇒ Object
Builds an RSS feed for past two days gem releases according to the gem’s date.
-
#collect_specs(gems = gem_file_list) ⇒ Object
Collect specifications from .gem files from the gem directory.
-
#compact_specs(specs) ⇒ Object
Compacts Marshal output for the specs index data source by using identical objects as much as possible.
-
#compress(filename, extension) ⇒ Object
Compress
filename
withextension
. -
#compress_indicies ⇒ Object
Compresses indicies on disk – All future files should be compressed using gzip, not deflate.
-
#gem_file_list ⇒ Object
List of gem file names to index.
-
#generate_index ⇒ Object
Builds and installs indicies.
-
#gzip(filename) ⇒ Object
Zlib::GzipWriter wrapper that gzips
filename
on disk. -
#initialize(directory, options = {}) ⇒ Indexer
constructor
Create an indexer that will index the gems in
directory
. -
#install_indicies ⇒ Object
Install generated indicies into the destination directory.
-
#make_temp_directories ⇒ Object
Make directories for index generation.
- #map_gems_to_specs(gems) ⇒ Object
-
#paranoid(path, extension) ⇒ Object
Ensure
path
and path withextension
are identical. -
#sanitize(spec) ⇒ Object
Sanitize the descriptive fields in the spec.
-
#sanitize_string(string) ⇒ Object
Sanitize a single string.
-
#update_index ⇒ Object
Perform an in-place update of the repository from newly added gems.
-
#update_specs_index(index, source, dest) ⇒ Object
Combines specs in
index
andsource
then writes out a new copy todest
.
Methods included from UserInteraction
#alert, #alert_error, #alert_warning, #ask, #ask_for_password, #ask_yes_no, #choose_from_list, #say, #terminate_interaction
Methods included from DefaultUserInteraction
ui, #ui, ui=, #ui=, use_ui, #use_ui
Constructor Details
#initialize(directory, options = {}) ⇒ Indexer
Create an indexer that will index the gems in directory
.
56 57 58 59 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rubygems/indexer.rb', line 56 def initialize(directory, = {}) require 'fileutils' require 'tmpdir' require 'zlib' unless defined?(Builder::XChar) then raise "Gem::Indexer requires that the XML Builder library be installed:" \ "\n\tgem install builder" end = { :build_legacy => true, :build_modern => true }.merge @build_legacy = [:build_legacy] @build_modern = [:build_modern] @rss_title = [:rss_title] @rss_host = [:rss_host] @rss_gems_host = [:rss_gems_host] @dest_directory = directory @directory = File.join(Dir.tmpdir, "gem_generate_index_#{$$}") marshal_name = "Marshal.#{Gem.marshal_version}" @master_index = File.join @directory, 'yaml' @marshal_index = File.join @directory, marshal_name @quick_dir = File.join @directory, 'quick' @quick_marshal_dir = File.join @quick_dir, marshal_name @quick_marshal_dir_base = File.join "quick", marshal_name # FIX: UGH @quick_index = File.join @quick_dir, 'index' @latest_index = File.join @quick_dir, 'latest_index' @specs_index = File.join @directory, "specs.#{Gem.marshal_version}" @latest_specs_index = File.join(@directory, "latest_specs.#{Gem.marshal_version}") @prerelease_specs_index = File.join(@directory, "prerelease_specs.#{Gem.marshal_version}") @dest_specs_index = File.join(@dest_directory, "specs.#{Gem.marshal_version}") @dest_latest_specs_index = File.join(@dest_directory, "latest_specs.#{Gem.marshal_version}") @dest_prerelease_specs_index = File.join(@dest_directory, "prerelease_specs.#{Gem.marshal_version}") @rss_index = File.join @directory, 'index.rss' @files = [] end |
Instance Attribute Details
#build_legacy ⇒ Object
Build indexes for RubyGems older than 1.2.0 when true
21 22 23 |
# File 'lib/rubygems/indexer.rb', line 21 def build_legacy @build_legacy end |
#build_modern ⇒ Object
Build indexes for RubyGems 1.2.0 and newer when true
26 27 28 |
# File 'lib/rubygems/indexer.rb', line 26 def build_modern @build_modern end |
#dest_directory ⇒ Object (readonly)
Index install location
31 32 33 |
# File 'lib/rubygems/indexer.rb', line 31 def dest_directory @dest_directory end |
#dest_latest_specs_index ⇒ Object (readonly)
Latest specs index install location
41 42 43 |
# File 'lib/rubygems/indexer.rb', line 41 def dest_latest_specs_index @dest_latest_specs_index end |
#dest_prerelease_specs_index ⇒ Object (readonly)
Prerelease specs index install location
46 47 48 |
# File 'lib/rubygems/indexer.rb', line 46 def dest_prerelease_specs_index @dest_prerelease_specs_index end |
#dest_specs_index ⇒ Object (readonly)
Specs index install location
36 37 38 |
# File 'lib/rubygems/indexer.rb', line 36 def dest_specs_index @dest_specs_index end |
#directory ⇒ Object (readonly)
Index build directory
51 52 53 |
# File 'lib/rubygems/indexer.rb', line 51 def directory @directory end |
Instance Method Details
#abbreviate(spec) ⇒ Object
Abbreviate the spec for downloading. Abbreviated specs are only used for searching, downloading and related activities and do not need deployment specific information (e.g. list of files). So we abbreviate the spec, making it much smaller for quicker downloads.
113 114 115 116 117 118 119 120 |
# File 'lib/rubygems/indexer.rb', line 113 def abbreviate(spec) spec.files = [] spec.test_files = [] spec. = [] spec.extra_rdoc_files = [] spec.cert_chain = [] spec end |
#build_indicies ⇒ Object
Build various indicies
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rubygems/indexer.rb', line 125 def build_indicies # Marshal gemspecs are used by both modern and legacy RubyGems Gem::Specification.dirs = [] Gem::Specification.add_specs(*map_gems_to_specs(gem_file_list)) build_marshal_gemspecs build_legacy_indicies if @build_legacy build_modern_indicies if @build_modern build_rss compress_indicies end |
#build_legacy_indicies ⇒ Object
Builds indicies for RubyGems older than 1.2.x
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/rubygems/indexer.rb', line 142 def build_legacy_indicies index = collect_specs say "Generating Marshal master index" Gem.time 'Generated Marshal master index' do open @marshal_index, 'wb' do |io| io.write index.dump end end @files << @marshal_index @files << "#{@marshal_index}.Z" end |
#build_marshal_gemspecs ⇒ Object
Builds Marshal quick index gemspecs.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rubygems/indexer.rb', line 160 def build_marshal_gemspecs count = Gem::Specification.count progress = ui.progress_reporter count, "Generating Marshal quick index gemspecs for #{count} gems", "Complete" files = [] Gem.time 'Generated Marshal quick index gemspecs' do Gem::Specification.each do |spec| spec_file_name = "#{spec.original_name}.gemspec.rz" marshal_name = File.join @quick_marshal_dir, spec_file_name marshal_zipped = Gem.deflate Marshal.dump(spec) open marshal_name, 'wb' do |io| io.write marshal_zipped end files << marshal_name progress.updated spec.original_name end progress.done end @files << @quick_marshal_dir files end |
#build_modern_index(index, file, name) ⇒ Object
Build a single index for RubyGems 1.2 and newer
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/rubygems/indexer.rb', line 192 def build_modern_index(index, file, name) say "Generating #{name} index" Gem.time "Generated #{name} index" do open(file, 'wb') do |io| specs = index.map do |*spec| # We have to splat here because latest_specs is an array, while the # others are hashes. spec = spec.flatten.last platform = spec.original_platform # win32-api-1.0.4-x86-mswin32-60 unless String === platform then alert_warning "Skipping invalid platform in gem: #{spec.full_name}" next end platform = Gem::Platform::RUBY if platform.nil? or platform.empty? [spec.name, spec.version, platform] end specs = compact_specs(specs) Marshal.dump(specs, io) end end end |
#build_modern_indicies ⇒ Object
Builds indicies for RubyGems 1.2 and newer. Handles full, latest, prerelease
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/rubygems/indexer.rb', line 222 def build_modern_indicies prerelease, released = Gem::Specification.partition { |s| s.version.prerelease? } latest_specs = Gem::Specification.latest_specs build_modern_index(released.sort, @specs_index, 'specs') build_modern_index(latest_specs.sort, @latest_specs_index, 'latest specs') build_modern_index(prerelease.sort, @prerelease_specs_index, 'prerelease specs') @files += [@specs_index, "#{@specs_index}.gz", @latest_specs_index, "#{@latest_specs_index}.gz", @prerelease_specs_index, "#{@prerelease_specs_index}.gz"] end |
#build_rss ⇒ Object
Builds an RSS feed for past two days gem releases according to the gem’s date.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rubygems/indexer.rb', line 245 def build_rss if @rss_host.nil? or @rss_gems_host.nil? then if Gem.configuration.really_verbose then alert_warning "no --rss-host or --rss-gems-host, RSS generation disabled" end return end require 'cgi' require 'rubygems/text' extend Gem::Text Gem.time 'Generated rss' do open @rss_index, 'wb' do |io| rss_host = CGI.escapeHTML @rss_host rss_title = CGI.escapeHTML(@rss_title || 'gems') io.puts <<-HEADER <?xml version="1.0"?> <rss version="2.0"> <channel> <title>#{rss_title}</title> <link>http://#{rss_host}</link> <description>Recently released gems from http://#{rss_host}</description> <generator>RubyGems v#{Gem::VERSION}</generator> <docs>http://cyber.law.harvard.edu/rss/rss.html</docs> HEADER today = Gem::Specification::TODAY yesterday = today - 86400 index = Gem::Specification.select do |spec| spec_date = spec.date # TODO: remove this and make YAML based specs properly normalized spec_date = Time.parse(spec_date.to_s) if Date === spec_date spec_date >= yesterday && spec_date <= today end index.sort_by { |spec| [-spec.date.to_i, spec] }.each do |spec| file_name = File.basename spec.cache_file gem_path = CGI.escapeHTML "http://#{@rss_gems_host}/gems/#{file_name}" size = File.stat(spec.loaded_from).size # rescue next description = spec.description || spec.summary || '' = Array spec. emails = Array spec.email = emails.zip().map do |email, | email += " (#{})" if and not .empty? end.join ', ' description = description.split(/\n\n+/).map do |chunk| format_text chunk, 78 end description = description.join "\n\n" item = '' item << <<-ITEM <item> <title>#{CGI.escapeHTML spec.full_name}</title> <description> <pre>#{CGI.escapeHTML description.chomp}</pre> </description> <author>#{CGI.escapeHTML }</author> <guid>#{CGI.escapeHTML spec.full_name}</guid> <enclosure url=\"#{gem_path}\" length=\"#{size}\" type=\"application/octet-stream\" /> <pubDate>#{spec.date.rfc2822}</pubDate> ITEM item << <<-ITEM if spec.homepage <link>#{CGI.escapeHTML spec.homepage}</link> ITEM item << <<-ITEM </item> ITEM io.puts item end io.puts <<-FOOTER </channel> </rss> FOOTER end end @files << @rss_index end |
#collect_specs(gems = gem_file_list) ⇒ Object
Collect specifications from .gem files from the gem directory.
379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/rubygems/indexer.rb', line 379 def collect_specs(gems = gem_file_list) Gem::Deprecate.skip_during do index = Gem::SourceIndex.new map_gems_to_specs(gems).each do |spec| index.add_spec spec, spec.original_name end index end end |
#compact_specs(specs) ⇒ Object
Compacts Marshal output for the specs index data source by using identical objects as much as possible.
417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/rubygems/indexer.rb', line 417 def compact_specs(specs) names = {} versions = {} platforms = {} specs.map do |(name, version, platform)| names[name] = name unless names.include? name versions[version] = version unless versions.include? version platforms[platform] = platform unless platforms.include? platform [names[name], versions[version], platforms[platform]] end end |
#compress(filename, extension) ⇒ Object
Compress filename
with extension
.
434 435 436 437 438 439 440 441 442 |
# File 'lib/rubygems/indexer.rb', line 434 def compress(filename, extension) data = Gem.read_binary filename zipped = Gem.deflate data open "#{filename}.#{extension}", 'wb' do |io| io.write zipped end end |
#compress_indicies ⇒ Object
Compresses indicies on disk – All future files should be compressed using gzip, not deflate
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/rubygems/indexer.rb', line 396 def compress_indicies say "Compressing indicies" Gem.time 'Compressed indicies' do if @build_legacy then compress @marshal_index, 'Z' paranoid @marshal_index, 'Z' end if @build_modern then gzip @specs_index gzip @latest_specs_index gzip @prerelease_specs_index end end end |
#gem_file_list ⇒ Object
List of gem file names to index.
447 448 449 |
# File 'lib/rubygems/indexer.rb', line 447 def gem_file_list Dir[File.join(@dest_directory, "gems", '*.gem')] end |
#generate_index ⇒ Object
Builds and installs indicies.
454 455 456 457 458 459 460 461 |
# File 'lib/rubygems/indexer.rb', line 454 def generate_index make_temp_directories build_indicies install_indicies rescue SignalException ensure FileUtils.rm_rf @directory end |
#gzip(filename) ⇒ Object
Zlib::GzipWriter wrapper that gzips filename
on disk.
466 467 468 469 470 |
# File 'lib/rubygems/indexer.rb', line 466 def gzip(filename) Zlib::GzipWriter.open "#{filename}.gz" do |io| io.write Gem.read_binary(filename) end end |
#install_indicies ⇒ Object
Install generated indicies into the destination directory.
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/rubygems/indexer.rb', line 475 def install_indicies verbose = Gem.configuration.really_verbose say "Moving index into production dir #{@dest_directory}" if verbose files = @files files.delete @quick_marshal_dir if files.include? @quick_dir if files.include? @quick_marshal_dir and not files.include? @quick_dir then files.delete @quick_marshal_dir dst_name = File.join(@dest_directory, @quick_marshal_dir_base) FileUtils.mkdir_p File.dirname(dst_name), :verbose => verbose FileUtils.rm_rf dst_name, :verbose => verbose FileUtils.mv(@quick_marshal_dir, dst_name, :verbose => verbose, :force => true) end files = files.map do |path| path.sub(/^#{Regexp.escape @directory}\/?/, '') # HACK? end files.each do |file| src_name = File.join @directory, file dst_name = File.join @dest_directory, file FileUtils.rm_rf dst_name, :verbose => verbose FileUtils.mv(src_name, @dest_directory, :verbose => verbose, :force => true) end end |
#make_temp_directories ⇒ Object
Make directories for index generation
511 512 513 514 515 |
# File 'lib/rubygems/indexer.rb', line 511 def make_temp_directories FileUtils.rm_rf @directory FileUtils.mkdir_p @directory, :mode => 0700 FileUtils.mkdir_p @quick_marshal_dir end |
#map_gems_to_specs(gems) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/rubygems/indexer.rb', line 339 def map_gems_to_specs gems gems.map { |gemfile| if File.size(gemfile) == 0 then alert_warning "Skipping zero-length gem: #{gemfile}" next end begin spec = Gem::Format.from_file_by_path(gemfile).spec spec.loaded_from = gemfile # HACK: fuck this shit - borks all tests that use pl1 # if File.basename(gemfile, ".gem") != spec.original_name then # exp = spec.full_name # exp << " (#{spec.original_name})" if # spec.original_name != spec.full_name # msg = "Skipping misnamed gem: #{gemfile} should be named #{exp}" # alert_warning msg # next # end abbreviate spec sanitize spec spec rescue SignalException => e alert_error "Received signal, exiting" raise rescue Exception => e msg = ["Unable to process #{gemfile}", "#{e.} (#{e.class})", "\t#{e.backtrace.join "\n\t"}"].join("\n") alert_error msg end }.compact end |
#paranoid(path, extension) ⇒ Object
Ensure path
and path with extension
are identical.
520 521 522 523 524 525 526 527 |
# File 'lib/rubygems/indexer.rb', line 520 def paranoid(path, extension) data = Gem.read_binary path compressed_data = Gem.read_binary "#{path}.#{extension}" unless data == Gem.inflate(compressed_data) then raise "Compressed file #{compressed_path} does not match uncompressed file #{path}" end end |
#sanitize(spec) ⇒ Object
Sanitize the descriptive fields in the spec. Sometimes non-ASCII characters will garble the site index. Non-ASCII characters will be replaced by their XML entity equivalent.
534 535 536 537 538 539 540 541 |
# File 'lib/rubygems/indexer.rb', line 534 def sanitize(spec) spec.summary = sanitize_string(spec.summary) spec.description = sanitize_string(spec.description) spec. = sanitize_string(spec.) spec. = spec..collect { |a| sanitize_string(a) } spec end |
#sanitize_string(string) ⇒ Object
Sanitize a single string.
546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/rubygems/indexer.rb', line 546 def sanitize_string(string) return string unless string # HACK the #to_s is in here because RSpec has an Array of Arrays of # Strings for authors. Need a way to disallow bad values on gemspec # generation. (Probably won't happen.) string = string.to_s begin Builder::XChar.encode string rescue NameError, NoMethodError string.to_xs end end |
#update_index ⇒ Object
Perform an in-place update of the repository from newly added gems. Only works for modern indicies, and sets #build_legacy to false when run.
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'lib/rubygems/indexer.rb', line 565 def update_index @build_legacy = false make_temp_directories specs_mtime = File.stat(@dest_specs_index).mtime newest_mtime = Time.at 0 updated_gems = gem_file_list.select do |gem| gem_mtime = File.stat(gem).mtime newest_mtime = gem_mtime if gem_mtime > newest_mtime gem_mtime >= specs_mtime end if updated_gems.empty? then say 'No new gems' terminate_interaction 0 end specs = map_gems_to_specs updated_gems prerelease, released = specs.partition { |s| s.version.prerelease? } files = build_marshal_gemspecs Gem.time 'Updated indexes' do update_specs_index released, @dest_specs_index, @specs_index update_specs_index released, @dest_latest_specs_index, @latest_specs_index update_specs_index(prerelease, @dest_prerelease_specs_index, @prerelease_specs_index) end compress_indicies verbose = Gem.configuration.really_verbose say "Updating production dir #{@dest_directory}" if verbose files << @specs_index files << "#{@specs_index}.gz" files << @latest_specs_index files << "#{@latest_specs_index}.gz" files << @prerelease_specs_index files << "#{@prerelease_specs_index}.gz" files = files.map do |path| path.sub(/^#{Regexp.escape @directory}\/?/, '') # HACK? end files.each do |file| src_name = File.join @directory, file dst_name = File.join @dest_directory, file # REFACTOR: duped above FileUtils.mv src_name, dst_name, :verbose => verbose, :force => true File.utime newest_mtime, newest_mtime, dst_name end end |
#update_specs_index(index, source, dest) ⇒ Object
Combines specs in index
and source
then writes out a new copy to dest
. For a latest index, does not ensure the new file is minimal.
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
# File 'lib/rubygems/indexer.rb', line 629 def update_specs_index(index, source, dest) specs_index = Marshal.load Gem.read_binary(source) index.each do |spec| platform = spec.original_platform platform = Gem::Platform::RUBY if platform.nil? or platform.empty? specs_index << [spec.name, spec.version, platform] end specs_index = compact_specs specs_index.uniq.sort open dest, 'wb' do |io| Marshal.dump specs_index, io end end |