blib/lib/Doit/Rpm.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 81 SCAR 47.0
CC
10
Cov
0%
rpm_enable_repo
56.0
rpm_missing_packages
12.0
rpm_install_packages
6.0
CRAP 110.0
1 # -*- perl -*-
2  
3 #
4 # Author: Slaven Rezic
5 #
6 # Copyright (C) 2017,2018,2020 Slaven Rezic. All rights reserved.
7 # This package is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10 # Mail: slaven@rezic.de
11 # WWW: http://www.rezic.de/eserte/
12 #
13  
14 package Doit::Rpm;
15  
16 use Doit::Log;
17  
18 use strict;
19 use warnings;
20 our $VERSION = '0.012';
21  
22 sub new { bless {}, shift }
23 sub functions { qw(rpm_install_packages rpm_missing_packages rpm_enable_repo) }
24  
25 sub rpm_install_packages {
26 my($self, @packages) = @_;
27 my @missing_packages = $self->rpm_missing_packages(@packages);
28 if (@missing_packages) {
29 $self->system('yum', '-y', 'install', @missing_packages);
30 }
31 @missing_packages;
32 }
33  
34 sub rpm_missing_packages {
35 my($self, @packages) = @_;
36  
37 my @missing_packages;
38  
39 if (@packages) {
40 my @cmd = ('env', 'LC_ALL=C', 'rpm', '--query', @packages);
41 open my $fh, '-|', @cmd
42 or error "Error running '@cmd': $!";
43 while(<$fh>) {
44 if (m{^package (\S+) is not installed}) {
45 push @missing_packages, $1;
46 }
47 }
48 }
49  
50 @missing_packages;
51 }
52  
53 sub rpm_enable_repo {
54 my($self, $repo, %opts) = @_;
55 my $do_update = delete $opts{update}; $do_update = 1 if !defined $do_update;
56 error "Unhandled options: " . join(" ", %opts) if %opts;
57  
58 for my $installed_repo_line (split /\n/, $self->info_qx({quiet=>1}, qw(yum repolist))) {
59 my($installed_repo) = split /\s+/, $installed_repo_line, 2;
60 if ($installed_repo eq $repo) {
61 return 0;
62 }
63 }
64 my @cm_cmd = (qw(yum config-manager --set-enabled), $repo);
65 my $err;
66 if (!eval { $self->open3({errref => \$err}, @cm_cmd); 1 }) {
67 if ($err =~ /No such command: config-manager/) {
68 $self->system(qw(dnf -y install), 'dnf-command(config-manager)');
69 $self->system(@cm_cmd);
70 } else {
71 error $err;
72 }
73 }
74 $self->system(qw(yum update)) if $do_update;
75 return 1;
76 }
77  
78 1;
79  
80 __END__