]> git.pond.sub.org Git - empserver/blob - tests/normalize.pl
Tidy up some comments
[empserver] / tests / normalize.pl
1 #!/usr/bin/perl
2
3 # TODO Don't hardcode xdump columns, get them from xdump meta
4
5 use warnings;
6 use strict;
7 use Getopt::Std;
8
9 $Getopt::Std::STANDARD_HELP_VERSION = 1;
10 our ($opt_j, $opt_s);
11 getopts('js')
12     or die "$0: invalid options\n";
13 die "$0: either -j or -s, not both\n"
14     if $opt_j && $opt_s;
15
16 my $ctime_re = qr/(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [ 123][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9] [0-9][0-9][0-9][0-9]/;
17 my $xdfld_re = qr/\([^)]*\)|[^ (][^ ]*/;
18
19 # Current dump, if any
20 # Either zero or the name of the dump we're currently processing.
21 # Legacy dump names start with an uppercase letter, and xdump names
22 # start with a lowercase letter.
23 my $dump = "";
24
25 sub norm_ctime {
26     my ($s) = @_;
27     $s =~ s/$ctime_re/Thu Jan  1 00:00:00 1970/g;
28     return $s;
29 }
30
31 while (<>) {
32     chomp;
33
34     # Strip log timestamp
35     if ($opt_j || $opt_s) {
36         die "$0: malformed line" unless /^$ctime_re /;
37         $_ = substr($_, 25);
38     }
39
40     # Strip trailing white space
41     # We don't really care for it in test output, and git's pre-commit
42     # hook flags it, which is annoying.
43     s/\s+$//;
44
45     # Split off prefix that is not to be normalized
46     my $pfx = '';
47     if ($opt_j) {
48         die "$0: malformed line" unless substr($_, 10, 1) eq ' ';
49         $pfx .= substr($_, 0, 11);
50         # Normalize only player output
51         $_ = substr($_, 11);
52         if (/(^output [^ ]* 1 )(.*)/) {
53             $pfx .= $1;
54             $_ = $2;
55         } else {
56             $pfx .= $_;
57             $_ = '';
58         }
59     }
60
61     if ($opt_s) {
62         $_ = norm_ctime($_);
63         ### Host environment in logs
64         # getrusage() results in server.log
65         s/(End update|done assembling paths) .* user .* system/$1 0.0 user 0.0 system/;
66         # PID in server.log
67         s/(Empire server \(pid) [0-9]+(\) started)/$1 42$2/;
68         ### Harmless races
69         # shutdown wins race with logout
70         next if /Waiting for player threads to terminate/;
71         print "$pfx$_\n";
72         next;
73     }
74
75     $dump = ""
76         if ($dump =~ /^[a-z]/
77             and /^\//)
78         or ($dump =~ /^[A-Z]/
79             and (/\: No (sector|ship|plane|unit|nuke)\(s\)|\: Nothing lost/
80                  or /^[0-9]+ (sector|ship|plane|unit|nuke|lost item)/));
81
82     ### Formatted time
83     # nat_timeused in prompt
84     s/^\[[0-9]+(:[0-9]+\] Command \:)/[0$1/;
85     $pfx =~ s/( output [^ ]* 6) [0-9]+ ([0-9]+$)/$1 0 $2/
86         if $opt_j;
87     # TODO command play column time
88     # result of ctime() in many commands
89     $_ = norm_ctime($_)
90         unless $dump;
91     ### Time values in legacy dumps
92     s/(DUMP (SECTOR|SHIPS|PLANES|LAND UNITS|NUKES|LOST ITEMS)) [0-9]+$/$1 0/;
93     s/ [0-9]+$/ 0/
94         if $dump eq 'LOST ITEMS';
95     ### Time values in xdump
96     s/(XDUMP (meta )?[-a-z0-9]+) [0-9]+$/$1 0/
97         unless $dump;
98     # HACK: assume any integer with more than 10 digits is time
99     # TODO don't do that, use xdump meta instead
100     s/(^| )[0-9]{10,}/${1}0/g
101         if $dump =~ /^[a-z]/;
102     # timeused in xdump country timeused (column 10)
103     s/^(($xdfld_re ){10})([0-9]+) /${1}255 /
104         if $dump eq 'country';
105     # timeused in xdump nat (column 15)
106     s/^(($xdfld_re ){15})([0-9]+) /${1}255 /
107         if $dump eq 'nat';
108     # duration in xdump news (column 4)
109     s/^(($xdfld_re ){4})([0-9]+) /${1}0 /
110         if $dump eq 'news';
111     ### nsc_type values in xdump
112     # Can vary between systems, because the width of enumeration types
113     # is implementation-defined.
114     # TODO type in xdump meta
115     ### nrndx values in xdump
116     # Encoding depends on the host, see resources[].  Too clever by half;
117     # perhaps we should change it.
118     # nrndx in xdump product (column 12)
119     s/^(($xdfld_re ){12})([0-9]+) /${1}0 /
120         if $dump eq 'product';
121     # value in xdump resources (column 0)
122     s/^[0-9]+ /0 /
123         if $dump eq 'resources';
124     ### Floating-point zero in xdump
125     # Windows %#g prints it with seven significant digits instead of six
126     s/ 0\.000000/ 0.00000/g
127         if $dump =~ /^[a-z]/;
128
129     print "$pfx$_\n";
130
131     if (/(XDUMP|^config) (meta )?([-a-z0-9]+)/) {
132         $dump = $3;
133         die unless $dump =~ /^[a-z]/;
134     } elsif (/DUMP (SECTOR|SHIPS|PLANES|LAND UNITS|NUKES|LOST ITEMS) /) {
135         $dump = $1;
136     }
137 }