18
18
import distutils .util
19
19
import subprocess
20
20
import logging
21
+ import collections
22
+ from argparse import ArgumentParser
21
23
22
24
logging .basicConfig ()
23
25
logger = logging .getLogger ("bootstrap" )
@@ -187,48 +189,50 @@ def find_executable(target):
187
189
return None , None
188
190
189
191
190
- if __name__ == "__main__" :
191
- home = os .path .dirname (os .path .abspath (__file__ ))
192
- LIBPATH = os .path .join (home , 'build' , _distutils_dir_name ('lib' ))
193
- cwd = os .getcwd ()
194
- os .chdir (home )
195
- build = subprocess .Popen ([sys .executable , "setup.py" , "build" ],
196
- shell = False , cwd = os .path .dirname (os .path .abspath (__file__ )))
197
- build_rc = build .wait ()
198
- if not os .path .exists (LIBPATH ):
199
- logger .warning ("`lib` directory does not exist, trying common Python3 lib" )
200
- LIBPATH = os .path .join (os .path .split (LIBPATH )[0 ], "lib" )
201
- os .chdir (cwd )
202
-
203
- if build_rc == 0 :
204
- logger .info ("Build process ended." )
205
- else :
206
- logger .error ("Build process ended with rc=%s" , build_rc )
207
- sys .exit (- 1 )
208
-
209
- if len (sys .argv ) < 2 :
210
- logger .warning ("usage: ./bootstrap.py <script>\n " )
211
- script = None
212
- else :
213
- script = sys .argv [1 ]
192
+ def main (argv ):
193
+ parser = ArgumentParser (prog = "bootstrap" , usage = "./bootstrap.py <script>" ,
194
+ description = __doc__ )
195
+ parser .add_argument ("script" , nargs = "*" )
196
+ parser .add_argument ("-m" , help = "run library module as a script (terminates option list)" )
214
197
215
- if script :
216
- logger .info ("Executing %s from source checkout" , script )
198
+ Options = collections .namedtuple ("Options" , ["script" , "module" ])
199
+ if len (argv ) == 1 :
200
+ options = Options (script = None , module = None )
217
201
else :
218
- logging .info ("Running iPython by default" )
219
- sys .path .insert (0 , LIBPATH )
220
- logger .info ("Patched sys.path with %s" , LIBPATH )
202
+ if argv [1 ] in ["-h" , "--help" ]:
203
+ parser .print_help ()
204
+ return
205
+ if argv [1 ] == "-m" :
206
+ if len (argv ) < 3 :
207
+ parser .parse_args (argv [1 :])
208
+ return
209
+ options = Options (script = None , module = argv [2 :])
210
+ else :
211
+ options = Options (script = argv [1 :], module = None )
221
212
222
- if script :
223
- argv = sys .argv [2 :]
213
+ if options .script is not None :
214
+ logger .info ("Executing %s from source checkout" , options .script )
215
+ script = options .script [0 ]
216
+ argv = options .script [1 :]
224
217
kind , target = find_executable (script )
225
218
if kind == "path" :
226
219
run_file (target , argv )
227
220
elif kind == "entry_point" :
228
221
run_entry_point (target , argv )
229
222
else :
230
- logger .error ("Script %s not found" , script )
223
+ logger .error ("Script %s not found" , options .script )
224
+ elif options .module is not None :
225
+ logging .info ("Running module %s" , options .module )
226
+ import runpy
227
+ module = options .module [0 ]
228
+ try :
229
+ old = sys .argv
230
+ sys .argv = [None ] + options .module [1 :]
231
+ runpy .run_module (module , run_name = "__main__" , alter_sys = True )
232
+ finally :
233
+ sys .argv = old
231
234
else :
235
+ logging .info ("Running IPython by default" )
232
236
logger .info ("Patch the sys.argv: %s" , sys .argv )
233
237
sys .path .insert (2 , "" )
234
238
try :
@@ -240,3 +244,29 @@ def find_executable(target):
240
244
code .interact ()
241
245
else :
242
246
embed ()
247
+
248
+
249
+ if __name__ == "__main__" :
250
+ home = os .path .dirname (os .path .abspath (__file__ ))
251
+ LIBPATH = os .path .join (home , 'build' , _distutils_dir_name ('lib' ))
252
+ cwd = os .getcwd ()
253
+ os .chdir (home )
254
+ build = subprocess .Popen ([sys .executable , "setup.py" , "build" ],
255
+ shell = False , cwd = os .path .dirname (os .path .abspath (__file__ )))
256
+ build_rc = build .wait ()
257
+ if not os .path .exists (LIBPATH ):
258
+ logger .warning ("`lib` directory does not exist, trying common Python3 lib" )
259
+ LIBPATH = os .path .join (os .path .split (LIBPATH )[0 ], "lib" )
260
+ os .chdir (cwd )
261
+
262
+ if build_rc == 0 :
263
+ logger .info ("Build process ended." )
264
+ else :
265
+ logger .error ("Build process ended with rc=%s" , build_rc )
266
+ sys .exit (- 1 )
267
+
268
+ sys .path .insert (0 , LIBPATH )
269
+ logger .info ("Patched sys.path with %s" , LIBPATH )
270
+
271
+ main (sys .argv )
272
+
0 commit comments