summaryrefslogtreecommitdiffstats
path: root/support/scripts/pycompile.py
blob: 9f7eb9fd9fe9d2692c9ba68904955c47ea62f4d8 (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
#!/usr/bin/env python

'''Wrapper for python2 and python3 around compileall to raise exception
when a python byte code generation failed.

Inspired from:
   http://stackoverflow.com/questions/615632/how-to-detect-errors-from-compileall-compile-dir
'''
from __future__ import print_function
import sys
import py_compile
import compileall

def check_for_errors(comparison):
    '''Wrap comparison operator with code checking for PyCompileError.
    If PyCompileError was raised, re-raise it again to abort execution,
    otherwise perform comparison as expected.
    '''
    def operator(self, other):
        exc_type, value, traceback = sys.exc_info()
        if exc_type is not None and issubclass(exc_type,
                                               py_compile.PyCompileError):
            print("Cannot compile %s" % value.file)
            raise value

        return comparison(self, other)

    return operator

class ReportProblem(int):
    '''Class that pretends to be an int() object but implements all of its
    comparison operators such that it'd detect being called in
    PyCompileError handling context and abort execution
    '''
    VALUE = 1

    def __new__(cls, *args, **kwargs):
        return int.__new__(cls, ReportProblem.VALUE, **kwargs)

    @check_for_errors
    def __lt__(self, other):
        return ReportProblem.VALUE < other

    @check_for_errors
    def __eq__(self, other):
        return ReportProblem.VALUE == other

    def __ge__(self, other):
        return not self < other

    def __gt__(self, other):
        return not self < other and not self == other

    def __ne__(self, other):
        return not self == other

compileall.compile_dir(sys.argv[1], quiet=ReportProblem())
OpenPOWER on IntegriCloud