aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/python/main_py3/__main__.pyx
blob: 6f4ca943584a873ba5985565ca54d33dc8e2693d (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
import os
import runpy
import importlib

import __res


cdef env_entry_point = 'Y_PYTHON_ENTRY_POINT'


cdef extern from 'main.h':
    pass


def find_pymain():
    py_main = __res.find('PY_MAIN')

    if isinstance(py_main, bytes):
        py_main = py_main.decode('utf8')

    if isinstance(py_main, unicode):
        return py_main

    return None


def run_main():
    entry_point = os.environ.pop(env_entry_point, None)

    if entry_point is None:
        entry_point = find_pymain()

    if entry_point is None:
        raise RuntimeError('No entry point found')

    module_name, colon, func_name = entry_point.partition(':')

    if not colon:
        runpy._run_module_as_main(module_name, alter_argv=False)
        return

    if not module_name:
        module_name = 'library.python.runtime_py3.entry_points'

    module = importlib.import_module(module_name)
    func = getattr(module, func_name)
    func()


run_main()