-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalpine.rb
161 lines (137 loc) · 4.73 KB
/
alpine.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
# frozen_string_literal: true
module Ecosystem
class Alpine < Base
def sync_in_batches?
true
end
def purl(package, version = nil)
PackageURL.new(
type: 'apk',
namespace: 'alpine',
name: package.name.encode('iso-8859-1'),
version: version.try(:number).try(:encode,'iso-8859-1'),
qualifiers: { 'arch'=> package.metadata['architecture']},
).to_s
end
def registry_url(package, version = nil)
"https://pkgs.alpinelinux.org/package/#{@registry.version}/#{package.metadata['repository']}/#{package.metadata['architecture']}/#{package.name}"
end
def download_url(package, version)
"https://dl-cdn.alpinelinux.org/alpine/#{@registry.version}/#{package.metadata['repository']}/#{package.metadata['architecture']}/#{package.name}-#{version.number}.apk"
end
def install_command(package, version = nil)
"apk add #{package.name}"
end
def check_status(package)
return "removed" if fetch_package_metadata(package.name).blank?
end
def fetch_packages(repository, architecture)
version = @registry.version
url = "https://dl-cdn.alpinelinux.org/alpine/#{version}/#{repository}/#{architecture}/APKINDEX.tar.gz"
packages = []
package = {'r' => repository}
Dir.mktmpdir do |dir|
destination = "#{dir}/APKINDEX"
`wget -P #{dir} #{url}`
`tar -xzf #{dir}/APKINDEX.tar.gz -C #{dir}`
File.foreach(destination) do |line|
if line.blank?
packages << package
package = {'r' => repository}
end
key = line.split(':')[0]
value = line.split(':')[1..-1].join(':').strip
package[key] = value if key.present?
end
packages << package if package['P'].present?
end
packages
end
def packages
@packages ||= begin
# TODO architectures ['x86_64', 'x86', 'aarch64', 'armhf', 'ppc64le', 's390x', 'armv7', 'riscv64']
main_packages = fetch_packages('main', 'x86_64')
community_packages = fetch_packages('community', 'x86_64')
if @registry.version == 'edge'
testing_packages = fetch_packages('testing', 'x86_64')
main_packages + community_packages + testing_packages
else
main_packages + community_packages
end
end
end
def all_package_names
packages.map{|p| p['P'] }
end
def recently_updated_package_names
packages.sort_by{|p| p['t']}.reverse.map{|p| p['P'] }.first(100)
end
def fetch_package_metadata(name)
packages.find{|p| p['P'] == name }
end
def map_package_metadata(pkg_metadata)
return false if pkg_metadata.blank? || pkg_metadata["P"].blank?
# format: https://wiki.alpinelinux.org/wiki/Apk_spec
{
name: pkg_metadata["P"],
description: pkg_metadata["T"],
homepage: pkg_metadata["U"],
licenses: pkg_metadata["L"],
repository_url: repo_fallback(pkg_metadata["U"], pkg_metadata["U"]),
namespace: pkg_metadata["r"],
metadata: {
repository: pkg_metadata["r"],
architecture: pkg_metadata['A'],
}
}
end
def versions_metadata(pkg_metadata, existing_version_numbers = [])
data = fetch_package_metadata(pkg_metadata[:name])
[
{
number: data['V'],
published_at: Time.at(data['t'].to_i),
licenses: data['L'],
integrity: data['C'],
metadata: {
architecture: data['A'],
size: data['S'],
installed_size: data['I'],
checksum: data['C'],
commit: data['c'],
origin: data['o'],
provides: data['p'],
install_if: data['i'],
}
}
]
end
def dependencies_metadata(name, version, pkg_metadata)
data = fetch_package_metadata(name)
deps = data['D'].split(' ').map{|dep| packages.select{|pkg| pkg['p']&& pkg['p'].include?(dep)}.first || packages.select{|pkg| pkg['P'] == dep}.first }.uniq
deps.map do |dep|
{
package_name: dep['P'],
requirements: '*',
kind: 'install',
ecosystem: self.class.name.demodulize.downcase,
}
end
end
def maintainers_metadata(name)
data = fetch_package_metadata(name)
return [] if data.blank? || data['m'].blank?
d = data['m'].split('<')
name = d[0].strip
email = d[1].gsub('>','').strip
[{
uuid: email,
name: name,
url: "https://pkgs.alpinelinux.org/packages?maintainer=#{name}",
}]
end
def maintainer_url(maintainer)
"https://pkgs.alpinelinux.org/packages?maintainer=#{maintainer.name}"
end
end
end