summaryrefslogtreecommitdiff
path: root/pkgtools/pkg_p5up2date/files/pkg_p5up2date.pl
blob: d78c8fb1b5cb4421d84566a5ce257fb886a593c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!@PERL5@ -w

use strict;
use warnings;
use 5.010;

use Carp qw(carp croak);
use Cwd qw(abs_path);
use File::Basename;
use File::Spec;
use version;
use File::Find::Rule;
use Getopt::Long;
use Pod::Usage qw(pod2usage);

use CPAN;
use CPAN::DistnameInfo;
use Module::CoreList;

package PkgP5UpToDate::AsciiWriter;

sub new
{
    my ($class, $filename) = @_;
    my %inst;
    open( $inst{fh}, '>', $filename ) or die "Can't open $filename for writing: $!";
    return bless( \%inst, $_[0] );
}

sub DESTROY
{
    defined( $_[0]->{finished} ) or $_[0]->finish();
    defined( $_[0]->{fh} ) and close( $_[0]->{fh} );
}

sub init
{
}

sub write_entry
{
    my ($self, $pkgloc, $maint, $comment, $distver, $op, $pkgver, $installed) = @_;
    $installed = $installed ? " (I)" : "";
    say {$self->{fh}} $pkgloc . " (" . $maint . ") $comment: ". $distver . " $op " . $pkgver . $installed;
    return;
}

sub finish
{
    my ($self, $pkg2update, $pkgok, $pkgcrank) = @_;
    scalar(@_) > 1 and
      say {$self->{fh}} "$pkg2update p5-packages needing updates, $pkgok p5-packages are up-to-date, $pkgcrank p5-packages needs review";
    $self->{finished} = 1;
    return;
}

package PkgP5UpToDate::HtmlWriter;

our @ISA = ('PkgP5UpToDate::AsciiWriter');

sub init
{
    my $self = $_[0];
    my $intro = <<EOH;
<html>
<head>
  <title>Outdated Perl5 modules in pkgsrc</title>
</head>

<body>
  <h1>Outdated Perl5 modules in pkgsrc</h1>
  <table class="table-datatable">
    <thead>
      <tr>
        <th>Package location</th>
        <th class="filter">Maintainer</th>
        <th class="filter">Comment</th>
        <th>Operation</th>
        <th class="filter">Installed</th>
      </tr>
    </thead>
    <tbody>
EOH
    say {$self->{fh}} $intro;
    return;
}

sub write_entry
{
    my ($self, $pkgloc, $maint, $comment, $distver, $op, $pkgver, $installed) = @_;
    $op =~ s/</&lt;/;
    $op =~ s/>/&gt;/;
    my $line = sprintf( "    <tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
                        $pkgloc, $maint, $comment, $distver . " $op " . $pkgver, $installed ? "Yes" : "-" );
    say {$self->{fh}} $line;
    return;
}

sub finish
{
    my ($self, $pkg2update, $pkgok, $pkgcrank) = @_;
    say {$self->{fh}} "    </tbody>\n</table>\n";
    scalar(@_) > 1 and
      say {$self->{fh}} "$pkg2update p5-packages needing updates, $pkgok p5-packages are up-to-date, $pkgcrank p5-packages needs review";
    my $extro = <<EOE;
</body>
</html>
EOE
    say {$self->{fh}} $extro;
    return;
}

package main;

my $make_bin = '@MAKE@';
my $pkg_info_bin = '@PKG_INFO_CMD@';

sub is_gt
{
    my $gt;
    defined($_[0]) and $_[0] =~ /^v/ and $_[1] !~ /^v/ and $_[1] = "v$_[1]";
    defined($_[0]) and $_[0] !~ /^v/ and $_[1] =~ /^v/ and $_[0] = "v$_[0]";
    eval {
	$gt = defined( $_[0] ) && (version->parse($_[0]) > version->parse($_[1]));
    };
    if( $@ ) {
	$gt = defined( $_[0] ) && ($_[0] gt $_[1]);
    }
    return $gt;
}

sub is_ne
{
    my $ne;
    defined($_[0]) and $_[0] =~ /^v/ and $_[1] !~ /^v/ and $_[1] = "v$_[1]";
    defined($_[0]) and $_[0] !~ /^v/ and $_[1] =~ /^v/ and $_[0] = "v$_[0]";
    eval {
	$ne = defined( $_[0] ) && (version->parse($_[0]) != version->parse($_[1]));
    };
    if( $@ ) {
	$ne = defined( $_[0] ) && ($_[0] ne $_[1]);
    }
    return $ne;
}

sub get_pkg_vars
{
    my $varnames = join( " ", @_ );
    my @vals = qx($make_bin show-vars VARNAMES="$varnames");  chomp @vals;
    return @vals;
}

sub get_inst_pkgs
{
    my $fh;
    open( $fh, "$pkg_info_bin |" ) or die "Can't exec pkg_info: $!";
    my @havepkgs = <$fh>;
    close( $fh ) or die "Can't close pipe to pkg_info: $!";
    chomp @havepkgs;
    @havepkgs = map { ( split( m/\s+/, $_ ) )[0] } grep { 0 == index( $_, 'p5-' ) } @havepkgs;
    my %havepkgs = map { $_ => 1 } @havepkgs;

    return %havepkgs;
}

sub get_cpan_versions
{
    my $update_idx = $_[0];

    defined( $update_idx ) and $update_idx and
      $CPAN::Index::LAST_TIME = 0;
    CPAN::Index->reload( defined( $update_idx ) and $update_idx );
    $CPAN::Index::LAST_TIME or
      carp( "Can't reload CPAN Index" );

    my @all_dists = $CPAN::META->all_objects( "CPAN::Distribution" );
    my %pkgdist;

    foreach my $dist (@all_dists)
    {
        my $dinfo = CPAN::DistnameInfo->new( $dist->id() );
	my ($distname, $distver) = ($dinfo->dist(), $dinfo->version());
	defined( $distname ) or next;
	defined( $distver ) or next;
        if( !defined( $pkgdist{$distname} ) || ( defined( $pkgdist{$distname} ) && is_gt( $distver, $pkgdist{$distname} ) ) )
        {
            $pkgdist{$distname} = $distver;
        }
    }

    return %pkgdist;
}

sub get_modules_by_distribution
{
    CPAN::Index->reload();
    $CPAN::Index::LAST_TIME or
      carp( "Can't reload CPAN Index" );

    my @all_modules = $CPAN::META->all_objects( "CPAN::Module" );
    my %modsbydist;

    foreach my $module (@all_modules)
    {
	my $modname = $module->id();
	$module->cpan_version() or next;
	my $distfile = $module->cpan_file();
        my $dinfo = CPAN::DistnameInfo->new( $distfile );
	my ($distname, $distver) = ($dinfo->dist(), $dinfo->version());
	defined( $distname ) or next;
	defined( $distver ) or next;
	$modsbydist{$distname} //= [];
	push( @{$modsbydist{$distname}}, $modname );
    }

    return %modsbydist;
}

sub find_pkgsrc_dir
{
    defined( $ENV{PKGSRCDIR} ) and return $ENV{PKGSRCDIR};

    foreach my $dir (qw(@PKGSRCDIR@ . .. ../.. /usr/pkgsrc))
    {
        -d $dir and -f File::Spec->catfile( $dir, "mk", "bsd.pkg.mk" )
          and return abs_path($dir); # Cwd::abs_path()
    }

    return;
}

my %writers = (
    ascii => 'PkgP5UpToDate::AsciiWriter',
    html => 'PkgP5UpToDate::HtmlWriter',
);

my %opts = (
  "output-fmt" => "ascii",
  "output-file" => File::Spec->catfile( $ENV{HOME}, "p5up2date.log" ),
  "scan-installed" => 1,
  "update-cpan-index" => 0,
);
GetOptions(
  \%opts,
  "cpan-home=s",
  "output-fmt=s",
  "output-file=s",
  "scan-installed!",
  "update-cpan-index!",
  "help|h",
  "usage|?"
) or pod2usage(2);

defined( $opts{help} ) and $opts{help} and pod2usage( { -verbose => 2, -exitval => 0 } );
defined( $opts{usage} ) and $opts{usage} and pod2usage(1);
defined( $writers{$opts{'output-fmt'}} ) or pod2usage( { -verbose => 1, -exitval => 1, -message => 'Unknown output format' } );

if( defined($opts{"cpan-home"}) and -e File::Spec->catfile( $opts{'cpan-home'}, 'CPAN', 'MyConfig.pm' ) )
{
    my $file = File::Spec->catfile( $opts{'cpan-home'}, 'CPAN', 'MyConfig.pm' );

    # XXX taken from App:Cpan::_load_config()
    $CPAN::Config = {};
    delete $INC{'CPAN/Config.pm'};

    my $rc = eval "require '$file'";

    # CPAN::HandleConfig::require_myconfig_or_config looks for this
    $INC{'CPAN/MyConfig.pm'} = 'fake out!';

    # CPAN::HandleConfig::load looks for this
    $CPAN::Config_loaded = 'fake out';

    croak( "Could not load [$file]: $@\n") unless $rc;
}
else
{
    CPAN::HandleConfig->load();
    defined( $opts{"cpan-home"} ) and -d $opts{"cpan-home"} and
      $CPAN::Config{cpan_home} = $opts{'cpan-home'}
}

$CPAN::Config_loaded or die "Can't load CPAN::Config - please setup CPAN first";

my %havepkgs;
$opts{"scan-installed"} and %havepkgs = get_inst_pkgs();

my $writer = $writers{$opts{'output-fmt'}}->new( $opts{'output-file'} );
$writer->init();

my %pkgdist = get_cpan_versions( $opts{'update-cpan-index'} );
my %distmods = get_modules_by_distribution();

my ( $pkg2update, $pkgok, $pkgcrank ) = ( 0, 0, 0 );

my $pkgsrc_base = find_pkgsrc_dir();
my @p5_pkg_dirs = find( directory => name => "p5-*", in => $pkgsrc_base );
   @p5_pkg_dirs = sort @p5_pkg_dirs;

foreach my $dn (@p5_pkg_dirs)
{
    chdir( $dn );
    my ($distnm, $extract_sufx, $pkgnm, $maint ) = get_pkg_vars( qw(DISTNAME EXTRACT_SUFX PKGNAME MAINTAINER) );
    my $pkgver = 0;
    if( $distnm =~ m/^(.*)-(v?[0-9].*?)$/ )
    {
        $distnm = $1;
        $pkgver = $2;
    }
    if( defined( $pkgdist{$distnm} ) && ($pkgdist{$distnm} =~ m/^(.*)$extract_sufx$/ ) )
    {
        $pkgdist{$distnm} = $1;
    }

    my @mods = map { @{$distmods{$_}} } grep { $_ =~ m/^$distnm/ } keys %distmods;
    foreach my $distmod (@{mods})
    {
	my $mod = $CPAN::META->instance( "CPAN::Module", $distmod );
	if( is_gt( $Module::CoreList::version{$]}->{$distmod}, $mod->cpan_version() ) )
	{
	    (my $pkgdir  = $dn) =~ s,$pkgsrc_base/,,;
	    $writer->write_entry( File::Spec->catfile( $pkgdir, $pkgnm ), $maint, "is older than core for $distmod", $Module::CoreList::version{$]}->{$distmod}, ">", $mod->cpan_version(), defined( $havepkgs{$pkgnm} ) );
	    ++$pkgcrank;
	    last;
	}
    }

    if( is_gt($pkgdist{$distnm}, $pkgver ) )
    {
        (my $pkgdir  = $dn) =~ s,$pkgsrc_base/,,;
	$writer->write_entry( File::Spec->catfile( $pkgdir, $pkgnm ), $maint, "need update", $pkgdist{$distnm}, ">", $pkgver, defined( $havepkgs{$pkgnm} ) );
	++$pkg2update;
    }
    elsif( is_ne($pkgdist{$distnm}, $pkgver ) )
    {
        (my $pkgdir  = $dn) =~ s,$pkgsrc_base/,,;
	$writer->write_entry( File::Spec->catfile( $pkgdir, $pkgnm ), $maint, "out of sync?", $pkgdist{$distnm}, "!=", $pkgver, defined( $havepkgs{$pkgnm} ) );
	++$pkgcrank;
    }
    else
    {
	++$pkgok;
    }
}

$writer->finish( $pkg2update, $pkgok, $pkgcrank );

exit( 0 );

=pod

=head1 NAME

pkg_p5up2date - check p5-* pkgsrc packages whether being up-to-date or need updating

=head1 SYNOPSIS

  pkg_p5up2date [--cpan-home <dir>] [--output-fmt <ascii|html>]
		[--output-file <filename>]
                [--[no-]scan-installed] [--[no-]update-cpan-index]
                [--help] [--usage]

=head1 DESCRIPTION

This script scans pkgsrc directory for outdated cpan packages.
Packages which will need updates are listed and packages which are
newer than their cpan pendants are reported as "needs review".

=head1 OPTIONS

=over 8

=item C<--cpan-home>

Specifies home directory for cpan configuration and cache.
Loads following files:

  ${cpan-home}/CPAN/MyConfig.pm
  ${cpan-home}/sources/modules/02packages.details.txt.gz

=item C<--output-fmt>

Let you choose the output format, either I<ascii> or I<html>.

Default is ascii.

=item C<--output-file>

Let you specify another target file then C<~/p5up2date.log> for
writing packages which needs attention.

=item C<--scan-installed|--no-scan-installed>

Specifies whether installed packages shall be marked explicitely.

=item C<--update-cpan-index|--no-update-cpan-index>

Specifies whether cpan index cache shall be forced to update or not.

=item C<--help>

Prints help and exit.

=item C<--usage>

Prints usage and exit.

=back

=head1 AUTHOR

Jens Rehsack <sno@NetBSD.org>

=cut