Jörg Thalheim | 3e67e5c | 2017-05-01 02:26:56 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Zbigniew Jędrzejewski-Szmek | 35df744 | 2017-11-18 17:32:46 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: LGPL-2.1+ |
| 3 | # |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 4 | # Simple udev rules syntax checker |
| 5 | # |
| 6 | # (C) 2010 Canonical Ltd. |
| 7 | # Author: Martin Pitt <martin.pitt@ubuntu.com> |
| 8 | # |
Kay Sievers | 0228a7e | 2013-08-14 22:55:40 +0200 | [diff] [blame] | 9 | # systemd is free software; you can redistribute it and/or modify it |
| 10 | # under the terms of the GNU Lesser General Public License as published by |
| 11 | # the Free Software Foundation; either version 2.1 of the License, or |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 12 | # (at your option) any later version. |
Kay Sievers | 0228a7e | 2013-08-14 22:55:40 +0200 | [diff] [blame] | 13 | |
| 14 | # systemd is distributed in the hope that it will be useful, but |
| 15 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | # Lesser General Public License for more details. |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 18 | # |
Kay Sievers | 0228a7e | 2013-08-14 22:55:40 +0200 | [diff] [blame] | 19 | # You should have received a copy of the GNU Lesser General Public License |
| 20 | # along with systemd; If not, see <http://www.gnu.org/licenses/>. |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 21 | |
| 22 | import re |
| 23 | import sys |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 24 | import os |
| 25 | from glob import glob |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 26 | |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 27 | if len(sys.argv) > 1: |
| 28 | # explicit rule file list |
| 29 | rules_files = sys.argv[1:] |
| 30 | else: |
| 31 | # take them from the build dir |
| 32 | root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 33 | rules_dir = os.path.join(os.environ.get('top_srcdir', root_dir), 'rules') |
| 34 | if not os.path.isdir(rules_dir): |
Zbigniew Jędrzejewski-Szmek | 2956395 | 2017-11-22 12:29:46 +0100 | [diff] [blame^] | 35 | print('No rules files given, and {} does not exist, aborting'.format(rules_dir), file=sys.stderr) |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 36 | sys.exit(2) |
| 37 | rules_files = glob(os.path.join(rules_dir, '*.rules')) |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 38 | |
Zbigniew Jędrzejewski-Szmek | cda3997 | 2016-12-01 18:29:54 -0500 | [diff] [blame] | 39 | no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$') |
| 40 | args_tests = re.compile(r'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$') |
| 41 | no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$') |
| 42 | args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*"([^"]*)"$') |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 43 | |
| 44 | result = 0 |
| 45 | buffer = '' |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 46 | for path in rules_files: |
Zbigniew Jędrzejewski-Szmek | 2956395 | 2017-11-22 12:29:46 +0100 | [diff] [blame^] | 47 | print('# looking at {}'.format(path)) |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 48 | lineno = 0 |
| 49 | for line in open(path): |
| 50 | lineno += 1 |
| 51 | |
| 52 | # handle line continuation |
| 53 | if line.endswith('\\\n'): |
| 54 | buffer += line[:-2] |
| 55 | continue |
| 56 | else: |
| 57 | line = buffer + line |
| 58 | buffer = '' |
| 59 | |
| 60 | # filter out comments and empty lines |
| 61 | line = line.strip() |
| 62 | if not line or line.startswith('#'): |
| 63 | continue |
| 64 | |
| 65 | for clause in line.split(','): |
| 66 | clause = clause.strip() |
| 67 | if not (no_args_tests.match(clause) or args_tests.match(clause) or |
| 68 | no_args_assign.match(clause) or args_assign.match(clause)): |
| 69 | |
Zbigniew Jędrzejewski-Szmek | 2956395 | 2017-11-22 12:29:46 +0100 | [diff] [blame^] | 70 | print('Invalid line {}:{}: {}'.format(path, lineno, line)) |
| 71 | print(' clause:', clause) |
| 72 | print() |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 73 | result = 1 |
| 74 | break |
| 75 | |
| 76 | sys.exit(result) |