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_compact ⇒ Object
Build compact index files 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
-
#build_compact_index(specs) ⇒ Object
Builds compact index files.
-
#build_indices ⇒ Object
Build various indices.
-
#build_marshal_gemspecs(specs) ⇒ 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_indices(specs) ⇒ Object
Builds indices for RubyGems 1.2 and newer.
- #compact_index_gems(specs) ⇒ Object
-
#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_indices ⇒ Object
Compresses indices 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 indices.
-
#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_indices ⇒ Object
Install generated indices 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. -
#update_compact_index(new_specs, source, dest) ⇒ Object
Combines specs in
index
andsource
then writes out a new compact index todest
. -
#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
.
Constructor Details
#initialize(directory, options = {}) ⇒ Indexer
Create an indexer that will index the gems in directory
.
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 |
# File 'lib/rubygems/indexer.rb', line 60 def initialize(directory, = {}) require "fileutils" require "tmpdir" require "zlib" if Gem::VERSION < "3.2.0" && !defined?(Builder::XChar) raise "Gem::Indexer requires that the XML Builder library be installed:" \ "\n\tgem install builder" end = { :build_modern => true }.merge @build_modern = [:build_modern] @build_compact = [:build_compact] @dest_directory = directory @directory = Dir.mktmpdir "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}") @files = [] end |
Instance Attribute Details
#build_compact ⇒ Object
Build compact index files when true
30 31 32 |
# File 'lib/rubygems/indexer.rb', line 30 def build_compact @build_compact end |
#build_modern ⇒ Object
Build indexes for RubyGems 1.2.0 and newer when true
25 26 27 |
# File 'lib/rubygems/indexer.rb', line 25 def build_modern @build_modern end |
#dest_directory ⇒ Object (readonly)
Index install location
35 36 37 |
# File 'lib/rubygems/indexer.rb', line 35 def dest_directory @dest_directory end |
#dest_latest_specs_index ⇒ Object (readonly)
Latest specs index install location
45 46 47 |
# File 'lib/rubygems/indexer.rb', line 45 def dest_latest_specs_index @dest_latest_specs_index end |
#dest_prerelease_specs_index ⇒ Object (readonly)
Prerelease specs index install location
50 51 52 |
# File 'lib/rubygems/indexer.rb', line 50 def dest_prerelease_specs_index @dest_prerelease_specs_index end |
#dest_specs_index ⇒ Object (readonly)
Specs index install location
40 41 42 |
# File 'lib/rubygems/indexer.rb', line 40 def dest_specs_index @dest_specs_index end |
#directory ⇒ Object (readonly)
Index build directory
55 56 57 |
# File 'lib/rubygems/indexer.rb', line 55 def directory @directory end |
Instance Method Details
#build_compact_index(specs) ⇒ Object
Builds compact index files
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/rubygems/indexer.rb', line 210 def build_compact_index(specs) gems = compact_index_gems(specs) progress = ui.progress_reporter gems.count + 2, "Generating compact index files for #{gems.count} gems", "Complete" Gem.time "Generated compact index files" do info_dir = File.join(@directory, "info") FileUtils.mkdir_p info_dir gems.each do |gem| info = CompactIndex.info(gem.versions) info_checksum = Digest::MD5.hexdigest(info) gem[:versions].last[:info_checksum] = info_checksum info_path = File.join(@directory, "info", gem.name) if File.exist?(info_path) raise "info file already exists: #{info_path}" end File.open(info_path, "wb") do |io| io.write info end progress.updated "/info/#{gem.name}" end File.open(File.join(@directory, "names"), "wb") do |io| io.write CompactIndex.names(gems.map(&:name)) end progress.updated "/names" CompactIndex::VersionsFile.new( File.join(@directory, "versions") ).create( gems, ) progress.updated "/versions" @files << info_dir << File.join(@directory, "names") << File.join(@directory, "versions") progress.done end end |
#build_indices ⇒ Object
Build various indices
108 109 110 111 112 113 114 115 116 |
# File 'lib/rubygems/indexer.rb', line 108 def build_indices specs = map_gems_to_specs gem_file_list Gem::Specification._resort! specs build_marshal_gemspecs specs build_modern_indices specs if @build_modern build_compact_index specs if @build_compact compress_indices end |
#build_marshal_gemspecs(specs) ⇒ Object
Builds Marshal quick index gemspecs.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rubygems/indexer.rb', line 121 def build_marshal_gemspecs(specs) count = specs.count progress = ui.progress_reporter count, "Generating Marshal quick index gemspecs for #{count} gems", "Complete" files = [] Gem.time "Generated Marshal quick index gemspecs" do specs.each do |spec| next if spec.default_gem? 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) File.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
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rubygems/indexer.rb', line 157 def build_modern_index(index, file, name) say "Generating #{name} index" Gem.time "Generated #{name} index" do File.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 alert_warning "Skipping invalid platform in gem: #{spec.full_name}" next end platform = Gem::Platform::RUBY if platform.nil? || platform.empty? [spec.name, spec.version, platform] end specs = compact_specs(specs) Marshal.dump(specs, io) end end end |
#build_modern_indices(specs) ⇒ Object
Builds indices for RubyGems 1.2 and newer. Handles full, latest, prerelease
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rubygems/indexer.rb', line 187 def build_modern_indices(specs) prerelease, released = specs.partition do |s| s.version.prerelease? end latest_specs = Gem::Specification._latest_specs 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 |
#compact_index_gems(specs) ⇒ Object
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
# File 'lib/rubygems/indexer.rb', line 568 def compact_index_gems(specs) versions_by_name = Hash.new {|h, k| h[k] = [] } specs.each do |spec| checksum = spec.loaded_from && Digest::SHA256.file(spec.loaded_from).base64digest! versions_by_name[spec.name] << CompactIndex::GemVersion.new( spec.version.version, spec.platform.to_s, checksum, nil, # info_checksum spec.runtime_dependencies.map {|d| CompactIndex::Dependency.new(d.name, d.requirement.to_s) }, spec.required_ruby_version&.to_s, spec.required_rubygems_version&.to_s ) end versions_by_name.map do |name, versions| versions.sort! CompactIndex::Gem.new(name, versions) end.tap(&:sort!) end |
#compact_specs(specs) ⇒ Object
Compacts Marshal output for the specs index data source by using identical objects as much as possible.
306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/rubygems/indexer.rb', line 306 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
.
323 324 325 326 327 328 329 330 331 |
# File 'lib/rubygems/indexer.rb', line 323 def compress(filename, extension) data = Gem.read_binary filename zipped = Gem.deflate data File.open "#{filename}.#{extension}", "wb" do |io| io.write zipped end end |
#compress_indices ⇒ Object
Compresses indices on disk – All future files should be compressed using gzip, not deflate
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/rubygems/indexer.rb', line 290 def compress_indices say "Compressing indices" Gem.time "Compressed indices" do if @build_modern 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.
336 337 338 |
# File 'lib/rubygems/indexer.rb', line 336 def gem_file_list Gem::Util.glob_files_in_dir("*.gem", File.join(@dest_directory, "gems")) end |
#generate_index ⇒ Object
Builds and installs indices.
343 344 345 346 347 348 349 350 |
# File 'lib/rubygems/indexer.rb', line 343 def generate_index make_temp_directories build_indices install_indices rescue SignalException ensure FileUtils.rm_rf @directory end |
#gzip(filename) ⇒ Object
Zlib::GzipWriter wrapper that gzips filename
on disk.
355 356 357 358 359 |
# File 'lib/rubygems/indexer.rb', line 355 def gzip(filename) Zlib::GzipWriter.open "#{filename}.gz" do |io| io.write Gem.read_binary(filename) end end |
#install_indices ⇒ Object
Install generated indices into the destination directory.
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/rubygems/indexer.rb', line 364 def install_indices 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) && !files.include?(@quick_dir) 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(%r{^#{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
400 401 402 403 404 |
# File 'lib/rubygems/indexer.rb', line 400 def make_temp_directories FileUtils.rm_rf @directory FileUtils.mkdir_p @directory, :mode => 0o700 FileUtils.mkdir_p @quick_marshal_dir end |
#map_gems_to_specs(gems) ⇒ Object
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 |
# File 'lib/rubygems/indexer.rb', line 258 def map_gems_to_specs(gems) gems.map do |gemfile| if File.size(gemfile) == 0 alert_warning "Skipping zero-length gem: #{gemfile}" next end begin spec = Gem::Package.new(gemfile).spec spec.loaded_from = gemfile spec.abbreviate spec.sanitize spec rescue SignalException alert_error "Received signal, exiting" raise rescue StandardError => e msg = ["Unable to process #{gemfile}", "#{e.} (#{e.class})", "\t#{e.backtrace.join "\n\t"}"].join("\n") alert_error msg end end.compact end |
#paranoid(path, extension) ⇒ Object
Ensure path
and path with extension
are identical.
409 410 411 412 413 414 415 416 |
# File 'lib/rubygems/indexer.rb', line 409 def paranoid(path, extension) data = Gem.read_binary path compressed_data = Gem.read_binary "#{path}.#{extension}" unless data == Gem::Util.inflate(compressed_data) raise "Compressed file #{compressed_path} does not match uncompressed file #{path}" end end |
#update_compact_index(new_specs, source, dest) ⇒ Object
Combines specs in index
and source
then writes out a new compact index to dest
.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/rubygems/indexer.rb', line 511 def update_compact_index(new_specs, source, dest) files = [] source_versions_path = File.join(source, "versions") versions_file = CompactIndex::VersionsFile.new source_versions_path gems = compact_index_gems new_specs FileUtils.mkdir_p File.join(dest, "info") new_names = [] gems.each do |gem| existing_info_path = File.join(source, "info", gem.name) if File.exist? existing_info_path info = Gem.read_binary(existing_info_path) + CompactIndex.info(gem.versions).sub(/\A---\n/, "") else new_names << gem.name info = CompactIndex.info gem.versions end info_checksum = Digest::MD5.hexdigest(info) gem[:versions].last[:info_checksum] = info_checksum info_path = File.join(dest, "info", gem.name) if File.exist?(info_path) raise "info file already exists: #{info_path}" end File.open(info_path, "wb") do |io| io.write info end files << info_path end versions_path = File.join(dest, "versions") File.open(versions_path, "wb") do |io| io.write versions_file.contents(gems) end files << versions_path unless new_names.empty? old_names = Gem.read_binary(File.join(source, "names")).lines(chomp: true)[2..] names_path = File.join(dest, "names") names = old_names + new_names names.sort! File.open(names_path, "wb") do |io| io.write CompactIndex.names(names) end files << names_path end files end |
#update_index ⇒ Object
Perform an in-place update of the repository from newly added gems.
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/rubygems/indexer.rb', line 421 def update_index 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? 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 specs 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 if @build_compact Gem.time "Updated compact index files" do files += update_compact_index released, @dest_directory, @directory end end compress_indices 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(%r{^#{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 ensure FileUtils.rm_rf @directory 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.
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/rubygems/indexer.rb', line 491 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? || platform.empty? specs_index << [spec.name, spec.version, platform] end specs_index = compact_specs specs_index.uniq.sort File.open dest, "wb" do |io| Marshal.dump specs_index, io end end |