From a9bd4e3a14500e1c3e7fffbc91bd9f0923d42712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tina=20M=C3=BCller?= Date: Fri, 22 Jan 2021 14:28:30 +0100 Subject: [PATCH] flatpak: Replace source urls with file paths This allows users to keep the original url in the flatpak manifest for reference. At build time flatpak-builder will work with the file urls. --- Build/Flatpak.pm | 76 +++++++++++++++++++++++------------ build-recipe-flatpak | 4 ++ t/data/flatpak-rewritten.yaml | 62 ++++++++++++++++++++++++++++ t/data/flatpak.json | 8 ++-- t/data/flatpak.yaml | 8 ++-- t/flatpak.t | 42 ++++++++++++++----- 6 files changed, 158 insertions(+), 42 deletions(-) create mode 100644 t/data/flatpak-rewritten.yaml diff --git a/Build/Flatpak.pm b/Build/Flatpak.pm index ceb05a1a0..0cd07790a 100644 --- a/Build/Flatpak.pm +++ b/Build/Flatpak.pm @@ -59,11 +59,30 @@ sub _load_yaml_file { die "Neither YAML::PP nor YAML::XS available\n"; } +sub _read_manifest { + my ($fn) = @_; + my $data; + if ($fn =~ m/\.ya?ml\z/) { + $data = _load_yaml_file($fn); + return { error => "Failed to parse YAML file '$fn'" } unless defined $data; + } elsif ($fn =~ m/\.json\z/) { + # We don't have JSON::PP, but YAML is a superset of JSON anyway + $data = _load_yaml_file($fn); + return { error => "Failed to parse JSON file '$fn'" } unless defined $data; + } elsif (ref($fn) eq 'SCALAR') { + $data = _load_yaml($$fn); # used in the unit test + return { error => "Failed to parse '$fn'" } unless defined $data; + } else { + $data = _load_yaml_file($fn); + return { error => "Failed to parse file '$fn'" } unless defined $data; + } + return $data; +} + sub parse { my ($cf, $fn) = @_; my $version = ''; - my $data; my @lines; if (ref($fn) eq 'SCALAR') { @lines = split m/(?<=\n)/, $$fn; @@ -86,26 +105,7 @@ sub parse { } } - if ($fn =~ m/\.ya?ml\z/) { - $data = _load_yaml_file($fn); - return { error => "Failed to parse YAML file '$fn'" } unless defined $data; - } elsif ($fn =~ m/\.json\z/) { - # We don't have JSON::PP, but YAML is a superset of JSON - $data = _load_yaml_file($fn); - return { error => "Failed to parse JSON file '$fn'" } unless defined $data; -# open my $fh, '<:encoding(UTF-8)', $fn or die $!; -# my $json = do { local $/; <$fh> }; -# close $fh; -# $data = eval { decode_json($json) }; -# return { error => "Failed to parse JSON file" } unless defined $data; - } elsif (ref($fn) eq 'SCALAR') { - $data = _load_yaml($$fn); # used in the unit test - return { error => "Failed to parse '$fn'" } unless defined $data; - } else { - $data = _load_yaml_file($fn); - return { error => "Failed to parse file '$fn'" } unless defined $data; - } - + my $data = _read_manifest($fn); my $ret = {}; $ret->{version} = $version if $version; $ret->{name} = $data->{'app-id'} or die "Flatpak file is missing 'app-id'"; @@ -124,10 +124,7 @@ sub parse { if (my $sources = $module->{sources}) { for my $source (@$sources) { if ($source->{type} eq 'archive') { - my $url = $source->{url}; - my $path = $url; - $path =~ s{.*/}{}; # Get filename - push @sources, $path; + push @sources, $source->{url}; } } } @@ -153,4 +150,33 @@ sub show { } } +# This replaces http urls with local file urls because during build +# flatpak-builder has no network +sub rewrite { + my ($fn) = @ARGV; + my $data = _read_manifest($fn); + if (my $modules = $data->{modules}) { + for my $module (@$modules) { + if (my $sources = $module->{sources}) { + for my $source (@$sources) { + if ($source->{type} eq 'archive') { + my $path = $source->{url}; + $path =~ s{.*/}{}; # Get filename + $source->{url} = "file:///usr/src/packages/SOURCES/$path"; + } + } + } + } + } + my $yaml = ''; + if ($yamlpp) { + # YAML::PP would allow us to keep key order + $yaml = $yamlpp->dump_string($data); + } + elsif ($yamlxs) { + $yaml = YAML::XS::Dump($data); + } + print $yaml; +} + 1; diff --git a/build-recipe-flatpak b/build-recipe-flatpak index 4d287e812..d0abc600a 100644 --- a/build-recipe-flatpak +++ b/build-recipe-flatpak @@ -81,8 +81,12 @@ recipe_build_flatpak() { cp $BUILD_ROOT$TOPDIR/SOURCES/* . local FLATPAK_FILE="$TOPDIR/SOURCES/$RECIPEFILE" + local FLATPAK_FILE_LOCAL="$TOPDIR/SOURCES/flatpak-local.yaml" local FLATPAK_APP=$(perl -I$BUILD_DIR -MBuild::Flatpak -e 'Build::Flatpak::show' -- "$BUILD_ROOT$FLATPAK_FILE" name) local FLATPAK_APP_VERSION=$(perl -I$BUILD_DIR -MBuild::Flatpak -e 'Build::Flatpak::show' -- "$BUILD_ROOT$FLATPAK_FILE" version) + # Rewrite http urls to file urls + perl -I$BUILD_DIR -MBuild::Flatpak -e 'Build::Flatpak::rewrite' -- "$BUILD_ROOT$FLATPAK_FILE" > "$BUILD_ROOT$FLATPAK_FILE_LOCAL" + FLATPAK_FILE="$FLATPAK_FILE_LOCAL" FLATPAK_APP_VERSION="${FLATPAK_APP_VERSION:-0}" local FLATPAK_REPO=/tmp/flatpakrepo local FLATPAK_REMOTE=https://dl.flathub.org/repo/flathub.flatpakrepo diff --git a/t/data/flatpak-rewritten.yaml b/t/data/flatpak-rewritten.yaml new file mode 100644 index 000000000..5cbc65922 --- /dev/null +++ b/t/data/flatpak-rewritten.yaml @@ -0,0 +1,62 @@ +--- +app-id: org.gnome.Chess +cleanup: +- /share/gnuchess +- /share/info +- /share/man +- /include +command: gnome-chess +finish-args: +- --share=ipc +- --socket=fallback-x11 +- --socket=wayland +- --metadata=X-DConf=migrate-path=/org/gnome/Chess/ +modules: +- build-commands: + - make + - install -D phalanx /app/bin/phalanx + - install -D pbook.phalanx /app/share/phalanx/pbook.phalanx + - install -D sbook.phalanx /app/share/phalanx/sbook.phalanx + - install -D eco.phalanx /app/share/phalanx/eco.phalanx + buildsystem: simple + name: phalanx + sources: + - sha256: b3874d5dcd22c64626b2c955b18b27bcba3aa727855361a91eab57a6324b22dd + type: archive + url: file:///usr/src/packages/SOURCES/phalanx-XXV-source.tgz + - path: phalanx-books-path.patch + type: patch +- build-commands: + - make build + - install -D stockfish /app/bin/stockfish + build-options: + arch: + aarch64: + env: + ARCH: armv7 + arm: + env: + ARCH: armv7 + x86_64: + env: + ARCH: x86-64 + buildsystem: simple + name: stockfish + sources: + - sha256: 29bd01e7407098aa9e851b82f6ea4bf2b46d26e9075a48a269cb1e40c582a073 + type: archive + url: file:///usr/src/packages/SOURCES/stockfish-10-src.zip +- name: gnuchess + sources: + - sha256: 9a99e963355706cab32099d140b698eda9de164ebce40a5420b1b9772dd04802 + type: archive + url: file:///usr/src/packages/SOURCES/gnuchess-6.2.5.tar.gz +- buildsystem: meson + name: gnome-chess + sources: + - sha256: b195c9f17a59d7fcc892ff55e6a6ebdd16e7329157bf37e3c2fe593b349aab98 + type: archive + url: file:///usr/src/packages/SOURCES/gnome-chess-3.36.1.tar.xz +runtime: org.gnome.Platform +runtime-version: '3.36' +sdk: org.gnome.Sdk diff --git a/t/data/flatpak.json b/t/data/flatpak.json index d26e014b9..67f5c7d4f 100644 --- a/t/data/flatpak.json +++ b/t/data/flatpak.json @@ -25,7 +25,7 @@ "sources": [ { "type": "archive", - "url": "file:///usr/src/packages/SOURCES/phalanx-XXV-source.tgz", + "url": "https://example.org/phalanx-XXV-source.tgz", "sha256": "b3874d5dcd22c64626b2c955b18b27bcba3aa727855361a91eab57a6324b22dd" }, { @@ -63,7 +63,7 @@ "sources": [ { "type": "archive", - "url": "file:///usr/src/packages/SOURCES/stockfish-10-src.zip", + "url": "https://example.org/stockfish-10-src.zip", "sha256": "29bd01e7407098aa9e851b82f6ea4bf2b46d26e9075a48a269cb1e40c582a073" } ] @@ -73,7 +73,7 @@ "sources": [ { "type": "archive", - "url": "file:///usr/src/packages/SOURCES/gnuchess-6.2.5.tar.gz", + "url": "https://example.org/gnuchess-6.2.5.tar.gz", "sha256": "9a99e963355706cab32099d140b698eda9de164ebce40a5420b1b9772dd04802" } ] @@ -84,7 +84,7 @@ "sources": [ { "type": "archive", - "url": "file:///usr/src/packages/SOURCES/gnome-chess-3.36.1.tar.xz", + "url": "https://example.org/gnome-chess-3.36.1.tar.xz", "sha256": "b195c9f17a59d7fcc892ff55e6a6ebdd16e7329157bf37e3c2fe593b349aab98" } ] diff --git a/t/data/flatpak.yaml b/t/data/flatpak.yaml index 5484bd53a..5fd76960c 100644 --- a/t/data/flatpak.yaml +++ b/t/data/flatpak.yaml @@ -23,7 +23,7 @@ modules: - install -D eco.phalanx /app/share/phalanx/eco.phalanx sources: - type: archive - url: "file:///usr/src/packages/SOURCES/phalanx-XXV-source.tgz" + url: "https://example.org/phalanx-XXV-source.tgz" sha256: b3874d5dcd22c64626b2c955b18b27bcba3aa727855361a91eab57a6324b22dd - type: patch path: phalanx-books-path.patch @@ -46,18 +46,18 @@ modules: - install -D stockfish /app/bin/stockfish sources: - type: archive - url: "file:///usr/src/packages/SOURCES/stockfish-10-src.zip" + url: "https://example.org/stockfish-10-src.zip" sha256: 29bd01e7407098aa9e851b82f6ea4bf2b46d26e9075a48a269cb1e40c582a073 - name: gnuchess sources: - type: archive - url: "file:///usr/src/packages/SOURCES/gnuchess-6.2.5.tar.gz" + url: "https://example.org/gnuchess-6.2.5.tar.gz" sha256: 9a99e963355706cab32099d140b698eda9de164ebce40a5420b1b9772dd04802 - name: gnome-chess buildsystem: meson sources: - type: archive - url: "file:///usr/src/packages/SOURCES/gnome-chess-3.36.1.tar.xz" + url: "https://example.org/gnome-chess-3.36.1.tar.xz" sha256: b195c9f17a59d7fcc892ff55e6a6ebdd16e7329157bf37e3c2fe593b349aab98 diff --git a/t/flatpak.t b/t/flatpak.t index 72cccb2c6..0619e9846 100644 --- a/t/flatpak.t +++ b/t/flatpak.t @@ -9,6 +9,11 @@ my $path = "$Bin/data"; use Build; use Build::Flatpak; +my $rewritten_manifest = do { + local $/; + open my $fh, '<', "$Bin/data/flatpak-rewritten.yaml" or die $!; + <$fh>; +}; my $conf = Build::read_config('x86_64'); sub capture_stdout { @@ -19,15 +24,19 @@ sub capture_stdout { return $cap; } +my @sources = map { "https://example.org/$_" } ( + "phalanx-XXV-source.tgz", + "stockfish-10-src.zip", + "gnuchess-6.2.5.tar.gz", + "gnome-chess-3.36.1.tar.xz", +); + subtest parse => sub { my $expected = { name => 'org.gnome.Chess', version => '3.36.2', sources => [ - "phalanx-XXV-source.tgz", - "stockfish-10-src.zip", - "gnuchess-6.2.5.tar.gz", - "gnome-chess-3.36.1.tar.xz", + @sources, ], deps => [ 'org.gnome.Sdk-v3.36', @@ -52,7 +61,22 @@ subtest show => sub { @ARGV = ("$path/flatpak.yaml", 'sources'); $data = capture_stdout(sub { Build::Flatpak::show() }); - is $data, "phalanx-XXV-source.tgz\nstockfish-10-src.zip\ngnuchess-6.2.5.tar.gz\ngnome-chess-3.36.1.tar.xz\n", 'Build::Flatpak::show sources'; + my $exp_sources = join '', map { "$_\n" } @sources; + is $data, $exp_sources, 'Build::Flatpak::show sources'; +}; + +subtest rewrite => sub { + local @ARGV = ("$path/flatpak.yaml"); + my $yaml = capture_stdout(sub { Build::Flatpak::rewrite() }); + my $ok = is $yaml, $rewritten_manifest, 'Build::Flatpak::show rewrite'; + unless ($ok) { + open my $fh, '>', "/tmp/flatpak.t.compare.yaml" or die $!; + print $fh $yaml; + close $fh; + my $diff = qx{diff $Bin/data/flatpak-rewritten.yaml /tmp/flatpak.t.compare.yaml}; + diag "Diff:\n$diff"; + } + }; done_testing; @@ -85,7 +109,7 @@ __DATA__ "sources": [ { "type": "archive", - "url": "file:///usr/src/packages/SOURCES/phalanx-XXV-source.tgz", + "url": "https://example.org/phalanx-XXV-source.tgz", "sha256": "b3874d5dcd22c64626b2c955b18b27bcba3aa727855361a91eab57a6324b22dd" }, { @@ -123,7 +147,7 @@ __DATA__ "sources": [ { "type": "archive", - "url": "file:///usr/src/packages/SOURCES/stockfish-10-src.zip", + "url": "https://example.org/stockfish-10-src.zip", "sha256": "29bd01e7407098aa9e851b82f6ea4bf2b46d26e9075a48a269cb1e40c582a073" } ] @@ -133,7 +157,7 @@ __DATA__ "sources": [ { "type": "archive", - "url": "file:///usr/src/packages/SOURCES/gnuchess-6.2.5.tar.gz", + "url": "https://example.org/gnuchess-6.2.5.tar.gz", "sha256": "9a99e963355706cab32099d140b698eda9de164ebce40a5420b1b9772dd04802" } ] @@ -144,7 +168,7 @@ __DATA__ "sources": [ { "type": "archive", - "url": "file:///usr/src/packages/SOURCES/gnome-chess-3.36.1.tar.xz", + "url": "https://example.org/gnome-chess-3.36.1.tar.xz", "sha256": "b195c9f17a59d7fcc892ff55e6a6ebdd16e7329157bf37e3c2fe593b349aab98" } ]