aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/runtime_py3/stage0pycc/main.cpp
blob: f66ea33b6e149ef0e69e389e977d6c60ab6d00d4 (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
65
66
67
68
69
70
#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>;

constexpr TStringBuf modPrefix = "mod=";

int main(int argc, char** argv) {
    if ((argc - 1) % 3 != 0) {
        Cerr << "Usage:\n\t" << argv[0] << " (mod=SRC_PATH_X SRC OUT)+" << Endl;
        return 1;
    }

    PyConfig cfg{};
    PyConfig_InitIsolatedConfig(&cfg);
    cfg._install_importlib = 0;
    Y_SCOPE_EXIT(&cfg) {PyConfig_Clear(&cfg);};

    for (int i = 0; i < (argc - 1)/3; ++i) {
        const TString srcpath{TStringBuf{argv[3*i + 1]}.substr(modPrefix.size())};
        const TFsPath inPath{argv[3*i + 2]};
        const char* outPath = argv[3*i + 3];

        const auto status = Py_InitializeFromConfig(&cfg);
        if (PyStatus_Exception(status)) {
            Py_ExitStatusException(status);
        }
        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;
}