~ci-train-bot/ubuntu-settings-components/ubuntu-settings-components-ubuntu-zesty-2106

« back to all changes in this revision

Viewing changes to tests/imports/check_imports.py

  • Committer: Michał Sawicz
  • Date: 2016-01-29 01:07:59 UTC
  • mto: This revision was merged to the branch mainline in revision 161.
  • Revision ID: michal.sawicz@canonical.com-20160129010759-3ynalempr3dq7cc4
Add import verifier test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python3
 
2
 
 
3
#
 
4
# Copyright (C) 2015 Canonical Ltd
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License version 3 as
 
8
# published by the Free Software Foundation.
 
9
#
 
10
# This program 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
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
#
 
18
 
 
19
#
 
20
# Little helper program to test that all source files import
 
21
# versions we want
 
22
#
 
23
# Usage: check_imports.py directory [ignore_prefix]
 
24
#
 
25
# The directory specifies the (recursive) location of the source files. Any
 
26
# files with a path that starts with ignore_prefix are not checked. This is
 
27
# useful to exclude files that are generated into the build directory.
 
28
#
 
29
# See the file_pat definition below for a list of files that are checked.
 
30
#
 
31
 
 
32
from __future__ import print_function
 
33
 
 
34
import argparse
 
35
import os
 
36
import re
 
37
import sys
 
38
 
 
39
 
 
40
# Print msg on stderr, preceded by program name and followed by newline
 
41
def error(msg):
 
42
    print(os.path.basename(sys.argv[0]) + ": " + msg, file=sys.stderr)
 
43
 
 
44
 
 
45
# Function to raise errors encountered by os.walk
 
46
def raise_error(e):
 
47
    raise e
 
48
 
 
49
# Qt Quick patterns
 
50
# If you increase this make sure you increase
 
51
# the Qt version in debian/control and in CMakeLists.txt
 
52
quick_pat = re.compile(r'.*import QtQuick.*$')
 
53
quick_good_pat = re.compile(r'.*import QtQuick 2\.4.*$')
 
54
quick_layouts_good_pat = re.compile(r'.*import QtQuick.Layouts 1\.1.*$')
 
55
quick_window_good_pat = re.compile(r'.*import QtQuick.Window 2\.2.*$')
 
56
 
 
57
# Ubuntu Components patterns
 
58
ubuntu_components_pat = re.compile(r'.*import Ubuntu.Components.*')
 
59
ubuntu_good_components_pat = re.compile(r'.*import Ubuntu.Components.*1\.3.*')
 
60
 
 
61
def scan_for_bad_import(file_path, all_pat, good_pats):
 
62
    errors = []
 
63
    with open(file_path, 'rt', encoding='utf-8') as ifile:
 
64
        for lino, line in enumerate(ifile, start=1):
 
65
            if all_pat.match(line):
 
66
                good_found = False
 
67
                for good_pat in good_pats:
 
68
                    if good_pat.match(line):
 
69
                        good_found = True
 
70
                if not good_found:
 
71
                    errors.append(lino)
 
72
    if 0 < len(errors) <= 10:
 
73
        if len(errors) > 1:
 
74
            plural = 's'
 
75
        else:
 
76
            plural = ''
 
77
        print(
 
78
            "%s: bad import version in line%s %s" % (
 
79
                file_path, plural, ", ".join((str(i) for i in errors))))
 
80
    elif errors:
 
81
        print("%s: bad import version in multiple lines" % file_path)
 
82
    return bool(errors)
 
83
 
 
84
# Parse args
 
85
 
 
86
parser = argparse.ArgumentParser(
 
87
    description='Test that source files contain the wanted import version.')
 
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.')
 
94
args = parser.parse_args()
 
95
 
 
96
# Files we want to check for import version.
 
97
 
 
98
file_pat = (
 
99
    r'(.*\.(js|qml)$)')
 
100
pat = re.compile(file_pat)
 
101
 
 
102
# Find all the files with matching file extension in the specified
 
103
# directory and check them
 
104
 
 
105
directory = os.path.abspath(args.dir[0])
 
106
ignore = args.ignore_prefix and os.path.abspath(args.ignore_prefix) or None
 
107
 
 
108
found_bad_import = False
 
109
try:
 
110
    for root, dirs, files in os.walk(directory, onerror=raise_error):
 
111
        for file in files:
 
112
            path = os.path.join(root, file)
 
113
            if not (ignore and path.startswith(ignore)) and pat.match(file):
 
114
                quick_good_pats = [quick_good_pat, quick_layouts_good_pat, quick_window_good_pat]
 
115
                if scan_for_bad_import(path, quick_pat, quick_good_pats):
 
116
                    found_bad_import = True
 
117
                if scan_for_bad_import(path, ubuntu_components_pat, [ubuntu_good_components_pat]):
 
118
                    found_bad_import = True
 
119
 
 
120
except OSError as e:
 
121
    error("cannot create file list for \"" + dir + "\": " + e.strerror)
 
122
    sys.exit(1)
 
123
 
 
124
if found_bad_import:
 
125
    sys.exit(1)