Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_presubmit / py / pw_presubmit / environment.py
1 # Copyright 2020 The Pigweed Authors
2 #
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
5 # the License at
6 #
7 #     https://www.apache.org/licenses/LICENSE-2.0
8 #
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
13 # the License.
14 """Functions for initializing CIPD and the Pigweed virtualenv."""
15
16 import logging
17 import os
18 from pathlib import Path
19 import sys
20 from typing import Iterable, Union
21
22 from pw_presubmit import call
23
24 _LOG = logging.getLogger(__name__)
25
26
27 def init_cipd(
28     pigweed_root: Path,
29     output_directory: Path,
30     package_files: Iterable[Path] = ()) -> None:
31     """Runs CIPD."""
32
33     # TODO(mohrr): invoke by importing rather than by subprocess.
34
35     cmd = [
36         sys.executable,
37         pigweed_root.joinpath('pw_env_setup', 'py', 'pw_env_setup',
38                               'cipd_setup', 'update.py'),
39         '--install-dir', output_directory,
40     ]  # yapf: disable
41
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)
46
47     for package_file in final_package_files:
48         cmd.extend(('--package-file', package_file))
49
50     call(*cmd)
51
52     paths = [output_directory, output_directory.joinpath('bin')]
53     for base in output_directory.glob('*'):
54         paths.append(base)
55         paths.append(base.joinpath('bin'))
56
57     paths.append(Path(os.environ['PATH']))
58
59     os.environ['PATH'] = os.pathsep.join(str(x) for x in paths)
60     _LOG.debug('PATH %s', os.environ['PATH'])
61
62
63 def init_virtualenv(
64         pigweed_root: Path,
65         output_directory: Path,
66         requirements: Iterable[Union[Path, str]] = (),
67         gn_targets: Iterable[str] = (),
68 ) -> None:
69     """Sets up a virtualenv, assumes recent Python 3 is already installed."""
70     virtualenv_source = pigweed_root.joinpath('pw_env_setup', 'py',
71                                               'pw_env_setup',
72                                               'virtualenv_setup')
73
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')),
79         os.environ['PATH'],
80     ))
81
82     if not gn_targets:
83         gn_targets = (f'{os.environ["PW_ROOT"]}#:python.install', )
84
85     call(
86         'python3',
87         virtualenv_source,
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),
92     )