summaryrefslogtreecommitdiffstats
path: root/tools/py2cc/stage0pycc/main.cpp
blob: 1c296267f99e51da87433b82bec26d1531aee8a9 (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
#include <util/folder/path.h>
#include <util/generic/scope.h>
#include <util/generic/string.h>
#include <util/stream/file.h>
#include <util/stream/output.h>

#include <Python.h>
#include <marshal.h>

#include <cstdio>
#include <system_error>

struct TPyObjDeleter {
    static void Destroy(PyObject* o) noexcept {
        Py_XDECREF(o);
    }
};
using TPyObject = THolder<PyObject, TPyObjDeleter>;

int main(int argc, char** argv) {
    if (argc < 4) {
        Cerr << "Usage: " << argv[0] << " SRC_PATH_X SRC OUT" << Endl;
        return 1;
    }

    TString srcpath{argv[1]};
    srcpath.pop_back();
    const TFsPath inPath{argv[2]};
    const char* outPath = argv[3];

    Py_Initialize();
    Y_SCOPE_EXIT() {
        Py_Finalize();
    };

    TPyObject bytecode{Py_CompileString(
        TFileInput{inPath}.ReadAll().c_str(),
        srcpath.c_str(),
        Py_file_input)};
    if (!bytecode) {
        Cerr << "Failed to compile " << outPath << Endl;
        PyErr_Print();
        return 1;
    }

    if (FILE* out = fopen(outPath, "wb")) {
        PyMarshal_WriteObjectToFile(bytecode.Get(), out, Py_MARSHAL_VERSION);
        fclose(out);
        if (PyErr_Occurred()) {
            Cerr << "Failed to marshal " << outPath << Endl;
            PyErr_Print();
            return 1;
        }
    } else {
        Cerr << "Failed to write " << outPath << ": " << std::error_code{errno, std::system_category()}.message() << Endl;
        return 1;
    }

    return 0;
}