1
#! /usr/bin/env python3
4
# Copyright (C) 2015 Canonical Ltd
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.
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.
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/>.
20
# Little helper program to test that all source files import
23
# Usage: check_imports.py directory [ignore_prefix]
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.
29
# See the file_pat definition below for a list of files that are checked.
32
from __future__ import print_function
40
# Print msg on stderr, preceded by program name and followed by newline
42
print(os.path.basename(sys.argv[0]) + ": " + msg, file=sys.stderr)
45
# Function to raise errors encountered by os.walk
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.*$')
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.*')
61
def scan_for_bad_import(file_path, all_pat, good_pats):
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):
67
for good_pat in good_pats:
68
if good_pat.match(line):
72
if 0 < len(errors) <= 10:
78
"%s: bad import version in line%s %s" % (
79
file_path, plural, ", ".join((str(i) for i in errors))))
81
print("%s: bad import version in multiple lines" % file_path)
86
parser = argparse.ArgumentParser(
87
description='Test that source files contain the wanted import version.')
90
help='The directory to (recursively) search for source files')
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()
96
# Files we want to check for import version.
100
pat = re.compile(file_pat)
102
# Find all the files with matching file extension in the specified
103
# directory and check them
105
directory = os.path.abspath(args.dir[0])
106
ignore = args.ignore_prefix and os.path.abspath(args.ignore_prefix) or None
108
found_bad_import = False
110
for root, dirs, files in os.walk(directory, onerror=raise_error):
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
121
error("cannot create file list for \"" + dir + "\": " + e.strerror)