blob: 4391a1fedacc75b5950fad98413c63a5d09d43ff [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>
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 + '$')
20args_tests = re.compile(r'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*' + quoted_string_re + '$')
21no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*' + quoted_string_re + '$')
22args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*' + quoted_string_re + '$')
Filipe Brandenburgerc9715ff2018-02-27 13:11:07 -080023# Find comma-separated groups, but allow commas that are inside quoted strings.
Filipe Brandenburger27e27792018-02-27 16:11:38 -080024# Using quoted_string_re + '?' so that strings missing the last double quote
25# will still match for this part that splits on commas.
26comma_separated_group_re = re.compile(r'(?:[^,"]|' + quoted_string_re + '?)+')
Martin Pittb2ad12e2010-09-21 12:45:52 +020027
28result = 0
29buffer = ''
Martin Pitte8015e62015-01-20 20:50:35 +010030for path in rules_files:
Zbigniew Jędrzejewski-Szmek29563952017-11-22 12:29:46 +010031 print('# looking at {}'.format(path))
Martin Pittb2ad12e2010-09-21 12:45:52 +020032 lineno = 0
33 for line in open(path):
34 lineno += 1
35
36 # handle line continuation
37 if line.endswith('\\\n'):
38 buffer += line[:-2]
39 continue
40 else:
41 line = buffer + line
42 buffer = ''
43
44 # filter out comments and empty lines
45 line = line.strip()
46 if not line or line.startswith('#'):
47 continue
48
Franck Bui75a56cb2018-02-23 17:12:50 +010049 # Separator ',' is normally optional but we make it mandatory here as
50 # it generally improves the readability of the rules.
Filipe Brandenburgerc9715ff2018-02-27 13:11:07 -080051 for clause_match in comma_separated_group_re.finditer(line):
52 clause = clause_match.group().strip()
Martin Pittb2ad12e2010-09-21 12:45:52 +020053 if not (no_args_tests.match(clause) or args_tests.match(clause) or
54 no_args_assign.match(clause) or args_assign.match(clause)):
55
Zbigniew Jędrzejewski-Szmek29563952017-11-22 12:29:46 +010056 print('Invalid line {}:{}: {}'.format(path, lineno, line))
57 print(' clause:', clause)
58 print()
Martin Pittb2ad12e2010-09-21 12:45:52 +020059 result = 1
60 break
61
62sys.exit(result)