aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/runtime/entry_points.py
blob: ca5c38a26af56e92ee6c8b8fe7a0b767abb3bc58 (plain) (blame)
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
import sys

import __res


def repl():
    user_ns = {}
    py_main = __res.find('PY_MAIN')

    if py_main:
        py_main_split = py_main.split(':', 1)
        if len(py_main_split) == 2:
            mod_name, func_name = py_main_split
        else:
            mod_name, func_name = py_main_split[0], 'main'

        if not mod_name:
            mod_name = 'library.python.runtime.entry_points'

        try:
            import importlib
            mod = importlib.import_module(mod_name)
            user_ns = mod.__dict__
        except:  # noqa E722
            import traceback
            traceback.print_exc()

        if '__main__' not in user_ns:
            def run(args):
                if isinstance(args, basestring):
                    import shlex
                    args = shlex.split(args)

                import sys
                sys.argv = [sys.argv[0]] + args
                getattr(mod, func_name)()

            user_ns['__main__'] = run
    else:
        try:
            mod = __res.importer.load_module('__main__', fix_name='__main_real')
            user_ns = mod.__dict__
        except ImportError:
            pass

    try:
        import IPython
    except ImportError:
        import code
        code.interact(local=user_ns)
    else:
        IPython.start_ipython(user_ns=user_ns)


def resource_files():
    sys.stdout.write('\n'.join(sorted(__res.resfs_files()) + ['']))


def run_constructors():
    for key, module_name in __res.iter_keys('py/constructors/'):
        import importlib
        module = importlib.import_module(module_name)
        init_func = getattr(module, __res.find(key))
        init_func()