1#!/usr/bin/env python
2
3import os
4import sys
5import gobject
6import dbus
7import dbus.service
8import dbus.mainloop.glib
9import obmc.dbuslib.propertycacher as PropertyCacher
10from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
11
12
13INTF_NAME = 'org.openbmc.InventoryItem'
14DBUS_NAME = 'org.openbmc.Inventory'
15
16FRUS = {}
17
18
19class Inventory(DbusProperties, DbusObjectManager):
20    def __init__(self, bus, name):
21        super(Inventory, self).__init__(
22            conn=bus,
23            object_path=name)
24
25
26class InventoryItem(DbusProperties):
27    def __init__(self, bus, name, data):
28        super(InventoryItem, self).__init__(
29            conn=bus,
30            object_path=name)
31
32        self.name = name
33
34        if 'present' not in data:
35            data['present'] = 'False'
36        if 'fault' not in data:
37            data['fault'] = 'False'
38        if 'version' not in data:
39            data['version'] = ''
40
41        self.SetMultiple(INTF_NAME, data)
42
43        ## this will load properties from cache
44        PropertyCacher.load(name, INTF_NAME, self.properties)
45
46    @dbus.service.method(
47        INTF_NAME, in_signature='a{sv}', out_signature='')
48    def update(self, data):
49        self.SetMultiple(INTF_NAME, data)
50        PropertyCacher.save(self.name, INTF_NAME, self.properties)
51
52    @dbus.service.method(
53        INTF_NAME, in_signature='s', out_signature='')
54    def setPresent(self, present):
55        self.Set(INTF_NAME, 'present', present)
56        PropertyCacher.save(self.name, INTF_NAME, self.properties)
57
58    @dbus.service.method(
59        INTF_NAME, in_signature='s', out_signature='')
60    def setFault(self, fault):
61        self.Set(INTF_NAME, 'fault', fault)
62        PropertyCacher.save(self.name, INTF_NAME, self.properties)
63
64
65def getVersion():
66    version = "Error"
67    with open('/etc/os-release', 'r') as f:
68        for line in f:
69            p = line.rstrip('\n')
70            parts = line.rstrip('\n').split('=')
71            if (parts[0] == "VERSION_ID"):
72                version = parts[1]
73                version = version.strip('"')
74    return version
75
76
77if __name__ == '__main__':
78    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
79    bus = get_dbus()
80    mainloop = gobject.MainLoop()
81    obj_parent = Inventory(bus, '/org/openbmc/inventory')
82    INVENTORY_FILE = os.path.join(sys.prefix, 'share',
83                                  'inventory', 'inventory.json')
84
85    if os.path.exists(INVENTORY_FILE):
86        import json
87        with open(INVENTORY_FILE, 'r') as f:
88            try:
89                inv = json.load(f)
90            except ValueError:
91                print "Invalid JSON detected in " + INVENTORY_FILE
92            else:
93                FRUS = inv
94    else:
95        try:
96            import obmc_system_config as System
97            FRUS = System.FRU_INSTANCES
98        except ImportError:
99            pass
100
101    for f in FRUS.keys():
102        import obmc.inventory
103        obj_path = f.replace("<inventory_root>", obmc.inventory.INVENTORY_ROOT)
104        obj = InventoryItem(bus, obj_path, FRUS[f])
105        obj_parent.add(obj_path, obj)
106
107        ## TODO:  this is a hack to update bmc inventory item with version
108        ## should be done by flash object
109        if (FRUS[f]['fru_type'] == "BMC"):
110            version = getVersion()
111            obj.update({'version': version})
112
113    obj_parent.unmask_signals()
114    name = dbus.service.BusName(DBUS_NAME, bus)
115    print "Running Inventory Manager"
116    mainloop.run()
117
118# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
119