Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .plzconfig
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ Help = A path or build label for the pex tool, which is used to create .pex file

[PluginConfig "interpreter_options"]
ConfigKey = InterpreterOptions
DefaultValue = ""
Help = Any additional flags to pass to the python interpreter
Optional = true
Repeatable = true
Help = Additional command line arguments to pass to the Python interpreter.

[PluginConfig "test_runner"]
ConfigKey = TestRunner
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ The available configuration options are:

```ini
[Plugin "python"]
InterpreterOptions = -b -s
InterpreterOptions = -b
InterpreterOptions = -s
DefaultInterpreter = python3
PexTool = //tools/please_pex
TestRunner = unittest
Expand Down
12 changes: 9 additions & 3 deletions build_defs/python.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ def python_binary(name:str, main:str, srcs:list=[], resources:list=[], out:str=N

shebang = shebang or _interpreter_cmd(interpreter)
zipsafe_flag = '' if zip_safe is False else '--zip_safe'
cmd = '$TOOLS_PEX -s "%s" -m "%s" %s --interpreter_options="%s"' % (
shebang, module_dir, zipsafe_flag, CONFIG.PYTHON.INTERPRETER_OPTIONS)
interpreter_opts = _interpreter_opts(CONFIG.PYTHON.INTERPRETER_OPTIONS)
cmd = '$TOOLS_PEX -s "%s" -m "%s" %s %s' % (
shebang, module_dir, zipsafe_flag, interpreter_opts)

# If content hashing feature flag is enabled, we use the hash of the built
# dependencies instead of the RULE_HASH as the base of the pex extraction
Expand Down Expand Up @@ -270,7 +271,8 @@ def python_test(name:str, srcs:list, data:list|dict=[], resources:list=[], deps:
"""
test_runner = test_runner or CONFIG.PYTHON.TEST_RUNNER
shebang = shebang or _interpreter_cmd(interpreter)
cmd = f'$TOOLS_PEX -t -s "{shebang}" -m "{module_dir}" -r "{test_runner}" --zip_safe --add_test_runner_deps --interpreter_options="{CONFIG.PYTHON.INTERPRETER_OPTIONS}" --stamp="$RULE_HASH"'
interpreter_opts = _interpreter_opts(CONFIG.PYTHON.INTERPRETER_OPTIONS)
cmd = f'$TOOLS_PEX -t -s "{shebang}" -m "{module_dir}" -r "{test_runner}" --zip_safe --add_test_runner_deps {interpreter_opts} --stamp="$RULE_HASH"'
if site:
cmd += ' -S'

Expand Down Expand Up @@ -746,6 +748,10 @@ def _interpreter_cmd(interpreter:str):
return f'$(out {interpreter})' if looks_like_build_label(interpreter) else interpreter


def _interpreter_opts(opts:list):
return " ".join([f'--interpreter_options="{o}"' for o in opts])


def _patch_cmd(patch):
"""Returns a command for the given patch or patches, with OS-specific tweaks where needed."""
patches = [patch] if isinstance(patch, str) else patch
Expand Down
6 changes: 3 additions & 3 deletions tools/please_pex/pex/pex.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Writer struct {
}

// NewWriter constructs a new Writer.
func NewWriter(entryPoint, interpreter, options, stamp string, zipSafe, noSite bool) *Writer {
func NewWriter(entryPoint, interpreter string, options []string, stamp string, zipSafe, noSite bool) *Writer {
pw := &Writer{
zipSafe: zipSafe,
noSite: noSite,
Expand All @@ -59,8 +59,8 @@ func NewWriter(entryPoint, interpreter, options, stamp string, zipSafe, noSite b
}

// SetShebang sets the leading shebang that will be written to the file.
func (pw *Writer) SetShebang(shebang string, options string) {
shebang = strings.TrimSpace(fmt.Sprintf("%s %s", shebang, options))
func (pw *Writer) SetShebang(shebang string, options []string) {
shebang = strings.TrimSpace(fmt.Sprintf("%s %s", shebang, strings.Join(options, " ")))
if !path.IsAbs(shebang) {
shebang = "/usr/bin/env " + shebang
}
Expand Down
2 changes: 1 addition & 1 deletion tools/please_pex/pex_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var opts = struct {
TestRunner string `short:"r" long:"test_runner" default:"unittest" description:"Test runner to use"`
Shebang string `short:"s" long:"shebang" description:"Explicitly set shebang to this"`
Stamp string `long:"stamp" description:"Unique value used to derive cache directory for pex"`
InterpreterOptions string `long:"interpreter_options" description:"Options-string to pass to the python interpreter"`
InterpreterOptions []string `long:"interpreter_options" description:"Additional command line arguments to pass to the Python interpreter"`
Test bool `short:"t" long:"test" description:"True if we're to build a test"`
Debug pex.Debugger `short:"d" long:"debug" optional:"true" optional-value:"pdb" choice:"pdb" choice:"debugpy" description:"Debugger to generate a debugging pex"`
Site bool `short:"S" long:"site" description:"Allow the pex to import site at startup"`
Expand Down