Marti Bolivar | 9e3edbb | 2018-09-25 15:08:16 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Marti Bolivar | b6af8eb | 2018-05-09 11:08:38 -0400 | [diff] [blame] | 2 | |
Marti Bolivar | 9e3edbb | 2018-09-25 15:08:16 -0600 | [diff] [blame] | 3 | # Zephyr launcher which is interoperable with: |
| 4 | # |
| 5 | # 1. "mono-repo" Zephyr installations that have 'make flash' |
| 6 | # etc. supplied by a copy of some west code in scripts/meta. |
| 7 | # |
| 8 | # 2. "multi-repo" Zephyr installations where west is provided in a |
| 9 | # separate Git repository elsewhere. |
| 10 | # |
| 11 | # This is basically a copy of the "wrapper" functionality in the west |
| 12 | # bootstrap script for the multi-repo case, plus a fallback onto the |
| 13 | # copy in scripts/meta/west for mono-repo installs. |
| 14 | |
| 15 | import os |
| 16 | import subprocess |
| 17 | import sys |
| 18 | |
Marti Bolivar | 18e6c10 | 2018-11-16 08:49:43 -0700 | [diff] [blame] | 19 | import colorama |
| 20 | |
Marti Bolivar | 9e3edbb | 2018-09-25 15:08:16 -0600 | [diff] [blame] | 21 | if sys.version_info < (3,): |
| 22 | sys.exit('fatal error: you are running Python 2') |
| 23 | |
| 24 | # Top-level west directory, containing west itself and the manifest. |
| 25 | WEST_DIR = 'west' |
| 26 | # Subdirectory to check out the west source repository into. |
| 27 | WEST = 'west' |
| 28 | # File inside of WEST_DIR which marks it as the top level of the |
| 29 | # Zephyr project installation. |
| 30 | # |
| 31 | # (The WEST_DIR name is not distinct enough to use when searching for |
| 32 | # the top level; other directories named "west" may exist elsewhere, |
| 33 | # e.g. zephyr/doc/west.) |
| 34 | WEST_MARKER = '.west_topdir' |
| 35 | |
| 36 | |
| 37 | class WestError(RuntimeError): |
| 38 | pass |
| 39 | |
| 40 | |
| 41 | class WestNotFound(WestError): |
| 42 | '''Neither the current directory nor any parent has a West installation.''' |
| 43 | |
| 44 | |
| 45 | def find_west_topdir(start): |
| 46 | '''Find the top-level installation directory, starting at ``start``. |
| 47 | |
| 48 | If none is found, raises WestNotFound.''' |
| 49 | cur_dir = start |
| 50 | |
| 51 | while True: |
| 52 | if os.path.isfile(os.path.join(cur_dir, WEST_DIR, WEST_MARKER)): |
| 53 | return cur_dir |
| 54 | |
| 55 | parent_dir = os.path.dirname(cur_dir) |
| 56 | if cur_dir == parent_dir: |
| 57 | # At the root |
| 58 | raise WestNotFound('Could not find a West installation ' |
| 59 | 'in this or any parent directory') |
| 60 | cur_dir = parent_dir |
| 61 | |
| 62 | |
| 63 | def append_to_pythonpath(directory): |
| 64 | pp = os.environ.get('PYTHONPATH') |
| 65 | os.environ['PYTHONPATH'] = ':'.join(([pp] if pp else []) + [directory]) |
| 66 | |
| 67 | |
| 68 | def wrap(topdir, argv): |
| 69 | # Replace the wrapper process with the "real" west |
| 70 | |
| 71 | # sys.argv[1:] strips the argv[0] of the wrapper script itself |
| 72 | west_git_repo = os.path.join(topdir, WEST_DIR, WEST) |
| 73 | argv = ([sys.executable, |
| 74 | os.path.join(west_git_repo, 'src', 'west', 'main.py')] + |
| 75 | argv) |
| 76 | |
| 77 | try: |
| 78 | append_to_pythonpath(os.path.join(west_git_repo, 'src')) |
| 79 | subprocess.check_call(argv) |
| 80 | except subprocess.CalledProcessError as e: |
| 81 | sys.exit(e.returncode) |
| 82 | |
| 83 | |
| 84 | def run_scripts_meta_west(): |
| 85 | try: |
| 86 | subprocess.check_call([sys.executable, |
| 87 | os.path.join(os.environ['ZEPHYR_BASE'], |
| 88 | 'scripts', 'meta', 'west', |
| 89 | 'main.py')] + sys.argv[1:]) |
| 90 | except subprocess.CalledProcessError as e: |
| 91 | sys.exit(e.returncode) |
| 92 | |
| 93 | |
| 94 | def main(): |
| 95 | try: |
| 96 | topdir = find_west_topdir(__file__) |
| 97 | except WestNotFound: |
| 98 | topdir = None |
| 99 | |
Marti Bolivar | 18e6c10 | 2018-11-16 08:49:43 -0700 | [diff] [blame] | 100 | try: |
| 101 | if topdir is not None: |
| 102 | wrap(topdir, sys.argv[1:]) |
| 103 | else: |
| 104 | run_scripts_meta_west() |
| 105 | finally: |
| 106 | print(colorama.Fore.LIGHTRED_EX, end='') |
Marti Bolivar | c41703a | 2018-11-20 09:18:44 -0500 | [diff] [blame] | 107 | print('NOTE: you just ran a copy of west from {};'. |
| 108 | format(os.path.dirname(__file__)), |
| 109 | 'this will be removed from the Zephyr repository in the future.', |
| 110 | 'West is now developed separately.') |
Marti Bolivar | 18e6c10 | 2018-11-16 08:49:43 -0700 | [diff] [blame] | 111 | print(colorama.Style.RESET_ALL, end='', flush=True) |
Marti Bolivar | 9e3edbb | 2018-09-25 15:08:16 -0600 | [diff] [blame] | 112 | |
| 113 | |
| 114 | if __name__ == '__main__': |
| 115 | main() |