Felipe Sateler | b95f552 | 2016-08-21 20:25:37 -0300 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 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 |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 22 | import os |
| 23 | from glob import glob |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 24 | |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 25 | if len(sys.argv) > 1: |
| 26 | # explicit rule file list |
| 27 | rules_files = sys.argv[1:] |
| 28 | else: |
| 29 | # take them from the build dir |
| 30 | root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 31 | rules_dir = os.path.join(os.environ.get('top_srcdir', root_dir), 'rules') |
| 32 | if not os.path.isdir(rules_dir): |
| 33 | sys.stderr.write('No rules files given, and %s does not exist, aborting' % rules_dir) |
| 34 | sys.exit(2) |
| 35 | rules_files = glob(os.path.join(rules_dir, '*.rules')) |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 36 | |
Zbigniew Jędrzejewski-Szmek | cda3997 | 2016-12-01 18:29:54 -0500 | [diff] [blame] | 37 | no_args_tests = re.compile(r'(ACTION|DEVPATH|KERNELS?|NAME|SYMLINK|SUBSYSTEMS?|DRIVERS?|TAG|RESULT|TEST)\s*(?:=|!)=\s*"([^"]*)"$') |
| 38 | args_tests = re.compile(r'(ATTRS?|ENV|TEST){([a-zA-Z0-9/_.*%-]+)}\s*(?:=|!)=\s*"([^"]*)"$') |
| 39 | no_args_assign = re.compile(r'(NAME|SYMLINK|OWNER|GROUP|MODE|TAG|PROGRAM|RUN|LABEL|GOTO|OPTIONS|IMPORT)\s*(?:\+=|:=|=)\s*"([^"]*)"$') |
| 40 | args_assign = re.compile(r'(ATTR|ENV|IMPORT|RUN){([a-zA-Z0-9/_.*%-]+)}\s*(=|\+=)\s*"([^"]*)"$') |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 41 | |
| 42 | result = 0 |
| 43 | buffer = '' |
Martin Pitt | e8015e6 | 2015-01-20 20:50:35 +0100 | [diff] [blame] | 44 | for path in rules_files: |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 45 | lineno = 0 |
| 46 | for line in open(path): |
| 47 | lineno += 1 |
| 48 | |
| 49 | # handle line continuation |
| 50 | if line.endswith('\\\n'): |
| 51 | buffer += line[:-2] |
| 52 | continue |
| 53 | else: |
| 54 | line = buffer + line |
| 55 | buffer = '' |
| 56 | |
| 57 | # filter out comments and empty lines |
| 58 | line = line.strip() |
| 59 | if not line or line.startswith('#'): |
| 60 | continue |
| 61 | |
| 62 | for clause in line.split(','): |
| 63 | clause = clause.strip() |
| 64 | if not (no_args_tests.match(clause) or args_tests.match(clause) or |
| 65 | no_args_assign.match(clause) or args_assign.match(clause)): |
| 66 | |
Kay Sievers | be903bf | 2011-05-31 02:18:33 +0200 | [diff] [blame] | 67 | print('Invalid line %s:%i: %s' % (path, lineno, line)) |
Martin Pitt | c6be83c | 2015-01-20 12:18:40 +0100 | [diff] [blame] | 68 | print(' clause: %s' % clause) |
| 69 | print('') |
Martin Pitt | b2ad12e | 2010-09-21 12:45:52 +0200 | [diff] [blame] | 70 | result = 1 |
| 71 | break |
| 72 | |
| 73 | sys.exit(result) |