Skip to content

Commit

Permalink
[mycpp] Move Python script out of shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Sep 8, 2022
1 parent f73182b commit ae481f9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 62 deletions.
56 changes: 1 addition & 55 deletions mycpp/NINJA-steps.sh
Expand Up @@ -235,65 +235,11 @@ checksum() {
lines "$@" | sort | xargs md5sum
}

# TODO: Could rewrite this in shell:
# - md5sum
# - read hash1 path1; read hash2 path2;
# - and then invoke diff if they're not equal

compare-pairs() {
python2 -c '
from __future__ import print_function
import subprocess
import sys
def Check(left, right):
with open(left) as f1, open(right) as f2:
b1 = f1.read()
b2 = f2.read()
if b1 != b2:
print("%s != %s" % (left, right))
# Only invoke a subprocess when they are NOT equal
subprocess.call(["diff", "-u", left, right])
return False
return True
num_failures = 0
paths = sys.argv[1:]
n = len(paths)
i = 0
while i < n:
log_path = paths[i]
py_path = paths[i+1]
#print(log_path, py_path)
if not Check(log_path, py_path):
num_failures += 1
else:
print("OK %s" % log_path)
print(" %s" % py_path)
i += 2
if num_failures != 0:
print("logs-equal: %d failures" % num_failures)
sys.exit(1)
' "$@"
}

logs-equal() {
local out=$1
shift

{
compare-pairs "$@"
# echo
# checksum "$@"
} | tee $out
mycpp/compare_pairs.py "$@" | tee $out
}

if test $(basename $0) = 'NINJA-steps.sh'; then
Expand Down
17 changes: 10 additions & 7 deletions mycpp/NINJA_subgraph.py
Expand Up @@ -119,8 +119,8 @@ def ShouldSkipBuild(name):
return True

if name in [
# these 3 use Oil code, and don't type check or compile
# Maybe give up on these? pgen2_demo might be useful later.
# these use Oil code, and don't type check or compile. Maybe give up on
# them? pgen2_demo might be useful later.
'lexer_main',
'pgen2_demo',
]:
Expand All @@ -143,6 +143,8 @@ def ExamplesToBuild():


def ShouldSkipTest(name):
#return False

# '%5d' doesn't work yet. TODO: fix this.
if name in (
'strings',
Expand Down Expand Up @@ -575,7 +577,7 @@ def NinjaGraph(n):
#log('Skipping test of %s', ex)
continue

log_out = '%s.log.txt' % prefix
log_out = '%s.log' % prefix
n.build([task_out, log_out], 'example-task',
EXAMPLES_PY.get(ex, []) + ['mycpp/examples/%s.py' % ex],
variables=[
Expand All @@ -589,7 +591,7 @@ def NinjaGraph(n):

# Don't run it for now; just compile
if translator == 'pea':
continue
continue

# minimal
MATRIX = [
Expand All @@ -612,8 +614,8 @@ def NinjaGraph(n):
#log('Skipping test of %s', ex)
continue

cc_log_out = '_test/tasks/%s/%s.%s.%s.log.txt' % (mode, ex, translator, variant)
py_log_out = '_test/tasks/%s/%s.py.log.txt' % (mode, ex)
cc_log_out = '_test/tasks/%s/%s.%s.%s.log' % (mode, ex, translator, variant)
py_log_out = '_test/tasks/%s/%s.py.log' % (mode, ex)

to_compare.append(cc_log_out)
to_compare.append(py_log_out)
Expand All @@ -627,10 +629,11 @@ def NinjaGraph(n):
n.newline()

# Compare the log of all examples
out = '_test/mycpp-logs-equal.txt'
out = '_test/mycpp-compare-passing.txt'
n.build([out], 'logs-equal', to_compare)
n.newline()

# NOTE: Don't really need this
phony['mycpp-logs-equal'].append(out)

# Timing of benchmarks
Expand Down
58 changes: 58 additions & 0 deletions mycpp/compare_pairs.py
@@ -0,0 +1,58 @@
#!/usr/bin/env python2
"""
compare_pairs.py
"""
from __future__ import print_function

import subprocess
import sys

def Check(left, right):
with open(left) as f1, open(right) as f2:
b1 = f1.read()
b2 = f2.read()

if b1 != b2:
print("%s != %s" % (left, right))
sys.stdout.flush() # prevent interleaving

# Only invoke a subprocess when they are NOT equal
subprocess.call(["diff", "-u", left, right])
return False

return True

def main(argv):
num_failures = 0

paths = argv[1:]
n = len(paths)
i = 0
while i < n:
log_path = paths[i]
py_path = paths[i+1]

#print(log_path, py_path)

if not Check(log_path, py_path):
num_failures += 1
else:
print("OK %s" % log_path)
print(" %s" % py_path)
#sys.stdout.flush()

i += 2

if num_failures != 0:
print("logs-equal: %d failures" % num_failures)
return 1

return 0


if __name__ == '__main__':
try:
sys.exit(main(sys.argv))
except RuntimeError as e:
print('FATAL: %s' % e, file=sys.stderr)
sys.exit(1)

0 comments on commit ae481f9

Please sign in to comment.