OILS / opy / _regtest / src / bin / opy_.py View on Github | oilshell.org

88 lines, 56 significant
1#!/usr/bin/env python
2from __future__ import print_function
3"""
4opy_.py
5"""
6
7import os
8import sys
9
10this_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
11sys.path.append(os.path.join(this_dir, '..'))
12
13from core import args
14from core import util
15
16from opy.util_opy import log
17from opy import opy_main
18
19
20# TODO: move to quick ref?
21_OPY_USAGE = 'Usage: opy_ MAIN [OPTION]... [ARG]...'
22
23
24def _ShowVersion():
25 util.ShowAppVersion('OPy')
26
27
28# Run the bytecode too. Should this have an option to use byterun?
29def OpyMain(argv0, main_argv):
30 raise NotImplementedError("Can't run bytecode yet")
31
32
33def AppBundleMain(argv):
34 b = os.path.basename(argv[0])
35 main_name, ext = os.path.splitext(b)
36
37 if main_name in ('opy_', 'opy') and ext: # opy_.py or opy.ovm
38 try:
39 first_arg = argv[1]
40 except IndexError:
41 raise args.UsageError('Missing required applet name.')
42
43 # TODO: We don't have this
44 if first_arg in ('-h', '--help'):
45 #builtin.Help(['bundle-usage'], util.GetResourceLoader())
46 raise NotImplementedError('OPy help not implemented')
47 sys.exit(0)
48
49 if first_arg in ('-V', '--version'):
50 _ShowVersion()
51 sys.exit(0)
52
53 main_name = first_arg
54 argv0 = argv[1]
55 main_argv = argv[2:]
56 else:
57 argv0 = argv[0]
58 main_argv = argv[1:]
59
60 if main_name == 'opy':
61 status = OpyMain(argv0, main_argv)
62 return status
63 elif main_name == 'opyc':
64 return opy_main.OpyCommandMain(main_argv)
65
66 else:
67 raise args.UsageError('Invalid applet name %r.' % main_name)
68
69
70def main(argv):
71 try:
72 sys.exit(AppBundleMain(argv))
73 except args.UsageError as e:
74 #print(_OPY_USAGE, file=sys.stderr)
75 log('opy: %s', e)
76 sys.exit(2)
77 except RuntimeError as e:
78 log('FATAL: %s', e)
79 sys.exit(1)
80
81
82if __name__ == '__main__':
83 # NOTE: This could end up as opy.InferTypes(), opy.GenerateCode(), etc.
84 if os.getenv('CALLGRAPH') == '1':
85 from opy import callgraph
86 callgraph.Walk(main, sys.modules)
87 else:
88 main(sys.argv)