Bazel: Update pex rules for compatibility with 0.27 (#3315)

* update pex rules

* update python flag

* empty
diff --git a/tools/bazel.rc b/tools/bazel.rc
index b1584c2..2346ca4 100644
--- a/tools/bazel.rc
+++ b/tools/bazel.rc
@@ -17,8 +17,8 @@
 
 
 build --genrule_strategy=standalone
+build --host_force_python=PY2
 build --ignore_unsupported_sandboxing
-build --python2_path /usr/bin/python2.7
 build --spawn_strategy=standalone
 build --workspace_status_command scripts/release/status.sh
 
@@ -32,7 +32,6 @@
 build:centos --linkopt -lm
 build:centos --linkopt -lpthread
 build:centos --linkopt -lrt
-build:centos --python2_path /usr/bin/python2.7
 build:centos --spawn_strategy=standalone
 build:centos --workspace_status_command scripts/release/status.sh
 
@@ -46,7 +45,6 @@
 build:debian --linkopt -lm
 build:debian --linkopt -lpthread
 build:debian --linkopt -lrt
-build:debian --python2_path /usr/bin/python2.7
 build:debian --spawn_strategy=standalone
 build:debian --workspace_status_command scripts/release/status.sh
 
@@ -57,7 +55,6 @@
 build:darwin --experimental_action_listener=tools/python:compile_python
 build:darwin --genrule_strategy=standalone
 build:darwin --ignore_unsupported_sandboxing
-build:darwin --python2_path /usr/bin/python2.7
 build:darwin --spawn_strategy=standalone
 build:darwin --workspace_status_command scripts/release/status.sh
 
@@ -71,7 +68,6 @@
 build:ubuntu --linkopt -lm
 build:ubuntu --linkopt -lpthread
 build:ubuntu --linkopt -lrt
-build:ubuntu --python2_path /usr/bin/python2.7
 build:ubuntu --spawn_strategy=standalone
 build:ubuntu --workspace_status_command scripts/release/status.sh
 
@@ -85,7 +81,6 @@
 build:centos_nostyle --linkopt -lm
 build:centos_nostyle --linkopt -lpthread
 build:centos_nostyle --linkopt -lrt
-build:centos_nostyle --python2_path /usr/bin/python2.7
 build:centos_nostyle --spawn_strategy=standalone
 build:centos_nostyle --workspace_status_command scripts/release/status.sh
 
@@ -96,7 +91,6 @@
 build:debian_nostyle --linkopt -lm
 build:debian_nostyle --linkopt -lpthread
 build:debian_nostyle --linkopt -lrt
-build:debian_nostyle --python2_path /usr/bin/python2.7
 build:debian_nostyle --spawn_strategy=standalone
 build:debian_nostyle --workspace_status_command scripts/release/status.sh
 
@@ -104,7 +98,6 @@
 # To use it: bazel build --config=darwin_nostyle
 build:darwin_nostyle --genrule_strategy=standalone
 build:darwin_nostyle --ignore_unsupported_sandboxing
-build:darwin_nostyle --python2_path /usr/bin/python2.7
 build:darwin_nostyle --spawn_strategy=standalone
 build:darwin_nostyle --workspace_status_command scripts/release/status.sh
 
@@ -115,7 +108,5 @@
 build:ubuntu_nostyle --linkopt -lm
 build:ubuntu_nostyle --linkopt -lpthread
 build:ubuntu_nostyle --linkopt -lrt
-build:ubuntu_nostyle --python2_path /usr/bin/python2.7
 build:ubuntu_nostyle --spawn_strategy=standalone
 build:ubuntu_nostyle --workspace_status_command scripts/release/status.sh
-
diff --git a/tools/rules/pex/pex_rules.bzl b/tools/rules/pex/pex_rules.bzl
index fa9365e..d22f7a5 100644
--- a/tools/rules/pex/pex_rules.bzl
+++ b/tools/rules/pex/pex_rules.bzl
@@ -101,7 +101,7 @@
 
   pex_files = []
 
-  for f in runfiles.files:
+  for f in runfiles.files.to_list():
     dpath = f.short_path
     if dpath.startswith("../"):
       dpath = dpath[3:]
@@ -127,8 +127,8 @@
 
   return struct(
       modules = pex_files,
-      requirements = list(py.transitive_reqs),
-      prebuiltLibraries = [f.path for f in py.transitive_eggs],
+      requirements = py.transitive_reqs.to_list(),
+      prebuiltLibraries = [f.path for f in py.transitive_eggs.to_list()],
       resources = res_files,
   )
 
@@ -150,8 +150,7 @@
     main_pkg = main_file.short_path.replace('/', '.')[:-3]
     transitive_files += [main_file]
 
-  deploy_pex = ctx.new_file(
-      ctx.configuration.bin_dir, ctx.outputs.executable, '.pex')
+  deploy_pex = ctx.actions.declare_file('%s.pex' % ctx.attr.name)
 
   py = _collect_transitive(ctx)
 
@@ -164,12 +163,11 @@
   )
 
   resources = ctx.files.resources
-  manifest_file = ctx.new_file(
-      ctx.configuration.bin_dir, deploy_pex, '_manifest')
+  manifest_file = ctx.actions.declare_file('%s.pex_manifest' % ctx.attr.name)
 
   manifest = _gen_manifest(py, runfiles, resources)
 
-  ctx.file_action(
+  ctx.actions.write(
       output = manifest_file,
       content = manifest.to_json(),
   )
@@ -183,7 +181,7 @@
     arguments += ["--python", ctx.attr.interpreter]
   for platform in ctx.attr.platforms:
     arguments += ["--platform", platform]
-  for egg in py.transitive_eggs:
+  for egg in py.transitive_eggs.to_list():
     arguments += ["--find-links", egg.dirname]
   arguments += [
       "--pex-root", ".pex",  # May be redundant since we also set PEX_ROOT
@@ -197,12 +195,12 @@
   # form the inputs to pex builder
   _inputs = (
       [manifest_file] +
-      list(runfiles.files) +
-      list(py.transitive_eggs) +
+      runfiles.files.to_list() +
+      py.transitive_eggs.to_list() +
       list(resources)
   )
 
-  ctx.action(
+  ctx.actions.run(
       mnemonic = "PexPython",
       inputs = _inputs,
       outputs = [deploy_pex],
@@ -227,7 +225,7 @@
   # There isn't much point in having both foo.pex and foo as identical pex
   # files, but someone is probably relying on that behaviour by now so we might
   # as well keep doing it.
-  ctx.action(
+  ctx.actions.run_shell(
       mnemonic = "LinkPex",
       inputs = [deploy_pex],
       outputs = [executable],
@@ -256,14 +254,14 @@
   output_file = ctx.outputs.executable
 
   test_file_paths = ["${RUNFILES}/" + _get_runfile_path(ctx, f) for f in ctx.files.srcs]
-  ctx.template_action(
+  ctx.actions.expand_template(
       template = ctx.file.launcher_template,
       output = output_file,
       substitutions = {
           "%test_runner%": _get_runfile_path(ctx, test_runner),
           "%test_files%": " \\\n    ".join(test_file_paths),
       },
-      executable = True,
+      is_executable = True,
   )
 
   transitive_files = depset(ctx.files.srcs + [test_runner])
@@ -307,8 +305,7 @@
 
 
 pex_bin_attrs = _dmerge(pex_attrs, {
-    "main": attr.label(allow_files = True,
-                       single_file = True),
+    "main": attr.label(allow_single_file = True),
     "entrypoint": attr.string(),
     "interpreter": attr.string(),
     "platforms": attr.string_list(),
@@ -396,8 +393,7 @@
             cfg = "target",
         ),
         "launcher_template": attr.label(
-            allow_files = True,
-            single_file = True,
+            allow_single_file = True,
             default = Label("//tools/rules/pex:testlauncher.sh.template"),
         ),
     }),