1 # Copyright 2020 The Pigweed Authors
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 # use this file except in compliance with the License. You may obtain a copy of
7 # https://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations under
14 """Functions for initializing CIPD and the Pigweed virtualenv."""
18 from pathlib import Path
20 from typing import Iterable, Union
22 from pw_presubmit import call
24 _LOG = logging.getLogger(__name__)
29 output_directory: Path,
30 package_files: Iterable[Path] = ()) -> None:
33 # TODO(mohrr): invoke by importing rather than by subprocess.
37 pigweed_root.joinpath('pw_env_setup', 'py', 'pw_env_setup',
38 'cipd_setup', 'update.py'),
39 '--install-dir', output_directory,
42 final_package_files = list(
43 pigweed_root.joinpath('pw_env_setup', 'py', 'pw_env_setup',
44 'cipd_setup').glob('*.json'))
45 final_package_files.extend(package_files)
47 for package_file in final_package_files:
48 cmd.extend(('--package-file', package_file))
52 paths = [output_directory, output_directory.joinpath('bin')]
53 for base in output_directory.glob('*'):
55 paths.append(base.joinpath('bin'))
57 paths.append(Path(os.environ['PATH']))
59 os.environ['PATH'] = os.pathsep.join(str(x) for x in paths)
60 _LOG.debug('PATH %s', os.environ['PATH'])
65 output_directory: Path,
66 requirements: Iterable[Union[Path, str]] = (),
67 gn_targets: Iterable[str] = (),
69 """Sets up a virtualenv, assumes recent Python 3 is already installed."""
70 virtualenv_source = pigweed_root.joinpath('pw_env_setup', 'py',
74 # Need to set VIRTUAL_ENV before invoking GN because the GN targets install
75 # directly to the current virtual env.
76 os.environ['VIRTUAL_ENV'] = str(output_directory)
77 os.environ['PATH'] = os.pathsep.join((
78 str(output_directory.joinpath('bin')),
83 gn_targets = (f'{os.environ["PW_ROOT"]}#:python.install', )
88 f'--venv_path={output_directory}',
89 f'--requirements={virtualenv_source / "requirements.txt"}',
90 *(f'--requirements={x}' for x in requirements),
91 *(f'--gn-target={t}' for t in gn_targets),