~mzanetti/unity8/fade-out-launcher

« back to all changes in this revision

Viewing changes to tests/whitespace/check_whitespace.py

  • Committer: Michael Zanetti
  • Date: 2014-07-08 10:15:46 UTC
  • mfrom: (977.1.35 unity8)
  • Revision ID: michael.zanetti@canonical.com-20140708101546-01dbieenbk9123il
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! /usr/bin/env python3
2
2
 
3
3
#
4
 
# Copyright (C) 2013 Canonical Ltd
 
4
# Copyright (C) 2013, 2014 Canonical Ltd
5
5
#
6
6
# This program is free software: you can redistribute it and/or modify
7
7
# it under the terms of the GNU General Public License version 3 as
19
19
#
20
20
 
21
21
#
22
 
# Little helper program to test that source files do not contain trailing whitespace
23
 
# or tab indentation.
 
22
# Little helper program to test that source files do not contain trailing
 
23
# whitespace or tab indentation.
24
24
#
25
25
# Usage: check_whitespace.py directory [ignore_prefix]
26
26
#
31
31
# See the file_pat definition below for a list of files that are checked.
32
32
#
33
33
 
 
34
from __future__ import print_function
 
35
 
34
36
import argparse
35
37
import os
36
38
import re
37
39
import sys
38
40
 
 
41
 
39
42
# Print msg on stderr, preceded by program name and followed by newline
40
 
 
41
43
def error(msg):
42
44
    print(os.path.basename(sys.argv[0]) + ": " + msg, file=sys.stderr)
43
45
 
 
46
 
44
47
# Function to raise errors encountered by os.walk
45
 
 
46
48
def raise_error(e):
47
49
    raise e
48
50
 
52
54
whitespace_pat = re.compile(r'.*[ \t]$')
53
55
tab_indent_pat = re.compile(r'^ *\t')
54
56
 
 
57
 
55
58
def scan_for_bad_whitespace(file_path):
56
59
    global tab_indent_pat, whitespace_pat
57
60
    errors = []
69
72
            plural = 's'
70
73
        else:
71
74
            plural = ''
72
 
        print("%s: bad whitespace in line%s %s" % (file_path, plural, ", ".join((str(i) for i in errors))))
 
75
        print(
 
76
            "%s: bad whitespace in line%s %s" % (
 
77
                file_path, plural, ", ".join((str(i) for i in errors))))
73
78
    elif errors:
74
79
        print("%s: bad whitespace in multiple lines" % file_path)
75
80
    if newlines_at_end:
78
83
 
79
84
# Parse args
80
85
 
81
 
parser = argparse.ArgumentParser(description = 'Test that source files do not contain trailing whitespace.')
82
 
parser.add_argument('dir', nargs = 1, help = 'The directory to (recursively) search for source files')
83
 
parser.add_argument('ignore_prefix', nargs = '?', default=None,
84
 
                    help = 'Ignore source files with a path that starts with the given prefix.')
 
86
parser = argparse.ArgumentParser(
 
87
    description='Test that source files do not contain trailing whitespace.')
 
88
parser.add_argument(
 
89
    'dir', nargs=1,
 
90
    help='The directory to (recursively) search for source files')
 
91
parser.add_argument(
 
92
    'ignore_prefix', nargs='?', default=None,
 
93
    help='Ignore source files with a path that starts with the given prefix.')
85
94
args = parser.parse_args()
86
95
 
87
96
# Files we want to check for trailing whitespace.
88
97
 
89
 
file_pat = r'(.*\.(c|cpp|h|hpp|hh|in|install|js|py|qml|sh)$)|(.*CMakeLists\.txt$)'
 
98
file_pat = (
 
99
    r'(.*\.(c|cpp|h|hpp|hh|in|install|js|py|qml|sh)$)|(.*CMakeLists\.txt$)')
90
100
pat = re.compile(file_pat)
91
101
 
92
102
# Find all the files with matching file extension in the specified
97
107
 
98
108
found_whitespace = False
99
109
try:
100
 
    for root, dirs, files in os.walk(directory, onerror = raise_error):
 
110
    for root, dirs, files in os.walk(directory, onerror=raise_error):
101
111
        for file in files:
102
112
            path = os.path.join(root, file)
103
113
            if not (ignore and path.startswith(ignore)) and pat.match(file):