~osomon/phatch/extract-all-metadata

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2007-2010  www.stani.be
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/
#
# Follows PEP8


import os
import pickle
import time
import shelve
import sys

try:
    from other import pep8
except ImportError:
    print("You need to run this script from the 'tests' directory.")
    sys.exit(1)


ERROR_HEADER = "Shame on you! These file(s) don't follow PEP8:\n%s\n"
PEP8_APP = os.path.join('..', 'phatch', 'other', 'pep8.py')
PEP8_ARGS = ['python', PEP8_APP, '--show-source', '--repeat', '--count']
WXGLADE = 'wxGlade'
OTHER = os.path.join('phatch', 'other')
OUTPUT = os.path.join('tests', 'output')
SETUP_IMAGES = os.path.join('setup', 'images')

ERROR_MESSAGE = ''


def message(x):
    global ERROR_MESSAGE
    print(x)
    ERROR_MESSAGE += '%s\n' % x

pep8.process_options(PEP8_ARGS)
pep8.message = message

BLACK_LIST = [
    os.path.join('phatch', 'lib', 'metadataTest.py'),
    os.path.join('phatch', 'lib', 'pyWx', 'about.py'),
    os.path.join('phatch', 'lib', 'pyWx', 'dialogsInspector.py'),
    os.path.join('phatch', 'lib', 'pyWx', 'folderFileBrowser.py'),
]


def test(dirname='..'):
    global ERROR_MESSAGE

    def needs_pep8(filename):
        key = os.path.abspath(filename)[n:]
        return filename.endswith('.py') and not (
            OTHER in filename
            or OUTPUT in filename
            or WXGLADE in filename
            or SETUP_IMAGES in filename
            or key in BLACK_LIST
        )

    time_start = time.time()
    total = 0
    dirname = os.path.abspath(dirname)
    n = len(dirname) + 1
    summary = []
    if not os.path.exists('cache'):
        os.mkdir('cache')
    cache = shelve.open(os.path.join('cache', 'pep8'),
        protocol=pickle.HIGHEST_PROTOCOL)
    for root, dirs, files in os.walk(dirname):
        for name in files:
            filename = os.path.join(root, name)
            if not needs_pep8(filename):
                continue
            ERROR_MESSAGE = ''
            count = None
            mtime = os.path.getmtime(filename)
            if filename in cache:
                error_message, count, cmtime = cache[filename]
                if mtime != cmtime:
                    count = None
                elif error_message:
                    message(error_message)
            if count is None:
                checker = pep8.Checker(filename)  # updates ERROR_MESSAGE
                count = checker.check_all()
                cache[filename] = ERROR_MESSAGE, count, mtime
            if count or ERROR_MESSAGE:
                summary.append((filename, count))
            total += 1
    cache.close()
    if summary:
        print(ERROR_HEADER % '\n'.join(
            ['%s (%d)' % (filename, count) for filename, count in summary]
        ))
    print('Ran %d PEP8 tests in %.3fs'
        % (total, time.time() - time_start))
    return summary


def test_with_exit(dirname=''):
    summary = test(dirname)
    if summary:
        sys.exit(1)


if __name__ == '__main__':
    test_with_exit('..')