1#!/usr/bin/env python3
2
3r"""
4VPD functions.
5"""
6
7import json
8
9import bmc_ssh_utils as bsu
10import func_args as fa
11
12
13def vpdtool(option_string, **bsu_options):
14    r"""
15    Run vpdtool on the BMC with the caller's option string and return the result.
16
17    Example:
18
19    ${vpd_results}=  vpd-tool -i
20    Rprint Vars  vpd_results
21
22    vpd_results:
23      [/system/chassis/motherboard]:
24        [PN]:                PN12345
25        [SN]:                YL2E2D010000
26        [LocationCode]:      U78DA.ND1.       -P0
27        [CC]:                2E2D
28        [DR]:                SYSTEM BACKPLANE
29        [FN]:                F191014
30        [type]:              xyz.openbmc_project.Inventory.Item.Board.Motherboard
31      [/system/chassis/motherboard/ebmc_card_bmc]:
32        [PN]:                PN12345
33        [SN]:                YL6B58010000
34        [LocationCode]:      U78DA.ND1.       -P0-C5
35        [CC]:                6B58
36        [DR]:                EBMC
37        [FN]:                F191014
38        [type]:              xyz.openbmc_project.Inventory.Item.Bmc
39
40    Description of argument(s):
41    option_string            A string of options which are to be processed by
42                             the vpd-tool command.
43    bsu_options              Options to be passed directly to bmc_execute_command.
44                             See its prolog for details.
45    """
46
47    bsu_options = fa.args_to_objects(bsu_options)
48    out_buf, stderr, rc = bsu.bmc_execute_command(
49        "vpd-tool " + option_string, **bsu_options
50    )
51
52    # Only return output if its not a VPD write command.
53    if "-w" not in option_string:
54        out_buf = json.loads(out_buf)
55        if "-r" in option_string:
56            return out_buf
57        else:
58            return out_buf[0]
59