summaryrefslogtreecommitdiff
path: root/loaders/python/peas-python-compile.py
blob: 9959ccfa2f0f345746c6a74ab0ca56614dd81cd4 (plain)
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
# -*- coding: utf-8 -*-

#  Copyright (C) 2015 - Garrett Regier
#
# libpeas is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# libpeas is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.

import marshal
import sys
import traceback


def compile_file(filename, output):
    """Byte-compiles a Python source file to Python bytecode.

       Unlike py_compile the output is not prefixed
       by a magic value, mtime or size.
    """
    # Open with universal newlines
    with open(filename, 'U') as f:
        code = f.read() + '\n'

    try:
        code_object = compile(code, filename, 'exec')

    except (SyntaxError, TypeError) as error:
        tb = traceback.format_exc(0).rstrip('\n')
        raise Exception('Failed to compile "{0}":\n{1}'.format(filename, tb))

    with open(output, 'wb') as f:
        marshal.dump(code_object, f)
        f.flush()


def main(args):
    try:
        for i in range(0, len(args), 2):
            compile_file(args[i], args[i + 1])

    except Exception as error:
        sys.exit('Error: {0!s}'.format(error))


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))

# ex:ts=4:et: