CPAN2Pkg/Worker/RPM.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 123 CC 1 SCAR 6.9
CC
1
Cov
0%
_build_pkgname
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #
2 # This file is part of App-CPAN2Pkg
3 #
4 # This software is copyright (c) 2009 by Jerome Quelin.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 use 5.010;
10 use strict;
11 use warnings;
12  
13 package App::CPAN2Pkg::Worker::RPM;
14 # ABSTRACT: worker specialized in rpm distributions
15 $App::CPAN2Pkg::Worker::RPM::VERSION = '3.004';
16 use Moose;
17 use MooseX::ClassAttribute;
18 use MooseX::Has::Sugar;
19 use MooseX::POE;
20 use Path::Class;
21 use Readonly;
22  
23 use App::CPAN2Pkg::Lock;
24  
25 extends 'App::CPAN2Pkg::Worker';
26  
27 Readonly my $K => $poe_kernel;
28  
29  
30 # -- class attributes
31  
32  
33 class_has rpmlock => ( ro, isa=>'App::CPAN2Pkg::Lock', default=>sub{ App::CPAN2Pkg::Lock->new } );
34  
35  
36 # -- attributes
37  
38  
39 has srpm => ( rw, isa=>'Path::Class::File' );
40 has rpm => ( rw, isa=>'Path::Class::File' );
41 has pkgname => ( rw, isa=>'Str', lazy_build );
42  
43  
44 # -- initialization
45  
46 sub _build_pkgname {
47 my $self = shift;
48 my $pkgname = $self->srpm->basename;
49 $pkgname =~ s/-\d.*$//;
50 return $pkgname;
51 }
52  
53  
54 # -- cpan2pkg logic implementation
55  
56 { # _install_from_upstream_result
57 override _install_from_upstream_result => sub {
58 my $self = shift;
59 $self->rpmlock->release;
60 super();
61 };
62 }
63  
64 { # _cpanplus_create_package_result
65 override _cpanplus_create_package_result => sub {
66 my ($self, $status, $output) = @_[OBJECT, ARG0 .. $#_ ];
67 my $module = $self->module;
68 my $modname = $module->name;
69  
70 # check whether the package has been built correctly.
71 my ($rpm, $srpm);
72 $rpm = $1 if $output =~ /rpm created successfully: (.*\.rpm)/;
73 $srpm = $1 if $output =~ /srpm available: (.*\.src.rpm)/;
74  
75 # detecting error cannot be done on $status - sigh.
76 if ( not ( $rpm && $srpm ) ) {
77 $module->local->set_status( "error" );
78 $K->post( main => module_state => $module );
79 $K->post( main => log_result => $modname => "Error during package creation" );
80 return;
81 }
82  
83 # logging result
84 $K->post( main => log_result => $modname => "Package built successfully" );
85 $K->post( main => log_result => $modname => "SRPM: $srpm" );
86 $K->post( main => log_result => $modname => "RPM: $rpm" );
87  
88 # storing path to packages
89 $self->set_srpm( file($srpm) );
90 $self->set_rpm ( file($rpm) );
91  
92 $self->yield( "local_install_from_package" );
93 };
94 }
95  
96 { # local_install_from_package
97 override local_install_from_package => sub {
98 super();
99 $K->yield( get_rpm_lock => "_local_install_from_package_with_rpm_lock" );
100 };
101  
102 event _local_install_from_package_with_rpm_lock => sub {
103 my $self = shift;
104 my $rpm = $self->rpm;
105 my $cmd = "sudo rpm -Uv --force $rpm";
106 $self->run_command( $cmd => "_local_install_from_package_result" );
107 };
108  
109 override _local_install_from_package_result => sub {
110 my $self = shift;
111 $self->rpmlock->release;
112 super();
113 };
114 }
115  
116  
117 # -- events
118  
119  
120 event get_rpm_lock => sub {
121 my ($self, $event) = @_[OBJECT, ARG0];
122 my $module = $self->module;
123 my $modname = $module->name;
124 my $rpmlock = $self->rpmlock;
125  
126 # check whether there's another rpm transaction
127 if ( ! $rpmlock->is_available ) {
128 my $owner = $rpmlock->owner;
129 my $comment = "waiting for rpm lock... (owned by $owner)";
130 $K->post( main => log_comment => $modname => $comment );
131 $K->delay( get_rpm_lock => 5, $event );
132 return;
133 }
134  
135 # rpm lock available, grab it
136 $rpmlock->get( $modname );
137 $self->yield( $event );
138 };
139  
140  
141 no Moose;
142 __PACKAGE__->meta->make_immutable;
143 1;
144  
145 __END__