gn2bp: Fix action_foreach with GnRunBinary

* Remove the python3 calls and replace them with the binary right away.
* Fix action_foreach where sources can be used with {{source_file_name}}

Test: None
Change-Id: I0c2e6fd5135de70339168b9ba5d3b7f3a6866fdc
diff --git a/tools/gn2bp/Android.bp.swp b/tools/gn2bp/Android.bp.swp
index 6a43ee8..a0696d9 100644
--- a/tools/gn2bp/Android.bp.swp
+++ b/tools/gn2bp/Android.bp.swp
@@ -10990,22 +10990,28 @@
     name: "cronet_aml_net_base_registry_controlled_domains_lookup_strings_test_sets__testing",
     cmd: "$(location net/tools/dafsa/make_dafsa.py) $(location net/base/registry_controlled_domains/effective_tld_names.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest1.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest1-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest2.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest2-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest3.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest3-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest4.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest4-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest5.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest5-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest6.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest6-inc.cc)",
     out: [
@@ -11038,27 +11044,33 @@
     cmd: "$(location net/tools/dafsa/make_dafsa.py) --reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest1.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest1-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest2.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest2-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest3.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest3-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest4.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest4-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest5.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest5-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest6.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest6-reversed-inc.cc)",
@@ -11092,27 +11104,33 @@
     cmd: "$(location net/tools/dafsa/make_dafsa.py) --reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest1.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest1-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest2.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest2-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest3.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest3-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest4.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest4-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest5.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest5-reversed-inc.cc) " +
-         "&& python3 $(location net/tools/dafsa/make_dafsa.py) " +
+         "&& " +
+         "python3 $(location net/tools/dafsa/make_dafsa.py) " +
          "--reverse " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest6.gperf) " +
          "$(location net/base/registry_controlled_domains/effective_tld_names_unittest6-reversed-inc.cc)",
@@ -12754,16 +12772,19 @@
         "cronet_aml_net_tools_transport_security_state_generator_transport_security_state_generator__testing",
     ],
     cmd: "$(location cronet_aml_net_tools_transport_security_state_generator_transport_security_state_generator__testing) " +
+         "$(location net/http/transport_security_state_static_unittest1.json) " +
          "$(location net/http/transport_security_state_static_unittest.pins) " +
          "$(location net/http/transport_security_state_static_unittest.template) " +
          "$(location net/http/transport_security_state_static_unittest1.h) " +
-         "&& python3 $(location build/gn_run_binary.py) " +
-         "clang_x64/transport_security_state_generator " +
+         "&& " +
+         "$(location cronet_aml_net_tools_transport_security_state_generator_transport_security_state_generator__testing) " +
+         "$(location net/http/transport_security_state_static_unittest2.json) " +
          "$(location net/http/transport_security_state_static_unittest.pins) " +
          "$(location net/http/transport_security_state_static_unittest.template) " +
          "$(location net/http/transport_security_state_static_unittest2.h) " +
-         "&& python3 $(location build/gn_run_binary.py) " +
-         "clang_x64/transport_security_state_generator " +
+         "&& " +
+         "$(location cronet_aml_net_tools_transport_security_state_generator_transport_security_state_generator__testing) " +
+         "$(location net/http/transport_security_state_static_unittest3.json) " +
          "$(location net/http/transport_security_state_static_unittest.pins) " +
          "$(location net/http/transport_security_state_static_unittest.template) " +
          "$(location net/http/transport_security_state_static_unittest3.h)",
diff --git a/tools/gn2bp/gen_android_bp b/tools/gn2bp/gen_android_bp
index 4970fb9..df12d42 100755
--- a/tools/gn2bp/gen_android_bp
+++ b/tools/gn2bp/gen_android_bp
@@ -1041,10 +1041,19 @@
       return "$(location %s)" % arg.replace("gen/", "")
     return arg
 
+  def _replace_binary(self, arg):
+    if arg in self.binary_to_target:
+      return '$(location %s)' % self.binary
+    return arg
+
+  def _remove_python_args(self):
+    self.target.args = [arg for arg in self.target.args if "python3" not in arg]
+
   def _sanitize_args(self):
     self._update_all_args(self._sanitize_filepath_with_location_tag)
     self._update_all_args(self._replace_gen_with_location_tag)
-    self._set_arg_at(0, '$(location %s)' % self.binary)
+    self._update_all_args(self._replace_binary)
+    self._remove_python_args()
     super()._sanitize_args()
 
   def get_tools(self):
@@ -1238,7 +1247,8 @@
     # don't add script arg for the first source -- create_action_module
     # already does this.
     if i != 0:
-      new_args.append('&& python3 $(location %s)' %
+      new_args.append('&&')
+      new_args.append('python3 $(location %s)' %
                    gn_utils.label_to_path(target.script))
     for arg in target.args:
       if '{{source}}' in arg:
@@ -1252,6 +1262,10 @@
         for out in target.outputs:
           if out.endswith(file_name):
             new_args.append('$(location %s)' % out)
+
+        for file in target.sources:
+          if file.endswith(file_name):
+            new_args.append('$(location %s)' % gn_utils.label_to_path(file))
       else:
         new_args.append(arg)