Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Simple udev rules syntax checker |
| 3 | # |
| 4 | # (C) 2010 Canonical Ltd. |
| 5 | # Author: Martin Pitt <martin.pitt@ubuntu.com> |
| 6 | # |
Kay Sievers | 0228a7e | 2013-08-14 22:55:40 +0200 | [diff] [blame] | 7 | # systemd is free software; you can redistribute it and/or modify it |
| 8 | # under the terms of the GNU Lesser General Public License as published by |
| 9 | # the Free Software Foundation; either version 2.1 of the License, or |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 10 | # (at your option) any later version. |
Kay Sievers | 0228a7e | 2013-08-14 22:55:40 +0200 | [diff] [blame] | 11 | |
| 12 | # systemd is distributed in the hope that it will be useful, but |
| 13 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | # Lesser General Public License for more details. |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 16 | # |
Kay Sievers | 0228a7e | 2013-08-14 22:55:40 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU Lesser General Public License |
| 18 | # along with systemd; If not, see <http://www.gnu.org/licenses/>. |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 19 | |
| 20 | import re |
| 21 | import sys |
| 22 | |
| 23 | if len(sys.argv) < 2: |
| 24 | print >> sys.stderr, 'Usage: %s <rules file> [...]' % sys.argv[0] |
| 25 | sys.exit(2) |
| 26 | |
| 27 | no_args_tests = re.compile('(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$') |
| 28 | 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] | 29 | 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] | 30 | 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] | 31 | |
| 32 | result = 0 |
| 33 | buffer = '' |
| 34 | for path in sys.argv[1:]: |
| 35 | lineno = 0 |
| 36 | for line in open(path): |
| 37 | lineno += 1 |
| 38 | |
| 39 | # handle line continuation |
| 40 | if line.endswith('\\\n'): |
| 41 | buffer += line[:-2] |
| 42 | continue |
| 43 | else: |
| 44 | line = buffer + line |
| 45 | buffer = '' |
| 46 | |
| 47 | # filter out comments and empty lines |
| 48 | line = line.strip() |
| 49 | if not line or line.startswith('#'): |
| 50 | continue |
| 51 | |
| 52 | for clause in line.split(','): |
| 53 | clause = clause.strip() |
| 54 | if not (no_args_tests.match(clause) or args_tests.match(clause) or |
| 55 | no_args_assign.match(clause) or args_assign.match(clause)): |
| 56 | |
Kay Sievers | be903bf | 2011-05-31 02:18:33 +0200 | [diff] [blame] | 57 | print('Invalid line %s:%i: %s' % (path, lineno, line)) |
| 58 | print(' clause:', clause) |
| 59 | print() |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 60 | result = 1 |
| 61 | break |
| 62 | |
| 63 | sys.exit(result) |