-
Notifications
You must be signed in to change notification settings - Fork 1
/
outdated_packages.rb
executable file
·75 lines (62 loc) · 2.09 KB
/
outdated_packages.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
#!/usr/bin/env ruby
require 'repomd_parser'
class OutdatedPackagesCounter
def initialize(mirror_dir)
@mirror_dir = mirror_dir
@packages = {}
@inodes = {}
end
def start
repo_dirs = []
referenced_size = 0
orphaned_size = 0
Dir[@mirror_dir + '/**/*'].each do |f|
next unless File.directory?(f)
if File.basename(f) == 'repodata'
repo_dirs << File.expand_path(File.join(f, '..'))
end
end
repo_dirs.each do |repo_dir|
Dir[repo_dir + '/**/*'].each do |f|
@packages[f] = true if f =~ /rpm$/
end
primary_xml = nil
delta_xml = nil
metadata_files = RepomdParser::RepomdXmlParser.new(File.join(repo_dir, 'repodata/repomd.xml')).parse
metadata_files.each do |metadata_file|
primary_xml = File.join(repo_dir, metadata_file.location) if metadata_file.type == :primary
delta_xml = File.join(repo_dir, metadata_file.location) if metadata_file.type == :deltainfo
end
if primary_xml
rpm_packages = RepomdParser::PrimaryXmlParser.new(primary_xml).parse
rpm_packages.each do |rpm|
full_path = File.join(repo_dir, rpm.location)
referenced_size += get_size(full_path)
@packages.delete(full_path)
end
end
next unless delta_xml
drpm_packages = RepomdParser::DeltainfoXmlParser.new(delta_xml).parse
drpm_packages.each do |rpm|
full_path = File.join(repo_dir, rpm.location)
referenced_size += get_size(full_path)
@packages.delete(full_path)
end
end
@packages.keys.each do |file|
orphaned_size += get_size(file)
end
puts format('Size of up-to-date files (MB): % 6d', referenced_size / 1024 / 1024)
puts format('Size of outdated files (MB): % 6d', orphaned_size / 1024 / 1024)
end
protected
def get_size(full_path)
return 0 unless File.exist?(full_path)
ino = File.stat(full_path).ino
return 0 if @inodes[ino]
@inodes[ino] = true
File.size(full_path)
end
end
mirror_dir = ARGV[0] || '/srv/www/htdocs/'
OutdatedPackagesCounter.new(mirror_dir).start