rule-syntax-check.py revision 23757887f768b0b7339239cc98aee879d9f4d87f
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# Simple udev rules syntax checker
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# (C) 2010 Canonical Ltd.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# Author: Martin Pitt <martin.pitt@ubuntu.com>
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# This program is free software; you can redistribute it and/or modify
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# it under the terms of the GNU General Public License as published by
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# the Free Software Foundation; either version 2 of the License, or
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# (at your option) any later version.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# This program is distributed in the hope that it will be useful,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# but WITHOUT ANY WARRANTY; without even the implied warranty of
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# GNU General Public License for more details.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# You should have received a copy of the GNU General Public License along
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# with this program; if not, write to the Free Software Foundation, Inc.,
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen print >> sys.stderr, 'Usage: %s <rules file> [...]' % sys.argv[0]
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenno_args_tests = re.compile('(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$')
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenargs_tests = re.compile('(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$')
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenno_args_assign = re.compile('(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|WAIT_FOR|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$')
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenargs_assign = re.compile('(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*"([^"]*)"$')
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen # handle line continuation
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen # filter out comments and empty lines
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if not line or line.startswith('#'):
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen for clause in line.split(','):
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if not (no_args_tests.match(clause) or args_tests.match(clause) or
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen no_args_assign.match(clause) or args_assign.match(clause)):
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen print('Invalid line %s:%i: %s' % (path, lineno, line))