]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/rule-syntax-check.py
4391a1fedacc75b5950fad98413c63a5d09d43ff
2 # SPDX-License-Identifier: LGPL-2.1+
4 # Simple udev rules syntax checker
6 # (C) 2010 Canonical Ltd.
7 # Author: Martin Pitt <martin.pitt@ubuntu.com>
14 rules_files
= sys
.argv
[1:]
16 sys
.exit('Specify files to test as arguments')
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
+ '$')
20 args_tests
= re
.compile(r
'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re
+ '$')
21 no_args_assign
= re
.compile(r
'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re
+ '$')
22 args_assign
= re
.compile(r
'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*' + quoted_string_re
+ '$')
23 # Find comma-separated groups, but allow commas that are inside quoted strings.
24 # Using quoted_string_re + '?' so that strings missing the last double quote
25 # will still match for this part that splits on commas.
26 comma_separated_group_re
= re
.compile(r
'(?:[^,"]|' + quoted_string_re
+ '?)+')
30 for path
in rules_files
:
31 print('# looking at {}'.format(path
))
33 for line
in open(path
):
36 # handle line continuation
37 if line
.endswith('\\\n'):
44 # filter out comments and empty lines
46 if not line
or line
.startswith('#'):
49 # Separator ',' is normally optional but we make it mandatory here as
50 # it generally improves the readability of the rules.
51 for clause_match
in comma_separated_group_re
.finditer(line
):
52 clause
= clause_match
.group().strip()
53 if not (no_args_tests
.match(clause
) or args_tests
.match(clause
) or
54 no_args_assign
.match(clause
) or args_assign
.match(clause
)):
56 print('Invalid line {}:{}: {}'.format(path
, lineno
, line
))
57 print(' clause:', clause
)