rule-syntax-check.py revision 0b87a07761dcad31583142a3a454c42833e65a09
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#!/usr/bin/python
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# Simple udev rules syntax checker
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# (C) 2010 Canonical Ltd.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# Author: Martin Pitt <martin.pitt@ubuntu.com>
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# This program is free software; you can redistribute it and/or modify it
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# under the terms of the GNU General Public License as published by
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# the Free Software Foundation; either version 2 of the License, or
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# (at your option) any later version.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# This program is distributed in the hope that it will be useful, but
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# WITHOUT ANY WARRANTY; without even the implied warranty of
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# General Public License for more details.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# You should have received a copy of the GNU General Public License
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# along with this program; if not, write to the Free Software Foundation,
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringimport re
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringimport sys
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringif len(sys.argv) < 2:
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering print >> sys.stderr, 'Usage: %s <rules file> [...]' % sys.argv[0]
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering sys.exit(2)
0591220f339c313761f9a208e88fb719db566993Zbigniew Jędrzejewski-Szmek
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poetteringno_args_tests = re.compile('(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$')
6482f6269c87d2249e52e889a63adbdd50f2d691Ronny Chevalierargs_tests = re.compile('(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$')
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringno_args_assign = re.compile('(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|WAIT_FOR|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$')
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringargs_assign = re.compile('(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*=\s*"([^"]*)"$')
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringresult = 0
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringbuffer = ''
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringfor path in sys.argv[1:]:
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering lineno = 0
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering for line in open(path):
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering lineno += 1
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering # handle line continuation
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if line.endswith('\\\n'):
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering buffer += line[:-2]
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering continue
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering else:
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering line = buffer + line
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering buffer = ''
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering # filter out comments and empty lines
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering line = line.strip()
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering if not line or line.startswith('#'):
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering continue
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering for clause in line.split(','):
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering clause = clause.strip()
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering if not (no_args_tests.match(clause) or args_tests.match(clause) or
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering no_args_assign.match(clause) or args_assign.match(clause)):
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering print('Invalid line %s:%i: %s' % (path, lineno, line))
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering print(' clause:', clause)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering print()
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering result = 1
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering break
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringsys.exit(result)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering