|
| 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