-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdebRelease.rb
executable file
·276 lines (237 loc) · 7.58 KB
/
debRelease.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'debian'
require 'fileutils'
require 'time'
require 'pathname'
require 'xz'
require 'zlib'
require_relative 'downloader'
# Debian / Ubuntu release file download / parsing
class DebRelease
include Downloader
@@tempdir = '/tmp/errata_parser_cache/debian'
attr_reader :data, :files
attr_accessor :suite, :base_url, :whitelist_arch, :whitelist_comp
attr_writer :release_name
RE_FILES = /^\s*(?<digest>[0-9a-f]+)\s+(?<size>\d+)\s*(?<path>\S.*)$/
def initialize(uri=nil, suite='stable')
init
url = URI(uri)
return unless url.scheme =~ /^http(s)?$/
@base_url = uri
@suite = suite
cachedir = create_cache
parse download_file_cached "#{release_base_url}/Release", "#{cachedir}/Release"
end
def init
@data = {}
@files = {}
@base_url = nil
end
def create_cache(subdir='')
dir = Pathname.new(File.join(@@tempdir, @suite, URI(@base_url).hostname, subdir))
dir.mkpath
dir
end
def release_name
@release_name || @data['codename'] || @data['suite']
end
def parse(release_text)
state = nil
release_text.each_line do |line|
if line[0] !~ /\s/
state = nil
key, value = line.split ':'
key = key.strip.downcase
store = case key
when 'date', 'valid-until'
Time.parse value
when 'architectures', 'components'
value.split
when 'md5sum', 'sha1', 'sha256'
state = key
next
else
value.strip
end
@data[key] = store
elsif (fileinfo = RE_FILES.match(line))
@files[fileinfo['path']] = {} unless @files.key? fileinfo['path']
@files[fileinfo['path']]['size'] = fileinfo['size']
@files[fileinfo['path']][state] = fileinfo['digest']
else
warn "line could not be matched: #{line.inspect}"
end
end
end
def release_base_url
"#{@base_url}/dists/#{@suite}"
end
def get_package(component, architecture)
rel_path = "#{component}/binary-#{architecture}"
paths = @files.keys.select { |path| path.start_with? "#{rel_path}/Packages" }
# sort-reverse to prefer Packages.xz files before .gz and plain-files
paths = paths.sort.reverse
raise 'base_url not set' if @base_url.nil?
cache_dir = create_cache rel_path
# check cached-files first!
paths_exist = paths.select { |p| File.exist? "#{cache_dir}/#{p.split('/').last}" }
paths = paths_exist + (paths - paths_exist)
paths.each do |p|
basefilename = p.split('/').last
path = "#{cache_dir}/#{basefilename}"
data = download_file_cached "#{release_base_url}/#{p}", path
plainfile = "#{cache_dir}/Packages.plain"
File.open(plainfile, 'w') do |f|
f << case basefilename.downcase
when 'packages.xz'
XZ.decompress(data)
when 'packages.gz'
Zlib.gunzip(data)
else
data
end
return Debian::Packages.new(plainfile)
rescue StandardError => e
warn "#{e} for #{release_base_url}/#{p.inspect}"
FileUtils.rm_f path
ensure
FileUtils.rm_f plainfile if plainfile
end
return Debian::Packages.new(plainfile)
rescue StandardError => e
warn "#{e} for #{p.inspect}"
FileUtils.rm_f path
ensure
FileUtils.rm_f plainfile if plainfile
end
end
def architectures
arch = Set.new(data['architectures'])
arch &= Set.new(whitelist_arch) if whitelist_arch
# make sure architecture 'all' is always present
arch += Set.new(['all'])
arch.to_a
end
def components
# must work around debian-security having components like 'updates/main'
# but paths use 'main'
comp = Set.new(data['components'].map { |c| c.split('/').last })
comp &= Set.new(whitelist_comp) if whitelist_comp
comp.to_a
end
def all_packages
packages = {}
architectures.each do |arch|
components.each do |comp|
get_package(comp, arch).each do |p, d|
# necessary because sometimes 'all'-packages are also in the binary Package-files
architecture = d.info['Architecture'] if d.fields.include? 'Architecture'
architecture = arch if architecture.nil?
packages[d.source] = {} unless packages.key? d.source
packages[d.source][architecture] = [] unless packages[d.source].key? architecture
packages[d.source][architecture] << {
'name' => p,
'version' => d.version,
'arch' => architecture,
'comp' => comp,
'release' => release_name
}
# make sure we do not have duplicates
packages[d.source][architecture].uniq!
end
end
end
packages
end
def self.tempdir=(dirname)
@@tempdir = dirname
end
def self.get_all_packages(uri, suite, components=nil, architectures=nil)
rel = new(uri, suite)
rel.whitelist_arch = architectures unless architectures.nil?
rel.whitelist_comp = components unless components.nil?
rel.all_packages
end
def self.assemble_debian_packages(target, source)
source.each do |pkg_name, pkg|
target[pkg_name] = {} unless target.key? pkg_name
pkg.each do |arch_name, arch|
target[pkg_name][arch_name] = [] unless target[pkg_name].key? arch_name
target[pkg_name][arch_name].concat arch
end
end
end
def self.assemble_ubuntu_packages(target, source)
source.each_value do |pkg|
pkg.each do |arch_name, arch_pkgs|
arch_pkgs.each do |arch_pkg|
target[arch_name] = {} unless target.key? arch_name
target[arch_name][arch_pkg['release']] = {} unless target[arch_name].key? arch_pkg['release']
target[arch_name][arch_pkg['release']][arch_pkg['name']] = [] unless target[arch_name][arch_pkg['release']].key? arch_pkg['name']
target[arch_name][arch_pkg['release']][arch_pkg['name']] << arch_pkg['version']
end
end
end
end
end
if $PROGRAM_NAME == __FILE__
require 'json'
# always interpret files as UTF-8 instead of US-ASCII
Encoding.default_external = 'UTF-8'
type = ARGV[0]
HTTPDEBUG = true
case type
when 'debian'
suites = [
'buster/updates',
'bullseye-security',
'bookworm-security'
]
repository_url = 'http://security.debian.org/debian-security'
when 'ubuntu', 'ubuntu_debstyle'
suites = [
'focal-security',
'jammy-security',
'noble-security'
]
repository_url = 'http://security.ubuntu.com/ubuntu'
else
warn "Unsupported option #{type}"
exit 1
end
threads = []
pckgs = []
suites.each do |s|
threads << Thread.new do
warn "Loading Release for #{s.inspect}"
debrel = DebRelease.new(repository_url, s)
debrel.whitelist_arch = ['amd64', 'all']
debrel.whitelist_comp = ['main']
debrel.release_name = 'bullseye' if s == 'bullseye-security'
warn "From #{s.inspect} get archs:#{debrel.architectures.inspect} and comps: #{debrel.components.inspect}"
pckgs << debrel.all_packages
end
end
threads.each(&:join)
packages = {}
case type
when 'debian', 'ubuntu_debstyle'
pckgs.each do |p|
DebRelease.assemble_debian_packages(packages, p)
end
when 'ubuntu'
pckgs.each do |p|
DebRelease.assemble_ubuntu_packages(packages, p)
end
else
warn "Unsupported option #{type}"
exit 1
end
puts JSON.dump packages
# rel.get_package('main', 'amd64').each do |p, d|
# puts p.inspect
# puts "#{d.source} @ #{d.version}"
# end
end