Jump to content

User:AnomieBOT/source/test.pl

From Wikipedia, the free encyclopedia
#!/usr/bin/perl -w

use strict;

# binmodes
$|=1;
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';

my $debug=-1;
for(my $i=@ARGV-1; $i>=0; $i--){
    my $arg=$ARGV[$i];
    next unless $arg=~/^--/;
    splice(@ARGV,$i,1);
    if($arg eq '--nodebug'){ $debug=0; }
    elsif($arg =~ /--debug=(\d+)/){ $debug=$1; }
    else { die "Unrecognized option $arg\n"; }
}
die "USAGE: $0 [--nodebug|--debug=<n>] botnum taskfile\n" if @ARGV!=2;

my $dir="/tmp/anomiebot-test";
die "Could not create directory $dir: $!\n" if(!-d $dir && !mkdir($dir));
if(-e $dir.'/test'){
    unlink($dir.'/test');
    die "Could not remove test file in $dir: $!\n" if(-e $dir.'/test');
}
open(X, ">", $dir.'/test') or die("Could not create test file in $dir: $!\n");
close(X);
unlink($dir.'/test');

use Cwd;
use File::Basename;
use lib File::Basename::dirname( Cwd::realpath( __FILE__ ) );
use AnomieBOT::API;

my $task='';
open(X, '<:utf8', $ARGV[1]) or die("Could not open task file: $!\n");
while(<X>){
    $task=$1 if /^package (.*);$/;
}
close(X);
die "Invalid task file" unless $task ne '';
AnomieBOT::API::load($ARGV[1]);
$task=$task->new();

my $api=AnomieBOT::API->new('conf.ini', $ARGV[0]);
$api->{'nopause'}=1;
$api->{'noedit'}=$dir;
$api->login();
$api->DEBUG($debug);
my $t;
do {
    $t=$task->run($api);
    $t='undef' if !defined($t);
    warn "Returned $t\n";
} while($t ne 'undef' && $t <= 60);