| 1 | # -*- perl -*- | ||||||||||
| 2 | |||||||||||
| 3 | # | ||||||||||
| 4 | # Author: Slaven Rezic | ||||||||||
| 5 | # | ||||||||||
| 6 | # Copyright (C) 2020,2022 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: https://github.com/eserte/Doit | ||||||||||
| 12 | # | ||||||||||
| 13 | |||||||||||
| 14 | package Doit::Pip; # Convention: all commands here should be prefixed with 'pip_' | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 1 | our $VERSION = '0.012'; | |||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | use Doit::Log; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 1 | ▶ | sub new { bless {}, shift } | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | 1 | ▶ | sub functions { qw(pip_install_packages pip_missing_packages can_pip) } | ||||||||
|
Subroutine
functions: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | |||||||||||
| 26 | sub can_pip { | ||||||||||
| 27 | 1 | ▶ | my($self) = @_; | ||||||||
|
Subroutine
can_pip: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 28 | 1 | ▶ | $self->which('pip3') ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | sub pip_install_packages { | ||||||||||
| 32 | 0 | ▶ | my($self, @packages) = @_; | ||||||||
|
Subroutine
pip_install_packages: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 33 | 0 | my @missing_packages = $self->pip_missing_packages(@packages); | |||||||||
| 34 | 0 | ▶ | if (@missing_packages) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | $self->system('pip3', 'install', @missing_packages); | |||||||||
| 36 | } | ||||||||||
| 37 | 0 | @missing_packages; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | sub pip_missing_packages { | ||||||||||
| 41 | 0 | ▶ | my($self, @packages) = @_; | ||||||||
|
Subroutine
pip_missing_packages: not called CC=5 SCAR=30.3
Pod
documented
| |||||||||||
| 42 | 0 | my @missing_packages; | |||||||||
| 43 | 0 | if (0) { # XXX this is only possible with newer pip versions --- how new? | |||||||||
| 44 | my $stderr; | ||||||||||
| 45 | eval { | ||||||||||
| 46 | $self->info_open3({ | ||||||||||
| 47 | quiet => 1, | ||||||||||
| 48 | errref => \$stderr, | ||||||||||
| 49 | }, 'pip3', 'show', '-q', '--no-color', @packages); # note: --no-color is only available in newer pip versions | ||||||||||
| 50 | }; | ||||||||||
| 51 | if ($stderr) { | ||||||||||
| 52 | if ($stderr =~ m{\QWARNING: Package(s) not found:\E\s+(.+)}) { | ||||||||||
| 53 | @missing_packages = split /,\s+/, $1; | ||||||||||
| 54 | } else { | ||||||||||
| 55 | error "Unable to parse stderr output of 'pip3 show': '$stderr'"; | ||||||||||
| 56 | } | ||||||||||
| 57 | } | ||||||||||
| 58 | } else { | ||||||||||
| 59 | 0 | if (0) { # XXX this solution would be straightforward --- unfortunately some pip3 versions do not exit with a non-zero status for missing packages | |||||||||
| 60 | for my $package (@packages) { | ||||||||||
| 61 | if (!eval { $self->info_open3({quiet=>1}, 'pip3', 'show', '-q', $package); 1 }) { | ||||||||||
| 62 | push @missing_packages, $package; | ||||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | } else { | ||||||||||
| 66 | 0 | my %seen_package; | |||||||||
| 67 | 1 | ▶ | for my $line (do { no warnings "uninitialized"; split /\n/, eval { $self->info_open3({quiet=>1}, 'pip3', 'show', @packages) } }) { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||
| 68 | 0 | ▶ | if ($line =~ /^Name:\s+(\S+)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | $seen_package{$1}++; | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | 0 | for my $package (@packages) { | |||||||||
| 73 | 0 | ▶ | if (!$seen_package{$package}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | push @missing_packages, $package; | |||||||||
| 75 | } | ||||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | 0 | @missing_packages; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 1 | 1; | |||||||||
| 83 | |||||||||||
| 84 | __END__ | ||||||||||