Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 1 | # Simple udev rules syntax checker |
| 2 | # |
| 3 | # (C) 2010 Canonical Ltd. |
| 4 | # Author: Martin Pitt <martin.pitt@ubuntu.com> |
| 5 | # |
Kay Sievers | 0228a7e | 2013-08-14 22:55:40 +0200 | [diff] [blame] | 6 | # 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 Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 9 | # (at your option) any later version. |
Kay Sievers | 0228a7e | 2013-08-14 22:55:40 +0200 | [diff] [blame] | 10 | |
| 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 Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 15 | # |
Kay Sievers | 0228a7e | 2013-08-14 22:55:40 +0200 | [diff] [blame] | 16 | # 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 Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 18 | |
| 19 | import re |
| 20 | import sys |
| 21 | |
| 22 | if len(sys.argv) < 2: |
Martin Pitt | c6be83c | 2015-01-20 12:18:40 +0100 | [diff] [blame] | 23 | sys.stderr.write('Usage: %s <rules file> [...]\n' % sys.argv[0]) |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 24 | sys.exit(2) |
| 25 | |
| 26 | no_args_tests = re.compile('(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$') |
| 27 | args_tests = re.compile('(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$') |
Kay Sievers | 0f52fde | 2012-02-20 00:41:58 +0100 | [diff] [blame] | 28 | no_args_assign = re.compile('(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|WAIT_FOR|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$') |
Kay Sievers | 5488ac8 | 2012-10-09 00:49:15 +0200 | [diff] [blame] | 29 | args_assign = re.compile('(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*"([^"]*)"$') |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 30 | |
| 31 | result = 0 |
| 32 | buffer = '' |
| 33 | for path in sys.argv[1:]: |
| 34 | 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 | |
| 51 | for clause in line.split(','): |
| 52 | clause = clause.strip() |
| 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 | |
Kay Sievers | be903bf | 2011-05-31 02:18:33 +0200 | [diff] [blame] | 56 | print('Invalid line %s:%i: %s' % (path, lineno, line)) |
Martin Pitt | c6be83c | 2015-01-20 12:18:40 +0100 | [diff] [blame] | 57 | print(' clause: %s' % clause) |
| 58 | print('') |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 59 | result = 1 |
| 60 | break |
| 61 | |
| 62 | sys.exit(result) |