Jörg Thalheim | 3e67e5c | 2017-05-01 02:26:56 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Yu Watanabe | db9ecf0 | 2020-11-09 13:23:58 +0900 | [diff] [blame] | 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
Zbigniew Jędrzejewski-Szmek | 35df744 | 2017-11-18 17:32:46 +0100 | [diff] [blame] | 3 | # |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 4 | # Simple udev rules syntax checker |
| 5 | # |
Lennart Poettering | 810adae | 2018-06-12 17:15:23 +0200 | [diff] [blame] | 6 | # © 2010 Canonical Ltd. |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 7 | # Author: Martin Pitt <martin.pitt@ubuntu.com> |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 8 | |
| 9 | import re |
| 10 | import sys |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 11 | import os |
| 12 | from glob import glob |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 13 | |
Zbigniew Jędrzejewski-Szmek | 6b97bf2 | 2017-11-22 12:42:28 +0100 | [diff] [blame] | 14 | rules_files = sys.argv[1:] |
| 15 | if not rules_files: |
| 16 | sys.exit('Specify files to test as arguments') |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 17 | |
Filipe Brandenburger | d498347 | 2018-02-27 11:12:18 -0800 | [diff] [blame] | 18 | quoted_string_re = r'"(?:[^\\"]|\\.)*"' |
| 19 | no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|PROGRAM|RESULT|TEST)\s*(?:=|!)=\s*' + quoted_string_re + '$') |
Jan Synacek | ed2dc50 | 2019-01-30 12:22:41 +0100 | [diff] [blame] | 20 | # PROGRAM can also be specified as an assignment. |
| 21 | program_assign = re.compile(r'PROGRAM\s*=\s*' + quoted_string_re + '$') |
Jan Synacek | a7ab6b7 | 2019-10-17 10:52:38 +0200 | [diff] [blame] | 22 | args_tests = re.compile(r'(ATTRS?|ENV|CONST|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re + '$') |
Filipe Brandenburger | d498347 | 2018-02-27 11:12:18 -0800 | [diff] [blame] | 23 | no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$') |
| 24 | 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] | 25 | # Find comma-separated groups, but allow commas that are inside quoted strings. |
Filipe Brandenburger | 27e2779 | 2018-02-27 16:11:38 -0800 | [diff] [blame] | 26 | # Using quoted_string_re + '?' so that strings missing the last double quote |
| 27 | # will still match for this part that splits on commas. |
| 28 | comma_separated_group_re = re.compile(r'(?:[^,"]|' + quoted_string_re + '?)+') |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 29 | |
| 30 | result = 0 |
| 31 | buffer = '' |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 32 | for path in rules_files: |
Zbigniew Jędrzejewski-Szmek | 2956395 | 2017-11-22 12:29:46 +0100 | [diff] [blame] | 33 | print('# looking at {}'.format(path)) |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 34 | lineno = 0 |
| 35 | for line in open(path): |
| 36 | lineno += 1 |
| 37 | |
| 38 | # handle line continuation |
| 39 | if line.endswith('\\\n'): |
| 40 | buffer += line[:-2] |
| 41 | continue |
| 42 | else: |
| 43 | line = buffer + line |
| 44 | buffer = '' |
| 45 | |
| 46 | # filter out comments and empty lines |
| 47 | line = line.strip() |
| 48 | if not line or line.startswith('#'): |
| 49 | continue |
| 50 | |
Franck Bui | 75a56cb | 2018-02-23 17:12:50 +0100 | [diff] [blame] | 51 | # Separator ',' is normally optional but we make it mandatory here as |
| 52 | # it generally improves the readability of the rules. |
Filipe Brandenburger | c9715ff | 2018-02-27 13:11:07 -0800 | [diff] [blame] | 53 | for clause_match in comma_separated_group_re.finditer(line): |
| 54 | clause = clause_match.group().strip() |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 55 | if not (no_args_tests.match(clause) or args_tests.match(clause) or |
Jan Synacek | ed2dc50 | 2019-01-30 12:22:41 +0100 | [diff] [blame] | 56 | no_args_assign.match(clause) or args_assign.match(clause) or |
| 57 | program_assign.match(clause)): |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 58 | |
Zbigniew Jędrzejewski-Szmek | 2956395 | 2017-11-22 12:29:46 +0100 | [diff] [blame] | 59 | print('Invalid line {}:{}: {}'.format(path, lineno, line)) |
| 60 | print(' clause:', clause) |
| 61 | print() |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 62 | result = 1 |
| 63 | break |
| 64 | |
| 65 | sys.exit(result) |