File Coverage

blib/lib/Ixchel/functions/python_module_via_pkg.pm
Criterion Covered Total %
statement 23 72 31.9
branch 0 22 0.0
condition 0 15 0.0
subroutine 8 9 88.8
pod 1 1 100.0
total 32 119 26.8


line stmt bran cond sub pod time code
1             package Ixchel::functions::python_module_via_pkg;
2              
3 3     3   87629 use 5.006;
  3         13  
4 3     3   19 use strict;
  3         5  
  3         84  
5 3     3   17 use warnings;
  3         5  
  3         201  
6 3     3   19 use Exporter 'import';
  3         6  
  3         241  
7             our @EXPORT = qw(python_module_via_pkg);
8 3     3   1176 use Rex -feature => [qw/1.4/];
  3         149060  
  3         26  
9 3     3   566603 use Rex::Commands::Gather;
  3         6  
  3         22  
10 3     3   3154 use Rex::Commands::Pkg;
  3         6  
  3         25  
11 3     3   1438 use Ixchel::functions::status;
  3         9  
  3         2375  
12              
13             # prevents Rex from printing out rex is exiting after the script ends
14             $::QUIET = 2;
15              
16             =head1 NAME
17              
18             Ixchel::functions::python_module_via_pkg - Tries to install a module for python3 via the package manager.
19              
20             =head1 VERSION
21              
22             Version 0.0.1
23              
24             =cut
25              
26             our $VERSION = '0.0.1';
27              
28             =head1 SYNOPSIS
29              
30             use Ixchel::functions::python_module_via_pkg;
31             use Data::Dumper;
32              
33             eval{ python_module_via_pkg(module=>'Pillow') };
34             if ($@) {
35             print 'Failed to install Pillow...'.$@."\n";
36             }
37              
38             Supported OS families are...
39              
40             Alt Linux
41             Arch Linux
42             Debian Linux
43             FreeBSD
44             Mageia Linux
45             NetBSD
46             OpenBSD
47             Redhat Linux
48             Suse Linux
49             Void Linux
50              
51             =head1 Functions
52              
53             =head2 python_module_via_pkg
54              
55             Tries to install a python3 module via packages.
56              
57             eval{ python_module_via_pkg(module=>'Pillow') };
58             if ($@) {
59             print 'Failed to install python module ...'.$@;
60             }
61              
62             =cut
63              
64             sub python_module_via_pkg {
65 0     0 1   my (%opts) = @_;
66              
67 0 0         if ( !defined( $opts{module} ) ) {
68 0           die('modules is undef');
69             }
70              
71 0           my $status = '';
72 0           my $type = 'python_module_via_pkg';
73              
74             $status = $status
75             . status(
76             type => $type,
77             error => 0,
78             status => 'Trying to install Python module ' . $opts{module}
79 0           );
80              
81 0           my $pkg;
82 0           my $not_tried = 1;
83 0 0 0       if (is_freebsd) {
    0 0        
    0 0        
    0 0        
    0 0        
84 0           $not_tried = 0;
85 0           $status = $status
86             . status( type => 'python_module_via_pkg', error => 0, status => 'OS Family FreeBSD detectected' );
87 0           my $which_python3 = `which python3 2> /dev/null`;
88 0           chomp($which_python3);
89 0           $status = $status
90             . status(
91             type => $type,
92             error => 0,
93             status => 'python3 path is "' . $which_python3 . '"'
94             );
95 0 0         if ( $which_python3 !~ /python3$/ ) {
96 0           die( $status . 'Unable to locate python3 with PATH=' . $ENV{PATH} );
97             }
98 0           my $python_link = readlink($which_python3);
99 0           $status = $status
100             . status(
101             type => $type,
102             error => 0,
103             status => 'python3 linked to "' . $python_link . '"'
104             );
105 0           $python_link =~ s/.*python3\.//;
106 0           $pkg = 'py3' . $python_link . '-' . $opts{module};
107 0           $status = $status
108             . status(
109             type => $type,
110             error => 0,
111             status => 'Ensuring that package "' . $pkg . '" is present'
112             );
113 0           eval { pkg( $pkg, ensure => "present" ); };
  0            
114 0 0         if ($@) {
115 0           $status = $status
116             . status(
117             type => $type,
118             error => 1,
119             status => 'Failed ensuring that package "' . $pkg . '" is present... ' . $@
120             );
121 0           $pkg = lc($pkg);
122 0           $status = $status
123             . status(
124             type => $type,
125             error => 0,
126             status => 'Trying ensuring that package "' . $pkg . '" is present'
127             );
128 0           eval { pkg( $pkg, ensure => "present" ); };
  0            
129 0 0         if ($@) {
130 0           $status = $status
131             . status(
132             type => $type,
133             error => 1,
134             status => 'Failed ensuring that package "' . $pkg . '" is present... ' . $@
135             );
136 0           die( $status . 'Neither ' . $pkg . ' or ' . lc($pkg) . ' could be installed' );
137             }
138             } ## end if ($@)
139             } elsif (is_debian || is_redhat || is_arch || is_mageia || is_void) {
140 0           $status
141             = $status . status( type => 'python_module_via_pkg', error => 0, status => 'OS Family Debian, Redhat, Arch, Mageia, or Void detectected' );
142 0           $pkg = 'python3-' . lc( $opts{module} );
143             } elsif (is_suse) {
144 0           $status
145             = $status . status( type => 'python_module_via_pkg', error => 0, status => 'OS Family Suse detectected' );
146 0           $pkg = 'python311-' . lc( $opts{module} );
147             } elsif (is_alt) {
148 0           $status
149             = $status . status( type => 'python_module_via_pkg', error => 0, status => 'OS Family Alt detectected' );
150 0           $pkg = 'python3-module-' . lc( $opts{module} );
151             } elsif (is_netbsd || is_openbsd) {
152 0           $status
153             = $status . status( type => 'python_module_via_pkg', error => 0, status => 'OS Family NetBSD or OpenBSD detectected' );
154 0           $pkg = 'py311-' . lc( $opts{module} );
155             }
156              
157 0 0         if ($not_tried) {
158 0           $status = $status
159             . status(
160             type => $type,
161             error => 0,
162             status => 'Ensuring that package "' . $pkg . '" is present'
163             );
164 0           eval { pkg( $pkg, ensure => 'present' ); };
  0            
165 0 0         if ($@) {
166 0           $status = $status
167             . status(
168             type => $type,
169             error => 1,
170             status => 'Failed ensuring that package "' . $pkg . '" is present... ' . $@
171             );
172 0           die( $status . 'Neither ' . $pkg . ' or ' . lc($pkg) . ' could be installed' );
173             }
174             } ## end if ($not_tried)
175              
176 0           $status = $status . status( type => $type, error => 0, status => 'Package "' . $pkg . '" is present' );
177              
178 0           return $status;
179             } ## end sub python_module_via_pkg
180              
181             1;