Skip to content

Commit 57b93bf

Browse files
committed
Rework mkosi handling
- use PBuild::Mkosi instead of PBuild::Container - be more carful in the binlink generation code - we do not need the query function in Build::Mkosi.
1 parent 5a078c2 commit 57b93bf

File tree

5 files changed

+99
-127
lines changed

5 files changed

+99
-127
lines changed

Build.pm

-2
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,6 @@ sub query {
13671367
return Build::Kiwi::queryiso($handle, %opts) if $do_kiwi && $binname =~ /\.iso$/;
13681368
return Build::Arch::query($handle, %opts) if $do_arch && $binname =~ /\.pkg\.tar(?:\.gz|\.xz|\.zst)?$/;
13691369
return Build::Arch::query($handle, %opts) if $do_arch && $binname =~ /\.arch$/;
1370-
return Build::Mkosi::queryiso($handle, %opts) if $do_mkosi && $binname =~ /\.(raw|tar|cpio|qcow2)(?:\.gz|\.xz|\.zstd)?$/;
13711370
return undef;
13721371
}
13731372

@@ -1393,7 +1392,6 @@ sub queryhdrmd5 {
13931392
my ($binname) = @_;
13941393
return Build::Rpm::queryhdrmd5(@_) if $do_rpm && $binname =~ /\.d?rpm$/;
13951394
return Build::Deb::queryhdrmd5(@_) if $do_deb && $binname =~ /\.deb$/;
1396-
return Build::Mkosi::queryhdrmd5(@_) if $do_mkosi && $binname =~ /\.(raw|tar|cpio|qcow2)(?:\.gz|\.xz|\.zstd)?$/;
13971395
return Build::Kiwi::queryhdrmd5(@_) if $do_kiwi && $binname =~ /\.iso$/;
13981396
return Build::Kiwi::queryhdrmd5(@_) if $do_kiwi && $binname =~ /\.raw$/;
13991397
return Build::Kiwi::queryhdrmd5(@_) if $do_kiwi && $binname =~ /\.raw.install$/;

Build/Mkosi.pm

-61
Original file line numberDiff line numberDiff line change
@@ -78,65 +78,4 @@ sub parse {
7878
return $ret;
7979
}
8080

81-
sub queryiso {
82-
my ($file, %opts) = @_;
83-
my $json_fh;
84-
my $md5 = Digest::MD5->new;
85-
86-
open(my $fh, '<', $file) or die("Error opening $file: $!\n");
87-
$md5->addfile($fh);
88-
close($fh);
89-
# If we also have split verity artifacts, the manifest file is the same as the main image,
90-
# so remove the suffixes to find it
91-
$file =~ s/(\.root|\.usr)//g;
92-
$file = $file . ".manifest.gz";
93-
94-
eval { require JSON; };
95-
*JSON::decode_json = sub {die("JSON::decode_json is not available\n")} unless defined &JSON::decode_json;
96-
97-
eval { require IO::Uncompress::Gunzip; };
98-
*IO::Uncompress::Gunzip::new = sub {die("IO::Uncompress::Gunzip is not available\n")} unless defined &IO::Uncompress::Gunzip::new;
99-
100-
my $json_text = do {
101-
open($json_fh, "<", $file) or die("Error opening $file: $!\n");
102-
$json_fh = IO::Uncompress::Gunzip->new($json_fh) or die("Error opening $file: $IO::Uncompress::Gunzip::GunzipError\n");
103-
local $/;
104-
<$json_fh>
105-
};
106-
107-
my $metadata = JSON::decode_json($json_text);
108-
close $json_fh;
109-
110-
if (!$metadata || !$metadata->{'config'}) {
111-
return {};
112-
}
113-
114-
my $distribution = $metadata->{'config'}->{'distribution'};
115-
my $release = $metadata->{'config'}->{'release'};
116-
my $architecture = $metadata->{'config'}->{'architecture'};
117-
my $name = $metadata->{'config'}->{'name'};
118-
my $version = $metadata->{'config'}->{'version'};
119-
my @provides = ("$distribution:$release");
120-
121-
return {
122-
'provides' => \@provides,
123-
'version' => $version,
124-
'arch' => $architecture,
125-
'name' => $name,
126-
'source' => $name,
127-
'hdrmd5' => $md5->hexdigest(),
128-
};
129-
}
130-
131-
sub queryhdrmd5 {
132-
my ($bin) = @_;
133-
134-
open(my $fh, '<', $bin) or croak("could not open $bin");
135-
my $md5 = Digest::MD5->new;
136-
$md5->addfile($fh);
137-
close($fh);
138-
139-
return $md5->hexdigest();
140-
}
141-
14281
1;

PBuild/BuildResult.pm

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use Build::SimpleXML;
2828
use PBuild::Util;
2929
use PBuild::Verify;
3030
use PBuild::Container;
31+
use PBuild::Mkosi;
3132

3233
my @binsufs = qw{rpm deb pkg.tar.gz pkg.tar.xz pkg.tar.zst};
3334
my $binsufsre = join('|', map {"\Q$_\E"} @binsufs);
@@ -130,11 +131,11 @@ sub integrate_build_result {
130131
PBuild::Util::cp_a($result->{$file}, "$dst/$file");
131132
next;
132133
}
133-
if ($file =~ /(.*)\.manifest(?:\.(?:gz|bz2|xz|zst|zstd))?$/) {
134+
if ($p->{'buildtype'} eq 'mkosi' && $file =~ /(.*)\.manifest(?:\.(?:gz|bz2|xz|zst|zstd))?$/) {
134135
# create an obsbinlnk file from the mkosi manifest
135136
my $prefix = $1;
136137
die unless $result->{$file} =~ /^(.*)\/([^\/]+)$/;
137-
my $obsbinlnk = PBuild::Container::manifest2obsbinlnk($1, $2, $prefix, $p->{'pkg'});
138+
my $obsbinlnk = PBuild::Mkosi::manifest2obsbinlnk($1, $2, $prefix, $p->{'pkg'});
138139
PBuild::Util::store("$dst/$prefix.obsbinlnk", undef, $obsbinlnk) if $obsbinlnk;
139140
}
140141
PBuild::Util::cp($result->{$file}, "$dst/$file");

PBuild/Container.pm

-62
Original file line numberDiff line numberDiff line change
@@ -28,70 +28,8 @@ use PBuild::Verify;
2828
eval { require JSON::XS };
2929
*JSON::XS::decode_json = sub {die("JSON::XS is not available\n")} unless defined &JSON::XS::decode_json;
3030

31-
eval { require IO::Uncompress::Gunzip; };
32-
*IO::Uncompress::Gunzip::new = sub {die("IO::Uncompress::Gunzip is not available\n")} unless defined &IO::Uncompress::Gunzip::new;
33-
3431
use strict;
3532

36-
sub manifest2obsbinlnk {
37-
my ($dir, $file, $prefix, $packid) = @_;
38-
my $json_fh;
39-
my $md5 = Digest::MD5->new;
40-
my $image;
41-
my $json_text = do {
42-
unless (open($json_fh, "<", "$dir/$file")) {
43-
warn("Error opening $dir/$file: $!\n");
44-
return {};
45-
}
46-
if ($file =~ /\.gz$/) {
47-
$json_fh = IO::Uncompress::Gunzip->new($json_fh) or die("Error opening $dir/$file: $IO::Uncompress::Gunzip::GunzipError\n");
48-
}
49-
local $/;
50-
<$json_fh>
51-
};
52-
53-
my $metadata = JSON::XS::decode_json($json_text);
54-
if (!$metadata || !$metadata->{'config'}) {
55-
return {};
56-
}
57-
58-
for my $ext ("", ".raw", ".gz", ".xz", ".zst", ".zstd") {
59-
my $fn = "$dir/$prefix$ext";
60-
if (-e $fn) {
61-
if (-l $fn) {
62-
$prefix = readlink($fn);
63-
}
64-
open(my $fh, '<', "$dir/$prefix$ext") or die("Error opening $dir/$prefix$ext: $!\n");
65-
$md5->addfile($fh);
66-
close($fh);
67-
$image = $prefix . $ext;
68-
last;
69-
}
70-
}
71-
if (!$image) {
72-
return {};
73-
}
74-
75-
my $distribution = $metadata->{'config'}->{'distribution'};
76-
my $release = $metadata->{'config'}->{'release'};
77-
my $architecture = $metadata->{'config'}->{'architecture'};
78-
my $name = $metadata->{'config'}->{'name'};
79-
my $version = $metadata->{'config'}->{'version'} || '0';
80-
# Note: release here is not the RPM release, but the distribution release (eg: Debian 10)
81-
my @provides = ("$distribution:$release", "mkosi:$name = $version", "mkosi:$packid = $version");
82-
83-
return {
84-
'provides' => \@provides,
85-
'source' => $packid,
86-
'name' => "mkosi:$name",
87-
'version' => $version,
88-
'release' => '0',
89-
'arch' => $architecture,
90-
'hdrmd5' => $md5->hexdigest(),
91-
'lnk' => $image,
92-
};
93-
}
94-
9533
sub containerinfo2nevra {
9634
my ($d) = @_;
9735
my $lnk = {};

PBuild/Mkosi.pm

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
################################################################ #
2+
# Copyright (c) 2021 SUSE LLC
3+
#
4+
# This program is free software; you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License version 2 or 3 as
6+
# published by the Free Software Foundation.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program (see the file COPYING); if not, write to the
15+
# Free Software Foundation, Inc.,
16+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17+
#
18+
################################################################
19+
20+
package PBuild::Mkosi;
21+
22+
use Digest::MD5 ();
23+
use IO::Uncompress::Gunzip ();
24+
25+
use PBuild::Verify;
26+
27+
eval { require JSON::XS };
28+
*JSON::XS::decode_json = sub {die("JSON::XS is not available\n")} unless defined &JSON::XS::decode_json;
29+
30+
use strict;
31+
32+
sub manifest2obsbinlnk {
33+
my ($dir, $file, $prefix, $packid) = @_;
34+
my $json_fh;
35+
my $image;
36+
my $json_text = do {
37+
unless (open($json_fh, "<", "$dir/$file")) {
38+
warn("Error opening $dir/$file: $!\n");
39+
return {};
40+
}
41+
if ($file =~ /\.gz$/) {
42+
$json_fh = IO::Uncompress::Gunzip->new($json_fh) or die("Error opening $dir/$file: $IO::Uncompress::Gunzip::GunzipError\n");
43+
}
44+
local $/;
45+
<$json_fh>
46+
};
47+
48+
my $metadata = eval { JSON::XS::decode_json($json_text) };
49+
return unless $metadata && $metadata->{'config'} && ref($metadata->{'config'}) eq 'HASH';
50+
51+
for my $ext ("", ".raw", ".gz", ".xz", ".zst", ".zstd") {
52+
my $fn = "$dir/$prefix$ext";
53+
if (-e $fn) {
54+
if (-l $fn) {
55+
$prefix = readlink($fn);
56+
$prefix =~ s/.*\///;
57+
}
58+
$image = $prefix . $ext;
59+
last
60+
}
61+
}
62+
return unless $image;
63+
eval { PBuild::Verify::verify_filename($image) };
64+
return undef if $@;
65+
66+
open(my $fh, '<', "$dir/$image") or die("Error opening $dir/$image: $!\n");
67+
my $md5 = Digest::MD5->new;
68+
$md5->addfile($fh);
69+
close($fh);
70+
71+
my $config = $metadata->{'config'};
72+
my $distribution = $config->{'distribution'};
73+
my $distrelease = $config->{'release'}; # distribution release (eg: Debian 10)
74+
my $architecture = $config->{'architecture'};
75+
my $name = $config->{'name'};
76+
my $version = $config->{'version'} || '0';
77+
my $release = '0';
78+
my @provides = ("mkosi:$distribution:$distrelease", "mkosi:$name = $version-$release");
79+
80+
my $lnk = {
81+
'name' => "mkosi:$name",
82+
'version' => $version,
83+
'release' => $release,
84+
'arch' => 'noarch',
85+
'provides' => \@provides,
86+
'source' => $packid,
87+
};
88+
eval { PBuild::Verify::verify_nevraquery($lnk) };
89+
return undef if $@;
90+
91+
$lnk->{'hdrmd5'} = $md5->hexdigest();
92+
$lnk->{'lnk'} = $image;
93+
return $lnk;
94+
}
95+
96+
1;

0 commit comments

Comments
 (0)