Magpie/Action/Update.pm

filter: Statementstmt 30.3% 30 / 99 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 30.7% 40 / 130 CC 11 SCAR 42.9
CC
11
Cov
25%
run
132.0
BEGIN
1.0
BEGIN
1.0
CRAP 73.0
« Magpie/App/Command/old.pm ↑ Summary Magpie/Action/BSWait.pm »
1 #
2 # This file is part of App-Magpie
3 #
4 # This software is copyright (c) 2011 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 1 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package App::Magpie::Action::Update;
14 # ABSTRACT: update command implementation
15 1 $App::Magpie::Action::Update::VERSION = '2.010';
16 1 ▶use CPAN::Mini;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Parse::CPAN::Packages::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 with 'App::Magpie::Role::Logging';
24 1 with 'App::Magpie::Role::RunningCommand';
25  
26  
27  
28 0 ▶sub run {
Subroutine run: not called CC=11 SCAR=48.8
Pod documented
29 0 my ($self) = @_;
30  
31 # check if there's a spec file to update...
32 0 my $specdir = path("SPECS");
33 0 ▶ -e $specdir or $self->log_fatal("cannot find a SPECS directory, aborting");
Branch: 1 branch
BranchTrueFalseTotal
-e $specdir or $self->log_fatal("cannot find a SPECS directory, aborting") 0 0 0
34 my @specfiles =
35 0 grep { /\.spec$/ }
36 $specdir->children;
37 0 ▶ scalar(@specfiles) > 0
Branch: 1 branch
BranchTrueFalseTotal
scalar @specfiles > 0 or $self->log_fatal("could not find a spec file, aborting") 0 0 0
38 or $self->log_fatal("could not find a spec file, aborting");
39 0 ▶ scalar(@specfiles) < 2
Branch: 1 branch
BranchTrueFalseTotal
scalar @specfiles < 2 or $self->log_fatal("more than one spec file found, aborting") 0 0 0
40 or $self->log_fatal("more than one spec file found, aborting");
41 0 my $specfile = shift @specfiles;
42 0 my $spec = $specfile->slurp;
43 0 my $pkgname = $specfile->basename; $pkgname =~ s/\.spec$//;
44 0 $self->log( "updating $pkgname" );
45  
46 # check if package uses %upstream_{name|version}
47 0 my ($distname) = ( $spec =~ /^%define\s+upstream_name\s+(.*)$/m );
48 0 my ($distvers) = ( $spec =~ /^%define\s+upstream_version\s+(.*)$/m );
49 0 ▶ defined($distname) or $self->log_fatal( "package does not use %upstream_name" );
Branch: 1 branch
BranchTrueFalseTotal
defined $distname or $self->log_fatal("package does not use %upstream_name") 0 0 0
50 0 ▶ defined($distvers) or $self->log_fatal( "package does not use %upstream_version" );
Branch: 1 branch
BranchTrueFalseTotal
defined $distvers or $self->log_fatal("package does not use %upstream_version") 0 0 0
51 0 $self->log_debug( "perl distribution to update: $distname v$distvers" );
52  
53 # check if we have a minicpan at hand
54 0 my $cpanmconf = CPAN::Mini->config_file;
55 0 ▶ defined($cpanmconf)
Branch: 1 branch
BranchTrueFalseTotal
defined $cpanmconf or $self->log_fatal("no minicpan installation found, aborting") 0 0 0
56 or $self->log_fatal("no minicpan installation found, aborting");
57 0 my %config = CPAN::Mini->read_config( {quiet=>1} );
58 0 my $cpanmdir = path( $config{local} );
59 0 $self->log_debug( "found a minicpan installation in $cpanmdir" );
60  
61 # try to find a newer version
62 0 $self->log_debug( "parsing 02packages.details.txt.gz" );
63 0 my $modgz = $cpanmdir->child("modules", "02packages.details.txt.gz");
64 0 my $p = Parse::CPAN::Packages::Fast->new( $modgz->stringify );
65 0 my $dist = $p->latest_distribution( $distname );
66 0 my $newvers = $dist->version;
67 0 ▶ if ( version->new( $newvers ) <= version->new( $distvers ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->new($newvers) <= "version"->new($distvers)) 0 0 0
68 0 $self->log( "no new version found" );
69 0 ▶ if ( path("refresh")->exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if (path("refresh")->exists) 0 0 0
70 0 $self->log( "... but a previous 'refresh' script was found, trying to run it" );
71 0 $self->run_command( "./refresh" );
72 0 return;
73 }
74 0 $self->log_fatal( "... and no previous 'refresh' script found, aborting" );
75 }
76 0 $self->log( "new version found: $newvers" );
77  
78 # copy tarball
79 0 my $cpantarball = $cpanmdir->child( "authors", "id", $dist->prefix );
80 0 my $tarball = $dist->filename;
81 0 $self->log_debug( "copying $tarball to SOURCES" );
82 0 ▶ copy( $cpantarball->stringify, "SOURCES" )
Branch: 1 branch
BranchTrueFalseTotal
copy($cpantarball->stringify, "SOURCES") or $self->log_fatal("could not copy $cpantarball to SOURCES: $!") 0 0 0
83 or $self->log_fatal( "could not copy $cpantarball to SOURCES: $!" );
84 0 my $suffix = $tarball; $suffix =~ s/.*$newvers\.//g;
85 0 $self->log( "new suffix: $suffix" );
86  
87 # update spec file
88 0 $self->log_debug( "updating spec file $specfile" );
89 0 $spec =~ s/%mkrel \d+/%mkrel 1/;
90 0 $spec =~ s/^(%define\s+upstream_version)\s+.*/$1 $newvers/m;
91 0 $spec =~ s/^(source.*upstream_version[^.]*)\..*/$1.$suffix/mi;
92 0 my $specfh = $specfile->openw;
93 0 $specfh->print( $spec );
94 0 $specfh->close;
95  
96 # create script
97 0 my $script = path( "refresh" );
98 0 my $fh = $script->openw;
99 0 $fh->print(<<EOF);
100 #!/bin/bash
101 magpie fix -v && \\
102 bm -l && \\
103 mgarepo sync && \\
104 svn ci -m "update to $newvers" && \\
105 mgarepo submit && \\
106 rm \$0
107 EOF
108 0 $fh->close;
109 0 chmod 0755, $script;
110  
111 # try to install buildrequires
112 0 ▶ if ( ! $ENV{MAGPIE_NO_URPMI_BUILDREQUIRES} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"MAGPIE_NO_URPMI_BUILDREQUIRES"}) 0 0 0
113 0 $self->log( "installing buildrequires" );
114 0 $self->run_command( "LC_ALL=C sudo urpmi --wait-lock --buildrequires $specfile" );
115 }
116  
117 # fix spec file, update buildrequires
118 0 require App::Magpie::Action::FixSpec;
119 0 App::Magpie::Action::FixSpec->new->run;
120  
121 # local dry-run
122 0 $self->log( "trying to build package locally" );
123 0 $self->run_command( "bm -l" );
124  
125 # push changes
126 0 $self->log( "committing changes" );
127 0 $self->run_command( "mgarepo sync" );
128 0 $self->run_command( "svn ci -m 'update to $newvers'" );
129  
130 # submit
131 0 require App::Magpie::Action::BSWait;
132 0 App::Magpie::Action::BSWait->new->run;
133 0 $self->log( "submitting package" );
134 0 $self->run_command( "mgarepo submit" );
135 0 $script->remove;
136 }
137  
138  
139  
140 1 1;
141  
142 __END__
« Magpie/App/Command/old.pm ↑ Summary Magpie/Action/BSWait.pm »