blob: 9a9e4d1658542e2e5cc538a391013c4af9d3932b [file] [log] [blame]
Jörg Thalheim3e67e5c2017-05-01 02:26:56 +02001#!/usr/bin/env python3
Yu Watanabedb9ecf02020-11-09 13:23:58 +09002# SPDX-License-Identifier: LGPL-2.1-or-later
Zbigniew Jędrzejewski-Szmek35df7442017-11-18 17:32:46 +01003#
Martin Pittb2ad12e2010-09-21 12:45:52 +02004# Simple udev rules syntax checker
5#
Lennart Poettering810adae2018-06-12 17:15:23 +02006# © 2010 Canonical Ltd.
Martin Pittb2ad12e2010-09-21 12:45:52 +02007# Author: Martin Pitt <martin.pitt@ubuntu.com>
Martin Pittb2ad12e2010-09-21 12:45:52 +02008
9import re
10import sys
Martin Pitte8015e62015-01-20 20:50:35 +010011import os
12from glob import glob
Martin Pittb2ad12e2010-09-21 12:45:52 +020013
Zbigniew Jędrzejewski-Szmek6b97bf22017-11-22 12:42:28 +010014rules_files = sys.argv[1:]
15if not rules_files:
16 sys.exit('Specify files to test as arguments')
Martin Pittb2ad12e2010-09-21 12:45:52 +020017
Filipe Brandenburgerd4983472018-02-27 11:12:18 -080018quoted_string_re = r'"(?:[^\\"]|\\.)*"'
19no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|PROGRAM|RESULT|TEST)\s*(?:=|!)=\s*' + quoted_string_re + '$')
Jan Synaceked2dc502019-01-30 12:22:41 +010020# PROGRAM can also be specified as an assignment.
21program_assign = re.compile(r'PROGRAM\s*=\s*' + quoted_string_re + '$')
Jan Synaceka7ab6b72019-10-17 10:52:38 +020022args_tests = re.compile(r'(ATTRS?|ENV|CONST|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re + '$')
Filipe Brandenburgerd4983472018-02-27 11:12:18 -080023no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$')
24args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*' + quoted_string_re + '$')
Filipe Brandenburgerc9715ff2018-02-27 13:11:07 -080025# Find comma-separated groups, but allow commas that are inside quoted strings.
Filipe Brandenburger27e27792018-02-27 16:11:38 -080026# Using quoted_string_re + '?' so that strings missing the last double quote
27# will still match for this part that splits on commas.
28comma_separated_group_re = re.compile(r'(?:[^,"]|' + quoted_string_re + '?)+')
Martin Pittb2ad12e2010-09-21 12:45:52 +020029
30result = 0
31buffer = ''
Martin Pitte8015e62015-01-20 20:50:35 +010032for path in rules_files:
Zbigniew Jędrzejewski-Szmek29563952017-11-22 12:29:46 +010033 print('# looking at {}'.format(path))
Martin Pittb2ad12e2010-09-21 12:45:52 +020034 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 Bui75a56cb2018-02-23 17:12:50 +010051 # Separator ',' is normally optional but we make it mandatory here as
52 # it generally improves the readability of the rules.
Filipe Brandenburgerc9715ff2018-02-27 13:11:07 -080053 for clause_match in comma_separated_group_re.finditer(line):
54 clause = clause_match.group().strip()
Martin Pittb2ad12e2010-09-21 12:45:52 +020055 if not (no_args_tests.match(clause) or args_tests.match(clause) or
Jan Synaceked2dc502019-01-30 12:22:41 +010056 no_args_assign.match(clause) or args_assign.match(clause) or
57 program_assign.match(clause)):
Martin Pittb2ad12e2010-09-21 12:45:52 +020058
Zbigniew Jędrzejewski-Szmek29563952017-11-22 12:29:46 +010059 print('Invalid line {}:{}: {}'.format(path, lineno, line))
60 print(' clause:', clause)
61 print()
Martin Pittb2ad12e2010-09-21 12:45:52 +020062 result = 1
63 break
64
65sys.exit(result)