33import logging
44import os
55import pathlib as pl
6+ import typing as tp
67
78from cardano_clusterlib import clusterlib
9+ from cardano_clusterlib import consts
810from cardano_clusterlib import transaction_group
911
1012LOGGER = logging .getLogger (__name__ )
1113
1214
15+ def record_cli_coverage (cli_args : list [str ], coverage_dict : dict ) -> None :
16+ """Record coverage info for CLI commands.
17+
18+ Args:
19+ cli_args: A list of command and it's arguments.
20+ coverage_dict: A dictionary with coverage info.
21+ """
22+ parent_dict = coverage_dict
23+ prev_arg = ""
24+ for arg in cli_args :
25+ # If the current argument is a subcommand marker, record it and skip it
26+ if arg == consts .SUBCOMMAND_MARK :
27+ prev_arg = arg
28+ continue
29+
30+ # If the current argument is a parameter to an option, skip it
31+ if prev_arg .startswith ("--" ) and not arg .startswith ("--" ):
32+ continue
33+
34+ prev_arg = arg
35+
36+ cur_dict = parent_dict .get (arg )
37+ # Initialize record if it doesn't exist yet
38+ if not cur_dict :
39+ parent_dict [arg ] = {"_count" : 0 }
40+ cur_dict = parent_dict [arg ]
41+
42+ # Increment count
43+ cur_dict ["_count" ] += 1
44+
45+ # Set new parent dict
46+ if not arg .startswith ("--" ):
47+ parent_dict = cur_dict
48+
49+
1350def create_submitted_file (tx_file : clusterlib .FileType ) -> None :
1451 """Create a `.submitted` status file when the Tx was successfully submitted."""
1552 tx_path = pl .Path (tx_file )
@@ -23,13 +60,53 @@ def create_submitted_file(tx_file: clusterlib.FileType) -> None:
2360
2461
2562class ClusterLib (clusterlib .ClusterLib ):
63+ def __init__ (
64+ self ,
65+ state_dir : clusterlib .FileType ,
66+ slots_offset : int | None = None ,
67+ socket_path : clusterlib .FileType = "" ,
68+ command_era : str = consts .CommandEras .LATEST ,
69+ ):
70+ super ().__init__ (
71+ state_dir = state_dir ,
72+ slots_offset = slots_offset ,
73+ socket_path = socket_path ,
74+ command_era = command_era ,
75+ )
76+ self .cli_coverage : dict [str , tp .Any ] = {}
77+
2678 @property
2779 def g_transaction (self ) -> transaction_group .TransactionGroup :
2880 """Transaction group."""
2981 if not self ._transaction_group :
3082 self ._transaction_group = TransactionGroup (clusterlib_obj = self )
3183 return self ._transaction_group
3284
85+ def cli (
86+ self ,
87+ cli_args : list [str ],
88+ timeout : float | None = None ,
89+ add_default_args : bool = True ,
90+ ) -> clusterlib .CLIOut :
91+ """Run the `cardano-cli` command.
92+
93+ Args:
94+ cli_args: A list of arguments for cardano-cli.
95+ timeout: A timeout for the command, in seconds (optional).
96+ add_default_args: Whether to add default arguments to the command (optional).
97+
98+ Returns:
99+ clusterlib.CLIOut: A data container containing command stdout and stderr.
100+ """
101+ cli_args_strs_all = [str (arg ) for arg in cli_args ]
102+ if add_default_args :
103+ cli_args_strs_all .insert (0 , "cardano-cli" )
104+ cli_args_strs_all .insert (1 , self .command_era )
105+
106+ record_cli_coverage (cli_args = cli_args_strs_all , coverage_dict = self .cli_coverage )
107+
108+ return super ().cli (cli_args = cli_args , timeout = timeout , add_default_args = add_default_args )
109+
33110
34111class TransactionGroup (transaction_group .TransactionGroup ):
35112 def submit_tx (
0 commit comments