xref: /openbmc/openbmc-test-automation/extended/run_keyword.py (revision 16cbb7f7f9bfeb8736534f7675a850f54d71f107)
1#!/usr/bin/env python
2
3r"""
4This module is the python counterpart to run_keyword.robot.
5"""
6
7import gen_robot_print as grp
8import gen_robot_valid as grv
9import gen_robot_utils as gru
10
11from robot.libraries.BuiltIn import BuiltIn
12import re
13
14
15###############################################################################
16def main_py():
17
18    r"""
19    Do main program processing.
20    """
21
22    setup()
23
24    # NOTE: During code review the following question was raised: Why support
25    # 1) variable assignments 2) multiple keywords?  Couldn't a user simply
26    # call this program twice to get what they need.  If necessary, the user
27    # could take the output of the first call and specify it as a literal on
28    # the second call.
29    #
30    # However, this approach would not work in all cases.  The following case
31    # would be such an example:
32    # Let's say the first keyword string is as follows:
33    # Create Dictionary  foo=bar
34    # You wish to take the output of that call and specify it as a literal
35    # value when running the following:
36    # Want Dictionary  parm=<literal dictionary specification>
37    # The problem is that there is no way to specify a dictionary as a
38    # literal in Robot Framework.
39    # By having this program support variable assignments and multiple
40    # keywords, the user can invoke it with the following keyword string.
41    # ${my_dict}=  Create Dictionary  foo=bar ; Want Dictionary  ${my_dict}
42
43    # The user can pass multiple lib/resource paths by separating them with a
44    # colon.
45
46    lib_file_path_list = \
47        BuiltIn().get_variable_value("${lib_file_path}").split(":")
48    # Get rid of empty entry if it exists.
49    if lib_file_path_list[0] == "":
50        del lib_file_path_list[0]
51    for lib_file_path in lib_file_path_list:
52        # We don't want global variable getting changed when an import is done
53        # so we'll save it and restore it.
54        quiet = int(BuiltIn().get_variable_value("${quiet}"))
55        if lib_file_path.endswith(".py"):
56            grp.rdprint_issuing("import_library(\"" + lib_file_path + "\")")
57            BuiltIn().import_library(lib_file_path)
58        else:
59            grp.rdprint_issuing("my_import_resource(\"" + lib_file_path +
60                                "\")")
61            gru.my_import_resource(lib_file_path)
62        BuiltIn().set_global_variable("${quiet}", quiet)
63
64    # The user can pass multiple keyword strings by separating them with " ; ".
65    keyword_list = \
66        BuiltIn().get_variable_value("${keyword_string}").split(" ; ")
67    for keyword_string in keyword_list:
68        cmd_buf = keyword_string.split("  ")
69        if re.match(r"\$\{", cmd_buf[0]):
70            # This looks like an assignment (e.g. ${var}=  <keyword>).
71            # We'll extract the variable name, remove element 0 from
72            # cmd_buf and set the global variable with the results
73            # after running the keyword.
74            var_name = cmd_buf[0].strip("${}=")
75            del cmd_buf[0]
76        else:
77            var_name = ""
78
79        test_mode = int(BuiltIn().get_variable_value("${test_mode}"))
80        grp.rqprint_issuing_keyword(cmd_buf, test_mode)
81        if test_mode:
82            return
83
84        output = BuiltIn().run_keyword(*cmd_buf)
85
86        if var_name != "":
87            BuiltIn().set_global_variable("${" + var_name + "}", output)
88        else:
89            if output is not None:
90                grp.rprint_var(output)
91
92###############################################################################
93
94
95###############################################################################
96def setup():
97
98    r"""
99    Do general program setup tasks.
100    """
101
102    grp.rqprintn()
103
104    validate_parms()
105
106    grp.rqprint_pgm_header()
107
108###############################################################################
109
110
111###############################################################################
112def validate_parms():
113
114    r"""
115    Validate all program parameters.
116    """
117
118    grv.rvalid_value("keyword_string")
119
120    return True
121
122###############################################################################
123
124
125###############################################################################
126def program_teardown():
127
128    r"""
129    Clean up after this program.
130    """
131
132    grp.rqprint_pgm_footer()
133
134###############################################################################
135