|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use Getopt::Std; |
| 7 | + |
| 8 | +my (@luas, @tests); |
| 9 | + |
| 10 | +my %opts; |
| 11 | +getopts('Lse', \%opts) or die "Usage: lua-releng [-L] [-s] [-e] [files]\n"; |
| 12 | + |
| 13 | +my $silent = $opts{s}; |
| 14 | +my $stop_on_error = $opts{e}; |
| 15 | +my $no_long_line_check = $opts{L}; |
| 16 | + |
| 17 | +if ($#ARGV != -1) { |
| 18 | + @luas = @ARGV; |
| 19 | + |
| 20 | +} else { |
| 21 | + @luas = map glob, qw{ *.lua lib/*.lua lib/*/*.lua lib/*/*/*.lua lib/*/*/*/*.lua lib/*/*/*/*/*.lua }; |
| 22 | + if (-d 't') { |
| 23 | + @tests = map glob, qw{ t/*.t t/*/*.t t/*/*/*.t }; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +for my $f (sort @luas) { |
| 28 | + process_file($f); |
| 29 | +} |
| 30 | + |
| 31 | +for my $t (@tests) { |
| 32 | + blank(qq{grep -H -n --color -E '\\--- ?(ONLY|LAST)' $t}); |
| 33 | +} |
| 34 | +# p: prints a string to STDOUT appending \n |
| 35 | +# w: prints a string to STDERR appending \n |
| 36 | +# Both respect the $silent value |
| 37 | +sub p { print "$_[0]\n" if (!$silent) } |
| 38 | +sub w { warn "$_[0]\n" if (!$silent) } |
| 39 | + |
| 40 | +# blank: runs a command and looks at the output. If the output is not |
| 41 | +# blank it is printed (and the program dies if stop_on_error is 1) |
| 42 | +sub blank { |
| 43 | + my ($command) = @_; |
| 44 | + if ($stop_on_error) { |
| 45 | + my $output = `$command`; |
| 46 | + if ($output ne '') { |
| 47 | + die $output; |
| 48 | + } |
| 49 | + } else { |
| 50 | + system($command); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +my $version; |
| 55 | +sub process_file { |
| 56 | + my $file = shift; |
| 57 | + # Check the sanity of each .lua file |
| 58 | + open my $in, $file or |
| 59 | + die "ERROR: Can't open $file for reading: $!\n"; |
| 60 | + my $found_ver; |
| 61 | + while (<$in>) { |
| 62 | + my ($ver, $skipping); |
| 63 | + if (/(?x) (?:_VERSION|version) \s* = .*? ([\d\.]*\d+) (.*? SKIP)?/) { |
| 64 | + my $orig_ver = $ver = $1; |
| 65 | + $found_ver = 1; |
| 66 | + $skipping = $2; |
| 67 | + $ver =~ s{^(\d+)\.(\d{3})(\d{3})$}{join '.', int($1), int($2), int($3)}e; |
| 68 | + w("$file: $orig_ver ($ver)"); |
| 69 | + last; |
| 70 | + |
| 71 | + } elsif (/(?x) (?:_VERSION|version) \s* = \s* ([a-zA-Z_]\S*)/) { |
| 72 | + w("$file: $1"); |
| 73 | + $found_ver = 1; |
| 74 | + last; |
| 75 | + } |
| 76 | + |
| 77 | + if ($ver and $version and !$skipping) { |
| 78 | + if ($version ne $ver) { |
| 79 | + die "$file: $ver != $version\n"; |
| 80 | + } |
| 81 | + } elsif ($ver and !$version) { |
| 82 | + $version = $ver; |
| 83 | + } |
| 84 | + } |
| 85 | + if (!$found_ver) { |
| 86 | + w("WARNING: No \"_VERSION\" or \"version\" field found in `$file`."); |
| 87 | + } |
| 88 | + close $in; |
| 89 | + |
| 90 | + p("Checking use of Lua global variables in file $file..."); |
| 91 | + p("\top no.\tline\tinstruction\targs\t; code"); |
| 92 | + blank("luac -p -l $file | grep -E '[GS]ETGLOBAL' | grep -vE '\\<(require|type|tostring|error|ngx|ndk|jit|setmetatable|getmetatable|string|table|io|os|print|tonumber|math|pcall|xpcall|unpack|pairs|ipairs|assert|module|package|coroutine|[gs]etfenv|next|rawget|rawset|rawlen)\\>'"); |
| 93 | + unless ($no_long_line_check) { |
| 94 | + p("Checking line length exceeding 80..."); |
| 95 | + blank("grep -H -n -E --color '.{81}' $file"); |
| 96 | + } |
| 97 | +} |
| 98 | + |
0 commit comments