Rewrite run-tests in python (#647)

diff --git a/test/shaping/Makefile.am b/test/shaping/Makefile.am
index 592754b..4cf5574 100644
--- a/test/shaping/Makefile.am
+++ b/test/shaping/Makefile.am
@@ -26,7 +26,7 @@
 	hb-unicode-encode \
 	hb-unicode-prettyname \
 	record-test.sh \
-	run-tests.sh \
+	run-tests.py \
 	texts/in-tree \
 	fonts/sha1sum \
 	$(TESTS) \
@@ -104,7 +104,7 @@
 	$(NULL)
 endif
 
-TESTS_LOG_COMPILER = sh $(srcdir)/run-tests.sh
+TESTS_LOG_COMPILER = python $(srcdir)/run-tests.py
 
 .PHONY: manifests
 
diff --git a/test/shaping/run-tests.py b/test/shaping/run-tests.py
new file mode 100755
index 0000000..a9aec0c
--- /dev/null
+++ b/test/shaping/run-tests.py
@@ -0,0 +1,115 @@
+#!/usr/bin/env python
+
+from __future__ import print_function
+import sys, os, subprocess
+
+
+try:
+	input = raw_input
+except NameError:
+	pass
+
+
+def cmd(command):
+	p = subprocess.Popen (
+		command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+	p.wait ()
+	print (p.stderr.read (), file=sys.stderr)
+	return p.stdout.read ().decode ("utf-8"), p.returncode
+
+
+srcdir = os.environ.get ("srcdir", ".")
+builddir = os.environ.get ("builddir", ".")
+top_builddir = os.environ.get ("top_builddir",
+	os.path.normpath (os.path.join (os.getcwd (), "..", "..")))
+EXEEXT = os.environ.get ("EXEEXT", "")
+
+extra_options = "--verify"
+hb_shape = os.path.join (top_builddir, "util", "hb-shape" + EXEEXT)
+
+fails = 0
+args = sys.argv[1:]
+
+reference = False
+if len (args) and args[0] == "--reference":
+	reference = True
+	args = args[1:]
+
+if not len (args):
+	args = [sys.stdin]
+
+for f in args:
+	if not reference:
+		if f == sys.stdin:
+			print ("Running tests from standard input")
+		else:
+			print ("Running tests in " + f)
+
+	if f == sys.stdin:
+		def f():
+			while True:
+				try:
+					line = input ()
+				except EOFError:
+					break
+
+				if len (line):
+					yield line
+				else:
+					break
+		f = f()
+	else:
+		f = open (f)
+
+	for line in f:
+		if line.startswith ("#"):
+			continue
+
+		fontfile, options, unicodes, glyphs_expected = line.split (":")
+		if not reference:
+			print ("# hb-shape %s --unicodes %s" % (fontfile, unicodes))
+
+		if not reference:
+			print ("hb-shape %s %s %s --unicodes %s" %
+					 (fontfile, extra_options, options, unicodes))
+
+		glyphs1, returncode = cmd ([hb_shape, "--font-funcs=ft",
+			os.path.join (srcdir, fontfile), extra_options, "--unicodes",
+			unicodes] + (options.split (' ') if len(options) else []))
+
+		if returncode:
+			print ("hb-shape --font-funcs=ft failed.", file=sys.stderr)
+			fails = fails + 1
+			#continue
+
+		glyphs2, returncode = cmd ([hb_shape, "--font-funcs=ot",
+			os.path.join (srcdir, fontfile), extra_options, "--unicodes",
+			unicodes] + (options.split (' ') if len(options) else []))
+
+		if returncode:
+			print ("hb-shape --font-funcs=ot failed.", file=sys.stderr)
+			fails = fails + 1
+			#continue
+
+		if glyphs1 != glyphs2:
+			print ("FT funcs: " + glyphs1, file=sys.stderr)
+			print ("OT funcs: " + glyphs2, file=sys.stderr)
+			fails = fails + 1
+
+		if reference:
+			print (":".join ([fontfile, options, unicodes, glyphs1]))
+			continue
+
+		if glyphs1.strip() != glyphs_expected.strip():
+			print ("Actual:   " + glyphs1, file=sys.stderr)
+			print ("Expected: " + glyphs_expected, file=sys.stderr)
+			fails = fails + 1
+
+if fails != 0:
+	if not reference:
+		print (str (fails) + " tests failed.")
+	sys.exit (1)
+
+else:
+	if not reference:
+		print ("All tests passed.")
diff --git a/test/shaping/run-tests.sh b/test/shaping/run-tests.sh
deleted file mode 100755
index 5fa8017..0000000
--- a/test/shaping/run-tests.sh
+++ /dev/null
@@ -1,65 +0,0 @@
-#!/bin/sh
-
-test "x$srcdir" = x && srcdir=.
-test "x$builddir" = x && builddir=.
-test "x$top_builddir" = x && top_builddir=../..
-
-extra_options="--verify"
-hb_shape="$top_builddir/util/hb-shape$EXEEXT"
-#hb_shape="$top_builddir/util/hb-shape$EXEEXT"
-
-fails=0
-
-reference=false
-if test "x$1" = x--reference; then
-	reference=true
-	shift
-fi
-
-if test $# = 0; then
-	set /dev/stdin
-fi
-
-for f in "$@"; do
-	$reference || echo "Running tests in $f"
-	while IFS=: read fontfile options unicodes glyphs_expected; do
-		if echo "$fontfile" | grep -q '^#'; then
-			$reference || echo "# hb-shape $fontfile --unicodes $unicodes"
-			continue
-		fi
-		$reference || echo "hb-shape $fontfile $extra_options $options --unicodes $unicodes"
-		glyphs1=`$hb_shape --font-funcs=ft "$srcdir/$fontfile" $extra_options $options --unicodes "$unicodes"`
-		if test $? != 0; then
-			echo "hb-shape --font-funcs=ft failed." >&2
-			fails=$((fails+1))
-			#continue
-		fi
-		glyphs2=`$hb_shape --font-funcs=ot "$srcdir/$fontfile" $extra_options $options --unicodes "$unicodes"`
-		if test $? != 0; then
-			echo "hb-shape --font-funcs=ot failed." >&2
-			fails=$((fails+1))
-			#continue
-		fi
-		if ! test "x$glyphs1" = "x$glyphs2"; then
-			echo "FT funcs: $glyphs1" >&2
-			echo "OT funcs: $glyphs2" >&2
-			fails=$((fails+1))
-		fi
-		if $reference; then
-			echo "$fontfile:$options:$unicodes:$glyphs1"
-			continue
-		fi
-		if ! test "x$glyphs1" = "x$glyphs_expected"; then
-			echo "Actual:   $glyphs1" >&2
-			echo "Expected: $glyphs_expected" >&2
-			fails=$((fails+1))
-		fi
-	done < "$f"
-done
-
-if test $fails != 0; then
-	$reference || echo "$fails tests failed."
-	exit 1
-else
-	$reference || echo "All tests passed."
-fi