From 351c6c3dd5c929b80756145312c09cd40d395dd0 Mon Sep 17 00:00:00 2001 From: Rich Kadel Date: Fri, 16 Jul 2021 10:03:25 -0700 Subject: [PATCH] Adds python3_action GN target (flutter/engine#27211) Ninja runs `action` scripts using the default python interpretter, and most flutter build scripts that use python currently require python2. In order to support python3 scripts as well, the `python3_action` invokes a wrapper python2-based script via GN `action` target, and the wrapper invokes the python3 interpretter with the given script. This new target is required to support flutter/engine#26880, but is broken out to a separate PR (by request) to support other more immediate use cases. --- .../flutter/tools/python/python3_action.gni | 60 +++++++++++++++++++ .../tools/python/run_python3_action.py | 10 ++++ 2 files changed, 70 insertions(+) create mode 100644 engine/src/flutter/tools/python/python3_action.gni create mode 100644 engine/src/flutter/tools/python/run_python3_action.py diff --git a/engine/src/flutter/tools/python/python3_action.gni b/engine/src/flutter/tools/python/python3_action.gni new file mode 100644 index 0000000000..4d004dd45c --- /dev/null +++ b/engine/src/flutter/tools/python/python3_action.gni @@ -0,0 +1,60 @@ +# Copyright 2021 The Flutter Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# A ninja action script that calls the given python script with `python3` +# instead of the default `python` (which might be `python2`). +# +# Example +# +# ``` +# python3_action("run_this_guy_once") { +# script = "doprocessing.py" +# sources = [ "my_configuration.txt" ] +# outputs = [ "$target_gen_dir/insightful_output.txt" ] +# +# # Our script imports this Python file so we want to rebuild if it +# # changes. +# source_prereqs = [ "helper_library.py" ] +# +# # Note that we have to manually pass the sources to our script if +# # the script needs them as inputs. +# args = [ "--out", to_build_path(target_gen_dir) ] + sources +# } +# ``` +# +# Parameters +# args +# data +# datadeps +# depfile +# deps +# inputs +# metadata +# outputs (required) +# script (required) +# source_prereqs +# sources +template("python3_action") { + assert(defined(invoker.outputs), "outputs is required") + assert(defined(invoker.script), "script is required") + + action(target_name) { + forward_variables_from(invoker, + [ + "data", + "datadeps", + "depfile", + "deps", + "inputs", + "metadata", + "outputs", + "source_prereqs", + "sources", + ]) + + script = "//flutter/tools/python/run_python3_action.py" + + args = [ rebase_path(invoker.script) ] + invoker.args + } +} diff --git a/engine/src/flutter/tools/python/run_python3_action.py b/engine/src/flutter/tools/python/run_python3_action.py new file mode 100644 index 0000000000..11ba1d8381 --- /dev/null +++ b/engine/src/flutter/tools/python/run_python3_action.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +# +# Copyright 2021 The Flutter Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import os +import sys + +os.execv('/usr/bin/python3', ['python3'] + sys.argv[1:])