-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_deps.rb
executable file
·135 lines (110 loc) · 2.79 KB
/
find_deps.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!ruby
require 'yaml'
require 'rubygems'
require 'thread'
require 'progressbar'
require 'optparse'
# Parse options
options = {
:builddir => '~/builddir'
}
OptionParser.new do |opts|
opts.banner = "Usage: find_deps.rb [options] gem_name [gem_name ...]"
opts.on("-v", "--verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on("-f", "--fetch", "Fetch missing gems") do |f|
options[:fetch] = f
end
opts.on("-b", "--builddir BUILDDIR", "RPM build envorinment (default is: #{options[:builddir]})") do |b|
options[:builddir] = b
end
opts.on("-d", "--dev", "Also check development dependencies") do |f|
options[:dev] = f
end
opts.separator ""
opts.separator "Common options:"
#No argument, shows at tail. This will print an options summary.
# Try it and see!
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!(ARGV)
@options = options
@mutex = Mutex.new
@yum = {
:rubygem => [],
:ruby => [],
:none => [],
:dependencies => {}
}
deps = []
threads = []
def find_deps(deps, name)
deps << name
gem = Gem::Specification.find_by_name(name)
if gem
my_deps = gem.dependencies.collect{|x|
x.name if (@options[:dev] || x.type == :runtime)
}.compact
@yum[:dependencies][name] = my_deps
my_deps.each { |x|
unless deps.include?(x)
find_deps(deps, x)
end
}
end
deps
end
def add_to_hash(location,name)
@mutex.synchronize do
@yum[ location ] << name
@pbar.inc
end
end
def search(prefix,name)
combined = "#{prefix}-#{name}"
regex = Regexp.compile("^#{combined}\.",Regexp::MULTILINE)
!(regex.match(`yum search -q --disablerepo=li #{combined} 2> /dev/null`).nil?)
end
# Find all of the dependencies
# These print to STDERR so that the YAML dumped to STDOUT can be piped
STDERR.print "Checking dependencies for #{ARGV.join(', ')}..."
ARGV.each do |gem|
find_deps(deps, gem)
end
STDERR.puts "found #{deps.length}"
# Create progress bar
@pbar = ProgressBar.new('Checking yum',deps.length)
for gem in deps
threads << Thread.new(gem) do |mygem|
if search('rubygem',mygem)
add_to_hash(:rubygem,mygem)
elsif search('ruby',mygem)
add_to_hash(:ruby,mygem)
else
add_to_hash(:none,mygem)
end
end
end
threads.each{|t| t.join }
@pbar.finish
# Remove any gems that don't have any dependencies
[@yum,@yum[:dependencies]].each{|a| a.delete_if{|k,v| v.nil? || v.empty?}}
# Prefix the dependency name with the name it has in YUM
@yum[:dependencies ] = @yum[:dependencies].map do |name,deps|
{
name => deps.map{|dep|
prefix = '???'
%w(rubygem ruby).each do |p|
p = p.to_sym
if @yum[p] && @yum[p].include?(dep)
prefix = p
end
end
"#{prefix}-#{dep}"
}.sort
}
end
puts YAML.dump @yum