Create py2-cmd which behaves more like the normal `python`

Instead of executing the __main__.py inside the par file, it'll use all
the normal python command line options, allowing you to replace python
in $PATH with this executable.

Also add distutils, which some script expect to be in the stdlib.

Bug: 117811537
Test: move py2-cmd to prebuilts/build-tools/path/linux-x86/python and build
Change-Id: Iddab6ab4db23f99953ecf4d42cd7d966851b1e1b
diff --git a/Android.bp b/Android.bp
index 5d72eda..aba4538 100644
--- a/Android.bp
+++ b/Android.bp
@@ -211,8 +211,8 @@
     ],
 }
 
-cc_binary {
-    name: "py2-launcher",
+cc_defaults {
+    name: "py2-launcher-defaults",
     defaults: ["py2-interp-defaults"],
     cflags: [
         "-DVERSION=\"2.7\"",
@@ -226,8 +226,32 @@
         "-DDATE=\"Dec 31 1969\"",
         "-DTIME=\"23:59:59\"",
     ],
+    static_libs: [
+        "libbase",
+        "libcrypto",
+        "libexpat",
+        "libssl",
+        "libz",
+    ],
+    target: {
+        linux_glibc_x86_64: {
+            host_ldlibs: ["-lutil"],
+        },
+        host: {
+            static_libs: ["libsqlite"],
+        },
+        // Use shared libsqlite for device side, otherwise
+        // the executable size will be really huge.
+        android: {
+            shared_libs: ["libsqlite"],
+        },
+    },
+}
+
+cc_library_static {
+    name: "py2-launcher-lib",
+    defaults: ["py2-launcher-defaults"],
     srcs: [
-        "Launcher/launcher_main.cpp",
         "Modules/gcmodule.c",
         "Modules/getpath.c",
         "Modules/config.c",
@@ -246,8 +270,7 @@
         "Modules/main.c",
     ],
     // NOTE: Please update Modules/config.c if new lib get added in the static_libs.
-    static_libs: [
-        "libbase",
+    whole_static_libs: [
         // Be careful the order of these three static libraries below due to
         // missing symbols issues.
         "py2-interp-object",
@@ -307,22 +330,34 @@
         "py2-c-module-_sqlite3",
         "py2-c-module-_ctypes_test",
         "py2-c-module-_ctypes",
-        "libcrypto",
-        "libexpat",
-        "libssl",
-        "libz",
     ],
-    target: {
-        linux_glibc_x86_64: {
-            host_ldlibs: ["-lutil"],
+}
+
+cc_binary {
+    name: "py2-launcher",
+    defaults: ["py2-launcher-defaults"],
+    srcs: ["Launcher/launcher_main.cpp"],
+    static_libs: ["py2-launcher-lib"],
+}
+
+cc_binary {
+    name: "py2-launcher-autorun",
+    defaults: ["py2-launcher-defaults"],
+    srcs: ["Launcher/launcher_main.cpp"],
+    static_libs: ["py2-launcher-lib"],
+    cflags: ["-DANDROID_AUTORUN"],
+}
+
+python_binary_host {
+    name: "py2-cmd",
+    autorun: false,
+    version: {
+        py2: {
+            enabled: true,
+            embedded_launcher: true,
         },
-        host: {
-            static_libs: ["libsqlite"],
-        },
-        // Use shared libsqlite for device side, otherwise
-        // the executable size will be really huge.
-        android: {
-            shared_libs: ["libsqlite"],
+        py3: {
+            enabled: false,
         },
     },
 }
diff --git a/Launcher/launcher_main.cpp b/Launcher/launcher_main.cpp
index c0b0c49..0a34c18 100644
--- a/Launcher/launcher_main.cpp
+++ b/Launcher/launcher_main.cpp
@@ -32,8 +32,12 @@
   // everything's supposed to be hermetic.
   Py_NoUserSiteDirectory = 1;
 
-  // Ignore PYTHONPATH and PYTHONHOME from the environment.
+  // Ignore PYTHONPATH and PYTHONHOME from the environment. Unless we're not
+  // running from inside the zip file, in which case the user may have
+  // specified a PYTHONPATH.
+#ifdef ANDROID_AUTORUN
   Py_IgnoreEnvironmentFlag = 1;
+#endif
 
   Py_DontWriteBytecodeFlag = 1;
 
@@ -45,17 +49,20 @@
   // Set the equivalent of PYTHONHOME internally.
   Py_SetPythonHome(strdup(executable_path.c_str()));
 
-  int new_argc = argc + 1;
-  char **new_argv = reinterpret_cast<char**>(calloc(new_argc, sizeof(*argv)));
+#ifdef ANDROID_AUTORUN
+  argc += 1;
+  char **new_argv = reinterpret_cast<char**>(calloc(argc, sizeof(*argv)));
 
   // Inject the path to our binary into argv[1] so the Py_Main won't parse any
   // other options, and will execute the __main__.py script inside the zip file
   // attached to our executable.
   new_argv[0] = argv[0];
   new_argv[1] = strdup(executable_path.c_str());
-  for (int i = 1; i < argc; i++) {
+  for (int i = 1; i < argc - 1; i++) {
     new_argv[i+1] = argv[i];
   }
+  argv = new_argv;
+#endif
 
-  return Py_Main(new_argc, new_argv);
+  return Py_Main(argc, argv);
 }
diff --git a/Lib/Android.bp b/Lib/Android.bp
index 4fb6372..48e5caa 100644
--- a/Lib/Android.bp
+++ b/Lib/Android.bp
@@ -20,6 +20,7 @@
         "*.py",
         "compiler/**/*.py",
         "ctypes/**/*.py",
+        "distutils/**/*.py",
         "email/**/*.py",
         "encodings/**/*.py",
         "hotshot/**/*.py",