xref: /openbmc/openbmc-test-automation/extended/run_keyword.py (revision 20f38712b324e61a94e174017c487a0af4b373e1)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2a2a553c5SMichael Walsh
3a2a553c5SMichael Walshr"""
4a2a553c5SMichael WalshThis module is the python counterpart to run_keyword.robot.
5a2a553c5SMichael Walsh"""
6a2a553c5SMichael Walsh
7e635ddc0SGeorge Keishingimport re
85731818dSPatrick Williams
9*20f38712SPatrick Williamsimport gen_print as gp
10*20f38712SPatrick Williamsimport gen_robot_utils as gru
11*20f38712SPatrick Williamsimport gen_robot_valid as grv
12*20f38712SPatrick Williamsfrom robot.libraries.BuiltIn import BuiltIn
13*20f38712SPatrick Williams
14a2a553c5SMichael Walsh
15a2a553c5SMichael Walshdef setup():
16a2a553c5SMichael Walsh    r"""
17a2a553c5SMichael Walsh    Do general program setup tasks.
18a2a553c5SMichael Walsh    """
19a2a553c5SMichael Walsh
20edb5c949SMichael Walsh    gp.qprintn()
21a2a553c5SMichael Walsh
22a2a553c5SMichael Walsh    validate_parms()
23a2a553c5SMichael Walsh
24edb5c949SMichael Walsh    gp.qprint_pgm_header()
25a2a553c5SMichael Walsh
26a2a553c5SMichael Walsh
27a2a553c5SMichael Walshdef validate_parms():
28a2a553c5SMichael Walsh    r"""
29a2a553c5SMichael Walsh    Validate all program parameters.
30a2a553c5SMichael Walsh    """
31a2a553c5SMichael Walsh
32e7edb22cSMichael Walsh    grv.valid_value("keyword_string")
33a2a553c5SMichael Walsh
34a2a553c5SMichael Walsh    return True
35a2a553c5SMichael Walsh
36a2a553c5SMichael Walsh
37a2a553c5SMichael Walshdef program_teardown():
38a2a553c5SMichael Walsh    r"""
39a2a553c5SMichael Walsh    Clean up after this program.
40a2a553c5SMichael Walsh    """
41a2a553c5SMichael Walsh
42edb5c949SMichael Walsh    gp.qprint_pgm_footer()
43a2a553c5SMichael Walsh
44e9d78c46SMichael Walsh
45*20f38712SPatrick Williamsdef my_run_keywords(lib_file_path, keyword_string, quiet=0, test_mode=0):
46e9d78c46SMichael Walsh    r"""
47e9d78c46SMichael Walsh    Run the keywords in the keyword string.
48e9d78c46SMichael Walsh
49e9d78c46SMichael Walsh    Description of arguments:
50e9d78c46SMichael Walsh    lib_file_path   The path to a library or resource needed to run the
51e9d78c46SMichael Walsh                    keywords.  This may contain a colon-delimited list of
52e9d78c46SMichael Walsh                    library/resource paths.
53e9d78c46SMichael Walsh    keyword_string  The keyword string to be run by this function.  If this
54e9d78c46SMichael Walsh                    keyword string contains " ; " anywhere, it will be taken to
55e9d78c46SMichael Walsh                    be multiple keyword strings.  Each keyword may also include
56e9d78c46SMichael Walsh                    a variable assignment.  Example:
57e9d78c46SMichael Walsh                    ${my_var}=  My Keyword
58e9d78c46SMichael Walsh    quiet           If this parameter is set to "1", this program will print
59e9d78c46SMichael Walsh                    only essential information, i.e. it will not echo
60e9d78c46SMichael Walsh                    parameters, echo commands, print the total run time, etc.
61e9d78c46SMichael Walsh    test_mode       This means that this program should go through all the
62e9d78c46SMichael Walsh                    motions but not actually do anything substantial.
63e9d78c46SMichael Walsh    """
64e9d78c46SMichael Walsh
65e9d78c46SMichael Walsh    # NOTE: During code review the following question was raised: Why support
66e9d78c46SMichael Walsh    # 1) variable assignments 2) multiple keywords?  Couldn't a user simply
67e9d78c46SMichael Walsh    # call this program twice to get what they need.  If necessary, the user
68e9d78c46SMichael Walsh    # could take the output of the first call and specify it as a literal on
69e9d78c46SMichael Walsh    # the second call.
70e9d78c46SMichael Walsh    #
71e9d78c46SMichael Walsh    # However, this approach would not work in all cases.  The following case
72e9d78c46SMichael Walsh    # would be such an example:
73e9d78c46SMichael Walsh    # Let's say the first keyword string is as follows:
74e9d78c46SMichael Walsh    # Create Dictionary  foo=bar
75e9d78c46SMichael Walsh    # You wish to take the output of that call and specify it as a literal
76e9d78c46SMichael Walsh    # value when running the following:
77e9d78c46SMichael Walsh    # Want Dictionary  parm=<literal dictionary specification>
78e9d78c46SMichael Walsh    # The problem is that there is no way to specify a dictionary as a
79e9d78c46SMichael Walsh    # literal in Robot Framework.
80e9d78c46SMichael Walsh    # By having this program support variable assignments and multiple
81e9d78c46SMichael Walsh    # keywords, the user can invoke it with the following keyword string.
82e9d78c46SMichael Walsh    # ${my_dict}=  Create Dictionary  foo=bar ; Want Dictionary  ${my_dict}
83e9d78c46SMichael Walsh
84e9d78c46SMichael Walsh    # The user can pass multiple lib/resource paths by separating them with a
85e9d78c46SMichael Walsh    # colon.
86e9d78c46SMichael Walsh    lib_file_path_list = lib_file_path.split(":")
87e9d78c46SMichael Walsh    # Get rid of empty entry if it exists.
88e9d78c46SMichael Walsh    if lib_file_path_list[0] == "":
89e9d78c46SMichael Walsh        del lib_file_path_list[0]
90e9d78c46SMichael Walsh    for lib_file_path in lib_file_path_list:
91e9d78c46SMichael Walsh        if lib_file_path.endswith(".py"):
92*20f38712SPatrick Williams            gp.dprint_issuing('import_library("' + lib_file_path + '")')
93e9d78c46SMichael Walsh            BuiltIn().import_library(lib_file_path)
94e9d78c46SMichael Walsh        else:
95*20f38712SPatrick Williams            gp.dprint_issuing('my_import_resource("' + lib_file_path + '")')
96e9d78c46SMichael Walsh            gru.my_import_resource(lib_file_path)
97e9d78c46SMichael Walsh
98e9d78c46SMichael Walsh    # The user can pass multiple keyword strings by separating them with " ; ".
99e9d78c46SMichael Walsh    keyword_list = keyword_string.split(" ; ")
100e9d78c46SMichael Walsh    for keyword_string in keyword_list:
101e9d78c46SMichael Walsh        cmd_buf = keyword_string.split("  ")
102e9d78c46SMichael Walsh        if re.match(r"\$\{", cmd_buf[0]):
103e9d78c46SMichael Walsh            # This looks like an assignment (e.g. ${var}=  <keyword>).
104e9d78c46SMichael Walsh            # We'll extract the variable name, remove element 0 from
105e9d78c46SMichael Walsh            # cmd_buf and set the global variable with the results
106e9d78c46SMichael Walsh            # after running the keyword.
107e9d78c46SMichael Walsh            var_name = cmd_buf[0].strip("${}=")
108e9d78c46SMichael Walsh            del cmd_buf[0]
109e9d78c46SMichael Walsh        else:
110e9d78c46SMichael Walsh            var_name = ""
111e9d78c46SMichael Walsh
112e9d78c46SMichael Walsh        if not quiet:
113edb5c949SMichael Walsh            gp.print_issuing(cmd_buf, test_mode)
114e9d78c46SMichael Walsh        if test_mode:
115e9d78c46SMichael Walsh            continue
116e9d78c46SMichael Walsh
117e9d78c46SMichael Walsh        output = BuiltIn().run_keyword(*cmd_buf)
118e9d78c46SMichael Walsh
119e9d78c46SMichael Walsh        if var_name != "":
120e9d78c46SMichael Walsh            BuiltIn().set_global_variable("${" + var_name + "}", output)
121e9d78c46SMichael Walsh        else:
122e9d78c46SMichael Walsh            if output is not None:
123edb5c949SMichael Walsh                gp.gp_print(output)
124e9d78c46SMichael Walsh
125e9d78c46SMichael Walsh
126e9d78c46SMichael Walshdef main_py():
127e9d78c46SMichael Walsh    r"""
128e9d78c46SMichael Walsh    Do main program processing.
129e9d78c46SMichael Walsh    """
130e9d78c46SMichael Walsh
131e9d78c46SMichael Walsh    setup()
132e9d78c46SMichael Walsh
133e9d78c46SMichael Walsh    lib_file_path = BuiltIn().get_variable_value("${lib_file_path}")
134e9d78c46SMichael Walsh    keyword_string = BuiltIn().get_variable_value("${keyword_string}")
135e9d78c46SMichael Walsh    quiet = int(BuiltIn().get_variable_value("${quiet}"))
136e9d78c46SMichael Walsh    test_mode = int(BuiltIn().get_variable_value("${test_mode}"))
137e9d78c46SMichael Walsh
138e9d78c46SMichael Walsh    my_run_keywords(lib_file_path, keyword_string, quiet, test_mode)
139