| 1 | package CPANPLUS::YACSmoke; | ||
| 2 | $CPANPLUS::YACSmoke::VERSION = '1.06'; | ||
| 3 | # Dist::Zilla: +PodWeaver | ||
| 4 | #ABSTRACT: Yet Another CPANPLUS Smoke Tester | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | use CPANPLUS::Backend; | ||
| 11 | use CPANPLUS::Configure; | ||
| 12 | use CPANPLUS::Error; | ||
| 13 | use CPANPLUS::Internals::Constants; | ||
| 14 | use POSIX qw( O_CREAT O_RDWR O_RDONLY ); # for SDBM_File | ||
| 15 | use SDBM_File; | ||
| 16 | use File::Fetch; | ||
| 17 | use IO::File; | ||
| 18 | use File::Spec::Unix; | ||
| 19 | use File::Spec::Functions; | ||
| 20 | use File::Path; | ||
| 21 | use CPANPLUS::YACSmoke::ReAssemble; | ||
| 22 | use CPANPLUS::YACSmoke::SortVers; | ||
| 23 | use CPANPLUS::YACSmoke::IniFiles; | ||
| 24 | |||
| 25 | use constant DATABASE_FILE => 'cpansmoke.dat'; | ||
| 26 | use constant CONFIG_FILE => 'cpansmoke.ini'; | ||
| 27 | use constant RECENT_FILE => 'RECENT'; | ||
| 28 | |||
| 29 | require Exporter; | ||
| 30 | |||
| 31 | our @ISA = qw( Exporter ); | ||
| 32 | our %EXPORT_TAGS = ( | ||
| 33 | 'all' => [ qw( mark test excluded purge flush reindex) ], | ||
| 34 | 'default' => [ qw( mark test excluded ) ], | ||
| 35 | ); | ||
| 36 | |||
| 37 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 38 | our @EXPORT = ( @{ $EXPORT_TAGS{'default'} } ); | ||
| 39 | |||
| 40 | { | ||
| 41 | my %Checked; | ||
| 42 | my $TiedObj; | ||
| 43 | |||
| 44 | |||
| 45 | sub _connect_db { | ||
| 46 | my $self = shift; | ||
| 47 | return if $TiedObj; | ||
| 48 | my $filename = catfile( $self->{conf}->get_conf('base'), DATABASE_FILE ); | ||
| 49 | $TiedObj = tie %Checked, 'SDBM_File', $filename, O_CREAT|O_RDWR, 0644; | ||
| 50 | $self->{checked} = \%Checked; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _disconnect_db { | ||
| 54 | my $self = shift; | ||
| 55 | return unless $TiedObj; | ||
| 56 | $TiedObj = undef; | ||
| 57 | $self->{checked} = undef; | ||
| 58 | untie %Checked; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub new { | ||
| 62 | my $package = shift; | ||
| 63 | my $nconf = shift if ref $_[0] and $_[0]->isa('CPANPLUS::Configure'); | ||
| 64 | |||
| 65 | $ENV{AUTOMATED_TESTING} = 1; | ||
| 66 | $ENV{NONINTERACTIVE_TESTING} = 1; # Lancaster Consensus | ||
| 67 | $ENV{PERL_MM_USE_DEFAULT} = 1; # despite verbose setting | ||
| 68 | $ENV{PERL_EXTUTILS_AUTOINSTALL} = '--defaultdeps'; | ||
| 69 | |||
| 70 | my $conf = $nconf || CPANPLUS::Configure->new(); | ||
| 71 | |||
| 72 | # Override configure settings | ||
| 73 | $conf->set_conf( prereqs => 2 ); # force to ask callback | ||
| 74 | $conf->set_conf( skiptest => 0 ); | ||
| 75 | $conf->set_conf( no_update => 1 ) | ||
| 76 | if glob( catfile( $conf->get_conf('base'), $conf->_get_source('stored') .'*'. STORABLE_EXT, ) ); | ||
| 77 | $conf->set_conf( dist_type => 'CPANPLUS::Dist::YACSmoke' ); # this is where the magic happens. | ||
| 78 | $conf->set_conf( cpantest => 'dont_cc' ); # Yes, we want to report test results. But not CC | ||
| 79 | $conf->set_conf( verbose => 1 ); # set verbosity to true. | ||
| 80 | $conf->set_conf( allow_unknown_prereqs => 0 ); # don't allow unknown prereqs | ||
| 81 | |||
| 82 | unless ( defined $ENV{MAILDOMAIN} ) { | ||
| 83 | my $hostpart = ( split /\@/, ( $conf->get_conf( 'email' ) || 'smoker@cpantesters.org' ) )[1]; | ||
| 84 | $ENV{MAILDOMAIN} = $hostpart =~ /^(cpan\.org|gmail\.com)$/i ? 'cpantesters.org' : $hostpart; | ||
| 85 | } | ||
| 86 | |||
| 87 | if ( $^V gt v5.9.5 ) { | ||
| 88 | $conf->set_conf( prefer_makefile => 0 ); # Prefer Build.PL if we have M::B | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | eval "require Module::Build"; | ||
| 92 | $conf->set_conf( prefer_makefile => 0 ) unless $@; # | ||
| 93 | } | ||
| 94 | |||
| 95 | my $cb = CPANPLUS::Backend->new($conf); | ||
| 96 | |||
| 97 | my $exclude_dists; | ||
| 98 | my $exclude_auths; | ||
| 99 | my $local_lib; | ||
| 100 | my $config_file = catfile( $conf->get_conf('base'), CONFIG_FILE ); | ||
| 101 | if ( -r $config_file ) { | ||
| 102 | my $cfg = CPANPLUS::YACSmoke::IniFiles->new(-file => $config_file); | ||
| 103 | { | ||
| 104 | my @list = $cfg->val( 'CONFIG', 'exclude_dists' ); | ||
| 105 | if ( @list ) { | ||
| 106 | $exclude_dists = CPANPLUS::YACSmoke::ReAssemble->new(); | ||
| 107 | $exclude_dists->add( @list ); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | { | ||
| 111 | my @list = $cfg->val( 'CONFIG', 'exclude_auths' ); | ||
| 112 | if ( @list ) { | ||
| 113 | $exclude_auths = CPANPLUS::YACSmoke::ReAssemble->new(); | ||
| 114 | $exclude_auths->add( @list ); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | $local_lib = $cfg->val( 'CONFIG', 'local::lib' ); | ||
| 118 | } | ||
| 119 | |||
| 120 | my $self = bless { @_ }, $package; | ||
| 121 | $self->{conf} = $conf; | ||
| 122 | $self->{cpanplus} = $cb; | ||
| 123 | $self->{exclude_dists} = $exclude_dists; | ||
| 124 | $self->{exclude_auths} = $exclude_auths; | ||
| 125 | $self->{allow_retries} = 'aborted|ungraded'; | ||
| 126 | $self->{local_lib} = $local_lib; | ||
| 127 | return $self; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub test { | ||
| 131 | my $self; | ||
| 132 | eval { | ||
| 133 | if ( (ref $_[0]) && $_[0]->isa(__PACKAGE__) ) { | ||
| 134 | $self = shift; | ||
| 135 | } | ||
| 136 | }; | ||
| 137 | $self ||= __PACKAGE__->new(); | ||
| 138 | $self->_connect_db(); | ||
| 139 | |||
| 140 | my @dists = @_; | ||
| 141 | |||
| 142 | unless ( @dists ) { | ||
| 143 | @dists = $self->_download_list(); | ||
| 144 | } | ||
| 145 | |||
| 146 | my @mods; | ||
| 147 | |||
| 148 | foreach my $dist ( @dists ) { | ||
| 149 | my $mod = $self->{cpanplus}->parse_module( module => $dist ); | ||
| 150 | next unless $mod; | ||
| 151 | my $package = $mod->package_name .'-'. $mod->package_version; | ||
| 152 | my $grade = $self->{checked}->{$package} || 'ungraded'; | ||
| 153 | next if $self->_is_excluded_dist($package); | ||
| 154 | next if $mod->is_bundle; | ||
| 155 | next unless $grade =~ /$self->{allow_retries}/; | ||
| 156 | push @mods, $mod; | ||
| 157 | } | ||
| 158 | |||
| 159 | $self->_disconnect_db(); | ||
| 160 | |||
| 161 | my $target = 'create'; | ||
| 162 | |||
| 163 | if ( $self->{local_lib} ) { | ||
| 164 | $self->_setup_local_lib(); | ||
| 165 | $self->{conf}->_perl5lib( $ENV{PERL5LIB} ); | ||
| 166 | $self->{conf}->set_conf( makeflags => '' ); | ||
| 167 | $self->{conf}->set_conf( buildflags => '' ); | ||
| 168 | $target = 'install'; | ||
| 169 | msg("Setup local::lib environment in '$ENV{PERL_LOCAL_LIB_ROOT}'"); | ||
| 170 | } | ||
| 171 | |||
| 172 | foreach my $mod ( @mods ) { | ||
| 173 | eval { | ||
| 174 | CPANPLUS::Error->flush(); | ||
| 175 | my $stat = $self->{cpanplus}->install( | ||
| 176 | modules => [ $mod ], | ||
| 177 | target => $target, | ||
| 178 | allow_build_interactively => 0, | ||
| 179 | # other settings now set via set_config() method | ||
| 180 | ); | ||
| 181 | }; | ||
| 182 | } | ||
| 183 | |||
| 184 | if ( $self->{local_lib} ) { | ||
| 185 | my $build_dir = $self->_get_build_dir(); | ||
| 186 | msg("Flushing '$build_dir'"); | ||
| 187 | rmtree($build_dir) if -e $build_dir; | ||
| 188 | msg("Flushed '$build_dir'"); | ||
| 189 | } | ||
| 190 | |||
| 191 | $self->{cpanplus}->save_state() if !$self->{local_lib}; | ||
| 192 | |||
| 193 | return 1; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub mark { | ||
| 197 | my $self; | ||
| 198 | eval { | ||
| 199 | if ( (ref $_[0]) && $_[0]->isa(__PACKAGE__) ) { | ||
| 200 | $self = shift; | ||
| 201 | } | ||
| 202 | }; | ||
| 203 | $self ||= __PACKAGE__->new(); | ||
| 204 | $self->_connect_db(); | ||
| 205 | |||
| 206 | my $distver = shift || ''; | ||
| 207 | my $grade = lc shift || ''; | ||
| 208 | |||
| 209 | if ($grade) { | ||
| 210 | my $mod = $self->{cpanplus}->parse_module( module => $distver ); | ||
| 211 | return error(qq{Invalid distribution "$distver"}) unless $mod; | ||
| 212 | |||
| 213 | unless ($grade =~ /(pass|fail|unknown|na|none|ungraded|aborted|ignored)/) { | ||
| 214 | return error("Invalid grade: '$grade'"); | ||
| 215 | } | ||
| 216 | if ($grade eq "none") { | ||
| 217 | $grade = undef; | ||
| 218 | } | ||
| 219 | |||
| 220 | $distver = $mod->package_name .'-'. $mod->package_version; | ||
| 221 | $self->{checked}->{$distver} = $grade; | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | my @distros = ($distver ? ($distver) : $self->_download_list()); | ||
| 225 | foreach my $dist ( @distros ) { | ||
| 226 | my $mod = $self->{cpanplus}->parse_module( module => $dist ); | ||
| 227 | next unless $mod; | ||
| 228 | my $dist_ver = $mod->package_name .'-'. $mod->package_version; | ||
| 229 | next if $self->_is_excluded_dist( $dist_ver ); | ||
| 230 | $grade = $self->{checked}->{$dist_ver}; | ||
| 231 | if ( $grade ) { | ||
| 232 | msg(qq{result for "$dist_ver" is "$grade"}); | ||
| 233 | } | ||
| 234 | else { | ||
| 235 | msg(qq{no result for "$dist_ver"}); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | $self->_disconnect_db(); | ||
| 240 | return $grade if $distver; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub excluded { | ||
| 244 | my $self; | ||
| 245 | eval { | ||
| 246 | if ( (ref $_[0]) && $_[0]->isa(__PACKAGE__) ) { | ||
| 247 | $self = shift; | ||
| 248 | } | ||
| 249 | }; | ||
| 250 | $self ||= __PACKAGE__->new(); | ||
| 251 | |||
| 252 | my @dists = @_; | ||
| 253 | |||
| 254 | unless ( @dists ) { | ||
| 255 | @dists = $self->_download_list(); | ||
| 256 | } | ||
| 257 | |||
| 258 | my @mods; | ||
| 259 | |||
| 260 | foreach my $dist ( @dists ) { | ||
| 261 | my $mod = $self->{cpanplus}->parse_module( module => $dist ); | ||
| 262 | next unless $mod; | ||
| 263 | my $package = $mod->package_name .'-'. $mod->package_version; | ||
| 264 | my $auth = $mod->author->cpanid; | ||
| 265 | next unless $self->_is_excluded_dist($package) || $self->_is_excluded_auth($auth); | ||
| 266 | msg(qq{EXCLUDED: "$package"}); | ||
| 267 | push @mods, $package; | ||
| 268 | } | ||
| 269 | |||
| 270 | return @mods; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub purge { | ||
| 274 | my $self; | ||
| 275 | eval { | ||
| 276 | if ( (ref $_[0]) && $_[0]->isa(__PACKAGE__) ) { | ||
| 277 | $self = shift; | ||
| 278 | } | ||
| 279 | }; | ||
| 280 | $self ||= __PACKAGE__->new(); | ||
| 281 | my %config = ref($_[0]) eq 'HASH' ? %{ shift() } : (); | ||
| 282 | $self->_connect_db(); | ||
| 283 | |||
| 284 | my $flush = $config{flush_flag} || 0; | ||
| 285 | my %distvars; | ||
| 286 | my $override = 0; | ||
| 287 | |||
| 288 | if(@_) { | ||
| 289 | $override = 1; | ||
| 290 | for(@_) { | ||
| 291 | next unless(/^(.*)\-(.+)$/); | ||
| 292 | push @{$distvars{$1}}, $2; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | else { | ||
| 296 | for(keys %{$self->{checked}}) { | ||
| 297 | next unless(/^(.*)\-(.+)$/); | ||
| 298 | push @{$distvars{$1}}, $2; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | foreach my $dist (sort keys %distvars) { | ||
| 303 | my $passed = $override; | ||
| 304 | my @vers = sort { versioncmp($a, $b) } @{$distvars{$dist}}; | ||
| 305 | while(@vers) { | ||
| 306 | my $vers = pop @vers; # the latest | ||
| 307 | if($passed) { | ||
| 308 | msg("'$dist-$vers' ['". | ||
| 309 | uc($self->{checked}->{"$dist-$vers"}). | ||
| 310 | "'] has been purged"); | ||
| 311 | delete $self->{checked}->{"$dist-$vers"}; | ||
| 312 | if($flush) { | ||
| 313 | my $builddir = | ||
| 314 | catfile($self->_get_build_dir(), "$dist-$vers"); | ||
| 315 | rmtree($builddir) if(-d $builddir); | ||
| 316 | } | ||
| 317 | } | ||
| 318 | elsif($self->{checked}->{"$dist-$vers"} eq 'pass') { | ||
| 319 | $passed = 1; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | $self->_disconnect_db(); | ||
| 325 | return 1; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub flush { | ||
| 329 | my $self; | ||
| 330 | eval { | ||
| 331 | if ( (ref $_[0]) && $_[0]->isa(__PACKAGE__) ) { | ||
| 332 | $self = shift; | ||
| 333 | } | ||
| 334 | }; | ||
| 335 | $self ||= __PACKAGE__->new(); | ||
| 336 | my %config = ref($_[0]) eq 'HASH' ? %{ shift() } : (); | ||
| 337 | $self->_connect_db(); | ||
| 338 | |||
| 339 | my $param = shift || 'all'; | ||
| 340 | |||
| 341 | my $build_dir = $self->_get_build_dir(); | ||
| 342 | |||
| 343 | if ( $param eq 'old' ) { | ||
| 344 | my %dists; | ||
| 345 | opendir(my $DIR, $build_dir); | ||
| 346 | while(my $dir = readdir($DIR)) { | ||
| 347 | next if $dir =~ /^\.+$/; | ||
| 348 | $dir =~ /(.*)-(.+)/; | ||
| 349 | $dists{$1}->{$2} = "$dir"; | ||
| 350 | } | ||
| 351 | closedir($DIR); | ||
| 352 | for my $dist (keys %dists) { | ||
| 353 | for(sort { versioncmp($a, $b) } keys %{$dists{$dist}}) { | ||
| 354 | rmtree(catfile($build_dir,$dists{$dist}->{$_})); | ||
| 355 | msg("'$dists{$dist}->{$_}' flushed"); | ||
| 356 | } | ||
| 357 | } | ||
| 358 | } | ||
| 359 | else { | ||
| 360 | msg("Flushing '$build_dir'"); | ||
| 361 | rmtree($build_dir) if -e $build_dir; | ||
| 362 | msg("Flushed '$build_dir'"); | ||
| 363 | require File::Glob; | ||
| 364 | ( my $base = $self->{conf}->get_conf('base') ) =~ s![\\/]$!!; | ||
| 365 | foreach my $sourcefile ( File::Glob::bsd_glob( $base . q{/sourcefile*} ) ) { | ||
| 366 | msg("Removing '$sourcefile'"); | ||
| 367 | rmtree $sourcefile; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | $self->_disconnect_db(); | ||
| 372 | return 1; | ||
| 373 | } | ||
| 374 | |||
| 375 | sub reindex { | ||
| 376 | my $self; | ||
| 377 | eval { | ||
| 378 | if ( (ref $_[0]) && $_[0]->isa(__PACKAGE__) ) { | ||
| 379 | $self = shift; | ||
| 380 | } | ||
| 381 | }; | ||
| 382 | $self ||= __PACKAGE__->new(); | ||
| 383 | $self->{conf}->set_conf( no_update => 0 ); | ||
| 384 | $self->{cpanplus}->reload_indices( update_source => 1 ); | ||
| 385 | $self->{conf}->set_conf( no_update => 1 ) | ||
| 386 | if glob( catfile( $self->{conf}->get_conf('base'), $self->{conf}->_get_source('stored') .'*'. STORABLE_EXT, ) ); | ||
| 387 | return 1; | ||
| 388 | } | ||
| 389 | |||
| 390 | sub _is_excluded_dist { | ||
| 391 | my $self = shift; | ||
| 392 | my $dist = shift || return; | ||
| 393 | return unless $self->{exclude_dists}; | ||
| 394 | return 1 if $dist =~ $self->{exclude_dists}->re(); | ||
| 395 | } | ||
| 396 | |||
| 397 | sub _is_excluded_auth { | ||
| 398 | my $self = shift; | ||
| 399 | my $auth = shift || return; | ||
| 400 | return unless $self->{exclude_auths}; | ||
| 401 | return 1 if $auth =~ $self->{exclude_auths}->re(); | ||
| 402 | } | ||
| 403 | |||
| 404 | sub _download_list { | ||
| 405 | my $self = shift; | ||
| 406 | |||
| 407 | my $path = $self->{conf}->get_conf('base'); | ||
| 408 | my $local = catfile( $path, RECENT_FILE ); | ||
| 409 | |||
| 410 | my $hosts = $self->{conf}->get_conf('hosts'); | ||
| 411 | my $h_ind = 0; | ||
| 412 | |||
| 413 | while ($h_ind < @$hosts) { | ||
| 414 | my $host = $hosts->[$h_ind]; | ||
| 415 | my $mirror_path = File::Spec::Unix->catfile( | ||
| 416 | $host->{'path'}, | ||
| 417 | RECENT_FILE | ||
| 418 | ); | ||
| 419 | |||
| 420 | my %args = ( | ||
| 421 | scheme => $host->{scheme}, | ||
| 422 | host => $host->{host}, | ||
| 423 | path => $mirror_path, | ||
| 424 | ); | ||
| 425 | |||
| 426 | my $remote = $self->{cpanplus}->_host_to_uri( %args ); | ||
| 427 | |||
| 428 | my $ff = File::Fetch->new( uri => $remote ); | ||
| 429 | my $status = $ff->fetch( to => $path ); | ||
| 430 | last if $status; | ||
| 431 | $h_ind++; | ||
| 432 | } | ||
| 433 | |||
| 434 | return () if(@$hosts == $h_ind); # no host accessible | ||
| 435 | |||
| 436 | my @testlist; | ||
| 437 | my $fh = IO::File->new($local) | ||
| 438 | or croak("Cannot access local RECENT file [$local]: $!\n"); | ||
| 439 | while (<$fh>) { | ||
| 440 | next unless(/^authors/); | ||
| 441 | next unless(/\.(?:tar\.(?:bz2|gz|Z)|t(?:gz|bz)|(?<!ppm\.)zip|pm.gz)\n$/i); | ||
| 442 | s!authors/id/!!; | ||
| 443 | chomp; | ||
| 444 | push @testlist, $_; | ||
| 445 | } | ||
| 446 | |||
| 447 | return @testlist; | ||
| 448 | } | ||
| 449 | |||
| 450 | sub _get_build_dir { | ||
| 451 | my $self = shift; | ||
| 452 | File::Spec->catdir( | ||
| 453 | $self->{conf}->get_conf('base'), | ||
| 454 | $self->{cpanplus}->_perl_version( perl => $^X ), | ||
| 455 | $self->{conf}->_get_build('moddir') | ||
| 456 | ); | ||
| 457 | } | ||
| 458 | |||
| 459 | sub _setup_local_lib { | ||
| 460 | my $self = shift; | ||
| 461 | return if !$self->{local_lib}; | ||
| 462 | require Cwd; | ||
| 463 | require File::Temp; | ||
| 464 | require CPANPLUS::YACSmoke::locallib; | ||
| 465 | my $tmpdir = File::Temp::tempdir( DIR => '.', CLEANUP => 1 ); | ||
| 466 | my $abs = Cwd::abs_path($tmpdir); | ||
| 467 | CPANPLUS::YACSmoke::locallib->ensure_dir_structure_for( $abs, { quiet => 1 } ); | ||
| 468 | CPANPLUS::YACSmoke::locallib->setup_env_hash_for( $abs ); | ||
| 469 | } | ||
| 470 | |||
| 471 | } | ||
| 472 | |||
| 473 | 'Yakkity Yac'; | ||
| 474 | |||
| 475 | __END__ |