_append renamed in _stderr
[app-cpan2pkg.git] / lib / App / CPAN2Pkg / Module.pm
blobfe023cd7744291bc5b638dd30b9f60e0e76d0bbb
2 # This file is part of App::CPAN2Pkg.
3 # Copyright (c) 2009 Jerome Quelin, all rights reserved.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
10 package App::CPAN2Pkg::Module;
12 use strict;
13 use warnings;
15 use Class::XSAccessor
16 constructor => '_new',
17 accessors => {
18 name => 'name',
19 shortname => 'shortname',
20 _output => '_output',
21 _prereqs => '_prereqs',
22 _wheels => '_wheels',
24 use POE;
25 use POE::Filter::Line;
26 use POE::Wheel::Run;
30 # if ( not available in cooker ) is_in_dist
31 # then
32 # compute dependencies find_prereqs
33 # repeat with each dep
34 # cpan2dist cpan2dist
35 # install local install_from_local
36 # while ( not available locally ) is_installed
37 # do
38 # prompt user to fix manually
39 # done
40 # import import_local_to_dist
41 # submit (included above)
42 # ack available (manual?)
44 # else
45 # urpmi --auto perl(module::to::install) install_from_dist
46 # fi
48 # on debian / ubuntu
49 # $ apt-file find Audio/MPD.pm
50 # libaudio-mpd-perl: /usr/share/perl5/Audio/MPD.pm
51 # status:
52 # - find dist hosting module
53 # - computing dependencies
54 # - installing dependencies
55 # - check cooker availability
56 # - cpan2dist
57 # - install local
58 # - check local availability
59 # - mdvsys import
60 # - mdvsys submit
61 # - wait for kenobi build
67 #--
68 # CONSTRUCTOR
70 sub spawn {
71 my ($class, $module) = @_;
73 # creating the object
74 my $short = $module;
75 $short =~ s/::/:/g;
76 $short =~ s/[[:lower:]]//g;
77 my $obj = App::CPAN2Pkg::Module->_new(
78 name => $module,
79 shortname => $short,
80 _prereqs => {},
81 _wheels => {},
84 # spawning the session
85 my $session = POE::Session->create(
86 inline_states => {
87 # public events
88 find_prereqs => \&find_prereqs,
89 # private events
90 _find_prereqs_end => \&_find_prereqs_end,
91 _find_prereqs_stderr => \&_stderr,
92 _find_prereqs_stdout => \&_stdout,
93 # poe inline states
94 _start => \&_start,
95 _stop => sub { warn "stop"; },
97 heap => $obj,
99 return $session->ID;
104 # SUBS
106 # -- public events
108 sub find_prereqs {
109 my ($k, $self) = @_[KERNEL, HEAP];
111 # preparing command
112 my $module = $self->name;
113 my $cmd = "cpanp /prereqs show $module";
115 $self->_log_new_step($k, 'Finding module prereqs',
116 "Running command: $cmd" );
118 $self->_output('');
119 my $wheel = POE::Wheel::Run->new(
120 Program => $cmd,
121 CloseEvent => '_find_prereqs_end',
122 StdoutEvent => '_find_prereqs_stdout',
123 StderrEvent => '_find_prereqs_stderr',
124 #ErrorEvent => '_find_prereqs_error',
125 StdoutFilter => POE::Filter::Line->new,
126 StderrFilter => POE::Filter::Line->new,
128 $wheel->shutdown_stdin;
129 $self->_wheels->{ $wheel->ID } = $wheel;
132 # -- private events
134 sub _find_prereqs_end {
135 my ($k, $self, $id) = @_[KERNEL, HEAP, ARG0];
137 # terminate wheel
138 my $wheel = delete $self->{_wheels}->{$id};
140 # extract prereqs
141 my @lines =
142 grep { s/^\s+// }
143 split /\n/, $self->_output;
144 shift @lines; # remove the title line
145 my @prereqs =
146 map { (split /\s+/, $_)[0] }
147 @lines;
149 # store prereqs
150 foreach my $prereq ( @prereqs ) {
151 $k->post('ui', 'append', $self, "prereq found: $prereq\n");
152 $self->_prereqs->{$prereq} = 1;
155 $k->post('app', 'prereqs', $self, @prereqs);
158 sub _stderr {
159 my ($k, $self, $line) = @_[KERNEL, HEAP, ARG0];
160 $k->post('ui', 'append', $self, "$line\n");
163 sub _stdout {
164 my ($k, $self, $line) = @_[KERNEL, HEAP, ARG0];
165 $line .= "\n";
166 $self->_output( $self->_output . $line );
167 $k->post('ui', 'append', $self, $line);
171 # -- poe inline states
173 sub _start {
174 my ($k, $self) = @_[KERNEL, HEAP];
176 $k->alias_set($self);
177 $k->post('ui', 'new_module', $self);
178 $k->post('app', 'new_module', $self);
179 $k->yield('find_prereqs');
184 # METHODS
186 # -- private methods
188 sub _log_new_step {
189 my ($self, $k, $step, $comment) = @_;
191 my $out = "\n\n" . '*' x 10 . "\n$step\n\n$comment\n\n";
192 $k->post('ui', 'append', $self, $out);
196 __END__
199 =head1 NAME
201 App::CPAN2Pkg::Module - poe session to drive a module packaging
205 =head1 DESCRIPTION
207 C<App::CPAN2Pkg::Module> implements a POE session driving the whole
208 packaging process of a given module.
210 It is spawned by C<App::CPAN2Pkg> and implements the logic related to
211 the module availability in the distribution.
215 =head1 PUBLIC PACKAGE METHODS
217 =head2 my $id = App::CPAN2Pkg::Module->spawn( $module )
219 This method will create a POE session responsible for packaging &
220 installing the wanted C<$module>.
222 It will return the POE id of the session newly created.
226 =head1 PUBLIC EVENTS ACCEPTED
228 =head2 find_prereqs()
230 Start looking for any other module needed by current module.
234 =head1 METHODS
236 This package is also a class, used B<internally> to store private data
237 needed for the packaging stuff. The following accessors are therefore
238 available, but should not be used directly:
240 =over 4
242 =item name() - the module name
244 =item shortname() - the module shortname (only capital letters)
246 =back
250 =head1 SEE ALSO
252 For all related information (bug reporting, source code repository,
253 etc.), refer to C<App::CPAN2Pkg>'s pod, section C<SEE ALSO>.
257 =head1 AUTHOR
259 Jerome Quelin, C<< <jquelin@cpan.org> >>
263 =head1 COPYRIGHT & LICENSE
265 Copyright (c) 2009 Jerome Quelin, all rights reserved.
267 This program is free software; you can redistribute it and/or modify
268 it under the same terms as Perl itself.
270 =cut