Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 1 | # 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 | |
| 17 | import os |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 18 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 19 | import sys |
| 20 | if sys.version_info[0] < 3: |
| 21 | import cPickle as pickle |
| 22 | else: |
| 23 | import pickle |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 24 | import json |
| 25 | |
| 26 | CACHE_PATH = '/var/cache/obmc/' |
| 27 | |
| 28 | |
| 29 | def getCacheFilename(obj_path, iface_name): |
| 30 | name = obj_path.replace('/', '.') |
| 31 | filename = CACHE_PATH+name[1:]+"@"+iface_name+".props" |
| 32 | return filename |
| 33 | |
| 34 | |
| 35 | def save(obj_path, iface_name, properties): |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 36 | print("Caching: "+obj_path) |
Brad Bishop | d637227 | 2016-07-26 23:01:42 -0400 | [diff] [blame] | 37 | filename = getCacheFilename(obj_path, iface_name) |
| 38 | parent = os.path.dirname(filename) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 39 | try: |
Brad Bishop | d637227 | 2016-07-26 23:01:42 -0400 | [diff] [blame] | 40 | if not os.path.exists(parent): |
| 41 | os.makedirs(parent) |
Brad Bishop | 0127d51 | 2016-07-28 13:52:29 -0400 | [diff] [blame] | 42 | with open(filename, 'wb') as output: |
| 43 | try: |
| 44 | ## use json module to convert dbus datatypes |
| 45 | props = json.dumps(properties[iface_name]) |
| 46 | prop_obj = json.loads(props) |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 47 | pickle.dump(prop_obj, output) |
Brad Bishop | 0127d51 | 2016-07-28 13:52:29 -0400 | [diff] [blame] | 48 | except Exception as e: |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 49 | print("ERROR: "+str(e)) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 50 | except: |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 51 | print("ERROR opening cache file: "+filename) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 52 | |
| 53 | |
| 54 | def load(obj_path, iface_name, properties): |
| 55 | ## overlay with pickled data |
| 56 | filename = getCacheFilename(obj_path, iface_name) |
| 57 | if (os.path.isfile(filename)): |
| 58 | if iface_name in properties: |
| 59 | properties[iface_name] = {} |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 60 | print("Loading from cache: "+filename) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 61 | try: |
| 62 | p = open(filename, 'rb') |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 63 | data = pickle.load(p) |
| 64 | for prop in list(data.keys()): |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 65 | properties[iface_name][prop] = data[prop] |
| 66 | |
| 67 | except Exception as e: |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 68 | print("ERROR: Loading cache file: " + str(e)) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 69 | finally: |
| 70 | p.close() |