blob: 6086bc6cab776589bdd721edc01005dee55371e3 [file] [log] [blame]
Brad Bishop179b39b2016-05-12 16:45:57 -04001# Contributors Listed Below - COPYRIGHT 2016
2# [+] International Business Machines Corp.
3#
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied. See the License for the specific language governing
15# permissions and limitations under the License.
16
Brad Bishopaea38c62018-01-30 13:00:26 -050017import json
Brad Bishop179b39b2016-05-12 16:45:57 -040018import os
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060019# TODO: openbmc/openbmc#2994 remove python 2 support
20import sys
21if sys.version_info[0] < 3:
22 import cPickle as pickle
23else:
24 import pickle
Brad Bishop179b39b2016-05-12 16:45:57 -040025
26CACHE_PATH = '/var/cache/obmc/'
27
28
29def getCacheFilename(obj_path, iface_name):
30 name = obj_path.replace('/', '.')
Brad Bishopaea38c62018-01-30 13:00:26 -050031 filename = CACHE_PATH + name[1:] + "@" + iface_name + ".props"
Brad Bishop179b39b2016-05-12 16:45:57 -040032 return filename
33
34
35def save(obj_path, iface_name, properties):
Brad Bishopaea38c62018-01-30 13:00:26 -050036 print("Caching: "+ obj_path)
Brad Bishopd6372272016-07-26 23:01:42 -040037 filename = getCacheFilename(obj_path, iface_name)
38 parent = os.path.dirname(filename)
Brad Bishop179b39b2016-05-12 16:45:57 -040039 try:
Brad Bishopd6372272016-07-26 23:01:42 -040040 if not os.path.exists(parent):
41 os.makedirs(parent)
Brad Bishop0127d512016-07-28 13:52:29 -040042 with open(filename, 'wb') as output:
43 try:
Brad Bishopaea38c62018-01-30 13:00:26 -050044 # use json module to convert dbus datatypes
Brad Bishop0127d512016-07-28 13:52:29 -040045 props = json.dumps(properties[iface_name])
46 prop_obj = json.loads(props)
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060047 pickle.dump(prop_obj, output)
Brad Bishop0127d512016-07-28 13:52:29 -040048 except Exception as e:
Brad Bishopaea38c62018-01-30 13:00:26 -050049 print("ERROR: " + str(e))
50 except Exception:
51 print("ERROR opening cache file: " + filename)
Brad Bishop179b39b2016-05-12 16:45:57 -040052
53
54def load(obj_path, iface_name, properties):
Brad Bishopaea38c62018-01-30 13:00:26 -050055 # overlay with pickled data
Brad Bishop179b39b2016-05-12 16:45:57 -040056 filename = getCacheFilename(obj_path, iface_name)
57 if (os.path.isfile(filename)):
58 if iface_name in properties:
59 properties[iface_name] = {}
Brad Bishopaea38c62018-01-30 13:00:26 -050060 print("Loading from cache: " + filename)
Brad Bishop179b39b2016-05-12 16:45:57 -040061 try:
62 p = open(filename, 'rb')
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060063 data = pickle.load(p)
64 for prop in list(data.keys()):
Brad Bishop179b39b2016-05-12 16:45:57 -040065 properties[iface_name][prop] = data[prop]
66
67 except Exception as e:
CamVan Nguyen9172f3e2018-02-27 15:18:58 -060068 print("ERROR: Loading cache file: " + str(e))
Brad Bishop179b39b2016-05-12 16:45:57 -040069 finally:
70 p.close()