blob: 80bbe65bea0d763542f2c3585175fe09141a43ac [file] [log] [blame]
Martin Pittb2ad12e2010-09-21 12:45:52 +02001# Simple udev rules syntax checker
2#
3# (C) 2010 Canonical Ltd.
4# Author: Martin Pitt <martin.pitt@ubuntu.com>
5#
Kay Sievers0228a7e2013-08-14 22:55:40 +02006# 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
Martin Pittb2ad12e2010-09-21 12:45:52 +02009# (at your option) any later version.
Kay Sievers0228a7e2013-08-14 22:55:40 +020010
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.
Martin Pittb2ad12e2010-09-21 12:45:52 +020015#
Kay Sievers0228a7e2013-08-14 22:55:40 +020016# 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/>.
Martin Pittb2ad12e2010-09-21 12:45:52 +020018
19import re
20import sys
Martin Pitte8015e62015-01-20 20:50:35 +010021import os
22from glob import glob
Martin Pittb2ad12e2010-09-21 12:45:52 +020023
Martin Pitte8015e62015-01-20 20:50:35 +010024if len(sys.argv) > 1:
25 # explicit rule file list
26 rules_files = sys.argv[1:]
27else:
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)
33 sys.exit(2)
34 rules_files = glob(os.path.join(rules_dir, '*.rules'))
Martin Pittb2ad12e2010-09-21 12:45:52 +020035
36no_args_tests = re.compile('(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$')
37args_tests = re.compile('(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$')
Kay Sievers0f52fde2012-02-20 00:41:58 +010038no_args_assign = re.compile('(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|WAIT_FOR|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$')
Kay Sievers5488ac82012-10-09 00:49:15 +020039args_assign = re.compile('(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*"([^"]*)"$')
Martin Pittb2ad12e2010-09-21 12:45:52 +020040
41result = 0
42buffer = ''
Martin Pitte8015e62015-01-20 20:50:35 +010043for path in rules_files:
Martin Pittb2ad12e2010-09-21 12:45:52 +020044 lineno = 0
45 for line in open(path):
46 lineno += 1
47
48 # handle line continuation
49 if line.endswith('\\\n'):
50 buffer += line[:-2]
51 continue
52 else:
53 line = buffer + line
54 buffer = ''
55
56 # filter out comments and empty lines
57 line = line.strip()
58 if not line or line.startswith('#'):
59 continue
60
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)):
65
Kay Sieversbe903bf2011-05-31 02:18:33 +020066 print('Invalid line %s:%i: %s' % (path, lineno, line))
Martin Pittc6be83c2015-01-20 12:18:40 +010067 print(' clause: %s' % clause)
68 print('')
Martin Pittb2ad12e2010-09-21 12:45:52 +020069 result = 1
70 break
71
72sys.exit(result)