Skip to content

Commit 3e1cb41

Browse files
committed
Update platform database when it's out of date
And test it
1 parent d4d7f53 commit 3e1cb41

File tree

2 files changed

+58
-36
lines changed

2 files changed

+58
-36
lines changed

mbed_lstools/platform_database.py

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717

1818
"""Functions that manage a platform database"""
1919

20+
import datetime
2021
import json
2122
import re
2223
from collections import OrderedDict
2324
from copy import copy
2425
from io import open
2526
from os import makedirs
26-
from os.path import join, dirname
27+
from os.path import join, dirname, getmtime
2728
from appdirs import user_data_dir
2829
from fasteners import InterProcessLock
2930

@@ -32,6 +33,7 @@
3233
except NameError:
3334
unicode = str
3435

36+
3537
import logging
3638
logger = logging.getLogger("mbedls.platform_database")
3739
del logging
@@ -251,6 +253,40 @@
251253
u'RIOT': u'RIOT',
252254
}
253255

256+
257+
def get_modified_time(path):
258+
try:
259+
mtime = getmtime(path)
260+
except OSError:
261+
mtime = 0
262+
return datetime.date.fromtimestamp(mtime)
263+
264+
265+
def older_than_me(path):
266+
return get_modified_time(path) < get_modified_time(__file__)
267+
268+
269+
def overwrite_or_open(db):
270+
try:
271+
if db is LOCAL_PLATFORM_DATABASE and older_than_me(db):
272+
raise ValueError("Platform Database is out of date")
273+
with open(db, encoding="utf-8") as db_in:
274+
return json.load(db_in)
275+
except (IOError, ValueError) as exc:
276+
if db is LOCAL_PLATFORM_DATABASE:
277+
logger.warning(
278+
"Error loading database %s: %s; Recereating", db, str(exc))
279+
try:
280+
makedirs(dirname(db))
281+
except OSError:
282+
pass
283+
with open(db, "w", encoding="utf-8") as out:
284+
out.write(unicode(json.dumps(DEFAULT_PLATFORM_DB)))
285+
return copy(DEFAULT_PLATFORM_DB)
286+
else:
287+
return {}
288+
289+
254290
class PlatformDatabase(object):
255291
"""Represents a union of multiple platform database files.
256292
Handles inter-process synchronization of database files.
@@ -266,41 +302,15 @@ def __init__(self, database_files, primary_database=None):
266302
self._dbs = OrderedDict()
267303
self._keys = set()
268304
for db in database_files:
269-
try:
270-
new_db = json.load(open(db, encoding="utf-8"))
271-
duplicates = set(self._keys).intersection(set(new_db.keys()))
272-
if duplicates:
273-
logger.warning(
274-
"Duplicate platform ids found: %s,"
275-
" ignoring the definitions from %s",
276-
" ".join(duplicates), db)
277-
self._dbs[db] = new_db
278-
self._keys = self._keys.union(new_db.keys())
279-
except IOError as exc:
280-
if db is LOCAL_PLATFORM_DATABASE:
281-
logger.warning(
282-
"Could not open local platform database at %s; "
283-
"Recereating", db)
284-
try:
285-
makedirs(dirname(db))
286-
except OSError:
287-
pass
288-
with open(db, "w", encoding="utf-8") as out:
289-
out.write(unicode(json.dumps(DEFAULT_PLATFORM_DB)))
290-
new_db = copy(DEFAULT_PLATFORM_DB)
291-
self._dbs[db] = new_db
292-
self._keys = self._keys.union(new_db.keys())
293-
else:
294-
if db is not self._prim_db:
295-
logger.error(
296-
"Could not open platform database %s: %s; skipping",
297-
db, str(exc))
298-
self._dbs[db] = {}
299-
except ValueError as exc:
300-
logger.error(
301-
"Could not parse platform database %s because %s; "
302-
"skipping", db, str(exc))
303-
self._dbs[db] = {}
305+
new_db = overwrite_or_open(db)
306+
duplicates = set(self._keys).intersection(set(new_db.keys()))
307+
if duplicates:
308+
logger.warning(
309+
"Duplicate platform ids found: %s,"
310+
" ignoring the definitions from %s",
311+
" ".join(duplicates), db)
312+
self._dbs[db] = new_db
313+
self._keys = self._keys.union(new_db.keys())
304314

305315
def items(self):
306316
for db in self._dbs.values():

test/platform_database.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ def test_broken_database(self):
7878
self.pdb.add("1234", "MYTARGET")
7979
self.assertEqual(self.pdb.get("1234"), "MYTARGET")
8080

81+
def test_old_database(self):
82+
"""Verify that the platform database correctly updates's its database
83+
"""
84+
with patch("mbed_lstools.platform_database.open") as _open,\
85+
patch("mbed_lstools.platform_database.getmtime") as _getmtime:
86+
stringio = MagicMock()
87+
_open.return_value = stringio
88+
_getmtime.side_effect = (0, 1000000)
89+
self.pdb = PlatformDatabase([LOCAL_PLATFORM_DATABASE])
90+
stringio.__enter__.return_value.write.assert_called_with(
91+
unicode(json.dumps(DEFAULT_PLATFORM_DB)))
92+
8193
def test_bogus_database(self):
8294
"""Basic empty database test
8395
"""

0 commit comments

Comments
 (0)