File Coverage

lib/Rex/Pkg/Gentoo.pm
Criterion Covered Total %
statement 20 114 17.5
branch 0 56 0.0
condition 0 8 0.0
subroutine 7 13 53.8
pod 0 6 0.0
total 27 197 13.7


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Pkg::Gentoo;
6              
7 1     1   15 use v5.12.5;
  1         3  
8 1     1   5 use warnings;
  1         2  
  1         40  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL VERSION
11              
12 1     1   5 use Rex::Commands::Run;
  1         3  
  1         16  
13 1     1   10 use Rex::Helper::Run;
  1         7  
  1         69  
14 1     1   7 use Rex::Commands::File;
  1         3  
  1         7  
15 1     1   8 use Rex::Pkg::Base;
  1         2  
  1         9  
16 1     1   41 use base qw(Rex::Pkg::Base);
  1         3  
  1         1350  
17              
18             sub new {
19 0     0 0   my $that = shift;
20 0   0       my $proto = ref($that) || $that;
21 0           my $self = $proto->SUPER::new(@_);
22              
23 0           bless( $self, $proto );
24              
25             $self->{commands} = {
26 0           install => 'emerge --update --changed-use %s',
27             install_version => 'emerge --update --changed-use =%s-%s',
28             reinstall_check =>
29             'emerge --pretend --update --changed-use --nodeps --quiet --verbose =%s',
30             update_system => 'emerge --update --deep --with-bdeps=y --newuse world',
31             dist_update_system =>
32             'emerge --update --deep --with-bdeps=y --newuse world',
33             remove => 'emerge -C %s',
34             update_package_db => 'emerge --sync',
35             };
36              
37 0           return $self;
38             }
39              
40             sub bulk_install {
41 0     0 0   my ( $self, $packages_aref, $option ) = @_;
42              
43 0           delete $option->{version}; # makes no sense to specify the same version for several packages
44              
45 0           $self->update( "@{$packages_aref}", $option );
  0            
46              
47 0           return 1;
48             }
49              
50             sub is_installed {
51              
52 0     0 0   my ( $self, $pkg, $option ) = @_;
53 0           my $slot;
54 0           my $version = $option->{version};
55              
56             # Determine slot.
57 0           my $slot_idx = index( $pkg, ':' );
58 0 0         if ( $slot_idx != -1 ) {
    0          
59 0 0 0       die
60             "Illegal package spec. `$pkg-$version': Both package and version has SLOT"
61             if $version && index( $version, ':' ) != -1;
62 0           $slot = substr( $pkg, $slot_idx + 1 );
63 0           substr( $pkg, $slot_idx ) = '';
64             }
65             elsif ($version) {
66 0           $slot_idx = index( $version, ':' );
67 0 0         if ( $slot_idx != -1 ) {
68 0           $slot = substr( $version, $slot_idx + 1 );
69 0           substr( $version, $slot_idx ) = '';
70             }
71             }
72              
73 0           $self->{short} = 0;
74 0 0         Rex::Logger::debug( "Checking if $pkg"
    0          
75             . ( $version ? "-$version" : "" )
76             . ( $slot ? ":$slot" : '' )
77             . " is installed" );
78              
79 0           my @pkg_info = grep { $_->{name} eq $pkg } $self->get_installed();
  0            
80 0 0         @pkg_info = grep { $_->{version} eq $version } @pkg_info if defined $version;
  0            
81              
82 0 0         unless (@pkg_info) {
83 0           Rex::Logger::debug(
84             "Couldn't find package by category/packagename, trying with packagename only"
85             );
86 0           $self->{short} = 1;
87 0           @pkg_info = grep { $_->{name} eq $pkg } $self->get_installed();
  0            
88 0 0         @pkg_info = grep { $_->{version} eq $version } @pkg_info
  0            
89             if defined $version;
90             }
91              
92 0 0         unless (@pkg_info) {
93 0 0         Rex::Logger::debug( "$pkg"
    0          
94             . ( $version ? "-$version" : "" )
95             . ( $slot ? ":$slot" : '' )
96             . " is NOT installed." );
97 0           return 0;
98             }
99              
100             # Check for requested SLOT.
101 0           my $pkg_atom;
102              
103 0 0         if ( defined $slot ) {
104 0           my $slot_ok;
105              
106 0           for my $info (@pkg_info) {
107 0           $pkg_atom =
108             "$info->{name}-$info->{version}$info->{suffix}$info->{release}";
109              
110 0           my $fh = file_read("/var/db/pkg/$pkg_atom/SLOT");
111 0           chomp( my $slot_installed = $fh->read_all );
112 0           $fh->close;
113              
114 0 0         if ( $slot eq $slot_installed ) {
115 0           $pkg_atom .= ":$slot";
116 0           $slot_ok = 1;
117 0           last;
118             }
119             }
120              
121 0 0         unless ($slot_ok) {
122 0 0         Rex::Logger::debug( "$pkg"
    0          
123             . ( $version ? "-$version" : "" )
124             . ( $slot ? ":$slot" : '' )
125             . " is NOT installed." );
126 0           return 0;
127             }
128             }
129             else {
130 0           $pkg_atom =
131             "$pkg_info[0]->{name}-$pkg_info[0]->{version}$pkg_info[0]->{suffix}$pkg_info[0]->{release}";
132             }
133              
134             # Check for any USE flag changes.
135 0           my $rchk_cmd = sprintf( $self->{commands}->{reinstall_check}, $pkg_atom );
136 0           my @rchk_out = i_run $rchk_cmd;
137              
138 0           for my $line (@rchk_out) {
139 0 0         next unless $line =~ /\s*\[ebuild[^]]+\]\s+$pkg_info[0]->{name}/;
140              
141 0 0         Rex::Logger::debug( "$pkg"
    0          
142             . ( $version ? "-$version" : "" )
143             . ( $slot ? ":$slot" : '' )
144             . " is installed but USE flags have changed." );
145 0           return 0;
146             }
147              
148 0 0         Rex::Logger::debug( "$pkg"
    0          
149             . ( $version ? "-$version" : "" )
150             . ( $slot ? ":$slot" : '' )
151             . " is installed." );
152 0           return 1;
153              
154             }
155              
156             sub get_installed {
157 0     0 0   my ($self) = @_;
158 0           my $cut_cmd;
159 0 0         if ( $self->{short} ) {
160 0           $cut_cmd = "cut -d '/' -f6-";
161             }
162             else {
163 0           $cut_cmd = "cut -d '/' -f5-";
164             }
165              
166             # ,,stolen'' from epm
167 0           my $pkgregex = '(.+?)' . # name
168             '-(\d+(?:\.\d+)*\w*)' . # version, eg 1.23.4a
169             '((?:(?:_alpha|_beta|_pre|_rc)\d*)?)' . # special suffix
170             '((?:-r\d+)?)$'; # revision, eg r12
171              
172 0           my @ret;
173              
174 0           for my $line ( i_run("ls -d /var/db/pkg/*/* | $cut_cmd") ) {
175 0           my $r = qr{$pkgregex};
176 0           my ( $name, $version, $suffix, $revision ) = ( $line =~ $r );
177 0           push(
178             @ret,
179             {
180             name => $name,
181             version => $version,
182             suffix => $suffix,
183             release => $revision,
184             }
185             );
186             }
187              
188 0           return @ret;
189             }
190              
191             sub add_repository {
192 0     0 0   my ( $self, %data ) = @_;
193              
194 0           my $name = $data{"name"};
195 0   0       my $readd = $data{"readd"} // 0;
196              
197 0 0         if ( can_run("layman") ) {
198 0           my $op;
199              
200 0           i_run "layman -lqnN | grep -q '$name'", fail_ok => 1;
201 0 0         if ( $? == 0 ) {
202 0 0         if ($readd) {
203 0           $op = 'r'; # --readd
204             }
205             else {
206 0           Rex::Logger::info(
207             "Repository $name is present, use `readd' option to re-add from scratch."
208             );
209             }
210             }
211             else {
212 0           $op = 'a'; # --add
213             }
214 0 0         i_run "layman -$op $name" if defined $op;
215             }
216             else {
217 0           Rex::Logger::debug("You have to install layman, git and subversion.");
218 0           die("Please install layman, git and subversion");
219             }
220             }
221              
222             sub rm_repository {
223 0     0 0   my ( $self, $name ) = @_;
224              
225 0 0         if ( can_run("layman") ) {
226 0           i_run "layman -d $name";
227             }
228             else {
229 0           Rex::Logger::debug("You have to install layman, git and subversion.");
230 0           die("Please install layman, git and subversion");
231             }
232             }
233              
234             1;