File: tricensus-manager

package info (click to toggle)
regina-normal 4.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,824 kB
  • ctags: 7,862
  • sloc: cpp: 63,296; ansic: 12,913; sh: 10,556; perl: 3,294; makefile: 947; python: 188
file content (188 lines) | stat: -rwxr-xr-x 4,773 bytes parent folder | download
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
#!/usr/bin/perl -w
#
# Regina - A Normal Surface Theory Calculator
# Manage a triangulation census distributed amongst several machines
#
# Copyright (c) 2002-2008, Ben Burton
# For further details contact Ben Burton (bab@debian.org).
#
# Usage: tricensus-manager [ <tricensus-options> ] <pairs-file-prefix>
#
# See the manpage or users' handbook for full usage instructions.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
#

use Fcntl ':flock';
use strict;

sub usage {
    print STDERR "Usage: $0 [ <tricensus-options> ] <pairs-file-prefix>\n";
    exit(1);
}

# --- Parse the command-line options. ---

if (! @ARGV) {
    usage;
}

my $prefix = pop @ARGV;
if ($prefix =~ /^-/) {
    print STDERR "Invalid pairs file prefix: $prefix\n";
    usage;
}

my @tricensusArgs = ();
foreach my $arg (@ARGV) {
    if ((($arg !~ /^-/) and ($arg !~ /^\d+$/)) or ($arg =~ /\'/)) {
        print STDERR "Invalid tricensus argument: $arg\n";
        usage;
    }

    # Check for --genpairs.
    if (($arg =~ /^--genpairs$/i) or ($arg =~ /^-([^-].*)?p/)) {
        print STDERR "Face pairings should already have been generated.\n";
        print STDERR "Tricensus argument --genpairs should not be used " .
            "with this utility.\n";
        usage;
    }

    # Silently strip out --usepairs.
    next if (($arg =~ /^--usepairs$/i) or ($arg =~ /^-P+$/));
    if ($arg =~ /^-[^-]/) {
        $arg =~ s/P//g;
    }

    push @tricensusArgs, $arg;
}

# --- Find the full list of pairs files to work on. ---

if (($prefix =~ /\*/) or ($prefix =~ /\?/)) {
    print STDERR "The pairs file prefix [$prefix] " .
        "should not contain wildcards.\n";
    usage;
}

my @pairsFiles = <$prefix*.pairs>;
if (! @pairsFiles) {
    print STDERR "No pairs files could be found beginning with $prefix.\n";
    usage;
}

# --- Establish which real tricensus to run. ---

chomp (my $tricensusDir = `dirname "$0"`);
my $tricensus = $tricensusDir . '/tricensus';
if (! -e $tricensus) {
    print STDERR "Cannot find tricensus utility [$tricensus].\n";
    exit(1);
}

# --- Work through the files one by one. ---

my $base;
my $time;
my $progress;
my $output;

my $command;
my @commandArgs;
my $errMsg;

my @startTimes;
my @endTimes;
my $userTime;
my $sysTime;

# Switch on autoflush for stdout.
$| = 1;

# Establish our machine name.
my $hostname;
$hostname = `hostname` or $hostname = 'unknown';
chomp $hostname;

my $retVal = 0;
foreach my $pairs (@pairsFiles) {
    print "Trying $pairs ... ";

    $base = $pairs;
    $base =~ s/\.pairs$//;
    $time = $base . '.time';
    $progress = $base . '.progress';
    $output = $base . '.rga';

    if (-e $time) {
        print "skipped.\n";
        next;
    }

    if (! (open(TAKEN, '>>', $time) and flock(TAKEN, LOCK_EX | LOCK_NB))) {
        close TAKEN;
        print "skipped.\n";
        next;
    }

    # Take this pairs file!
    print 'taken and processing ... ';

    # Make a note of who's taken the process.
    print TAKEN "Taken by " . $hostname . ", PID " . $$ . ".\n";

    # Prepare the command-line.
    @commandArgs = @tricensusArgs;
    push @commandArgs, $output;
    $command = "'$tricensus' -P '" . join("' '", @commandArgs) .
        "' < '$pairs' > '$progress'";

    # Execute this piece of the census!
    $errMsg = '';

    print TAKEN "Started at " . localtime() . ".\n";
    @startTimes = times;

    if (system($command) != 0) {
        $errMsg = "ERROR: Command failed: $command\n";
    } else {
        @endTimes = times;
        $userTime = $endTimes[2] - $startTimes[2];
        $sysTime = $endTimes[3] - $endTimes[3];

        print TAKEN "Completed at " . localtime() . ".\n";
        print TAKEN "User time (s): $userTime\n";
        print TAKEN "System time (s): $sysTime\n";
    }

    # Did we have troubles with the child process?
    if ($errMsg) {
        print "error.\n";
        print STDERR $errMsg;
        print TAKEN $errMsg;
        $retVal = 1;
    }

    # Tidy up.
    print "done.\n" unless $errMsg;
    flock(TAKEN, LOCK_UN);
    close TAKEN;
}

# --- Return whether or not we encountered any problems. ---

exit($retVal);