File Coverage

blib/lib/Sys/OsPackage/Driver/Suse.pm
Criterion Covered Total %
statement 12 50 24.0
branch 0 22 0.0
condition n/a
subroutine 4 9 44.4
pod 0 5 0.0
total 16 86 18.6


line stmt bran cond sub pod time code
1             # Sys::OsPackage::Driver::Suse
2             # ABSTRACT: SUSE/OpenSUSE Zypper packaging handler for Sys::OsPackage
3             # Copyright (c) 2022 by Ian Kluft
4             # Open Source license Perl's Artistic License 2.0:
5             # SPDX-License-Identifier: Artistic-2.0
6              
7             # This module is maintained for minimal dependencies so it can build systems/containers from scratch.
8              
9             ## no critic (Modules::RequireExplicitPackage)
10             # This resolves conflicting Perl::Critic rules which want package and strictures each before the other
11 2     2   731 use strict;
  2         3  
  2         65  
12 2     2   9 use warnings;
  2         4  
  2         43  
13 2     2   8 use utf8;
  2         4  
  2         9  
14             ## use critic (Modules::RequireExplicitPackage)
15              
16             package Sys::OsPackage::Driver::Suse;
17             $Sys::OsPackage::Driver::Suse::VERSION = '0.1.6';
18 2     2   88 use base "Sys::OsPackage::Driver";
  2         4  
  2         1623  
19              
20             # check if packager command found (zypper)
21             sub pkgcmd
22             {
23 0     0 0   my ($class, $ospkg) = @_;
24              
25 0 0         return (defined $ospkg->sysenv("zypper") ? 1 : 0);
26             }
27              
28             # find name of package for Perl module (zypper)
29             sub modpkg
30             {
31 0     0 0   my ($class, $ospkg, $args_ref) = @_;
32 0 0         return if not $class->pkgcmd($ospkg);
33              
34             #return join("-", "perl", @{$args_ref->{mod_parts}}); # zypper/rpm format for Perl module packages
35 0           my @querycmd = $ospkg->sysenv("zypper");
36             my @pkglist = sort $ospkg->capture_cmd({list=>1}, @querycmd,
37             qw(--non-interactive --quiet --terse search --provides --type=package --match-exact),
38 0           "'perl(".$args_ref->{module}.")'");
39             $ospkg->debug()
40 0 0         and print STDERR "debug(".__PACKAGE__."->modpkg): ".$args_ref->{module}." -> ".join(" ", @pkglist)."\n";
41 0 0         return if not scalar @pkglist; # empty list means nothing found
42 0           splice @pkglist, 0, 3; # remove table header in 3 leading lines
43 0           my $pkg_found = $pkglist[-1]; # get last entry from table
44 0           $pkg_found =~ s/^[^\|]*\|\s*//x; # remove 1st column
45 0           $pkg_found =~ s/\s*\|.*$//x; # remove 3rd & following columns
46 0           return $pkg_found;
47             }
48              
49             # find named package in repository (zypper)
50             sub find
51             {
52 0     0 0   my ($class, $ospkg, $args_ref) = @_;
53 0 0         return if not $class->pkgcmd($ospkg);
54              
55 0           my @querycmd = $ospkg->sysenv("zypper");
56             my @pkglist = sort $ospkg->capture_cmd({list=>1}, @querycmd,
57             qw(--non-interactive --quiet --terse search --provides --type=package --match-exact),
58 0           $args_ref->{pkg});
59 0 0         return if not scalar @pkglist; # empty list means nothing found
60 0           splice @pkglist, 0, 3; # remove table header in 3 leading lines
61 0           my $pkg_found = $pkglist[-1]; # get last entry from table
62 0           $pkg_found =~ s/^[^\|]*\|\s*//x; # remove 1st column
63 0           $pkg_found =~ s/\s*\|.*$//x; # remove 3rd & following columns
64 0           return $pkg_found;
65             }
66              
67             # install package (zypper)
68             sub install
69             {
70 0     0 0   my ($class, $ospkg, $args_ref) = @_;
71 0 0         return if not $class->pkgcmd($ospkg);
72              
73             # determine packages to install
74 0           my @packages;
75 0 0         if (exists $args_ref->{pkg}) {
76 0 0         if (ref $args_ref->{pkg} eq "ARRAY") {
77 0           push @packages, @{$args_ref->{pkg}};
  0            
78             } else {
79 0           push @packages, $args_ref->{pkg};
80             }
81             }
82              
83             # install the packages
84 0           my $pkgcmd = $ospkg->sysenv("zypper");
85 0           return $ospkg->run_cmd($pkgcmd, qw(--non-interactive --quiet --terse install), @packages);
86             }
87              
88             # check if an OS package is installed locally
89             sub is_installed
90             {
91 0     0 0   my ($class, $ospkg, $args_ref) = @_;
92 0 0         return if not $class->pkgcmd($ospkg);
93              
94             # check if package is installed
95 0           my $querycmd = $ospkg->sysenv("rpm");
96 0           my @pkglist = $ospkg->capture_cmd({list=>1}, $querycmd, qw(--query), $args_ref->{pkg});
97 0 0         return (scalar @pkglist > 0) ? 1 : 0;
98             }
99              
100             1;
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =head1 NAME
107              
108             Sys::OsPackage::Driver::Suse - SUSE/OpenSUSE Zypper packaging handler for Sys::OsPackage
109              
110             =head1 VERSION
111              
112             version 0.1.6
113              
114             =head1 SYNOPSIS
115              
116             my $ospkg = Sys::OsPackage->instance();
117              
118             # check if packaging commands exist for this system
119             if (not $ospkg->call_pkg_driver(op => "implemented")) {
120             return 0;
121             }
122              
123             # find OS package name for Perl module
124             my $pkgname = $ospkg->call_pkg_driver(op => "find", module => $module);
125              
126             # install a Perl module as an OS package
127             my $result1 = $ospkg->call_pkg_driver(op => "modpkg", module => $module);
128              
129             # install an OS package
130             my $result2 = $ospkg->call_pkg_driver(op => "install", pkg => $pkgname);
131              
132             =head1 DESCRIPTION
133              
134             ⛔ This is for Sys::OsPackage internal use only.
135              
136             The Sys::OsPackage method call_pkg_driver() will call the correct driver for the running platform.
137             The driver implements these methods: I, I, I, I, I and I.
138              
139             =head1 SEE ALSO
140              
141             OpenSuSE Linux docs: Portal:Zypper L
142              
143             GitHub repository for Sys::OsPackage: L
144              
145             =head1 BUGS AND LIMITATIONS
146              
147             Please report bugs via GitHub at L
148              
149             Patches and enhancements may be submitted via a pull request at L
150              
151             =head1 LICENSE INFORMATION
152              
153             Copyright (c) 2022 by Ian Kluft
154              
155             This module is distributed in the hope that it will be useful, but it is provided “as is” and without any express or implied warranties. For details, see the full text of the license in the file LICENSE or at L.
156              
157             =head1 AUTHOR
158              
159             Ian Kluft
160              
161             =head1 COPYRIGHT AND LICENSE
162              
163             This software is Copyright (c) 2022 by Ian Kluft.
164              
165             This is free software, licensed under:
166              
167             The Artistic License 2.0 (GPL Compatible)
168              
169             =cut
170              
171             __END__