1#!/usr/bin/env python3
2
3import collections
4
5import gen_cmd as gc
6import gen_print as gp
7
8module_names = [
9    "Selenium2Library",
10    "SeleniumLibrary",
11    "SSHLibrary",
12    "requests",
13    "XvfbRobot",
14    "robotremoteserver",
15    "redfish",
16]
17
18import_versions = collections.OrderedDict()
19
20for module_name in module_names:
21    try:
22        cmd_buf = "import " + module_name
23        exec(cmd_buf)
24        cmd_buf = (
25            "import_versions['"
26            + module_name
27            + "'] = "
28            + module_name
29            + ".__version__"
30        )
31        exec(cmd_buf)
32    except ImportError:
33        import_versions[module_name] = "Not installed"
34
35
36def software_versions():
37    r"""
38    Get the versions for several of the software packages used by
39    openbmc-test-automation and return as a dictionary.
40
41    Example call:
42    ${software_versions}=  Software Versions
43    Rprint Vars  software_versions
44
45    Example output:
46    software_versions:
47      [python]:                                       Python 2.7.12
48      [robot]:                                        Robot Framework 3.1.2 (Python 3.6.8 on linux)
49      [firefox]:                                      Mozilla Firefox 54.0
50      [google-chrome]:                                Not installed
51      [Selenium2Library]:                             3.0.0
52      [SeleniumLibrary]:                              3.3.1
53      [SSHLibrary]:                                   3.3.0
54      [requests]:                                     2.22.0
55      [XvfbRobot]:                                    1.2.2
56      [robotremoteserver]:                            1.1
57      [redfish]:                                      2.1.1
58      [robotframework-angularjs]:                     0.0.10
59      [robotframework-scplibrary]:                    1.2.0
60      [robotframework-extendedselenium2library]:      0.9.1
61      [host OS]:                                      Ubuntu 16.04.6 LTS
62    """
63
64    quiet = 1
65    versions = collections.OrderedDict()
66    for package in ["python", "python3", "robot", "firefox", "google-chrome"]:
67        # Note: "robot --version" returns 0x00000000000000fb.
68        # Note: If package does not exist, 0x7f is returned.
69        rc, version = gc.shell_cmd(
70            package + " --version", valid_rcs=[0, 0x7F, 0xFB]
71        )
72        versions[package] = (
73            "Not installed" if rc == 0x7F else version.rstrip("\n")
74        )
75
76    versions.update(import_versions)
77
78    for package in [
79        "robotframework-angularjs",
80        "robotframework-scplibrary",
81        "robotframework-extendedselenium2library",
82    ]:
83        rc, version = gc.shell_cmd(
84            "pip3 show " + package + " | grep Version | sed -re 's/.*: //g'"
85        )
86        versions[package] = (
87            "Not installed" if not version else version.rstrip("\n")
88        )
89
90    rc, version = gc.shell_cmd("lsb_release -d -s")
91    versions["host OS"] = "Failed" if not version else version.rstrip("\n")
92    return versions
93