Rewrite the script to create art tests in python.

This makes it easier to use on Windows and makes it a lot easier to
read and modify as well.

Change-Id: I9055014e722ae08ad9d907b450b6ca3f20401adf
diff --git a/build.gradle b/build.gradle
index 4caf5fa..37745576 100644
--- a/build.gradle
+++ b/build.gradle
@@ -295,13 +295,12 @@
 
 task createArtTests(type: Exec) {
     def outputDir = "build/generated/test/java/com/android/tools/r8/art"
-    def createArtTestsScript = "scripts/create-art-tests.sh"
+    def createArtTestsScript = "tools/create_art_tests.py"
     inputs.file "tests/art.tar.gz"
     inputs.file createArtTestsScript
     outputs.dir outputDir
     dependsOn downloadDeps
-    executable "bash"
-    args "${projectDir}/${createArtTestsScript}"
+    commandLine "${createArtTestsScript}"
     workingDir = projectDir
 }
 
diff --git a/scripts/create-art-tests.sh b/scripts/create-art-tests.sh
deleted file mode 100755
index dc6d6d2..0000000
--- a/scripts/create-art-tests.sh
+++ /dev/null
@@ -1,75 +0,0 @@
-#!/bin/bash
-#
-# Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
-# for details. All rights reserved. Use of this source code is governed by a
-# BSD-style license that can be found in the LICENSE file.
-
-function generate_test() {
-  local name=$1
-  local testClassName=$2
-  local testGeneratingToolchain=$3
-  # The bash uppercase substitution ^^ is not supported on the bash version on Mac OS.
-  local testGeneratingToolchainEnum=$(echo $testGeneratingToolchain | tr /a-z/ /A-Z/)
-  local fileName=$4
-  local compilerUnderTest=$5
-  local compilerUnderTestEnum=$(echo ${compilerUnderTest} | tr /a-z/ /A-Z/)
-
-  cat <<EOF > $fileName
-// Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
-// for details. All rights reserved. Use of this source code is governed by a
-// BSD-style license that can be found in the LICENSE file.
-package com.android.tools.r8.art.${testGeneratingToolchain}.${compilerUnderTest};
-
-import static com.android.tools.r8.R8RunArtTestsTest.DexTool.${testGeneratingToolchainEnum};
-
-import com.android.tools.r8.R8RunArtTestsTest;
-import org.junit.Test;
-
-/**
- * Auto-generated test for the art ${name} test using the ${testGeneratingToolchain} toolchain.
- *
- * DO NOT EDIT THIS FILE. EDIT THE HERE DOCUMENT TEMPLATE IN scripts/create-art-tests.sh INSTEAD!
- */
-public class ${testClassName} extends R8RunArtTestsTest {
-
-    public ${testClassName}() {
-      super("${name}", ${testGeneratingToolchainEnum});
-    }
-
-    @Test
-    public void run${testClassName}() throws Throwable {
-      // For testing with other Art VMs than the default pass the VM version as a argument to
-      // runArtTest, e.g. runArtTest(ToolHelper.ART_4_4_4).
-      runArtTest(CompilerUnderTest.${compilerUnderTestEnum});
-    }
-}
-EOF
-}
-
-TESTDIR="tests/art"
-TOOLCHAINS=("dx" "jack" "none")
-DESTINATIONDIR="build/generated/test/java/com/android/tools/r8/art"
-
-if [ ! -e $TESTDIR ]; then
-  echo "Missing art tests in $TESTDIR."
-  exit
-fi
-
-for TOOLCHAIN in ${TOOLCHAINS[@]}; do
-  for d in $DESTINATIONDIR/$TOOLCHAIN/r8 $DESTINATIONDIR/$TOOLCHAIN/d8; do
-    rm -rf $d
-    mkdir -p $d
-  done
-  # class files are found in the dx directory.
-  if [ "$TOOLCHAIN" == "none" ]; then
-    SOURCEDIR=${TESTDIR}/dx
-  else
-    SOURCEDIR=${TESTDIR}/${TOOLCHAIN}
-  fi
-  for TEST in ${SOURCEDIR}/*; do
-    TESTNAME=$(basename $TEST)
-    TESTCLASSNAME="Art${TESTNAME//-/_}Test"
-    generate_test $TESTNAME $TESTCLASSNAME ${TOOLCHAIN} $DESTINATIONDIR/$TOOLCHAIN/r8/$TESTCLASSNAME.java r8
-    generate_test $TESTNAME $TESTCLASSNAME ${TOOLCHAIN} $DESTINATIONDIR/$TOOLCHAIN/d8/$TESTCLASSNAME.java d8
-  done
-done
diff --git a/tools/create_art_tests.py b/tools/create_art_tests.py
new file mode 100755
index 0000000..3a83cbc
--- /dev/null
+++ b/tools/create_art_tests.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+# Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
+# for details. All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+
+from os import makedirs, listdir
+from os.path import join, exists, isdir
+from string import Template, upper
+from sys import exit
+from shutil import rmtree
+
+OUTPUT_DIR = "build/generated/test/java/com/android/tools/r8/art"
+TEST_DIR = "tests/art"
+TOOLCHAINS = ["dx", "jack", "none"]
+TOOLS = ["r8", "d8"]
+TEMPLATE = Template(
+"""// Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.art.$testGeneratingToolchain.$compilerUnderTest;
+
+import static com.android.tools.r8.R8RunArtTestsTest.DexTool.$testGeneratingToolchainEnum;
+
+import com.android.tools.r8.R8RunArtTestsTest;
+import org.junit.Test;
+
+/**
+ * Auto-generated test for the art $name test using the $testGeneratingToolchain toolchain.
+ *
+ * DO NOT EDIT THIS FILE. EDIT THE HERE DOCUMENT TEMPLATE IN tools/create_art_tests.py INSTEAD!
+ */
+public class $testClassName extends R8RunArtTestsTest {
+
+    public $testClassName() {
+      super("$name", $testGeneratingToolchainEnum);
+    }
+
+    @Test
+    public void run$testClassName() throws Throwable {
+      // For testing with other Art VMs than the default pass the VM version as a argument to
+      // runArtTest, e.g. runArtTest(ToolHelper.ART_4_4_4).
+      runArtTest(CompilerUnderTest.$compilerUnderTestEnum);
+    }
+}
+""")
+
+def create_toolchain_dirs(toolchain):
+  toolchain_dir = join(OUTPUT_DIR, toolchain)
+  if exists(toolchain_dir):
+    rmtree(toolchain_dir)
+  makedirs(join(toolchain_dir, "d8"))
+  makedirs(join(toolchain_dir, "r8"))
+
+def write_file(toolchain, tool, class_name, contents):
+  file_name = join(OUTPUT_DIR, toolchain, tool, class_name + ".java")
+  with open(file_name, "w") as file:
+    file.write(contents)
+
+def create_tests(toolchain):
+  source_dir = join(TEST_DIR, "dx" if toolchain == "none" else toolchain)
+  dirs = [d for d in listdir(source_dir)
+          if isdir(join(source_dir, d))]
+  for dir in dirs:
+    class_name = "Art" + dir.replace("-", "_") + "Test"
+    for tool in TOOLS:
+      contents = TEMPLATE.substitute(
+          name=dir,
+          compilerUnderTestEnum=upper(tool),
+          compilerUnderTest=tool,
+          testGeneratingToolchain=toolchain,
+          testGeneratingToolchainEnum=upper(toolchain),
+          testClassName=class_name)
+      write_file(toolchain, tool, class_name, contents)
+
+
+def main():
+  for toolchain in TOOLCHAINS:
+    create_toolchain_dirs(toolchain)
+    create_tests(toolchain)
+
+if __name__ == "__main__":
+  exit(main())