-
Notifications
You must be signed in to change notification settings - Fork 118
/
main.py
198 lines (154 loc) · 5.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Copyright 2018 Open Source Foundries Limited.
#
# SPDX-License-Identifier: Apache-2.0
'''West's bootstrap/wrapper script.
'''
import argparse
import os
import importlib
import platform
import subprocess
import sys
if sys.version_info < (3,):
sys.exit('fatal error: you are running Python 2')
MANIFEST = 'manifest'
MANIFEST_DEFAULT = 'https://github.com/zephyrproject-rtos/manifest'
MANIFEST_REV_DEFAULT = 'master'
WEST = 'west'
WEST_DEFAULT = 'https://github.com/zephyrproject-rtos/west'
WEST_REV_DEFAULT = 'master'
WEST_DIR = 'west'
#
# Helpers shared between init and wrapper mode
#
class WestError(RuntimeError):
pass
class WestNotFound(WestError):
'''Neither the current directory nor any parent has a West installation.'''
def find_west_topdir(start):
'''Find the top-level installation directory, starting at ``start``.
If none is found, raises WestNotFound.'''
if not os.path.isdir(start):
raise WestNotFound()
if WEST_DIR in os.listdir(start):
return start
dirname = os.path.dirname(start)
if start == dirname:
# / on POSIX, top level drive name on Windows.
raise WestNotFound()
else:
return find_west_topdir(dirname)
def clone(url, rev, dest):
def repository_type(url):
if url.startswith(('http:', 'https:', 'git:', 'git+ssh:', 'file:')):
return 'GIT'
else:
return 'UNKNOWN'
if os.path.exists(dest):
msg = 'refusing to clone into existing location {}'.format(dest)
raise WestError(msg)
if repository_type(url) == 'GIT':
subprocess.check_call(['git', 'clone', url, '-b', rev, dest])
else:
raise WestError('Unknown URL scheme for repository: {}'.format(url))
#
# west init
#
def init(argv):
'''Command line handler for ``west init`` invocations.
This exits the program with a nonzero exit code if fatal errors occur.'''
init_parser = argparse.ArgumentParser(
prog='west init',
description='Bootstrap initialize a Zephyr installation')
init_parser.add_argument(
'-b', '--base-url',
help='''Base URL for both 'manifest' and 'zephyr' repositories; cannot
be given if either -u or -w are''')
init_parser.add_argument(
'-u', '--manifest-url',
help='Zephyr manifest fetch URL, default ' + MANIFEST_DEFAULT)
init_parser.add_argument(
'--mr', '--manifest-rev', default=MANIFEST_REV_DEFAULT,
dest='manifest_rev',
help='Manifest revision to fetch, default ' + MANIFEST_REV_DEFAULT)
init_parser.add_argument(
'-w', '--west-url',
help='West fetch URL, default ' + WEST_DEFAULT)
init_parser.add_argument(
'--wr', '--west-rev', default=WEST_REV_DEFAULT, dest='west_rev',
help='West revision to fetch, default ' + WEST_REV_DEFAULT)
init_parser.add_argument(
'directory', nargs='?', default=None,
help='Initializes in this directory, creating it if necessary')
args = init_parser.parse_args(args=argv)
directory = args.directory or os.getcwd()
if args.base_url:
if args.manifest_url or args.west_url:
sys.exit('fatal error: -b is incompatible with -u and -w')
args.manifest_url = args.base_url.rstrip('/') + '/manifest'
args.west_url = args.base_url.rstrip('/') + '/west'
else:
if not args.manifest_url:
args.manifest_url = MANIFEST_DEFAULT
if not args.west_url:
args.west_url = WEST_DEFAULT
try:
topdir = find_west_topdir(directory)
init_reinit(topdir, args)
except WestNotFound:
init_bootstrap(directory, args)
def init_bootstrap(directory, args):
'''Bootstrap a new manifest + West installation in the given directory.'''
if not os.path.isdir(directory):
try:
print('Initializing in new directory', directory)
os.makedirs(directory, exist_ok=False)
except PermissionError:
sys.exit('Cannot initialize in {}: permission denied'.format(
directory))
except FileExistsError:
sys.exit('Something else created {} concurrently; quitting'.format(
directory))
except Exception as e:
sys.exit("Can't create directory {}: {}".format(
directory, e.args))
else:
print('Initializing in', directory)
# Clone the west source code and the manifest into west/. Git will create
# the west/ directory if it does not exist.
clone(args.west_url, args.west_rev,
os.path.join(directory, WEST_DIR, WEST))
clone(args.manifest_url, args.manifest_rev,
os.path.join(directory, WEST_DIR, MANIFEST))
def init_reinit(directory, args):
# TODO
sys.exit('Re-initializing an existing installation is not yet supported.')
#
# Wrap a West command
#
def wrap(argv):
start = os.getcwd()
try:
topdir = find_west_topdir(start)
except WestNotFound:
sys.exit('Error: not a Zephyr directory (or any parent): {}\n'
'Use "west init" to install Zephyr here'.format(start))
# Put the top-level west source directory at the highest priority
# except for the script directory / current working directory.
sys.path.insert(1, os.path.join(topdir, WEST, 'src'))
main_module = importlib.import_module('west.main')
main_module.main(argv=argv)
#
# Main entry point
#
def main(wrap_argv=None):
'''Entry point to the wrapper script.'''
if wrap_argv is None:
wrap_argv = sys.argv[1:]
if not wrap_argv or wrap_argv[0] != 'init':
wrap(wrap_argv)
else:
init(wrap_argv[1:])
sys.exit(0)
if __name__ == '__main__':
main()