|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +import sys |
| 7 | +import argparse |
| 8 | +from pathlib import Path |
| 9 | +from glob import glob |
| 10 | +from idf_component_tools.manifest import ManifestManager |
| 11 | + |
| 12 | + |
| 13 | +def override_with_local_component(component, local_path, app): |
| 14 | + app_path = Path(app) |
| 15 | + |
| 16 | + absolute_local_path = Path(local_path).absolute() |
| 17 | + if not absolute_local_path.exists(): |
| 18 | + print('[Error] {} path does not exist'.format(local_path)) |
| 19 | + raise Exception |
| 20 | + if not app_path.exists(): |
| 21 | + print('[Error] {} path does not exist'.format(app_path)) |
| 22 | + raise Exception |
| 23 | + |
| 24 | + print('[Info] Processing app {}'.format(app)) |
| 25 | + manager = ManifestManager(app_path / 'main', 'app') |
| 26 | + if '/' not in component: |
| 27 | + # Prepend with default namespace |
| 28 | + component_with_namespace = 'espressif/' + component |
| 29 | + |
| 30 | + try: |
| 31 | + manager.manifest_tree['dependencies'][component_with_namespace] = { |
| 32 | + 'version': '*', |
| 33 | + 'override_path': str(absolute_local_path) |
| 34 | + } |
| 35 | + except KeyError: |
| 36 | + print('[Error] {} app does not depend on {}'.format(app, component_with_namespace)) |
| 37 | + raise KeyError |
| 38 | + |
| 39 | + manager.dump() |
| 40 | + |
| 41 | + |
| 42 | +def override_with_local_component_all(component, local_path, apps): |
| 43 | + # Process wildcard, e.g. "app_prefix_*" |
| 44 | + apps_with_glob = list() |
| 45 | + for app in apps: |
| 46 | + apps_with_glob += glob(app) |
| 47 | + |
| 48 | + # Go through all collected apps |
| 49 | + for app in apps_with_glob: |
| 50 | + try: |
| 51 | + override_with_local_component(component, local_path, app) |
| 52 | + except: |
| 53 | + print("[Error] Could not process app {}".format(app)) |
| 54 | + return -1 |
| 55 | + return 0 |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + parser = argparse.ArgumentParser() |
| 60 | + parser.add_argument('component', help='Existing component that the app depends on') |
| 61 | + parser.add_argument('local_path', help='Path to component that will be used instead of the managed version') |
| 62 | + parser.add_argument('apps', nargs='*', help='List of apps to process') |
| 63 | + args = parser.parse_args() |
| 64 | + sys.exit(override_with_local_component_all(args.component, args.local_path, args.apps)) |
0 commit comments