aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/gen_py_reg.py
blob: 0f38dfffe3586d88bfe116b4f24a110473ae6c81 (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
import sys

template = '''
extern "C" void PyImport_AppendInittab(const char* name, void (*fn)(void));
extern "C" void {1}();

namespace {
    struct TRegistrar {
        inline TRegistrar() {
            PyImport_AppendInittab("{0}", {1});
        }
    } REG;
}
'''


def mangle(name):
    if '.' not in name:
        return name
    return ''.join('{}{}'.format(len(s), s) for s in name.split('.'))


if __name__ == '__main__':
    if len(sys.argv) != 3:
        print >> sys.stderr, 'Usage: <path/to/gen_py_reg.py> <python_module_name> <output_file>'
        print >> sys.stderr, 'Passed: ' + ' '.join(sys.argv)
        sys.exit(1)

    with open(sys.argv[2], 'w') as f:
        modname = sys.argv[1]
        initname = 'init' + mangle(modname)
        code = template.replace('{0}', modname).replace('{1}', initname)
        f.write(code)