native-packages 0.12.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "rubygems/package"
5
+ require "stringio"
6
+ require_relative "hosted_repository"
7
+
8
+ module NativePackages
9
+ # A signed pacman repository for Arch Linux: a directory per architecture
10
+ # (x86_64/, aarch64/) holds the .pkg.tar.zst files, each with a detached
11
+ # .sig, and the databases pacman reads (REPO.db and REPO.files), written by
12
+ # this class as repo-add writes them, without pacman's own tools. Static
13
+ # hosts serve no symlinks, so REPO.db and REPO.db.tar.gz are two copies of
14
+ # the same file, as are REPO.files and REPO.files.tar.gz. Beside the
15
+ # architectures goes the public key (REPO.asc) for users to trust.
16
+ class PacmanRepository < HostedRepository
17
+ SECTION = "pacman"
18
+ KEYS = (HostedRepository::KEYS + %w[name bucket]).freeze
19
+ # The architecture directories an `any` package goes into when the
20
+ # repository has none yet.
21
+ ARCHITECTURES = %w[x86_64 aarch64].freeze
22
+ # .PKGINFO keys that may repeat, and the desc entries they become.
23
+ LISTS = { "group" => "GROUPS", "license" => "LICENSE", "replaces" => "REPLACES", "conflict" => "CONFLICTS",
24
+ "provides" => "PROVIDES", "depend" => "DEPENDS", "optdepend" => "OPTDEPENDS", "makedepend" => "MAKEDEPENDS",
25
+ "checkdepend" => "CHECKDEPENDS" }.freeze
26
+ PACKAGE = /\.pkg\.tar(?:\.(?:zst|xz|gz|bz2|lz4|lzo|lrz|Z))?\z/
27
+
28
+ def self.validate(data)
29
+ settings = data["pacman"]
30
+ return if settings.nil?
31
+ check(settings, "pacman", KEYS)
32
+ end
33
+
34
+ # The fields of the package at `path`, from its .PKGINFO: single values
35
+ # as strings, repeatable ones as arrays.
36
+ def self.info(path)
37
+ text = Open3.capture3("bsdtar", "-xOqf", path.to_s, ".PKGINFO", binmode: true).then do |output, error, status|
38
+ raise Error, "#{File.basename(path)} is not a pacman package: #{error.strip}" unless status.success? && !output.empty?
39
+ output.force_encoding(Encoding::UTF_8)
40
+ end
41
+ fields = text.each_line.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |line, result|
42
+ next if line.start_with?("#") || !line.include?("=")
43
+ key, value = line.split("=", 2).map(&:strip)
44
+ result[key] << value.gsub(/\s+/, " ")
45
+ end
46
+ %w[pkgname pkgver arch].each { |key| raise Error, "#{File.basename(path)}: .PKGINFO has no #{key}" if fields[key].empty? }
47
+ fields.to_h { |key, values| [key, LISTS.key?(key) || key == "backup" ? values : values.last] }
48
+ end
49
+
50
+ # The files the package installs, as repo-add lists them.
51
+ def self.contents(path)
52
+ output, error, status = Open3.capture3("bsdtar", "--exclude=^.*", "-tf", path.to_s, binmode: true)
53
+ raise Error, "#{File.basename(path)} could not be listed: #{error.strip}" unless status.success?
54
+ output.force_encoding(Encoding::UTF_8).lines.map(&:chomp).reject(&:empty?).uniq.sort_by(&:b)
55
+ end
56
+
57
+ # pacman's version order (vercmp): -1, 0 or 1.
58
+ def self.compare(left, right)
59
+ return 0 if left == right
60
+ left, right = [left, right].map do |version|
61
+ epoch, rest = version.match?(/\A\d*:/) ? version.split(":", 2) : ["0", version]
62
+ upstream, _, release = rest.include?("-") ? rest.rpartition("-") : [rest, nil, nil]
63
+ [epoch.empty? ? "0" : epoch, upstream, release]
64
+ end
65
+ order = compare_part(left[0], right[0]).nonzero? || compare_part(left[1], right[1])
66
+ return order unless order.zero? && left[2] && right[2]
67
+ compare_part(left[2], right[2])
68
+ end
69
+
70
+ # libalpm's rpmvercmp, which differs from RPM's: no special tilde or
71
+ # caret, a letter suffix sorts before the bare version (1.0a < 1.0), and
72
+ # a longer separator wins.
73
+ def self.compare_part(left, right)
74
+ return 0 if left == right
75
+ one, two = left, right
76
+ until one.empty? || two.empty?
77
+ separators = [one[/\A[^A-Za-z0-9]*/].length, two[/\A[^A-Za-z0-9]*/].length]
78
+ one, two = one[separators[0]..], two[separators[1]..]
79
+ break if one.empty? || two.empty?
80
+ return separators[0] < separators[1] ? -1 : 1 unless separators[0] == separators[1]
81
+ numeric = one.match?(/\A\d/)
82
+ pattern = numeric ? /\A\d+/ : /\A[A-Za-z]+/
83
+ mine, theirs = one[pattern], two[pattern]
84
+ # A number is newer than letters.
85
+ return numeric ? 1 : -1 unless theirs
86
+ order = if numeric
87
+ a, b = [mine, theirs].map { |number| number.sub(/\A0+/, "") }
88
+ (a.length <=> b.length).nonzero? || (a <=> b)
89
+ else
90
+ mine <=> theirs
91
+ end
92
+ return order unless order.zero?
93
+ one, two = one[mine.length..], two[theirs.length..]
94
+ end
95
+ return 0 if one.empty? && two.empty?
96
+ # A remaining letter never beats the end.
97
+ (one.empty? && !two.match?(/\A[A-Za-z]/)) || one.match?(/\A[A-Za-z]/) ? -1 : 1
98
+ end
99
+
100
+ def publish(packages, secret: nil, passphrase: nil)
101
+ raise Error, "the build contains no Arch packages to publish --to pacman" if packages.empty?
102
+ raise Error, "install bsdtar (libarchive) to publish --to pacman" unless available?("bsdtar")
103
+ infos = packages.to_h { |path| [path, self.class.info(path)] }
104
+ present = published.map { |path| path.dirname.basename.to_s }.uniq
105
+ built = infos.values.map { |info| info.fetch("arch") } - ["any"]
106
+ architectures = (present | built).then { |found| found.empty? ? ARCHITECTURES : found }
107
+ remaining = nil
108
+ signer(secret: secret, passphrase: passphrase).session do |signing|
109
+ infos.each do |path, info|
110
+ (info.fetch("arch") == "any" ? architectures : [info.fetch("arch")]).each do |architecture|
111
+ destination = directory / architecture / path.basename
112
+ place(path, destination)
113
+ signature = Pathname.new("#{destination}.sig")
114
+ signing.detach_sign(destination, signature, armor: false) unless signature.exist?
115
+ end
116
+ end
117
+ # Indexes cover every package in the directory, not only this build's.
118
+ entries = published.to_h do |path|
119
+ info = self.class.info(path)
120
+ [path, [[path.dirname.basename.to_s, info.fetch("pkgname")], info.fetch("pkgver"), info]]
121
+ end
122
+ remaining = prune(entries) { |left, right| self.class.compare(left, right) }
123
+ (entries.keys - remaining.keys).each do |path|
124
+ signature = Pathname.new("#{path}.sig")
125
+ signature.delete if signature.exist?
126
+ end
127
+ remaining.group_by { |path, _| path.dirname }.sort.each do |folder, rows|
128
+ write_databases(folder, rows.to_h, signing)
129
+ end
130
+ write(directory / "#{repository}.asc", signing.public_key(armor: true))
131
+ end
132
+ puts "Published #{remaining.length} package#{"s" unless remaining.length == 1} to #{directory}. Install with:", *instructions(infos.values.map { |info| info.fetch("pkgname") }.uniq).map { |line| " #{line}" }
133
+ end
134
+
135
+ # Packages and their signatures, which are written once; the databases
136
+ # change.
137
+ def immutable = published.flat_map { |path| [path, Pathname.new("#{path}.sig")] }.select(&:file?)
138
+
139
+ def entry_points = Pathname.glob(directory / "*/#{repository}.{db,files}{,.tar.gz}{,.sig}").sort
140
+
141
+ def instructions(packages = [name])
142
+ fingerprint = OpenPGP.fingerprint(settings.fetch("signing_key"), "signing_key")
143
+ ["curl -fsSL #{url}/#{repository}.asc | sudo pacman-key --add -",
144
+ "sudo pacman-key --lsign-key #{fingerprint}",
145
+ "printf '\\n[#{repository}]\\nSigLevel = Required\\nServer = #{url}/$arch\\n' | sudo tee -a /etc/pacman.conf",
146
+ "sudo pacman -Syu #{packages.join(' ')}"]
147
+ end
148
+
149
+ private
150
+
151
+ # Every package in the directory, whichever project published it.
152
+ def published = Pathname.glob(directory / "*/*.pkg.tar*").select { |path| PACKAGE.match?(path.basename.to_s) }.sort
153
+
154
+ # REPO.db and REPO.files in `folder`, as gzip-compressed tar archives,
155
+ # each signed and written twice: once under each name pacman may ask for.
156
+ def write_databases(folder, entries, signing)
157
+ rows = entries.map { |path, (_, _, info)| [path, info, desc(path, info)] }.sort_by { |_, info, _| [info.fetch("pkgname"), info.fetch("pkgver")] }
158
+ databases = {
159
+ "db" => rows.map { |_, info, text| [entry(info), { "desc" => text }] },
160
+ "files" => rows.map { |path, info, text| [entry(info), { "desc" => text, "files" => "%FILES%\n#{self.class.contents(path).map { |line| "#{line}\n" }.join}" }] }
161
+ }
162
+ databases.each do |kind, members|
163
+ archive = gzip(tar(members))
164
+ base = folder / "#{repository}.#{kind}"
165
+ write(base, archive)
166
+ signing.detach_sign(base, Pathname.new("#{base}.sig"), armor: false)
167
+ write(Pathname.new("#{base}.tar.gz"), archive)
168
+ write(Pathname.new("#{base}.tar.gz.sig"), Pathname.new("#{base}.sig").binread)
169
+ end
170
+ end
171
+
172
+ def entry(info) = "#{info.fetch('pkgname')}-#{info.fetch('pkgver')}"
173
+
174
+ # The package's desc entry, in repo-add's order. Signatures are served
175
+ # beside the packages rather than embedded, as repo-add does by default.
176
+ def desc(path, info)
177
+ fields = [["FILENAME", path.basename.to_s], ["NAME", info["pkgname"]], ["BASE", info["pkgbase"]], ["VERSION", info["pkgver"]],
178
+ ["DESC", info["pkgdesc"]], ["GROUPS", info["group"]], ["CSIZE", path.size.to_s], ["ISIZE", info["size"]],
179
+ ["SHA256SUM", Digest::SHA256.file(path).hexdigest], ["URL", info["url"]], ["LICENSE", info["license"]], ["ARCH", info["arch"]],
180
+ ["BUILDDATE", info["builddate"]], ["PACKAGER", info["packager"]], ["REPLACES", info["replaces"]], ["CONFLICTS", info["conflict"]],
181
+ ["PROVIDES", info["provides"]], ["DEPENDS", info["depend"]], ["OPTDEPENDS", info["optdepend"]],
182
+ ["MAKEDEPENDS", info["makedepend"]], ["CHECKDEPENDS", info["checkdepend"]]]
183
+ fields.filter_map do |key, value|
184
+ values = Array(value)
185
+ next if values.empty? || values.first.to_s.empty?
186
+ "%#{key}%\n#{values.map { |line| "#{line}\n" }.join}\n"
187
+ end.join
188
+ end
189
+
190
+ # A tar archive of one directory per package, holding its entries.
191
+ def tar(members)
192
+ io = StringIO.new("".b)
193
+ Gem::Package::TarWriter.new(io) do |writer|
194
+ members.each do |folder, files|
195
+ writer.mkdir(folder, 0o755)
196
+ files.each do |file, text|
197
+ bytes = text.b
198
+ writer.add_file_simple("#{folder}/#{file}", 0o644, bytes.bytesize) { |stream| stream.write(bytes) }
199
+ end
200
+ end
201
+ end
202
+ io.string
203
+ end
204
+ end
205
+ end
@@ -12,7 +12,7 @@ require "tmpdir"
12
12
  require "yaml"
13
13
 
14
14
  module NativePackages
15
- VERSION = "0.12.0"
15
+ VERSION = "0.14.0"
16
16
  class Error < StandardError; end
17
17
 
18
18
  module Support
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: native-packages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carmine Paolino
@@ -58,6 +58,7 @@ files:
58
58
  - lib/native_packages.rb
59
59
  - lib/native_packages/appimage.rb
60
60
  - lib/native_packages/apt_repository.rb
61
+ - lib/native_packages/bucket.rb
61
62
  - lib/native_packages/build.rb
62
63
  - lib/native_packages/cli.rb
63
64
  - lib/native_packages/configuration.rb
@@ -70,6 +71,7 @@ files:
70
71
  - lib/native_packages/native_recipe.rb
71
72
  - lib/native_packages/omarchy.rb
72
73
  - lib/native_packages/openpgp.rb
74
+ - lib/native_packages/pacman_repository.rb
73
75
  - lib/native_packages/project.rb
74
76
  - lib/native_packages/release_signing.rb
75
77
  - lib/native_packages/repositories.rb