diff --git a/build/fbcode_builder/docker_builder.py b/build/fbcode_builder/docker_builder.py index 87381558c8c..aa251f8a468 100644 --- a/build/fbcode_builder/docker_builder.py +++ b/build/fbcode_builder/docker_builder.py @@ -23,7 +23,7 @@ from fbcode_builder import FBCodeBuilder from shell_quoting import ( - raw_shell, shell_comment, shell_join, ShellQuoted + raw_shell, shell_comment, shell_join, ShellQuoted, path_join ) from utils import recursively_flatten_list, run_command @@ -47,7 +47,19 @@ def setup(self): ShellQuoted('FROM {}'.format(self.option('os_image'))), # /bin/sh syntax is a pain ShellQuoted('SHELL ["/bin/bash", "-c"]'), - ] + self.install_debian_deps() + [self._change_user()]) + ] + self.install_debian_deps() + [self._change_user()] + + [self.workdir(self.option('prefix')), + self.create_python_venv(), + self.python_venv()]) + + def python_venv(self): + # To both avoid calling venv activate on each RUN command AND to ensure + # it is present when the resulting container is run add to PATH + actions = [] + if self.option("PYTHON_VENV", "OFF") == "ON": + actions = ShellQuoted('ENV PATH={p}:$PATH').format( + p=path_join(self.option('prefix'), "venv", "bin")) + return(actions) def step(self, name, actions): assert '\n' not in name, 'Name {0} would span > 1 line'.format(name) diff --git a/build/fbcode_builder/fbcode_builder.py b/build/fbcode_builder/fbcode_builder.py index a6c055246c8..75b06316725 100644 --- a/build/fbcode_builder/fbcode_builder.py +++ b/build/fbcode_builder/fbcode_builder.py @@ -210,6 +210,7 @@ def debian_deps(self): 'sudo', 'unzip', 'wget', + 'python3-venv', ] # @@ -246,6 +247,20 @@ def install_debian_deps(self): return self.step('Install packages for Debian-based OS', actions) + def create_python_venv(self): + action = [] + if self.option("PYTHON_VENV", "OFF") == "ON": + action = self.run(ShellQuoted("python3 -m venv {p}").format( + p=path_join(self.option('prefix'), "venv"))) + return(action) + + def python_venv(self): + action = [] + if self.option("PYTHON_VENV", "OFF") == "ON": + action = ShellQuoted("source {p}").format( + p=path_join(self.option('prefix'), "venv", "bin", "activate")) + return(action) + def debian_ccache_setup_steps(self): return [] # It's ok to ship a renderer without ccache support. diff --git a/build/fbcode_builder/shell_builder.py b/build/fbcode_builder/shell_builder.py index 52964b430dd..5bb41fe57e1 100644 --- a/build/fbcode_builder/shell_builder.py +++ b/build/fbcode_builder/shell_builder.py @@ -51,7 +51,7 @@ def step(self, name, actions): def setup(self): steps = [ ShellQuoted('set -exo pipefail'), - ] + ] + [self.create_python_venv(), self.python_venv()] if self.has_option('ccache_dir'): ccache_dir = self.option('ccache_dir') steps += [