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 | # |
| 7 | # This program is free software; you can redistribute it and/or modify it |
| 8 | # under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program 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 | # General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program; if not, write to the Free Software Foundation, |
| 19 | # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 20 | |
| 21 | import re |
| 22 | import sys |
| 23 | |
| 24 | if len(sys.argv) < 2: |
| 25 | print >> sys.stderr, 'Usage: %s <rules file> [...]' % sys.argv[0] |
| 26 | sys.exit(2) |
| 27 | |
| 28 | no_args_tests = re.compile('(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$') |
| 29 | 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] | 30 | no_args_assign = re.compile('(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|WAIT_FOR|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$') |
Kay Sievers | 83cd6b7 | 2012-04-09 16:37:54 +0200 | [diff] [blame] | 31 | 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] | 32 | |
| 33 | result = 0 |
| 34 | buffer = '' |
| 35 | for path in sys.argv[1:]: |
| 36 | lineno = 0 |
| 37 | for line in open(path): |
| 38 | lineno += 1 |
| 39 | |
| 40 | # handle line continuation |
| 41 | if line.endswith('\\\n'): |
| 42 | buffer += line[:-2] |
| 43 | continue |
| 44 | else: |
| 45 | line = buffer + line |
| 46 | buffer = '' |
| 47 | |
| 48 | # filter out comments and empty lines |
| 49 | line = line.strip() |
| 50 | if not line or line.startswith('#'): |
| 51 | continue |
| 52 | |
| 53 | for clause in line.split(','): |
| 54 | clause = clause.strip() |
| 55 | if not (no_args_tests.match(clause) or args_tests.match(clause) or |
| 56 | no_args_assign.match(clause) or args_assign.match(clause)): |
| 57 | |
Kay Sievers | be903bf | 2011-05-31 02:18:33 +0200 | [diff] [blame] | 58 | print('Invalid line %s:%i: %s' % (path, lineno, line)) |
| 59 | print(' clause:', clause) |
| 60 | print() |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 61 | result = 1 |
| 62 | break |
| 63 | |
| 64 | sys.exit(result) |