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"""Install and check status of teensy-core.""" 15 16import json 17import logging 18import re 19import subprocess 20import tempfile 21from pathlib import Path 22from typing import Sequence 23 24from pw_arduino_build import core_installer 25 26import pw_package.package_manager 27 28_LOG: logging.Logger = logging.getLogger(__name__) 29 30 31class ArduinoCore(pw_package.package_manager.Package): 32 """Install and check status of arduino cores.""" 33 34 def __init__(self, core_name, *args, **kwargs): 35 super().__init__(*args, name=core_name, **kwargs) 36 37 def status(self, path: Path) -> bool: 38 return (path / 'hardware').is_dir() 39 40 def populate_download_cache_from_cipd(self, path: Path) -> None: 41 """Check for arduino core availability in pigweed_internal cipd.""" 42 package_path = path.parent.resolve() 43 core_name = self.name 44 core_cache_path = package_path / ".cache" / core_name 45 core_cache_path.mkdir(parents=True, exist_ok=True) 46 47 cipd_package_subpath = "pigweed_internal/third_party/" 48 cipd_package_subpath += core_name 49 cipd_package_subpath += "/${platform}" 50 51 # Check if teensy cipd package is readable 52 53 with tempfile.NamedTemporaryFile( 54 prefix='cipd', delete=True 55 ) as temp_json: 56 cipd_acl_check_command = [ 57 "cipd", 58 "acl-check", 59 cipd_package_subpath, 60 "-reader", 61 "-json-output", 62 temp_json.name, 63 ] 64 subprocess.run(cipd_acl_check_command, capture_output=True) 65 # Return if no packages are readable. 66 if not json.load(temp_json)['result']: 67 return 68 69 def _run_command(command): 70 _LOG.debug("Running: `%s`", " ".join(command)) 71 result = subprocess.run(command, capture_output=True) 72 _LOG.debug( 73 "Output:\n%s", result.stdout.decode() + result.stderr.decode() 74 ) 75 76 _run_command(["cipd", "init", "-force", core_cache_path.as_posix()]) 77 _run_command( 78 [ 79 "cipd", 80 "install", 81 cipd_package_subpath, 82 "-root", 83 core_cache_path.as_posix(), 84 "-force", 85 ] 86 ) 87 88 _LOG.debug( 89 "Available Cache Files:\n%s", 90 "\n".join([p.as_posix() for p in core_cache_path.glob("*")]), 91 ) 92 93 def install(self, path: Path) -> None: 94 self.populate_download_cache_from_cipd(path) 95 96 if self.status(path): 97 return 98 # Otherwise delete current version and reinstall 99 core_installer.install_core(path.parent.resolve().as_posix(), self.name) 100 101 def info(self, path: Path) -> Sequence[str]: 102 packages_root = path.parent.resolve() 103 arduino_package_path = path 104 arduino_package_name = None 105 106 message = [ 107 f'{self.name} currently installed in: {path}', 108 ] 109 # Make gn args sample copy/paste-able by omitting the starting timestamp 110 # and INF log on each line. 111 message_gn_args = [ 112 'Enable by running "gn args out" and adding these lines:', 113 f' pw_arduino_build_CORE_PATH = "{packages_root}"', 114 f' pw_arduino_build_CORE_NAME = "{self.name}"', 115 ] 116 117 # Search for first valid 'package/version' directory 118 for hardware_dir in [ 119 path for path in (path / 'hardware').iterdir() if path.is_dir() 120 ]: 121 if path.name in ["arduino", "tools"]: 122 continue 123 for subdir in [ 124 path for path in hardware_dir.iterdir() if path.is_dir() 125 ]: 126 if subdir.name == 'avr' or re.match(r'[0-9.]+', subdir.name): 127 arduino_package_name = f'{hardware_dir.name}/{subdir.name}' 128 break 129 130 if arduino_package_name: 131 message_gn_args += [ 132 f' pw_arduino_build_PACKAGE_NAME = "{arduino_package_name}"', 133 ' pw_arduino_build_BOARD = "BOARD_NAME"', 134 ] 135 message += ["\n".join(message_gn_args)] 136 message += [ 137 'Where BOARD_NAME is any supported board.', 138 # Have arduino_builder command appear on it's own line. 139 'List available boards by running:\n' 140 ' arduino_builder ' 141 f'--arduino-package-path {arduino_package_path} ' 142 f'--arduino-package-name {arduino_package_name} list-boards', 143 ] 144 return message 145 146 147for arduino_core_name in core_installer.supported_cores(): 148 pw_package.package_manager.register( 149 ArduinoCore, core_name=arduino_core_name 150 ) 151