blob: 7ee34eb7002c800539e749382f89e2cb3f2b633e [file] [log] [blame]
Jörg Thalheim3e67e5c2017-05-01 02:26:56 +02001#!/usr/bin/env python3
Zbigniew Jędrzejewski-Szmek35df7442017-11-18 17:32:46 +01002# SPDX-License-Identifier: LGPL-2.1+
3#
Martin Pittb2ad12e2010-09-21 12:45:52 +02004# Simple udev rules syntax checker
5#
6# (C) 2010 Canonical Ltd.
7# Author: Martin Pitt <martin.pitt@ubuntu.com>
8#
Kay Sievers0228a7e2013-08-14 22:55:40 +02009# 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 Pittb2ad12e2010-09-21 12:45:52 +020012# (at your option) any later version.
Kay Sievers0228a7e2013-08-14 22:55:40 +020013
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 Pittb2ad12e2010-09-21 12:45:52 +020018#
Kay Sievers0228a7e2013-08-14 22:55:40 +020019# 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 Pittb2ad12e2010-09-21 12:45:52 +020021
22import re
23import sys
Martin Pitte8015e62015-01-20 20:50:35 +010024import os
25from glob import glob
Martin Pittb2ad12e2010-09-21 12:45:52 +020026
Zbigniew Jędrzejewski-Szmek6b97bf22017-11-22 12:42:28 +010027rules_files = sys.argv[1:]
28if not rules_files:
29 sys.exit('Specify files to test as arguments')
Martin Pittb2ad12e2010-09-21 12:45:52 +020030
Filipe Brandenburgerd4983472018-02-27 11:12:18 -080031quoted_string_re = r'"(?:[^\\"]|\\.)*"'
32no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|PROGRAM|RESULT|TEST)\s*(?:=|!)=\s*' + quoted_string_re + '$')
33args_tests = re.compile(r'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re + '$')
34no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$')
35args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*' + quoted_string_re + '$')
Filipe Brandenburgerc9715ff2018-02-27 13:11:07 -080036# Find comma-separated groups, but allow commas that are inside quoted strings.
37comma_separated_group_re = re.compile(r'(?:[^,"]|' + quoted_string_re + ')+')
Martin Pittb2ad12e2010-09-21 12:45:52 +020038
39result = 0
40buffer = ''
Martin Pitte8015e62015-01-20 20:50:35 +010041for path in rules_files:
Zbigniew Jędrzejewski-Szmek29563952017-11-22 12:29:46 +010042 print('# looking at {}'.format(path))
Martin Pittb2ad12e2010-09-21 12:45:52 +020043 lineno = 0
44 for line in open(path):
45 lineno += 1
46
47 # handle line continuation
48 if line.endswith('\\\n'):
49 buffer += line[:-2]
50 continue
51 else:
52 line = buffer + line
53 buffer = ''
54
55 # filter out comments and empty lines
56 line = line.strip()
57 if not line or line.startswith('#'):
58 continue
59
Franck Bui75a56cb2018-02-23 17:12:50 +010060 # Separator ',' is normally optional but we make it mandatory here as
61 # it generally improves the readability of the rules.
Filipe Brandenburgerc9715ff2018-02-27 13:11:07 -080062 for clause_match in comma_separated_group_re.finditer(line):
63 clause = clause_match.group().strip()
Martin Pittb2ad12e2010-09-21 12:45:52 +020064 if not (no_args_tests.match(clause) or args_tests.match(clause) or
65 no_args_assign.match(clause) or args_assign.match(clause)):
66
Zbigniew Jędrzejewski-Szmek29563952017-11-22 12:29:46 +010067 print('Invalid line {}:{}: {}'.format(path, lineno, line))
68 print(' clause:', clause)
69 print()
Martin Pittb2ad12e2010-09-21 12:45:52 +020070 result = 1
71 break
72
73sys.exit(result)