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