Coverage for procpath/cmd/query.py: 100%
35 statements
« prev ^ index » next coverage.py v6.5.0, created at 2025-04-05 18:56 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2025-04-05 18:56 +0000
1import json
2import string
3import tempfile
4import time
5from typing import List, Optional, TextIO, Tuple
7from .. import procfile, procrec, procret, proctree, utility
8from . import CommandError
11__all__ = 'run',
14def run(
15 procfile_list: List[str],
16 output_file: TextIO,
17 procfs: str,
18 procfs_target: str,
19 delimiter: Optional[str] = None,
20 indent: Optional[int] = None,
21 query: Optional[str] = None,
22 sql_query: Optional[str] = None,
23 environment: Optional[List[Tuple[str, str]]] = None,
24):
25 readers = {k: v for k, v in procfile.registry.items() if k in procfile_list} (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_jsonpath_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
26 forest = proctree.Forest(readers, procfs, procfs_target) (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_jsonpath_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
27 result = forest.get_roots() (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_jsonpath_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
29 if environment: (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_jsonpath_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
30 evaluated = utility.evaluate(environment) procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_with_envrionment
31 query = string.Template(query or '').safe_substitute(evaluated) procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_with_envrionment
32 sql_query = string.Template(sql_query or '').safe_substitute(evaluated) procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_with_envrionment
34 if query: (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_jsonpath_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
35 try: (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_jsonpath_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
36 result = proctree.query(result, query) (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_jsonpath_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
37 except proctree.JsonPathQueryError as ex: (empty)procpath.test.cmd.TestQueryCommand.test_query_jsonpath_syntax_error
38 raise CommandError(str(ex)) from ex (empty)procpath.test.cmd.TestQueryCommand.test_query_jsonpath_syntax_error
40 if sql_query: (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
41 with tempfile.NamedTemporaryFile() as f: procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
42 meta = utility.get_meta(procfile_list, procfs, procfs_target) procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
43 with procrec.SqliteStorage(f.name, procfile_list, meta) as store: procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
44 store.record(time.time(), proctree.flatten(result, procfile_list)) procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
45 try: procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
46 result = procret.query(f.name, procret.Query(sql_query, '')) procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_sql_syntax_errorprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
47 except procret.QueryExecutionError as ex: procpath.test.cmd.TestQueryCommand.test_query_sql_syntax_error
48 raise CommandError(f'SQL error: {ex}') from ex procpath.test.cmd.TestQueryCommand.test_query_sql_syntax_error
50 if delimiter: (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
51 result = delimiter.join(map(str, result)) procpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_output
52 else:
53 result = json.dumps(result, indent=indent, sort_keys=True, ensure_ascii=False) (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
55 if result: (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_no_new_line_on_empty_outputprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql
56 output_file.write(f'{result}\n') (empty)procpath.test.cmd.TestPlayCommand.test_play_queryprocpath.test.cmd.TestQueryCommand.test_query_delimitedprocpath.test.cmd.TestQueryCommand.test_query_no_query_root_outputprocpath.test.cmd.TestQueryCommand.test_query_only_sqlprocpath.test.cmd.TestQueryCommand.test_query_query_node_list_json_outputprocpath.test.cmd.TestQueryCommand.test_query_thread_targetprocpath.test.cmd.TestQueryCommand.test_query_with_envrionmentprocpath.test.cmd.TestQueryCommand.test_query_with_sql