2006-07-31 20:53:40 +04:00
|
|
|
#!/usr/bin/perl
|
2007-03-15 01:22:36 +03:00
|
|
|
|
|
|
|
# $Id$
|
2006-07-31 20:53:40 +04:00
|
|
|
# given a moses.ini file, prints a copy to stdout but replaces all relative
|
|
|
|
# paths with absolute paths.
|
|
|
|
#
|
|
|
|
# Ondrej Bojar.
|
|
|
|
|
|
|
|
my $ini = shift;
|
|
|
|
die "usage: absolutize_moses_model.pl path-to-moses.ini > moses.abs.ini"
|
|
|
|
if !defined $ini;
|
|
|
|
|
|
|
|
open INI, $ini or die "Can't read $ini";
|
|
|
|
while (<INI>) {
|
|
|
|
if (/^\[([^\]]*)\]\s*$/) {
|
|
|
|
$section = $1;
|
|
|
|
}
|
|
|
|
if (/^[0-9]/) {
|
|
|
|
if ($section eq "ttable-file" || $section eq "lmodel-file") {
|
|
|
|
chomp;
|
2010-04-09 15:37:43 +04:00
|
|
|
my ($a, $b, $c, $d, $fn) = split / /;
|
2006-07-31 20:53:40 +04:00
|
|
|
$abs = ensure_absolute($fn, $ini);
|
|
|
|
die "File not found or empty: $fn (interpreted as $abs)"
|
|
|
|
if ! -s $abs;
|
2010-04-09 15:37:43 +04:00
|
|
|
$_ = "$a $b $c $d $abs\n";
|
2006-07-31 20:53:40 +04:00
|
|
|
}
|
|
|
|
if ($section eq "generation-file") {
|
|
|
|
chomp;
|
2006-08-08 01:55:06 +04:00
|
|
|
my ($a, $b, $c, $fn) = split / /;
|
2006-07-31 20:53:40 +04:00
|
|
|
$abs = ensure_absolute($fn, $ini);
|
|
|
|
die "File not found or empty: $fn (interpreted as $abs)"
|
|
|
|
if ! -s $abs;
|
2006-08-08 01:55:06 +04:00
|
|
|
$_ = "$a $b $c $abs\n";
|
2006-07-31 20:53:40 +04:00
|
|
|
}
|
|
|
|
if ($section eq "distortion-file") {
|
|
|
|
chomp;
|
2008-02-26 13:24:53 +03:00
|
|
|
my ($a, $b, $c, $fn) = split / /;
|
2006-07-31 20:53:40 +04:00
|
|
|
$abs = ensure_absolute($fn, $ini);
|
|
|
|
die "File not found or empty: $fn (interpreted as $abs)"
|
|
|
|
if ! -s $abs;
|
2008-02-26 13:24:53 +03:00
|
|
|
$_ = "$a $b $c $abs\n";
|
2006-07-31 20:53:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
print $_;
|
|
|
|
}
|
|
|
|
close INI;
|
|
|
|
|
|
|
|
sub safesystem {
|
|
|
|
print STDERR "Executing: @_\n";
|
|
|
|
system(@_);
|
|
|
|
if ($? == -1) {
|
|
|
|
print STDERR "Failed to execute: @_\n $!\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
elsif ($? & 127) {
|
|
|
|
printf STDERR "Execution of: @_\n died with signal %d, %s coredump\n",
|
|
|
|
($? & 127), ($? & 128) ? 'with' : 'without';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $exitcode = $? >> 8;
|
|
|
|
print STDERR "Exit code: $exitcode\n" if $exitcode;
|
|
|
|
return ! $exitcode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ensure_absolute {
|
|
|
|
my $target = shift;
|
|
|
|
my $originfile = shift;
|
|
|
|
|
2006-12-29 01:17:06 +03:00
|
|
|
my $cwd = `pawd`;
|
2007-03-25 09:46:05 +04:00
|
|
|
$cwd = `pwd` if ! defined $cwd; # not everyone has pawd!
|
|
|
|
die "Failed to absolutize $target. Failing to get cwd!" if ! defined $cwd;
|
2006-07-31 20:53:40 +04:00
|
|
|
chomp $cwd;
|
|
|
|
$cwd.="/";
|
|
|
|
|
|
|
|
my $absorigin = ensure_relative_to_origin($originfile, $cwd);
|
|
|
|
return ensure_relative_to_origin($target, $absorigin);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ensure_relative_to_origin {
|
|
|
|
my $target = shift;
|
|
|
|
my $originfile = shift;
|
|
|
|
return $target if $target =~ /^\/|^~/; # the target path is absolute already
|
|
|
|
$originfile =~ s/[^\/]*$//; # where does the origin reside
|
|
|
|
my $out = $originfile."/".$target;
|
|
|
|
$out =~ s/\/+/\//g;
|
2006-08-08 07:41:50 +04:00
|
|
|
$out =~ s/\/(\.\/)+/\//g;
|
2006-07-31 20:53:40 +04:00
|
|
|
return $out;
|
|
|
|
}
|