native-packages 0.11.0 → 0.12.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,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "hosted_repository"
4
+
5
+ module NativePackages
6
+ # A signed DNF (and Zypper) repository: packages/ holds the .rpm files and
7
+ # repodata/ the metadata createrepo_c writes, with repomd.xml signed
8
+ # beside it as repomd.xml.asc. Beside them go the public key (NAME.asc) and
9
+ # a NAME.repo file for users to install.
10
+ class DnfRepository < HostedRepository
11
+ TAGS = { 1000 => "name", 1001 => "version", 1002 => "release", 1003 => "epoch", 1022 => "arch", 1044 => "sourcerpm" }.freeze
12
+ # Signature header tags for DSA, RSA, PGP, GPG and OpenPGP signatures.
13
+ SIGNATURES = [267, 268, 1002, 1005, 278].freeze
14
+
15
+ def self.validate(data)
16
+ settings = data["dnf"]
17
+ check(settings, "dnf", KEYS) unless settings.nil?
18
+ end
19
+
20
+ def initialize(configuration)
21
+ super(configuration, configuration.data.fetch("dnf") { raise Error, "configure dnf to publish --to dnf" })
22
+ end
23
+
24
+ # The name, version, release, epoch and architecture of the RPM at
25
+ # `path`, and whether it carries a signature.
26
+ def self.header(path)
27
+ File.open(path, "rb") do |file|
28
+ raise Error, "not an RPM package: #{path}" unless file.read(96)&.start_with?("\xED\xAB\xEE\xDB".b)
29
+ signature = read_header(file, path)
30
+ file.seek((8 - (file.pos % 8)) % 8, IO::SEEK_CUR)
31
+ main = read_header(file, path)
32
+ raise Error, "#{File.basename(path)} is a source package" unless main.key?(1044)
33
+ TAGS.to_h { |tag, key| [key, main[tag]] }.merge("epoch" => main[1003]&.first.to_i, "signed" => SIGNATURES.any? { |tag| signature.key?(tag) })
34
+ end
35
+ end
36
+
37
+ def self.read_header(file, path)
38
+ raise Error, "invalid RPM header: #{path}" unless file.read(8)&.start_with?("\x8E\xAD\xE8\x01".b)
39
+ count, size = file.read(8).unpack("N2")
40
+ index = file.read(count * 16).unpack("N*").each_slice(4).to_a
41
+ store = file.read(size)
42
+ raise Error, "truncated RPM header: #{path}" unless store&.bytesize == size
43
+ index.to_h do |tag, type, offset, length|
44
+ value = case type
45
+ when 4 then store.byteslice(offset, 4 * length).unpack("N*")
46
+ when 6, 8, 9 then store.byteslice(offset..).unpack1("Z*").force_encoding(Encoding::UTF_8)
47
+ end
48
+ [tag, value]
49
+ end
50
+ end
51
+
52
+ # RPM's version order (rpmvercmp): -1, 0 or 1.
53
+ def self.compare(left, right)
54
+ return 0 if left == right
55
+ loop do
56
+ left, right = [left, right].map { |part| part.sub(/\A[^A-Za-z0-9~^]+/, "") }
57
+ # A tilde sorts before everything, even the end.
58
+ if left.start_with?("~") || right.start_with?("~")
59
+ return 1 unless left.start_with?("~")
60
+ return -1 unless right.start_with?("~")
61
+ left, right = left[1..], right[1..]
62
+ next
63
+ end
64
+ if left.start_with?("^") || right.start_with?("^")
65
+ return -1 if left.empty?
66
+ return 1 if right.empty?
67
+ return 1 unless left.start_with?("^")
68
+ return -1 unless right.start_with?("^")
69
+ left, right = left[1..], right[1..]
70
+ next
71
+ end
72
+ break if left.empty? || right.empty?
73
+ numeric = left.match?(/\A\d/)
74
+ pattern = numeric ? /\A\d+/ : /\A[A-Za-z]+/
75
+ mine, theirs = left[pattern], right[pattern]
76
+ # A number is newer than letters.
77
+ return numeric ? 1 : -1 unless theirs
78
+ order = if numeric
79
+ a, b = [mine, theirs].map { |number| number.sub(/\A0+/, "") }
80
+ (a.length <=> b.length).nonzero? || (a <=> b)
81
+ else
82
+ mine <=> theirs
83
+ end
84
+ return order unless order.zero?
85
+ left, right = left[mine.length..], right[theirs.length..]
86
+ end
87
+ return 0 if left.empty? && right.empty?
88
+ left.empty? ? -1 : 1
89
+ end
90
+
91
+ def self.compare_packages(left, right)
92
+ (left.fetch("epoch") <=> right.fetch("epoch")).nonzero? || compare(left.fetch("version"), right.fetch("version")).nonzero? ||
93
+ compare(left.fetch("release"), right.fetch("release"))
94
+ end
95
+
96
+ def publish(rpms, secret: nil, passphrase: nil)
97
+ raise Error, "the build contains no rpm packages to publish --to dnf" if rpms.empty?
98
+ raise Error, "install createrepo_c to publish --to dnf" unless available?("createrepo_c")
99
+ rpms.each do |path|
100
+ self.class.header(path)
101
+ place(path, directory / "packages" / path.basename)
102
+ end
103
+ entries = Pathname.glob(directory / "packages/*.rpm").sort.to_h do |path|
104
+ header = self.class.header(path)
105
+ [path, [header.values_at("name", "arch"), header]]
106
+ end
107
+ entries = prune(entries) { |left, right| self.class.compare_packages(left, right) }
108
+ signer(secret: secret, passphrase: passphrase).session do |signing|
109
+ # gzip, which every dnf and zypper reads; newer createrepo_c defaults to zstd.
110
+ run "createrepo_c", "--quiet", "--no-database", "--general-compress-type=gz", directory
111
+ signing.detach_sign(directory / "repodata/repomd.xml", directory / "repodata/repomd.xml.asc")
112
+ write(directory / "#{name}.asc", signing.public_key(armor: true))
113
+ end
114
+ signed = entries.values.all? { |_, header| header.fetch("signed") }
115
+ write(directory / "#{name}.repo", <<~REPO)
116
+ [#{name}]
117
+ name=#{name}
118
+ baseurl=#{url}
119
+ enabled=1
120
+ repo_gpgcheck=1
121
+ gpgcheck=#{signed ? 1 : 0}
122
+ gpgkey=#{url}/#{name}.asc
123
+ REPO
124
+ puts "Published #{entries.length} package#{"s" unless entries.length == 1} to #{directory}. Install with:", *instructions.map { |line| " #{line}" }
125
+ end
126
+
127
+ def instructions
128
+ ["sudo curl -fsSLo /etc/yum.repos.d/#{name}.repo #{url}/#{name}.repo", "sudo dnf install #{name}"]
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "staging"
4
+
5
+ module NativePackages
6
+ # Builds a single-file Flatpak bundle from the same contents nFPM packages.
7
+ # The app is already built, so this uses flatpak's own build commands
8
+ # (build-finish, build-export, build-bundle) on the staged files, without
9
+ # flatpak-builder, an SDK or the network. Files installed under /usr go to
10
+ # /app; the runtime supplies the libraries.
11
+ #
12
+ # This never writes or submits a Flathub manifest: Flathub does not accept
13
+ # AI-assisted manifests or submissions.
14
+ class Flatpak
15
+ include Support
16
+ include Staging
17
+ # nFPM architecture => Flatpak architecture.
18
+ ARCHES = { "amd64" => "x86_64", "arm64" => "aarch64" }.freeze
19
+ KEYS = %w[id runtime runtime_version sdk command finish_args metainfo branch runtime_repo repository].freeze
20
+ # Reverse-DNS: three or more elements, no hyphen in the last one.
21
+ ID = /\A(?=.{1,255}\z)(?:[A-Za-z_][A-Za-z0-9_-]*\.){2,}[A-Za-z_][A-Za-z0-9_]*\z/
22
+ RUNTIME = /\A(?:[A-Za-z_][A-Za-z0-9_-]*\.)+[A-Za-z_][A-Za-z0-9_]*\z/
23
+ BRANCH = /\A[A-Za-z0-9_][A-Za-z0-9_.-]*\z/
24
+ ICON_DIRECTORIES = %w[share/icons share/pixmaps].freeze
25
+ attr_reader :root, :settings
26
+
27
+ def initialize(configuration)
28
+ @root = configuration.root
29
+ @settings = configuration.data.fetch("flatpak", {})
30
+ end
31
+
32
+ def self.architecture(target)
33
+ ARCHES.fetch(target.fetch("arch")) { raise Error, "flatpak does not support #{target.fetch('arch')}; use #{ARCHES.keys.join(', ')}" }
34
+ end
35
+
36
+ def self.filename(name, version, target) = "#{name}-#{version}-#{architecture(target)}.flatpak"
37
+
38
+ def self.validate(data)
39
+ building = data.fetch("targets").values.any? { |target| target.fetch("formats", []).include?("flatpak") }
40
+ settings = data["flatpak"]
41
+ raise Error, "flatpak targets need a top-level flatpak section" if building && settings.nil?
42
+ return if settings.nil?
43
+ raise Error, "flatpak: expected a mapping" unless settings.is_a?(Hash)
44
+ raise Error, "flatpak: no target builds the flatpak format" unless building
45
+ unknown = settings.keys - KEYS
46
+ raise Error, "flatpak: unknown fields #{unknown.join(', ')}" unless unknown.empty?
47
+ raise Error, "flatpak.id: use a reverse-DNS application ID such as org.example.App" unless settings["id"].is_a?(String) && ID.match?(settings["id"])
48
+ %w[runtime sdk].each do |key|
49
+ next if key == "sdk" && !settings.key?(key)
50
+ raise Error, "flatpak.#{key}: expected a runtime ID such as org.freedesktop.Platform" unless settings[key].is_a?(String) && RUNTIME.match?(settings[key])
51
+ end
52
+ %w[runtime_version branch].each do |key|
53
+ next if key == "branch" && !settings.key?(key)
54
+ raise Error, "flatpak.#{key}: expected a branch such as 24.08 or stable" unless settings[key].is_a?(String) && BRANCH.match?(settings[key])
55
+ end
56
+ command = settings["command"]
57
+ unless command.is_a?(String) && (/\A[A-Za-z0-9_][A-Za-z0-9_.+-]*\z/.match?(command) || command.start_with?("/app/"))
58
+ raise Error, "flatpak.command: name a program in /app/bin, or an absolute /app path"
59
+ end
60
+ arguments = settings.fetch("finish_args", [])
61
+ unless arguments.is_a?(Array) && arguments.all? { |argument| argument.is_a?(String) && argument.start_with?("--") && !argument.start_with?("--command") }
62
+ raise Error, "flatpak.finish_args: use a list of flatpak build-finish options such as --socket=wayland"
63
+ end
64
+ if settings.key?("metainfo") && !(settings["metainfo"].is_a?(String) && Pathname.new(settings["metainfo"]).relative?)
65
+ raise Error, "flatpak.metainfo: expected a path relative to the configuration"
66
+ end
67
+ if settings.key?("runtime_repo") && !(settings["runtime_repo"].is_a?(String) && settings["runtime_repo"].start_with?("https://"))
68
+ raise Error, "flatpak.runtime_repo: expected an https:// .flatpakrepo URL"
69
+ end
70
+ end
71
+
72
+ def id = settings.fetch("id")
73
+ def branch = settings.fetch("branch", "stable")
74
+
75
+ def doctor(target)
76
+ self.class.architecture(target)
77
+ raise Error, "install flatpak to build Flatpak bundles" unless available?("flatpak")
78
+ end
79
+
80
+ # Returns the bundle's path and what went into it.
81
+ def build(package, target, destination, name:, version:, epoch:)
82
+ doctor(target)
83
+ arch = self.class.architecture(target)
84
+ output = destination / self.class.filename(name, version, target)
85
+ Dir.mktmpdir("native-packages-flatpak-") do |directory|
86
+ directory = Pathname.new(directory)
87
+ tree = directory / "tree"
88
+ tree.mkpath
89
+ stage(package, tree, "flatpak")
90
+ outside = tree.children.map { |path| "/#{path.basename}" } - ["/usr"]
91
+ raise Error, "flatpak: Flatpak apps install only under /usr, which becomes /app; move #{outside.join(', ')}" unless outside.empty?
92
+ build = directory / "build"
93
+ build.mkpath
94
+ files = build / "files"
95
+ (tree / "usr").exist? ? (tree / "usr").rename(files) : files.mkpath
96
+ details = integrate(files)
97
+ metadata = ["[Application]", "name=#{id}", "runtime=#{settings.fetch('runtime')}/#{arch}/#{settings.fetch('runtime_version')}"]
98
+ metadata << "sdk=#{settings.fetch('sdk')}/#{arch}/#{settings.fetch('runtime_version')}" if settings["sdk"]
99
+ write(build / "metadata", metadata.join("\n") + "\n")
100
+ # Installed runtimes would add their own permissions to the metadata;
101
+ # hide them so every host writes the same app.
102
+ installations = directory / "installations"
103
+ environment = { "FLATPAK_SYSTEM_DIR" => (installations / "system").to_s, "FLATPAK_USER_DIR" => (installations / "user").to_s }
104
+ command = settings.fetch("command")
105
+ run "flatpak", "build-finish", "--command=#{command}", *settings.fetch("finish_args", []), build, env: environment
106
+ normalize(build, epoch)
107
+ repository = directory / "repository"
108
+ run "flatpak", "build-export", "--arch=#{arch}", "--timestamp=#{Time.at(epoch).utc.iso8601}", "--no-update-summary",
109
+ "--disable-fsync", repository, build, branch, env: environment
110
+ bundle = ["flatpak", "build-bundle", "--arch=#{arch}"]
111
+ bundle << "--runtime-repo=#{settings['runtime_repo']}" if settings["runtime_repo"]
112
+ run(*bundle, repository, output, id, branch, env: environment)
113
+ check(output, arch)
114
+ # The same inputs give the same commit. The bundle file around it
115
+ # also records when it was written, which SOURCE_DATE_EPOCH does not
116
+ # change, so its hash differs between builds.
117
+ commit = (repository / "refs/heads/app/#{id}/#{arch}/#{branch}").read.strip
118
+ [output, details.merge("id" => id, "branch" => branch, "commit" => commit,
119
+ "runtime" => "#{settings.fetch('runtime')}//#{settings.fetch('runtime_version')}", "command" => command, "libraries" => "runtime")]
120
+ end
121
+ end
122
+
123
+ # Fails unless `path` is a Flatpak bundle of this app for `arch`.
124
+ def check(path, arch)
125
+ bytes = path.binread(4096) || ""
126
+ raise Error, "not a Flatpak bundle: #{path}" unless bytes.start_with?("flatpak\0".b)
127
+ raise Error, "#{path.basename} does not hold app/#{id}/#{arch}/#{branch}" unless bytes.include?("app/#{id}/#{arch}/#{branch}".b)
128
+ end
129
+
130
+ # Flatpak exports only desktop entries, icons and metainfo named after
131
+ # the app ID, so rename the package's own to match.
132
+ def integrate(files)
133
+ command = settings.fetch("command")
134
+ executable = command.start_with?("/") ? files / relative_path(command.delete_prefix("/app/")) : files / "bin" / command
135
+ raise Error, "flatpak: command #{command} is not an executable the package installs" unless executable.file? && executable.executable?
136
+ details = { "desktop" => nil, "icon" => nil, "metainfo" => nil }
137
+ entries = Pathname.glob(files / "share/applications/*.desktop")
138
+ raise Error, "flatpak: at most one desktop entry in /usr/share/applications; found #{entries.length}" if entries.length > 1
139
+ if (desktop = entries.first)
140
+ lines = desktop.read.lines
141
+ icon = lines.find { |line| line.start_with?("Icon=") }&.delete_prefix("Icon=")&.strip
142
+ if icon && icon != id
143
+ raise Error, "flatpak: Icon must be a name, not a path" if icon.include?("/")
144
+ icons = ICON_DIRECTORIES.flat_map { |path| Pathname.glob(files / path / "**" / "#{icon}.{svg,png}") }
145
+ raise Error, "flatpak: the package installs no #{icon}.svg or #{icon}.png icon" if icons.empty?
146
+ icons.each { |path| path.rename(path.dirname / "#{id}#{path.extname}") }
147
+ lines = lines.map { |line| line.start_with?("Icon=") && line.delete_prefix("Icon=").strip == icon ? "Icon=#{id}\n" : line }
148
+ end
149
+ desktop.delete
150
+ write(files / "share/applications/#{id}.desktop", lines.join)
151
+ details["desktop"] = "#{id}.desktop"
152
+ details["icon"] = id if icon
153
+ end
154
+ if settings["metainfo"]
155
+ source = root / relative_path(settings["metainfo"])
156
+ raise Error, "flatpak.metainfo: #{settings['metainfo']} does not exist" unless source.file?
157
+ write(files / "share/metainfo/#{id}.metainfo.xml", source.binread)
158
+ end
159
+ metainfo = Pathname.glob(files / "share/{metainfo,appdata}/*.xml")
160
+ unless metainfo.empty?
161
+ named = metainfo.find { |path| path.basename.to_s.start_with?("#{id}.") }
162
+ raise Error, "flatpak: name the metainfo file #{id}.metainfo.xml" unless named
163
+ unless named.read.match?(%r{<id>\s*#{Regexp.escape(id)}(?:\.desktop)?\s*</id>})
164
+ raise Error, "flatpak: #{named.basename} does not declare <id>#{id}</id>"
165
+ end
166
+ details["metainfo"] = named.basename.to_s
167
+ end
168
+ details
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "flatpak"
4
+ require_relative "hosted_repository"
5
+
6
+ module NativePackages
7
+ # A signed, self-hosted Flatpak repository: an OSTree repository that the
8
+ # build's bundles are imported into, served as static files, with a
9
+ # NAME.flatpakrepo and an ID.flatpakref beside it for users to add.
10
+ #
11
+ # This is not Flathub. Flathub does not accept AI-assisted manifests or
12
+ # submissions, so native-packages neither writes nor submits one.
13
+ class FlatpakRepository < HostedRepository
14
+ # Each bundle is a commit without a parent, so a ref has no history to
15
+ # keep: the repository serves the newest commit of each app.
16
+ KEYS = (HostedRepository::KEYS - %w[keep] + %w[title homepage]).freeze
17
+
18
+ def self.validate(data)
19
+ settings = data["flatpak"].is_a?(Hash) ? data["flatpak"]["repository"] : nil
20
+ return if settings.nil?
21
+ check(settings, "flatpak.repository", KEYS)
22
+ %w[title homepage].each do |key|
23
+ raise Error, "flatpak.repository.#{key}: expected a string" if settings.key?(key) && !settings[key].is_a?(String)
24
+ end
25
+ if settings.key?("homepage") && !settings["homepage"].start_with?("https://")
26
+ raise Error, "flatpak.repository.homepage: expected an https:// URL"
27
+ end
28
+ end
29
+
30
+ def initialize(configuration)
31
+ settings = configuration.data.fetch("flatpak", {})["repository"]
32
+ raise Error, "configure flatpak.repository to publish --to flatpak" unless settings
33
+ super(configuration, settings)
34
+ @flatpak = Flatpak.new(configuration)
35
+ end
36
+
37
+ def title = settings.fetch("title", name)
38
+
39
+ def publish(bundles, secret: nil, passphrase: nil)
40
+ raise Error, "the build contains no Flatpak bundles to publish --to flatpak" if bundles.empty?
41
+ missing = %w[flatpak ostree].reject { |tool| available?(tool) }
42
+ raise Error, "install #{missing.join(' and ')} to publish --to flatpak" unless missing.empty?
43
+ directory.mkpath
44
+ run "ostree", "init", "--mode=archive-z2", "--repo=#{directory}" unless (directory / "config").file?
45
+ signer(secret: secret, passphrase: passphrase).session do |signing|
46
+ bundles.each { |bundle| run "flatpak", "build-import-bundle", "--no-update-summary", *signing.flatpak_options, directory, bundle }
47
+ # Prune drops the objects only replaced commits used.
48
+ run "flatpak", "build-update-repo", *signing.flatpak_options, "--title=#{title}", "--default-branch=#{@flatpak.branch}",
49
+ "--prune", "--generate-static-deltas", directory
50
+ key = [signing.public_key].pack("m0")
51
+ homepage = settings["homepage"] ? ["Homepage=#{settings['homepage']}"] : []
52
+ write(directory / "#{name}.flatpakrepo", (["[Flatpak Repo]", "Title=#{title}", "Url=#{url}/", *homepage,
53
+ "DefaultBranch=#{@flatpak.branch}", "GPGKey=#{key}"].join("\n") + "\n"))
54
+ runtime = @flatpak.settings["runtime_repo"] ? ["RuntimeRepo=#{@flatpak.settings['runtime_repo']}"] : []
55
+ write(directory / "#{@flatpak.id}.flatpakref", (["[Flatpak Ref]", "Name=#{@flatpak.id}", "Branch=#{@flatpak.branch}", "Title=#{title}",
56
+ "Url=#{url}/", "SuggestRemoteName=#{name}", *homepage, *runtime, "IsRuntime=false", "GPGKey=#{key}"].join("\n") + "\n"))
57
+ end
58
+ puts "Published #{bundles.length} Flatpak bundle#{"s" unless bundles.length == 1} to #{directory}. Install with:", *instructions.map { |line| " #{line}" }
59
+ end
60
+
61
+ def instructions = ["flatpak install --user #{url}/#{@flatpak.id}.flatpakref"]
62
+ end
63
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "openpgp"
4
+
5
+ module NativePackages
6
+ # A signed package repository that native-packages writes into a directory,
7
+ # such as a GitHub Pages checkout, to be served as static files. Writing is
8
+ # all it does: the release process commits and pushes the directory.
9
+ class HostedRepository
10
+ include Support
11
+ KEYS = %w[directory url keep signing_key].freeze
12
+ KEEP = 3
13
+ attr_reader :root, :name, :settings, :directory
14
+
15
+ # Checks the settings shared by every hosted repository, under `label`.
16
+ def self.check(settings, label, keys)
17
+ raise Error, "#{label}: expected a mapping" unless settings.is_a?(Hash)
18
+ unknown = settings.keys - keys
19
+ raise Error, "#{label}: unknown fields #{unknown.join(', ')}" unless unknown.empty?
20
+ raise Error, "#{label}.directory: name the directory to write the repository into" unless settings["directory"].is_a?(String) && !settings["directory"].empty?
21
+ unless settings["url"].is_a?(String) && %r{\Ahttps://[^\s/]+(?:/\S*)?\z}.match?(settings["url"])
22
+ raise Error, "#{label}.url: expected the https:// URL the directory is served at"
23
+ end
24
+ keep = settings.fetch("keep", KEEP)
25
+ raise Error, "#{label}.keep: expected how many versions of each package to keep (1 or more)" unless keep.is_a?(Integer) && keep.positive?
26
+ OpenPGP.fingerprint(settings["signing_key"], "#{label}.signing_key")
27
+ end
28
+
29
+ def initialize(configuration, settings)
30
+ @root = configuration.root
31
+ @name = configuration.name
32
+ @settings = settings
33
+ @directory = Pathname.new(settings.fetch("directory")).expand_path(root)
34
+ end
35
+
36
+ def url = settings.fetch("url").chomp("/")
37
+ def keep = settings.fetch("keep", KEEP)
38
+
39
+ def signer(secret: nil, passphrase: nil)
40
+ OpenPGP.new(OpenPGP.fingerprint(settings.fetch("signing_key"), "signing_key"), secret: secret, passphrase: passphrase)
41
+ end
42
+
43
+ # Copies `path` to `destination`, refusing to change a published file.
44
+ def place(path, destination)
45
+ if destination.exist?
46
+ raise Error, "#{destination.relative_path_from(directory)} is already published with different contents" unless sha256(destination) == sha256(path)
47
+ return
48
+ end
49
+ destination.dirname.mkpath
50
+ FileUtils.cp(path, destination)
51
+ destination.chmod(0o644)
52
+ end
53
+
54
+ # Deletes all but the newest `keep` versions in each group of `entries`
55
+ # (path => [group, version, ...]), ordered by `compare`, and returns
56
+ # the entries that remain.
57
+ def prune(entries, &compare)
58
+ removed = entries.group_by { |_, details| details.first }.values.flat_map do |versions|
59
+ versions.sort { |left, right| compare.call(right.last[1], left.last[1]) }.drop(keep).map(&:first)
60
+ end
61
+ removed.each do |path|
62
+ path.delete
63
+ puts "Removed #{path.relative_path_from(directory)}"
64
+ parent = path.dirname
65
+ while parent != directory && parent.empty?
66
+ parent.rmdir
67
+ parent = parent.dirname
68
+ end
69
+ end
70
+ entries.except(*removed)
71
+ end
72
+ end
73
+ end
@@ -115,8 +115,9 @@ module NativePackages
115
115
  depends << (format == "deb" ? "libc6 (>= #{floor})" : "glibc >= #{floor}")
116
116
  end
117
117
  override["depends"] = depends.uniq
118
- elsif format == "appimage"
119
- # Nothing is bundled: the host supplies these, as for deb and rpm.
118
+ elsif %w[appimage flatpak snap].include?(format)
119
+ # Nothing is bundled: the host (AppImage), the Flatpak runtime or the
120
+ # snap's base supplies these.
120
121
  elsif !external.empty? && package.fetch("overrides", {}).fetch(format, {}).fetch("depends", package.fetch("depends", [])).empty?
121
122
  raise Error, "#{format}: declare dependencies for #{external.join(', ')}"
122
123
  end
@@ -114,14 +114,14 @@ module NativePackages
114
114
  end
115
115
 
116
116
  def notarize(package)
117
- execute "hdiutil", "verify", package
117
+ verify_image(package)
118
118
  execute "codesign", "--force", "--timestamp", "--keychain", @keychain,
119
119
  "--sign", @credentials.fetch("APPLE_SIGNING_IDENTITY"), package
120
120
  execute "codesign", "--verify", "--strict", package
121
121
  response = submit(package)
122
122
  execute "xcrun", "stapler", "staple", package
123
123
  execute "xcrun", "stapler", "validate", package
124
- execute "hdiutil", "verify", package
124
+ verify_image(package)
125
125
  puts "Apple accepted #{package.basename}; its notarization ticket is stapled and validated."
126
126
  { "signature" => "developer-id", "notarization" => "accepted", "submission_id" => response.fetch("id"), "stapled" => true }
127
127
  end
@@ -179,6 +179,24 @@ module NativePackages
179
179
  output
180
180
  end
181
181
 
182
+ # hdiutil can still hold a freshly written image for a moment and refuse
183
+ # to verify it with "Resource temporarily unavailable"; that is no fault
184
+ # of the image, so it gets a few more tries.
185
+ def verify_image(package)
186
+ attempts = 0
187
+ begin
188
+ execute "hdiutil", "verify", package
189
+ rescue Error => error
190
+ attempts += 1
191
+ raise unless error.message.include?("Resource temporarily unavailable") && attempts < 5
192
+
193
+ sleep verify_delay * attempts
194
+ retry
195
+ end
196
+ end
197
+
198
+ def verify_delay = 2
199
+
182
200
  def execute(*arguments)
183
201
  capture(*arguments)
184
202
  rescue Error => error
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NativePackages
4
+ # OpenPGP signatures for hosted package repositories, made with GnuPG.
5
+ #
6
+ # The key is an armored secret key in NATIVE_PACKAGES_GPG_KEY, imported
7
+ # into a temporary keyring that is deleted afterwards, or else a key the
8
+ # user's own GnuPG (and its agent) already holds. Either way it must be the
9
+ # configured fingerprint, and every signature is checked against that
10
+ # key's public half alone before it is published.
11
+ class OpenPGP
12
+ include Support
13
+ KEY ="NATIVE_PACKAGES_GPG_KEY"
14
+ PASSPHRASE = "NATIVE_PACKAGES_GPG_PASSPHRASE"
15
+ FINGERPRINT = /\A(?:\h{40}|\h{64})\z/
16
+
17
+ # The signing key and passphrase from the environment, removed from it
18
+ # so that no later subprocess inherits them.
19
+ def self.environment = { secret: ENV.delete(KEY), passphrase: ENV.delete(PASSPHRASE) }
20
+
21
+ def self.fingerprint(value, label)
22
+ fingerprint = value.is_a?(String) ? value.delete(" ").upcase : ""
23
+ raise Error, "#{label}: expected the signing key's full fingerprint (40 or 64 hexadecimal digits)" unless FINGERPRINT.match?(fingerprint)
24
+ fingerprint
25
+ end
26
+
27
+ attr_reader :fingerprint, :home
28
+
29
+ def initialize(fingerprint, secret: nil, passphrase: nil)
30
+ @fingerprint = fingerprint
31
+ @secret = secret
32
+ @passphrase = passphrase
33
+ end
34
+
35
+ # Yields self, ready to sign, then deletes any temporary keyring.
36
+ def session
37
+ raise Error, "install gpg (GnuPG) to sign repositories" unless available?("gpg")
38
+ Dir.mktmpdir("native-packages-gpg-") do |directory|
39
+ directory = Pathname.new(directory)
40
+ homes = []
41
+ begin
42
+ if @secret && !@secret.empty?
43
+ @home = keyring(directory / "signing", homes)
44
+ gpg("--import", input: @secret)
45
+ end
46
+ # gpg fails outright when it has no such key.
47
+ listed = begin
48
+ gpg("--list-secret-keys", "--with-colons", fingerprint).lines.grep(/\Afpr:/).map { |line| line.split(":")[9] }
49
+ rescue Error
50
+ []
51
+ end
52
+ unless listed.include?(fingerprint)
53
+ raise Error, "GnuPG has no secret key #{fingerprint}; set #{KEY} to its armored secret key, or import it"
54
+ end
55
+ @public = gpg("--export", fingerprint)
56
+ @verifier = keyring(directory / "verifying", homes)
57
+ gpg("--import", input: @public, home: @verifier)
58
+ yield self
59
+ ensure
60
+ # Stop the agents the temporary keyrings started.
61
+ homes.each { |home| Open3.capture3("gpgconf", "--homedir", home.to_s, "--kill", "all") } if available?("gpgconf")
62
+ @home = nil
63
+ end
64
+ end
65
+ end
66
+
67
+ # The public key, binary or ASCII-armored.
68
+ def public_key(armor: false) = armor ? gpg("--armor", "--export", fingerprint, home: @verifier) : @public
69
+
70
+ # Options that make flatpak sign with this key.
71
+ def flatpak_options = ["--gpg-sign=#{fingerprint}", *(home ? ["--gpg-homedir=#{home}"] : [])]
72
+
73
+ def clearsign(input, output)
74
+ sign("--clearsign", input, output)
75
+ verify(output)
76
+ end
77
+
78
+ def detach_sign(input, output)
79
+ sign("--detach-sign", input, output, "--armor")
80
+ verify(output, input)
81
+ end
82
+
83
+ # Fails unless `signature` (over `data`, when detached) was made by the
84
+ # configured key.
85
+ def verify(signature, data = nil)
86
+ status = gpg("--status-fd", "1", "--verify", signature.to_s, *(data ? [data.to_s] : []), home: @verifier)
87
+ signers = status.lines.grep(/\A\[GNUPG:\] VALIDSIG /).map { |line| line.split.last }
88
+ raise Error, "#{Pathname.new(signature).basename} is not signed by #{fingerprint}" unless signers == [fingerprint]
89
+ end
90
+
91
+ private
92
+
93
+ def keyring(path, homes)
94
+ path.mkpath
95
+ path.chmod(0o700)
96
+ homes << path
97
+ path
98
+ end
99
+
100
+ def sign(mode, input, output, *options)
101
+ unlock = @passphrase ? ["--pinentry-mode", "loopback", "--passphrase-fd", "0"] : []
102
+ gpg(*unlock, "--local-user", fingerprint, "--digest-algo", "SHA512", *options, mode, "--output", output.to_s, input.to_s,
103
+ input: @passphrase && "#{@passphrase}\n")
104
+ end
105
+
106
+ # Runs gpg and returns its output bytes, unchanged.
107
+ def gpg(*arguments, input: nil, home: @home)
108
+ command = ["gpg", "--batch", "--yes", "--no-tty", "--quiet", *(home ? ["--homedir", home.to_s] : []), *arguments]
109
+ output, error, status = Open3.capture3(*command, stdin_data: input.to_s, binmode: true)
110
+ raise Error, "gpg failed: #{error.strip}" unless status.success?
111
+ output
112
+ end
113
+ end
114
+ end