-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pw_build: Run all gn python_actions in a venv
This CL introduces a new gn arg that switches from the old behavior of installing every pw_* Python package to installing a single 'pigweed' Python package. This matches the 'pigweed' package available at https://pypi.org/project/pigweed/ but with a higher version so pip will always treat the version created in-tree as more recent. Additionally all python_actions are forced to be run within an isolated Python virtualenv created in the build_dir. This has a few benefits: 1. Greatly speeds up the build process as Python packages do not need to be pip installed before use. 2. Enforces Python dependency correctness. If a python_deps entry is missing in gn the build will fail. At this time it is disabled by default. Set this arg in the .gn file: pw_build_USE_NEW_PYTHON_BUILD=true Change-Id: I65b584727c66c1e7b2371ad1f8c57cd21d7df390 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/90060 Reviewed-by: Armando Montanez <[email protected]> Reviewed-by: Wyatt Hepler <[email protected]> Commit-Queue: Anthony DiGirolamo <[email protected]>
- Loading branch information
1 parent
4ed7050
commit dec2b24
Showing
26 changed files
with
821 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright 2022 The Pigweed Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
"""Pip install Pigweed Python packages.""" | ||
|
||
import argparse | ||
from pathlib import Path | ||
import subprocess | ||
import sys | ||
from typing import List, Tuple | ||
|
||
try: | ||
from pw_build.python_package import load_packages | ||
except ImportError: | ||
# Load from python_package from this directory if pw_build is not available. | ||
from python_package import load_packages # type: ignore | ||
|
||
|
||
def _parse_args() -> Tuple[argparse.Namespace, List[str]]: | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument( | ||
'--python-dep-list-files', | ||
type=Path, | ||
required=True, | ||
help= | ||
'Path to a text file containing the list of Python package metadata ' | ||
'json files.', | ||
) | ||
parser.add_argument('--gn-packages', | ||
required=True, | ||
help=('Comma separated list of GN python package ' | ||
'targets to install.')) | ||
parser.add_argument('--editable-pip-install', | ||
action='store_true', | ||
help=('If true run the pip install command with the ' | ||
'\'--editable\' option.')) | ||
return parser.parse_known_args() | ||
|
||
|
||
class NoMatchingGnPythonDependency(Exception): | ||
"""An error occurred while processing a Python dependency.""" | ||
|
||
|
||
def main(python_dep_list_files: Path, editable_pip_install: bool, | ||
gn_targets: List[str], pip_args: List[str]) -> int: | ||
"""Find matching python packages to pip install.""" | ||
pip_target_dirs: List[str] = [] | ||
|
||
py_packages = load_packages([python_dep_list_files], ignore_missing=True) | ||
for pkg in py_packages: | ||
valid_target = [target in pkg.gn_target_name for target in gn_targets] | ||
if not any(valid_target): | ||
continue | ||
top_level_source_dir = pkg.package_dir | ||
pip_target_dirs.append(str(top_level_source_dir.parent.resolve())) | ||
|
||
if not pip_target_dirs: | ||
raise NoMatchingGnPythonDependency( | ||
'No matching GN Python dependency found to install.\n' | ||
'GN Targets to pip install:\n' + '\n'.join(gn_targets) + '\n\n' | ||
'Declared Python Dependencies:\n' + | ||
'\n'.join(pkg.gn_target_name for pkg in py_packages) + '\n\n') | ||
|
||
for target in pip_target_dirs: | ||
command_args = [sys.executable, "-m", "pip"] | ||
command_args += pip_args | ||
if editable_pip_install: | ||
command_args.append('--editable') | ||
command_args.append(target) | ||
|
||
process = subprocess.run(command_args, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT) | ||
pip_output = process.stdout.decode() | ||
if process.returncode != 0: | ||
print(pip_output) | ||
return process.returncode | ||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
# Parse this script's args and pass any remaining args to pip. | ||
argparse_args, remaining_args_for_pip = _parse_args() | ||
|
||
# Split the comma separated string and remove leading slashes. | ||
gn_target_names = [ | ||
target.lstrip('/') for target in argparse_args.gn_packages.split(',') | ||
if target # The last target may be an empty string. | ||
] | ||
|
||
result = main(python_dep_list_files=argparse_args.python_dep_list_files, | ||
editable_pip_install=argparse_args.editable_pip_install, | ||
gn_targets=gn_target_names, | ||
pip_args=remaining_args_for_pip) | ||
sys.exit(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.