• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 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"""Install and check status of picotool."""
15
16from contextlib import contextmanager
17import logging
18import os
19import pathlib
20from pathlib import Path
21import shutil
22import subprocess
23from typing import Sequence
24
25import pw_package.git_repo
26import pw_package.package_manager
27
28_LOG = logging.getLogger(__package__)
29
30
31@contextmanager
32def change_working_dir(directory: Path):
33    original_dir = Path.cwd()
34    try:
35        os.chdir(directory)
36        yield directory
37    finally:
38        os.chdir(original_dir)
39
40
41class Picotool(pw_package.package_manager.Package):
42    """Install and check status of picotool."""
43
44    def __init__(self, *args, **kwargs):
45        super().__init__(*args, name='picotool', **kwargs)
46
47        self._pico_tool_repo = pw_package.git_repo.GitRepo(
48            name='picotool',
49            url=(
50                'https://pigweed.googlesource.com/third_party/'
51                'github/raspberrypi/picotool.git'
52            ),
53            commit='f6fe6b7c321a2def8950d2a440335dfba19e2eab',
54        )
55
56    def install(self, path: Path) -> None:
57        self._pico_tool_repo.install(path)
58
59        env = os.environ.copy()
60        env['PICO_SDK_PATH'] = str(path.parent.absolute() / 'pico_sdk')
61        bootstrap_env_path = Path(env.get('_PW_ACTUAL_ENVIRONMENT_ROOT', ''))
62
63        commands = (
64            ('cmake', '-S', './', '-B', 'out/', '-G', 'Ninja'),
65            ('ninja', '-C', 'out'),
66        )
67
68        with change_working_dir(path) as _picotool_repo:
69            for command in commands:
70                _LOG.info('==> %s', ' '.join(command))
71                subprocess.run(
72                    command,
73                    env=env,
74                    check=True,
75                )
76
77        picotool_bin = path / 'out' / 'picotool'
78        _LOG.info('Done! picotool binary located at:')
79        _LOG.info(picotool_bin)
80
81        if bootstrap_env_path.is_dir() and picotool_bin.is_file():
82            bin_path = (
83                bootstrap_env_path / 'cipd' / 'packages' / 'pigweed' / 'bin'
84            )
85            destination_path = bin_path / picotool_bin.name
86            _LOG.info('Copy %s -> %s', picotool_bin, destination_path)
87            shutil.copy(picotool_bin, destination_path)
88
89    def info(self, path: pathlib.Path) -> Sequence[str]:
90        return (f'{self.name} installed in: {path}',)
91
92
93pw_package.package_manager.register(Picotool)
94