| 
									
										
										
										
											2021-01-05 11:24:39 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  | import io | 
					
						
							| 
									
										
										
										
											2020-12-30 15:24:29 +01:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2021-01-05 11:24:39 +01:00
										 |  |  | import argparse | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import searx.search | 
					
						
							|  |  |  | import searx.search.checker | 
					
						
							| 
									
										
										
										
											2021-01-05 11:24:39 +01:00
										 |  |  | from searx.search import processors | 
					
						
							|  |  |  | from searx.engines import engine_shortcuts | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  | # configure logging | 
					
						
							|  |  |  | root = logging.getLogger() | 
					
						
							|  |  |  | handler = logging.StreamHandler(sys.stdout) | 
					
						
							|  |  |  | for h in root.handlers: | 
					
						
							|  |  |  |     root.removeHandler(h) | 
					
						
							|  |  |  | root.addHandler(handler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # color only for a valid terminal | 
					
						
							| 
									
										
										
										
											2020-12-30 15:24:29 +01:00
										 |  |  | if sys.stdout.isatty() and os.environ.get('TERM') not in ['dumb', 'unknown']: | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  |     RESET_SEQ = "\033[0m" | 
					
						
							|  |  |  |     COLOR_SEQ = "\033[1;%dm" | 
					
						
							|  |  |  |     BOLD_SEQ = "\033[1m" | 
					
						
							|  |  |  |     BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = map(lambda i: COLOR_SEQ % (30 + i), range(8)) | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     RESET_SEQ = "" | 
					
						
							|  |  |  |     COLOR_SEQ = "" | 
					
						
							|  |  |  |     BOLD_SEQ = "" | 
					
						
							|  |  |  |     BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = "", "", "", "", "", "", "", "" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  | # equivalent of 'python -u' (unbuffered stdout, stderr) | 
					
						
							|  |  |  | stdout = io.TextIOWrapper(open(sys.stdout.fileno(), 'wb', 0), write_through=True) | 
					
						
							|  |  |  | stderr = io.TextIOWrapper(open(sys.stderr.fileno(), 'wb', 0), write_through=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  | # iterator of processors | 
					
						
							| 
									
										
										
										
											2021-01-05 11:24:39 +01:00
										 |  |  | def iter_processor(engine_name_list): | 
					
						
							|  |  |  |     if len(engine_name_list) > 0: | 
					
						
							|  |  |  |         for name in engine_name_list: | 
					
						
							|  |  |  |             name = engine_shortcuts.get(name, name) | 
					
						
							|  |  |  |             processor = processors.get(name) | 
					
						
							|  |  |  |             if processor is not None: | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  |                 yield name, processor | 
					
						
							| 
									
										
										
										
											2021-01-05 11:24:39 +01:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  |                 stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}{RED}Engine does not exist{RESET_SEQ}') | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  |     else: | 
					
						
							|  |  |  |         for name, processor in searx.search.processors.items(): | 
					
						
							|  |  |  |             yield name, processor | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  | # actual check & display | 
					
						
							|  |  |  | def run(engine_name_list, verbose): | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  |     searx.search.initialize() | 
					
						
							| 
									
										
										
										
											2021-01-05 11:24:39 +01:00
										 |  |  |     for name, processor in iter_processor(engine_name_list): | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  |         stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}Checking\n') | 
					
						
							|  |  |  |         if not sys.stdout.isatty(): | 
					
						
							|  |  |  |             stderr.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}Checking\n') | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  |         checker = searx.search.checker.Checker(processor) | 
					
						
							|  |  |  |         checker.run() | 
					
						
							|  |  |  |         if checker.test_results.succesfull: | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  |             stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}{GREEN}OK{RESET_SEQ}\n') | 
					
						
							|  |  |  |             if verbose: | 
					
						
							|  |  |  |                 stdout.write(f'    {"found languages":15}: {" ".join(sorted(list(checker.test_results.languages)))}\n') | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  |             stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}{RESET_SEQ}{RED}Error{RESET_SEQ}') | 
					
						
							|  |  |  |             if not verbose: | 
					
						
							|  |  |  |                 errors = [test_name + ': ' + error for test_name, error in checker.test_results] | 
					
						
							|  |  |  |                 stdout.write(f'{RED}Error {str(errors)}{RESET_SEQ}\n') | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 stdout.write('\n') | 
					
						
							|  |  |  |                 stdout.write(f'    {"found languages":15}: {" ".join(sorted(list(checker.test_results.languages)))}\n') | 
					
						
							|  |  |  |                 for test_name, logs in checker.test_results.logs.items(): | 
					
						
							|  |  |  |                     for log in logs: | 
					
						
							| 
									
										
										
										
											2021-01-19 21:29:31 +01:00
										 |  |  |                         log = map(lambda l: l if isinstance(l, str) else repr(l), log) | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  |                         stdout.write(f'    {test_name:15}: {RED}{" ".join(log)}{RESET_SEQ}\n') | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  | # call by setup.py | 
					
						
							| 
									
										
										
										
											2021-01-05 11:24:39 +01:00
										 |  |  | def main(): | 
					
						
							|  |  |  |     parser = argparse.ArgumentParser(description='Check searx engines.') | 
					
						
							|  |  |  |     parser.add_argument('engine_name_list', metavar='engine name', type=str, nargs='*', | 
					
						
							|  |  |  |                         help='engines name or shortcut list. Empty for all engines.') | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  |     parser.add_argument('--verbose', '-v', | 
					
						
							|  |  |  |                         action='store_true', dest='verbose', | 
					
						
							|  |  |  |                         help='Display details about the test results', | 
					
						
							|  |  |  |                         default=False) | 
					
						
							| 
									
										
										
										
											2021-01-05 11:24:39 +01:00
										 |  |  |     args = parser.parse_args() | 
					
						
							| 
									
										
										
										
											2021-01-08 19:04:04 +01:00
										 |  |  |     run(args.engine_name_list, args.verbose) | 
					
						
							| 
									
										
										
										
											2021-01-05 11:24:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-24 09:28:16 +01:00
										 |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     main() |