]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/rule-syntax-check.py
1 # Simple udev rules syntax checker
3 # (C) 2010 Canonical Ltd.
4 # Author: Martin Pitt <martin.pitt@ubuntu.com>
6 # systemd is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation; either version 2.1 of the License, or
9 # (at your option) any later version.
11 # systemd is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 # explicit rule file list
26 rules_files
= sys
.argv
[1:]
28 # take them from the build dir
29 root_dir
= os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
)))
30 rules_dir
= os
.path
.join(os
.environ
.get('top_srcdir', root_dir
), 'rules')
31 if not os
.path
.isdir(rules_dir
):
32 sys
.stderr
.write('No rules files given, and %s does not exist, aborting' % rules_dir
)
34 rules_files
= glob(os
.path
.join(rules_dir
, '*.rules'))
36 no_args_tests
= re
.compile('(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$')
37 args_tests
= re
.compile('(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$')
38 no_args_assign
= re
.compile('(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$')
39 args_assign
= re
.compile('(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*"([^"]*)"$')
43 for path
in rules_files
:
45 for line
in open(path
):
48 # handle line continuation
49 if line
.endswith('\\\n'):
56 # filter out comments and empty lines
58 if not line
or line
.startswith('#'):
61 for clause
in line
.split(','):
62 clause
= clause
.strip()
63 if not (no_args_tests
.match(clause
) or args_tests
.match(clause
) or
64 no_args_assign
.match(clause
) or args_assign
.match(clause
)):
66 print('Invalid line %s:%i: %s' % (path
, lineno
, line
))
67 print(' clause: %s' % clause
)