Cron job

Note

This page is a stub

The idea is to have a set of packages defined in %packages and to run this script weekly to check if BioConda contains a newer version of it and if so to build a new image.

This relies on images with a fixed naming scheme, like packagename~version.

#!/usr/bin/env perl
#/hpc-home/telatina/singularity-cron

use 5.012;
use warnings;

my $filter = $ARGV[0];
use FindBin qw($RealBin);
use Term::ANSIColor qw(:constants);
use Data::Dumper;
use Getopt::Long;

my $images_dir = './images';
my %packages = (
  'mosdepth'   => 'binary=mosdepth;',
  'shovill'    => 'binary=shovill;',
  'checkv'     => 'binary=checkv;',
  'vibrant'    => 'binary=VIBRANT_run.py;',
  'das_tool'   => 'binary=DAS_Tool;',       #etc etc
);



my $download = 0;
GetOptions(
 "--download" => \$download,
);

for my $package (sort keys %packages) {
  if (defined $filter and $package !~/$filter/) {
    say STDERR " - skipping $package";
    next;
  }
  my $version = get_ver($package);
  my $properties = parsestring($packages{$package});
  my $binary = $properties->{'binary'} // $package;

  if (! $version ) {
   say STDERR YELLOW, " * WARNING: Version not found for $package: skipping", RESET;
   next;
  }
  my $image = "$package~$version";
  if ( -e "$images_dir/$image" ) {
   say STDERR YELLOW, " [SKIP] Package $package $version: image found", RESET;
  } else {
   say STDERR BOLD, GREEN, " [NEW!] Package $package $version: new image", RESET;
   if (! $download ) {
     next;
   }
   my $template = (-e "$RealBin/templates/$package.tmp") ? "$RealBin/templates/$package.tmp" : "$RealBin/templates/default.tmp";
   cmd( qq($RealBin/fill_template.pl $template  package=$package version=$version binary=$binary >  $RealBin/defs/$image.def 2>/dev/null));
   cmd( qq(sudo singularity build "$images_dir/$image" "$RealBin"/defs/"$image".def 2>&1 > "$images_dir/singularity_build_$image.log"));
  }
}
sub parsestring {
 my $string = shift @_;
 my %data = ();
 my @pairs = split /;/, $string;
 for my $pair (@pairs) {
   my ($key, $value) = split /=/, $pair;
   $data{$key} = $value;
 }
 return \%data;
}
sub msg {
 my $t = `date +%Y-%m-%d_%H:%S`;
 chomp($t);
 my $line = "[".$t."] @_\n";
 say STDERR $line;
}
sub cmd {
 msg("Running:", @_);
 system(@_)==0 or err("Could not run command:", @_);
}

sub err {
  msg(@_);
  exit(2);
}
sub get_ver {
 my $package = shift @_;
 my $version = `$RealBin/last_version.pl "$package"`;
 if ($?) {
  die " Unable to use <last_version.pl> to detect <$package> last version.\n"
 }
 chomp($version);
 if ($version) {
  return $version;
 } else {
  return 0;
 }
}