File: execcommand.pl

package info (click to toggle)
genomethreader 1.7.3%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 46,528 kB
  • sloc: ansic: 90,168; ruby: 1,769; makefile: 548; sh: 112; perl: 105
file content (118 lines) | stat: -rwxr-xr-x 2,270 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/env perl

use strict;
use warnings;

unless(@ARGV)
{
  print STDERR "Usage: $0 <inputfile>\n";
  exit 1;
}

if((scalar @ARGV) eq 0)
{
  print STDERR "$0: missing argument\n";
  exit 1;
}

my($inputfile) = $ARGV[0];

unless ( -e $inputfile)
{
  print STDERR "$0: file \"$inputfile\" does not exist\n";
  exit 1;
}

unless ( open(INPUTFILEHANDLE, $inputfile) )
{
  print STDERR "Cannot open file \"$inputfile\"";
  exit 1;
}

while (my $in = <INPUTFILEHANDLE>)
{
  if($in =~ m/^\\EXECUTE\{([^\}]*)\}/)
  {
    runtheprogram($1);
  } else
  {
    print $in;
  }
}

sub determinemaxfilewidth
{
  my ($filename) = @_;
  my $maxwidth = 0;
  my $width;

  unless(open(FILEHANDLE,$filename))
  {
    print STDERR "Cannot open file \"$filename\"\n";
    exit 1;
  }
  while(my $line = <FILEHANDLE>)
  {
    $width = length $line;
    if($maxwidth < $width)
    {
      $maxwidth = $width;
    }
  }
  close(FILEHANDLE);
  return $maxwidth;
}

sub runtheprogram
{
  my($argcommand) = @_;
  my(@argv) = split(' ',$argcommand);

  my $program = $argv[0];
  my $outfileprefix = $argcommand;
  $outfileprefix =~ s/ \|.*//;   # delete possible pipe at end
  $outfileprefix =~ s/\s+/-/g;     # delete white spaces
  $outfileprefix =~ s/\*//g;     # delete white spaces
  $outfileprefix =~ s/\'.*\'//g;     # ' ' expressions
  my $outfilename = "output/${outfileprefix}" . ".out";

  if(-f ${outfilename})
  {
    print STDERR "# file ${outfilename} already exists\n";
  } else
  {
    my $fh;
    my @skippipe = split(/\|/,$argcommand);
    unless(open($fh, ">" . $outfilename))
    {
      print STDERR "Cannot open file $outfilename\n";
      exit 1;
    }
    print $fh "\$ $skippipe[0]\n";
    close($fh);
    push(@argv,'>>');
    push(@argv,$outfilename);
    my $argstring = join(' ',@argv);
    print STDERR "run $argstring\n";
    my($retcode) = system($argstring);
    $retcode = $? >> 8;
    if($retcode ne 0)
    {
      print STDERR "failure: $argstring\n";
      unlink($outfilename);
      exit 1;
    }
  }
  my $maxwidth = determinemaxfilewidth($outfilename);
  my $env;
  if($maxwidth < 78)
  {
    $env = "footnotesize";
  } else
  {
    $env = "scriptsize";
  }
  print "\\begin{$env}";
  print "\\verbatiminput{$outfilename}";
  print "\\end{$env}\n";
}