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 | |
Zbigniew Jędrzejewski-Szmek | 6b97bf2 | 2017-11-22 12:42:28 +0100 | [diff] [blame] | 27 | rules_files = sys.argv[1:] |
| 28 | if not rules_files: |
| 29 | sys.exit('Specify files to test as arguments') |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 30 | |
Filipe Brandenburger | d498347 | 2018-02-27 11:12:18 -0800 | [diff] [blame] | 31 | quoted_string_re = r'"(?:[^\\"]|\\.)*"' |
| 32 | no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|PROGRAM|RESULT|TEST)\s*(?:=|!)=\s*' + quoted_string_re + '$') |
| 33 | args_tests = re.compile(r'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re + '$') |
| 34 | no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$') |
| 35 | args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*' + quoted_string_re + '$') |
Filipe Brandenburger | c9715ff | 2018-02-27 13:11:07 -0800 | [diff] [blame] | 36 | # Find comma-separated groups, but allow commas that are inside quoted strings. |
Filipe Brandenburger | 27e2779 | 2018-02-27 16:11:38 -0800 | [diff] [blame^] | 37 | # Using quoted_string_re + '?' so that strings missing the last double quote |
| 38 | # will still match for this part that splits on commas. |
| 39 | comma_separated_group_re = re.compile(r'(?:[^,"]|' + quoted_string_re + '?)+') |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 40 | |
| 41 | result = 0 |
| 42 | buffer = '' |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 43 | for path in rules_files: |
Zbigniew Jędrzejewski-Szmek | 2956395 | 2017-11-22 12:29:46 +0100 | [diff] [blame] | 44 | print('# looking at {}'.format(path)) |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 45 | lineno = 0 |
| 46 | for line in open(path): |
| 47 | lineno += 1 |
| 48 | |
| 49 | # handle line continuation |
| 50 | if line.endswith('\\\n'): |
| 51 | buffer += line[:-2] |
| 52 | continue |
| 53 | else: |
| 54 | line = buffer + line |
| 55 | buffer = '' |
| 56 | |
| 57 | # filter out comments and empty lines |
| 58 | line = line.strip() |
| 59 | if not line or line.startswith('#'): |
| 60 | continue |
| 61 | |
Franck Bui | 75a56cb | 2018-02-23 17:12:50 +0100 | [diff] [blame] | 62 | # Separator ',' is normally optional but we make it mandatory here as |
| 63 | # it generally improves the readability of the rules. |
Filipe Brandenburger | c9715ff | 2018-02-27 13:11:07 -0800 | [diff] [blame] | 64 | for clause_match in comma_separated_group_re.finditer(line): |
| 65 | clause = clause_match.group().strip() |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 66 | if not (no_args_tests.match(clause) or args_tests.match(clause) or |
| 67 | no_args_assign.match(clause) or args_assign.match(clause)): |
| 68 | |
Zbigniew Jędrzejewski-Szmek | 2956395 | 2017-11-22 12:29:46 +0100 | [diff] [blame] | 69 | print('Invalid line {}:{}: {}'.format(path, lineno, line)) |
| 70 | print(' clause:', clause) |
| 71 | print() |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 72 | result = 1 |
| 73 | break |
| 74 | |
| 75 | sys.exit(result) |