| 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
| |||||||||||
| 34 | my @specfiles = | ||||||||||
| 35 | 0 | grep { /\.spec$/ } | |||||||||
| 36 | $specdir->children; | ||||||||||
| 37 | 0 | ▶ | scalar(@specfiles) > 0 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | or $self->log_fatal("could not find a spec file, aborting"); | ||||||||||
| 39 | 0 | ▶ | scalar(@specfiles) < 2 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 50 | 0 | ▶ | defined($distvers) or $self->log_fatal( "package does not use %upstream_version" ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 68 | 0 | $self->log( "no new version found" ); | |||||||||
| 69 | 0 | ▶ | if ( path("refresh")->exists ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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__ | ||||||||||