]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/rule-syntax-check.py
network: make Link and NetDev always have the valid poiter to Manager
[thirdparty/systemd.git] / test / rule-syntax-check.py
CommitLineData
3e67e5c9 1#!/usr/bin/env python3
35df7443
ZJS
2# SPDX-License-Identifier: LGPL-2.1+
3#
b2ad12eb
MP
4# Simple udev rules syntax checker
5#
810adae9 6# © 2010 Canonical Ltd.
b2ad12eb 7# Author: Martin Pitt <martin.pitt@ubuntu.com>
b2ad12eb
MP
8
9import re
10import sys
e8015e6e
MP
11import os
12from glob import glob
b2ad12eb 13
6b97bf22
ZJS
14rules_files = sys.argv[1:]
15if not rules_files:
16 sys.exit('Specify files to test as arguments')
b2ad12eb 17
d498347a
FB
18quoted_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 + '$')
c9715ffc 23# Find comma-separated groups, but allow commas that are inside quoted strings.
27e2779b
FB
24# 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 + '?)+')
b2ad12eb
MP
27
28result = 0
29buffer = ''
e8015e6e 30for path in rules_files:
2956395c 31 print('# looking at {}'.format(path))
b2ad12eb
MP
32 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
75a56cb6
FB
49 # Separator ',' is normally optional but we make it mandatory here as
50 # it generally improves the readability of the rules.
c9715ffc
FB
51 for clause_match in comma_separated_group_re.finditer(line):
52 clause = clause_match.group().strip()
b2ad12eb
MP
53 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
2956395c
ZJS
56 print('Invalid line {}:{}: {}'.format(path, lineno, line))
57 print(' clause:', clause)
58 print()
b2ad12eb
MP
59 result = 1
60 break
61
62sys.exit(result)