@@ -0,0 +1,114 @@
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/>.
18+# Authored by: Michi Henning <michi.henning@canonical.com>
22+# Little helper program to test that all source files import
23+# the wanted SDK version
25+# Usage: check_sdk.py directory [ignore_prefix]
27+# The directory specifies the (recursive) location of the source files. Any
28+# files with a path that starts with ignore_prefix are not checked. This is
29+# useful to exclude files that are generated into the build directory.
31+# See the file_pat definition below for a list of files that are checked.
34+from __future__ import print_function
42+# Print msg on stderr, preceded by program name and followed by newline
44+ print(os.path.basename(sys.argv[0]) + ": " + msg, file=sys.stderr)
47+# Function to raise errors encountered by os.walk
51+# Scan lines in file_path for bad SDK version. For each file,
52+# print the line numbers that have SDK version issues
54+ubuntu_components_pat = re.compile(r'.*import Ubuntu.Components.*')
55+ubuntu_good_components_pat = re.compile(r'.*import Ubuntu.Components.*1\.3.*')
57+def scan_for_bad_sdk(file_path):
58+ global ubuntu_components_pat
60+ with open(file_path, 'rt', encoding='utf-8') as ifile:
61+ for lino, line in enumerate(ifile, start=1):
62+ if ubuntu_components_pat.match(line) and not ubuntu_good_components_pat.match(line):
64+ if 0 < len(errors) <= 10:
70+ "%s: bad SDK Version in line%s %s" % (
71+ file_path, plural, ", ".join((str(i) for i in errors))))
73+ print("%s: bad SDK Version in multiple lines" % file_path)
78+parser = argparse.ArgumentParser(
79+ description='Test that source files contain the wanted SDK version.')
82+ help='The directory to (recursively) search for source files')
84+ 'ignore_prefix', nargs='?', default=None,
85+ help='Ignore source files with a path that starts with the given prefix.')
86+args = parser.parse_args()
88+# Files we want to check for SDK version.
92+pat = re.compile(file_pat)
94+# Find all the files with matching file extension in the specified
95+# directory and check them
97+directory = os.path.abspath(args.dir[0])
98+ignore = args.ignore_prefix and os.path.abspath(args.ignore_prefix) or None
100+found_bad_sdk = False
102+ for root, dirs, files in os.walk(directory, onerror=raise_error):
104+ path = os.path.join(root, file)
105+ if not (ignore and path.startswith(ignore)) and pat.match(file):
106+ if scan_for_bad_sdk(path):
107+ found_bad_sdk = True
110+ error("cannot create file list for \"" + dir + "\": " + e.strerror)