blob: b01eaa7687a205bb9dc7367211bbd2b092f4456b [file] [log] [blame]
Marti Bolivar9e3edbb2018-09-25 15:08:16 -06001#!/usr/bin/env python3
Marti Bolivarb6af8eb2018-05-09 11:08:38 -04002
Marti Bolivar9e3edbb2018-09-25 15:08:16 -06003# 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
15import os
16import subprocess
17import sys
18
Marti Bolivar18e6c102018-11-16 08:49:43 -070019import colorama
20
Marti Bolivar9e3edbb2018-09-25 15:08:16 -060021if 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.
25WEST_DIR = 'west'
26# Subdirectory to check out the west source repository into.
27WEST = '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.)
34WEST_MARKER = '.west_topdir'
35
36
37class WestError(RuntimeError):
38 pass
39
40
41class WestNotFound(WestError):
42 '''Neither the current directory nor any parent has a West installation.'''
43
44
45def 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
63def append_to_pythonpath(directory):
64 pp = os.environ.get('PYTHONPATH')
65 os.environ['PYTHONPATH'] = ':'.join(([pp] if pp else []) + [directory])
66
67
68def 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
84def 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
94def main():
95 try:
96 topdir = find_west_topdir(__file__)
97 except WestNotFound:
98 topdir = None
99
Marti Bolivar18e6c102018-11-16 08:49:43 -0700100 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 Bolivarc41703a2018-11-20 09:18:44 -0500107 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 Bolivar18e6c102018-11-16 08:49:43 -0700111 print(colorama.Style.RESET_ALL, end='', flush=True)
Marti Bolivar9e3edbb2018-09-25 15:08:16 -0600112
113
114if __name__ == '__main__':
115 main()