From 957f3c4554728dbe9b520e7bb16b2c2a82addcce Mon Sep 17 00:00:00 2001 From: Andrew Krasichkov Date: Fri, 28 Apr 2017 18:56:34 +0300 Subject: [PATCH 1/2] Remove unnecessary check --- gixy/cli/main.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/gixy/cli/main.py b/gixy/cli/main.py index 09b5fd2..ecef89c 100644 --- a/gixy/cli/main.py +++ b/gixy/cli/main.py @@ -92,11 +92,6 @@ def _get_cli_parser(): return parser -def _is_nginx_file(file_path): - s = open(file_path).read() - return 'server {' in s or 'http {' in s - - def main(): parser = _get_cli_parser() args = parser.parse_args() @@ -108,10 +103,6 @@ def main(): parser.print_help() sys.exit(1) - if not _is_nginx_file(path): - sys.stderr.write('This is nginx config? Rly?\n') - sys.exit(1) - try: severity = gixy.severity.ALL[args.level] except IndexError: From c63435a760fd1698ecd0227d9d9580c9530c2fd0 Mon Sep 17 00:00:00 2001 From: Andrew Krasichkov Date: Fri, 28 Apr 2017 18:58:01 +0300 Subject: [PATCH 2/2] Nginx parser now returns original root if can't parse a config --- gixy/parser/nginx_parser.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/gixy/parser/nginx_parser.py b/gixy/parser/nginx_parser.py index a3e28ba..7922384 100644 --- a/gixy/parser/nginx_parser.py +++ b/gixy/parser/nginx_parser.py @@ -28,8 +28,14 @@ def parse(self, file_path, root=None): if not root: root = block.Root() - parser = raw_parser.RawParser() - parsed = parser.parse(file_path) + + try: + parser = raw_parser.RawParser() + parsed = parser.parse(file_path) + except ParseException as e: + LOG.error('Failed to parse config "{file}": {error}'.format(file=file_path, error=str(e))) + return root + if len(parsed) and parsed[0].getName() == 'file_delimiter': # Were parse nginx dump LOG.info('Switched to parse nginx configuration dump.') @@ -104,10 +110,7 @@ def _resolve_file_include(self, pattern, parent): for file_path in glob.iglob(path): include = block.IncludeBlock('include', [file_path]) parent.append(include) - try: - self.parse(file_path, include) - except ParseException as e: - LOG.error('Failed to parse include "{file}": {error}'.format(file=file_path, error=str(e))) + self.parse(file_path, include) if not file_path: LOG.warning("File not found: {}".format(path)) @@ -120,10 +123,7 @@ def _resolve_dump_include(self, pattern, parent): founded = True include = block.IncludeBlock('include', [file_path]) parent.append(include) - try: - self.parse_block(parsed, include) - except ParseException as e: - LOG.error('Failed to parse include "{file}": {error}'.format(file=file_path, error=str(e))) + self.parse_block(parsed, include) if not founded: LOG.warning("File not found: {}".format(path))