OILS / opy / _regtest / src / build / testdata / hello.py View on Github | oilshell.org

80 lines, 52 significant
1#!/usr/bin/env python
2"""
3hello.py
4"""
5from __future__ import print_function
6
7import sys
8print('Hello from hello.py', file=sys.stderr)
9
10import os
11
12print('sys.path:', sys.path, file=sys.stderr)
13print('sys.argv:', sys.argv, file=sys.stderr)
14print('hello _OVM_IS_BUNDLE', os.getenv('_OVM_IS_BUNDLE'), file=sys.stderr)
15
16# Default
17if not os.getenv('_OVM_DEPS'):
18 import inspect
19 print(inspect)
20
21import lib
22
23#import zipfile
24
25import zipimport
26
27if os.getenv('_OVM_IS_BUNDLE') == '1':
28 if 0:
29 print('ZIP')
30 z = zipfile.ZipFile(sys.argv[0])
31 print(z.infolist())
32 else:
33 z = zipimport.zipimporter(sys.argv[0])
34 print(z)
35 print(dir(z))
36 # None if we have the module, but no source.
37 print('SOURCE', repr(z.get_source('runpy')))
38 # TODO: Add a method to get a file? I think it just imports zlib.
39 r = z.get_data('hello-data.txt')
40 print('FILE', repr(r))
41
42
43def Busy(n):
44 s = 0
45 for i in xrange(n):
46 s += i
47 print(s)
48
49
50def main(argv):
51 try:
52 action = argv[0]
53 except IndexError:
54 action = 'busy'
55
56 if action == 'busy':
57 try:
58 n = int(argv[1])
59 except IndexError:
60 n = 100
61 Busy(n)
62
63 elif action == 'unicode': # For testing that we support 4-byte chars
64 print(repr(unichr(0x10000)))
65
66 elif action == 'crash': # For testing that stack trace shows code
67 lib.Crash()
68
69 elif action == 'exit42': # for testing exit code
70 return 42
71
72 else:
73 print('Unknown action %r' % action, file=sys.stderr)
74 return 1
75
76 return 0
77
78
79if __name__ == '__main__':
80 sys.exit(main(sys.argv[1:]))