Skip to content

Instantly share code, notes, and snippets.

@moio
Created September 23, 2014 07:10
Show Gist options
  • Save moio/53e87599e01a663d3599 to your computer and use it in GitHub Desktop.
Save moio/53e87599e01a663d3599 to your computer and use it in GitHub Desktop.
Ruby gem dependency graph outdegree sampler
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
sample_size = 1000
specs = Marshal.load(Gem.gunzip(File.read("latest_specs.4.8.gz")))
sample = specs.sample(sample_size)
total_dependencies = sample.map do |sample|
begin
compressed = open("http://rubygems.org/quick/Marshal.4.8/#{sample[0]}-#{sample[1].to_s}.gemspec.rz").read
inflated = Gem.inflate(compressed)
unmarshalled = Marshal.load(inflated)
count = unmarshalled.dependencies.count
puts "#{sample[0]} version #{sample[1].to_s} has #{count} dependencies"
count
rescue
0
end
end.inject(:+)
p "AVERAGE: #{total_dependencies.to_f / sample_size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment