-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo_stats.rb
executable file
·102 lines (80 loc) · 2.99 KB
/
repo_stats.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
#!/usr/bin/env ruby
require 'bundler/setup'
require 'repomd_parser'
require 'tmpdir'
require 'fileutils'
require 'net/http'
require 'uri'
def fetch(url, limit = 10)
raise RuntimeError, 'Too many redirects' if limit == 0
url = url.class == String ? URI.parse(url) : url
req = Net::HTTP::Get.new(url, { 'User-Agent' => "repomd-parser/#{RepomdParser::VERSION}" })
response = Net::HTTP.start(url.host, url.port, :use_ssl => url.instance_of?(URI::HTTPS)) do |http|
http.request(req)
end
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then fetch(response['location'], limit - 1)
else
response.error!
end
end
def make_url(repo_url, path)
repo_url = URI.parse(repo_url)
uri = URI.join(repo_url, path)
uri.query = repo_url.query
uri
end
def print_repo_stats(repo_url)
repo_url += '/' unless repo_url =~ /\/$/
tmpdir = Dir.mktmpdir
repomd_file = File.join(tmpdir, 'repomd.xml')
response = fetch(make_url(repo_url, 'repodata/repomd.xml'))
File.open(repomd_file, 'wb') do |file|
file.write(response.body)
end
stats = Hash.new { |hash, key| hash[key] = { rpm_count: 0, drpm_count: 0, total_size: 0 } }
metadata_files = Hash.new { |hash, key| hash[key] = [] }
RepomdParser::RepomdXmlParser.new(repomd_file).parse.each do |xml_file|
metadata_files[xml_file.type] << xml_file if %i[primary deltainfo].include?(xml_file.type)
end
metadata_files[:primary].each do |xml_file|
filename = File.join(tmpdir, File.basename(xml_file.location))
response = fetch(make_url(repo_url, xml_file.location))
File.open(filename, 'wb') do |file|
file.write(response.body)
end
rpms = RepomdParser::PrimaryXmlParser.new(filename).parse
rpms.each do |package|
stats[package.arch][:rpm_count] += 1
stats[package.arch][:total_size] += package.size
end
end
metadata_files[:deltainfo].each do |xml_file|
filename = File.join(tmpdir, File.basename(xml_file.location))
response = fetch(make_url(repo_url, xml_file.location))
File.open(filename, 'wb') do |file|
file.write(response.body)
end
rpms = RepomdParser::DeltainfoXmlParser.new(filename).parse
rpms.each do |package|
stats[package.arch][:drpm_count] += 1
stats[package.arch][:total_size] += package.size
end
end
pretty_url = URI.parse(repo_url)
pretty_url.query = nil
puts "Statistics for #{pretty_url}"
puts ''
stats.each { |arch, data| printf "%08s: %06s RPM packages, %06s deltainfo packages, %6.02f gigabytes\n", arch, data[:rpm_count], data[:drpm_count], data[:total_size].to_f / 1024 ** 3 }
puts ''
printf("Total size: %6.02f gigabytes\n", stats.inject(0) { |sum, (_, item)| sum + item[:total_size] }.to_f / 1024 ** 3)
ensure
FileUtils.rm_r(tmpdir)
end
if (!ARGV[0] || ARGV[0] == '-h' || ARGV[0] == '--help')
puts "Usage: #{File.basename($PROGRAM_NAME)} REPO_URL"
puts 'Authentication token can be supplied as query string, e.g.: https://repo.url/?auth_token'
exit 1
end
print_repo_stats(ARGV[0])