DPAN/SVNPostFlight.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 152 CC 10 SCAR 20.0
CC
10
Cov
0%
BEGIN
20.0
_get_commands
20.0
BEGIN
6.0
CRAP 7.4
1 package MyCPAN::App::DPAN::SVNPostFlight;
2 use strict;
3 use warnings;
4 use utf8;
5  
6 use vars qw( $logger );
7  
8 use Data::Dumper;
9 use IPC::Cmd;
10 use IPC::Run qw();
11 use IPC::System::Simple qw(capturex systemx);
12 use Log::Log4perl;
13 use XML::Simple;
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 MyCPAN::App::DPAN::SVNPostFlight - A No-op reports processor
20  
21 =head1 SYNOPSIS
22  
23 Use this from C<dpan> by specifying it as the C<postflight_class> class:
24  
25 # in dpan.conf
26 postflight_class MyCPAN::App::DPAN::SVNPostFlight
27 postflight_dry_run 1
28  
29 =head1 DESCRIPTION
30  
31 This class is an example for a user-defined class to run at the end of
32 C<dpan>'s normal processing. The class only needs to provide a C<run>
33 method, which is automatically called by C<dpan>. Be careful that you
34 don't import anything called C<run> (looking at you, C<IPC::Run>)!
35  
36 This example checks that the DPAN directory is under source control,
37 invokes an svn update, checks the svn status to see what's changes,
38 and creates a list of svn commands to run. It adds any new files it
39 finds and removes any missing miles. If it detects a conflict, it
40 stops the process before anything happens.
41  
42 If you've set the C<postflight_dry_run> configuration variable, this
43 class merely prints the svn adds and removes that it would run, but it
44 doesn't actually run them. That gives you a chance to see what it
45 would do without doing it.
46  
47 At the end of the run, this prints the URL you need to use to access the
48 repository.
49  
50 =head2 Logging
51  
52 This module uses the C<PostFlight> logging category in the C<Log::Log4perl>
53 setup.
54  
55 =head2 Writing your own
56  
57 If you want to maek your own class, check out the source for C<run>. The
58 code comments explain what you should be doing. However, most of the
59 code in this example isn't specific to the post flight processing.
60  
61 =head2 Methods
62  
63 =over 4
64  
65 =cut
66  
67 # Log::Log4perl should already be set up. The
68 BEGIN {
69 $logger = Log::Log4perl->get_logger( 'PostFlight' );
70 }
71  
72 BEGIN {
73 my $svn = IPC::Cmd::can_run( 'svn' );
74 $logger->debug( "svn commmand is [$svn]" );
75  
76 =item svn
77  
78 Returns the path to the svn binary.
79  
80 =cut
81  
82 sub svn { $svn }
83  
84 =item dry_run
85  
86 Returns the value of the postflight_dry_run configuration directive.
87  
88 =cut
89  
90 sub dry_run { $_[0]->{postflight_dry_run} }
91  
92 =item run_svn
93  
94 Runs an svn command. During a dry run it merely prints the command to
95 standard output. Otherwise, it actually runs the svn command.
96  
97 =cut
98  
99 sub run_svn
100 {
101 my( $self, @commands ) = @_;
102  
103 if( $self->dry_run )
104 {
105 print "dry run: $svn @commands\n";
106 }
107 else
108 {
109 $logger->debug( "$svn @commands" );
110 print capturex( $self->svn, @commands );
111 }
112 }
113  
114 }
115  
116 =item run( $application )
117  
118 Makes the hamsters go. This is called automatically from dpan. It gets
119 the application object as its argument.
120  
121 =cut
122  
123 sub run
124 {
125 # dpan calls this as a class method after it runs
126 # $application->cleanup. All of dpan's work is done and it's removed
127 # most of its mess. You're picking up control just before it would
128 # normally exit.
129 #
130 # The only argument is the $application object.
131 my( $class, $application ) = @_;
132  
133 # The coordinator object has references to all of the other components
134 # and the application notes. See MyCPAN::Indexer::Tutorial and
135 # MyCPAN::Indexer::Coordinator for more information
136 my $coordinator = $application->get_coordinator;
137  
138 # The Coordinator knows how to get the configuration object
139 my $config = $coordinator->get_config;
140  
141 # You are probably already in this directory, but it's nice to be
142 # sure.
143 my $dpan_dir = $config->get( 'dpan_dir' );
144 chdir $dpan_dir;
145  
146 # If there isn't a .svn directory, there's not much that we can do
147 unless( -e '.svn' )
148 {
149 $logger->logdie( "There isn't an .svn directory in [$dpan_dir]! I can't continue!" );
150 return;
151 }
152  
153 # Construct an object, although it's not necessary. We're going to
154 # use it to adjust some configuration, etc, that we can pass around.
155 # In this case, we just transfer the postflight_dry_run value.
156 #
157 # You can add any configuration directive that you like. You might
158 # want to give it a prefix that won't conflict with the standard
159 # dpan directives.
160 #
161 # In case we need the application object for something else, we'll
162 # store a a reference to that too.
163 my $self = bless
164 {
165 postflight_dry_run => $config->get( 'postflight_dry_run' ),
166 application => $application,
167 }, $class;
168  
169 # Now we're past all of the special parts. It's whatever you want to
170 # do now.
171 $logger->info( "Checking the svn status" );
172 my $commands = $self->_get_commands;
173  
174 $logger->info( "Handling svn additions and deletions" );
175 $self->_handle_commands( $commands );
176  
177 $logger->info( "All done. Have a nice day!" );
178 $self->_report_repo_url;
179 }
180  
181 BEGIN {
182 my %Commands = (
183 'unversioned' => 'add',
184 'deleted' => 'rm',
185 );
186  
187 sub _get_commands
188 {
189 my( $self ) = @_;
190 my $xml = $self->_get_svn_status_xml;
191  
192 my $ref = XMLin( $xml );
193  
194 my( @commands, @conflicts );
195 foreach my $entry ( @{ $ref->{target}{entry} } )
196 {
197 my $status = $entry->{'wc-status'}{item};
198 my $path = $entry->{path};
199 $logger->debug( "svn status for $path: $status" );
200  
201 if( exists $Commands{ $status } )
202 {
203 push @commands, [ $Commands{ $status }, $path ];
204 }
205 if( $status eq 'conflicted' )
206 {
207 push @conflicts, $path;
208 }
209 }
210  
211 if( @conflicts )
212 {
213 my $list = join "\n\t", @conflicts;
214  
215 $logger->logdie( "I can't continue. There are conflicts in svn:\n\t$list\n" );
216 return;
217 }
218  
219 \@commands;
220 }
221 }
222  
223 sub _svn_update
224 {
225 my( $self ) = @_;
226 # don't use run_svn because we have to still run for dry run
227 my $output = capturex( $self->svn, 'update' );
228 $logger->debug( "svn status output: $output" );
229 $output;
230 }
231  
232 sub _get_svn_status_xml
233 {
234 my( $self ) = @_;
235 # don't use run_svn because we have to still run for dry run
236 my $status = capturex( $self->svn, 'status', '--xml' );
237 $logger->debug( "svn status output: $status" );
238 $status;
239 }
240  
241 sub _handle_commands
242 {
243 my( $self, $commands ) = @_;
244  
245 my $svn = $self->svn;
246  
247 $self->_svn_update;
248  
249 foreach my $command ( @$commands )
250 {
251 $self->run_svn( @$command );
252 }
253  
254 $logger->info( "Committing work to svn" );
255 my @commit_command = ( $svn, 'commit', '-m', 'DPAN PostFlight commit' );
256  
257 my( $in, $output ) = ( '' );
258  
259 IPC::Run::run( \@commit_command, \$in, \$output, \$output )
260 or do {
261 $logger->debug( "svn commit output: $output" );
262 $logger->logdie( "Could not commit to svn!" );
263 return;
264 };
265 $logger->debug( "Output from commit: $output" );
266  
267 return 1;
268 }
269  
270 sub _report_repo_url
271 {
272 my( $self ) = @_;
273  
274 # don't use run_svn because we have to still run for dry run
275 my $xml = capturex( $self->svn, 'info', '--xml' );
276 $logger->debug( "svn info output: $xml" );
277  
278 my $ref = XMLin( $xml );
279 my $repo = $ref->{entry}{url};
280 print "To use this DPAN, point your CPAN tool to:\n\n\t$repo\n\n";
281 }
282  
283 =back
284  
285 =head1 SEE ALSO
286  
287 MyCPAN::App::DPAN, dpan
288  
289 =head1 SOURCE AVAILABILITY
290  
291 This code is in Github:
292  
293 git://github.com/briandfoy/mycpan-app-dpan.git
294  
295 =head1 AUTHOR
296  
297 brian d foy, C<< <bdfoy@cpan.org> >>
298  
299 =head1 COPYRIGHT AND LICENSE
300  
301 Copyright © 2010-2018, brian d foy <bdfoy@cpan.org>. All rights reserved.
302  
303 You may redistribute this under the terms of the Artistic License 2.0.
304  
305 =cut
306  
307 1;