1# -*- coding: utf-8 -*-
2
3#  Copyright (C) 2015 - Garrett Regier
4#
5# libpeas is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9#
10# libpeas is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with this library; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
18
19import marshal
20import sys
21import traceback
22
23
24def compile_file(filename, output):
25    """Byte-compiles a Python source file to Python bytecode.
26
27       Unlike py_compile the output is not prefixed
28       by a magic value, mtime or size.
29    """
30    # Open with universal newlines
31    with open(filename, 'U') as f:
32        code = f.read() + '\n'
33
34    try:
35        code_object = compile(code, filename, 'exec')
36
37    except (SyntaxError, TypeError) as error:
38        tb = traceback.format_exc(0).rstrip('\n')
39        raise Exception('Failed to compile "{0}":\n{1}'.format(filename, tb))
40
41    with open(output, 'wb') as f:
42        marshal.dump(code_object, f)
43        f.flush()
44
45
46def main(args):
47    try:
48        for i in range(0, len(args), 2):
49            compile_file(args[i], args[i + 1])
50
51    except Exception as error:
52        sys.exit('Error: {0!s}'.format(error))
53
54
55if __name__ == '__main__':
56    sys.exit(main(sys.argv[1:]))
57
58# ex:ts=4:et:
59