CPAN2Pkg/Worker/Mageia.pm untested

filter: Statementstmt 0.0% 0 / 125 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 149 CC 1 SCAR 6.9
CC
1
Cov
0%
_build__ua
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::Mageia;
14 # ABSTRACT: worker dedicated to Mageia distribution
15 $App::CPAN2Pkg::Worker::Mageia::VERSION = '3.004';
16 use HTML::TreeBuilder;
17 use HTTP::Request;
18 use Moose;
19 use MooseX::ClassAttribute;
20 use MooseX::Has::Sugar;
21 use MooseX::POE;
22 use POE;
23 use POE::Component::Client::HTTP;
24 use Readonly;
25  
26 extends 'App::CPAN2Pkg::Worker::RPM';
27  
28 Readonly my $K => $poe_kernel;
29  
30 # -- class attribute
31  
32 class_has _urpm_ok => (
33 rw,
34 traits => ['Counter'],
35 isa => 'Int',
36 default => 2,
37 handles => {
38 _urpm_release => "inc",
39 _urpm_grab => "dec",
40 },
41 );
42 class_has cpanplus_lock => ( ro, isa=>'App::CPAN2Pkg::Lock', default=>sub{ App::CPAN2Pkg::Lock->new } );
43  
44  
45 class_has _ua => ( ro, isa=>'Str', builder=>"_build__ua" );
46  
47 sub _build__ua {
48 my $ua = "mageia-bswait";
49 POE::Component::Client::HTTP->spawn( Alias => $ua );
50 return $ua;
51 }
52  
53  
54 # -- public methods
55  
56 override cpan2dist_flavour => sub { "CPANPLUS::Dist::Mageia" };
57  
58  
59 # -- cpan2pkg logic implementation
60  
61 { # check_upstream_availability
62 override check_upstream_availability => sub {
63 my $self = shift;
64 my $modname = $self->module->name;
65 $K->post( main => log_step => $modname => "Checking if module is packaged upstream");
66 $K->post( main => log_comment => $modname => "waiting for semaphore before running urpm");
67 $K->yield( "_check_upstream_availability_wait4lock" );
68 };
69  
70 event _check_upstream_availability_wait4lock => sub {
71 my $self = shift;
72 if ( $self->_urpm_ok ) {
73 $self->_urpm_grab;
74 $K->yield( "_check_upstream_availability_lock_acquired" );
75 return;
76 }
77 $K->delay( _check_upstream_availability_wait4lock => 0.1 );
78 };
79  
80 event _check_upstream_availability_lock_acquired => sub {
81 my $self = shift;
82 my $modname = $self->module->name;
83 $K->post( main => log_comment => $modname => "urpm semaphore acquired");
84 my $cmd = "urpmq --whatprovides 'perl($modname)'";
85 $self->run_command( $cmd => "_check_upstream_availability_done" );
86 };
87  
88 event _check_upstream_availability_done => sub {
89 my ($self, @args) = @_[OBJECT, ARG0 .. $#_];
90 my $modname = $self->module->name;
91 $self->_urpm_release;
92 $K->post( main => log_comment => $modname => "urpm semaphore released");
93 $K->yield( _check_upstream_availability_result => @args );
94 };
95 }
96  
97 { # install_from_upstream
98 override install_from_upstream => sub {
99 super();
100 $K->yield( get_rpm_lock => "_install_from_upstream_with_rpm_lock" );
101 };
102  
103 #
104 # _install_from_upstream_with_rpm_lock( )
105 #
106 # really install module from distribution, now that we have a lock
107 # on rpm operations.
108 #
109 event _install_from_upstream_with_rpm_lock => sub {
110 my $self = shift;
111 my $modname = $self->module->name;
112 my $cmd = "sudo urpmi --auto 'perl($modname)'";
113 $self->run_command( $cmd => "_install_from_upstream_result" );
114 };
115 }
116  
117 { # upstream_import_package
118 override upstream_import_package => sub {
119 super();
120 my $self = shift;
121 my $srpm = $self->srpm;
122 my $cmd = "mgarepo import $srpm";
123 $self->run_command( $cmd => "_upstream_import_package_result" );
124 };
125 }
126  
127 { # upstream_build_package
128 override upstream_build_package => sub {
129 super();
130 my $self = shift;
131 my $pkgname = $self->pkgname;
132 my $cmd = "mgarepo submit $pkgname";
133 $self->run_command( $cmd => "_upstream_build_package_result" );
134 };
135  
136 override _upstream_build_wait => sub {
137 my $self = shift;
138 $self->yield( "_upstream_build_wait_request" );
139 };
140  
141 event _upstream_build_wait_request => sub {
142 my $self = shift;
143 my $pkgname = $self->pkgname;
144 my $url = "http://pkgsubmit.mageia.org/?package=$pkgname&last";
145 my $request = HTTP::Request->new(HEAD => $url);
146 $K->post( $self->_ua => request => _upstream_build_wait_answer => $request );
147 };
148  
149 event _upstream_build_wait_answer => sub {
150 my ($self, $requests, $answers) = @_[OBJECT, ARG0, ARG1];
151 my $answer = $answers->[0];
152 my $status = $answer->header( 'x-bs-package-status' ) // "?";
153 my $modname = $self->module->name;
154 if ($status eq "uploaded") {
155 # nice, we finally made it!
156 my $min = 1;
157 $K->post( main => log_comment => $modname =>
158 "module successfully built, waiting $min minutes to index it" );
159 # wait some time to be sure package has been indexed
160 $K->delay( _upstream_build_package_ready => $min * 60 );
161 }
162 elsif ($status eq "failure" ) {
163 my $url = "http://pkgsubmit.mageia.org/";
164 $self->yield( _upstream_build_package_failed => $url );
165 }
166 else {
167 # no definitive result, wait a bit before checking again
168 $K->post( main => log_comment => $modname =>
169 "still not ready (current status: $status), waiting 1 more minute" );
170 $K->delay( _upstream_build_wait_request => 60 );
171 }
172  
173 };
174 }
175  
176  
177 no Moose;
178 __PACKAGE__->meta->make_immutable;
179 1;
180  
181 __END__