File Coverage

blib/lib/Sys/OsPackage/Driver/Debian.pm
Criterion Covered Total %
statement 12 45 26.6
branch 0 22 0.0
condition n/a
subroutine 4 9 44.4
pod 0 5 0.0
total 16 81 19.7


line stmt bran cond sub pod time code
1             # Sys::OsPackage::Driver::Debian
2             # ABSTRACT: Debian/Ubuntu DEB 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   859 use strict;
  2         4  
  2         60  
12 2     2   11 use warnings;
  2         4  
  2         420  
13 2     2   10 use utf8;
  2         4  
  2         8  
14             ## use critic (Modules::RequireExplicitPackage)
15              
16             package Sys::OsPackage::Driver::Debian;
17             $Sys::OsPackage::Driver::Debian::VERSION = '0.3.1';
18 2     2   107 use base "Sys::OsPackage::Driver";
  2         4  
  2         1341  
19              
20             # check if packager command found (deb)
21             sub pkgcmd
22             {
23 0     0 0   my ($class, $ospkg) = @_;
24              
25 0 0         return (defined $ospkg->sysenv("apt") ? 1 : 0);
26             }
27              
28             # find name of package for Perl module (deb)
29             sub modpkg
30             {
31 0     0 0   my ($class, $ospkg, $args_ref) = @_;
32 0 0         return if not $class->pkgcmd($ospkg);
33              
34             # search by deb format for Perl module packages
35 0           my $pkgname = "lib".join("-", (map {lc $_} @{$args_ref->{mod_parts}}), "perl");
  0            
  0            
36 0           $args_ref->{pkg} = $pkgname;
37 0 0         if (not $class->find($ospkg, $args_ref)) {
38 0           return;
39             }
40 0 0         $ospkg->debug() and print STDERR "debug(".__PACKAGE__."->modpkg): $pkgname\n";
41              
42             # package was found - return the simpler name since pkg add won't take this full string
43 0           return $pkgname;
44              
45             }
46              
47             # find named package in repository (deb)
48             sub find
49             {
50 0     0 0   my ($class, $ospkg, $args_ref) = @_;
51 0 0         return if not $class->pkgcmd($ospkg);
52              
53 0           my $querycmd = $ospkg->sysenv("apt-cache");
54             my @pkglist = sort $ospkg->capture_cmd({list=>1}, $ospkg->sudo_cmd(), $querycmd, qw(search --quiet=2),
55 0           '^'.$args_ref->{pkg}.'$');
56 0 0         return if not scalar @pkglist; # empty list means nothing found
57 0           return $pkglist[-1]; # last of sorted list should be most recent version
58             }
59              
60             # install package (deb)
61             sub install
62             {
63 0     0 0   my ($class, $ospkg, $args_ref) = @_;
64 0 0         return if not $class->pkgcmd($ospkg);
65              
66             # determine packages to install
67 0           my @packages;
68 0 0         if (defined $args_ref->{pkg}) {
69 0 0         if (ref $args_ref->{pkg} eq "ARRAY") {
70 0           push @packages, @{$args_ref->{pkg}};
  0            
71             } else {
72 0           push @packages, $args_ref->{pkg};
73             }
74             }
75              
76             # install the packages
77 0           my $pkgcmd = $ospkg->sysenv("apt");
78 0           return $ospkg->run_cmd($ospkg->sudo_cmd(), $pkgcmd, "install", "--yes", @packages);
79             }
80              
81             # check if an OS package is installed locally
82             sub is_installed
83             {
84 0     0 0   my ($class, $ospkg, $args_ref) = @_;
85 0 0         return if not $class->pkgcmd($ospkg);
86              
87             # check if package is installed
88 0           my $querycmd = $ospkg->sysenv("dpkg-query");
89             my @pkglist = $ospkg->capture_cmd({list=>1}, $ospkg->sudo_cmd(), $querycmd, '--show',
90 0           '--showformat=\${package}\n', $args_ref->{pkg});
91 0 0         return (scalar @pkglist > 0) ? 1 : 0;
92             }
93              
94              
95             1;
96              
97             =pod
98              
99             =encoding UTF-8
100              
101             =head1 NAME
102              
103             Sys::OsPackage::Driver::Debian - Debian/Ubuntu DEB packaging handler for Sys::OsPackage
104              
105             =head1 VERSION
106              
107             version 0.3.1
108              
109             =head1 SYNOPSIS
110              
111             my $ospkg = Sys::OsPackage->instance();
112              
113             # check if packaging commands exist for this system
114             if (not $ospkg->call_pkg_driver(op => "implemented")) {
115             return 0;
116             }
117              
118             # find OS package name for Perl module
119             my $pkgname = $ospkg->call_pkg_driver(op => "find", module => $module);
120              
121             # install a Perl module as an OS package
122             my $result1 = $ospkg->call_pkg_driver(op => "modpkg", module => $module);
123              
124             # install an OS package
125             my $result2 = $ospkg->call_pkg_driver(op => "install", pkg => $pkgname);
126              
127             =head1 DESCRIPTION
128              
129             ⛔ This is for Sys::OsPackage internal use only.
130              
131             The Sys::OsPackage method call_pkg_driver() will call the correct driver for the running platform.
132             The driver implements these methods: I, I, I, I and I.
133              
134             =head1 SEE ALSO
135              
136             Debian Linux docs: Debian package management system L
137              
138             GitHub repository for Sys::OsPackage: L
139              
140             =head1 BUGS AND LIMITATIONS
141              
142             Please report bugs via GitHub at L
143              
144             Patches and enhancements may be submitted via a pull request at L
145              
146             =head1 LICENSE INFORMATION
147              
148             Copyright (c) 2022 by Ian Kluft
149              
150             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.
151              
152             =head1 AUTHOR
153              
154             Ian Kluft
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is Copyright (c) 2022 by Ian Kluft.
159              
160             This is free software, licensed under:
161              
162             The Artistic License 2.0 (GPL Compatible)
163              
164             =cut
165              
166             __END__