Skip to content

Update platform database when it's out of date #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 46 additions & 37 deletions mbed_lstools/platform_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

"""Functions that manage a platform database"""

import datetime
import json
import re
from collections import OrderedDict
from copy import copy
from io import open
from os import makedirs
from os.path import join, dirname
from os.path import join, dirname, getmtime
from appdirs import user_data_dir
from fasteners import InterProcessLock

Expand All @@ -32,6 +33,7 @@
except NameError:
unicode = str


import logging
logger = logging.getLogger("mbedls.platform_database")
del logging
Expand Down Expand Up @@ -251,6 +253,40 @@
u'RIOT': u'RIOT',
}


def _get_modified_time(path):
try:
mtime = getmtime(path)
except OSError:
mtime = 0
return datetime.date.fromtimestamp(mtime)


def _older_than_me(path):
return _get_modified_time(path) < _get_modified_time(__file__)


def _overwrite_or_open(db):
try:
if db is LOCAL_PLATFORM_DATABASE and _older_than_me(db):
raise ValueError("Platform Database is out of date")
with open(db, encoding="utf-8") as db_in:
return json.load(db_in)
except (IOError, ValueError) as exc:
if db is LOCAL_PLATFORM_DATABASE:
logger.warning(
"Error loading database %s: %s; Recreating", db, str(exc))
try:
makedirs(dirname(db))
except OSError:
pass
with open(db, "w", encoding="utf-8") as out:
out.write(unicode(json.dumps(DEFAULT_PLATFORM_DB)))
return copy(DEFAULT_PLATFORM_DB)
else:
return {}


class PlatformDatabase(object):
"""Represents a union of multiple platform database files.
Handles inter-process synchronization of database files.
Expand All @@ -266,42 +302,15 @@ def __init__(self, database_files, primary_database=None):
self._dbs = OrderedDict()
self._keys = set()
for db in database_files:
try:
with open(db, encoding="utf-8") as db_json_file:
new_db = json.load(db_json_file)
duplicates = set(self._keys).intersection(set(new_db.keys()))
if duplicates:
logger.warning(
"Duplicate platform ids found: %s,"
" ignoring the definitions from %s",
" ".join(duplicates), db)
self._dbs[db] = new_db
self._keys = self._keys.union(new_db.keys())
except IOError as exc:
if db is LOCAL_PLATFORM_DATABASE:
logger.warning(
"Could not open local platform database at %s; "
"Recereating", db)
try:
makedirs(dirname(db))
except OSError:
pass
with open(db, "w", encoding="utf-8") as out:
out.write(unicode(json.dumps(DEFAULT_PLATFORM_DB)))
new_db = copy(DEFAULT_PLATFORM_DB)
self._dbs[db] = new_db
self._keys = self._keys.union(new_db.keys())
else:
if db is not self._prim_db:
logger.error(
"Could not open platform database %s: %s; skipping",
db, str(exc))
self._dbs[db] = {}
except ValueError as exc:
logger.error(
"Could not parse platform database %s because %s; "
"skipping", db, str(exc))
self._dbs[db] = {}
new_db = _overwrite_or_open(db)
duplicates = set(self._keys).intersection(set(new_db.keys()))
if duplicates:
logger.warning(
"Duplicate platform ids found: %s,"
" ignoring the definitions from %s",
" ".join(duplicates), db)
self._dbs[db] = new_db
self._keys = self._keys.union(new_db.keys())

def items(self):
for db in self._dbs.values():
Expand Down
12 changes: 12 additions & 0 deletions test/platform_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ def test_broken_database(self):
self.pdb.add("1234", "MYTARGET")
self.assertEqual(self.pdb.get("1234"), "MYTARGET")

def test_old_database(self):
"""Verify that the platform database correctly updates's its database
"""
with patch("mbed_lstools.platform_database.open") as _open,\
patch("mbed_lstools.platform_database.getmtime") as _getmtime:
stringio = MagicMock()
_open.return_value = stringio
_getmtime.side_effect = (0, 1000000)
self.pdb = PlatformDatabase([LOCAL_PLATFORM_DATABASE])
stringio.__enter__.return_value.write.assert_called_with(
unicode(json.dumps(DEFAULT_PLATFORM_DB)))

def test_bogus_database(self):
"""Basic empty database test
"""
Expand Down