Skip to content

Commit ce1d65b

Browse files
Merge pull request #349 from ARMmbed/revert-341-online_plat_db_updates
Revert "Adding a --update command for pulling new target defintions from online"
2 parents f028f09 + 6b32fa7 commit ce1d65b

File tree

3 files changed

+1
-230
lines changed

3 files changed

+1
-230
lines changed

mbed_lstools/main.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
# Make sure that any global generic setup is run
2828
from . import lstools_base
29-
from .platform_database import DEFAULT_UPDATE_URL, RemotePlatformDataException,\
30-
DatabaseUpdateException
3129

3230
import logging
3331
logger = logging.getLogger("mbedls.main")
@@ -132,44 +130,6 @@ def mock_platform(mbeds, args):
132130
else:
133131
logger.error("Could not parse mock from token: '%s'", token)
134132

135-
def update_from_web(mbeds, args):
136-
try:
137-
print("Fetching platform data from %s..." % args.update_url)
138-
update_info = mbeds.plat_db.update_from_web(args.update_url)
139-
except (RemotePlatformDataException, DatabaseUpdateException):
140-
logger.error(
141-
"Failed to update the platform database. For more information, "
142-
"run with \"--debug\"."
143-
)
144-
return 1
145-
146-
rows = []
147-
148-
if update_info["new"]:
149-
for target_id, platform_name in update_info["new"].items():
150-
rows.append([target_id, platform_name])
151-
152-
if update_info["updated"]:
153-
for target_id, info in update_info["updated"].items():
154-
rows.append([target_id, "%s -> %s" % (info["old"], info["new"])])
155-
156-
if rows:
157-
from prettytable import PrettyTable
158-
columns = ["target_id", "platform_name"]
159-
pt = PrettyTable(columns)
160-
pt.align = 'l'
161-
162-
for row in rows:
163-
pt.add_row(row)
164-
165-
print(pt.get_string(
166-
border=True, header=True, padding_width=1,
167-
sortby='target_id'
168-
))
169-
print("The platform database was updated.")
170-
else:
171-
print("Platform database up-to-date.")
172-
173133
def list_platforms(mbeds, args):
174134
print(mbeds.list_manufacture_ids())
175135

@@ -244,10 +204,6 @@ def parse_cli(to_parse):
244204
'-m', '--mock', metavar='ID:NAME',
245205
help='substitute or create a target ID to platform name mapping used'
246206
'when invoking mbedls in the current directory')
247-
commands.add_argument(
248-
'-U', '--update', nargs='?', default=False,
249-
const=DEFAULT_UPDATE_URL, dest='update_url',
250-
help='update the platform database from a URL (default: %s)' % (DEFAULT_UPDATE_URL))
251207

252208
parser.add_argument(
253209
'--skip-retarget', dest='skip_retarget', default=False,
@@ -265,8 +221,6 @@ def parse_cli(to_parse):
265221
args = parser.parse_args(to_parse)
266222
if args.mock:
267223
args.command = mock_platform
268-
elif args.update_url:
269-
args.command = update_from_web
270224
return args
271225

272226
def start_logging():

mbed_lstools/platform_database.py

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import datetime
2121
import json
2222
import re
23-
import requests
2423
from collections import OrderedDict, defaultdict
2524
from copy import copy
2625
from io import open
@@ -42,7 +41,6 @@
4241

4342
LOCAL_PLATFORM_DATABASE = join(user_data_dir("mbedls"), "platforms.json")
4443
LOCAL_MOCKS_DATABASE = join(user_data_dir("mbedls"), "mock.json")
45-
DEFAULT_UPDATE_URL = "https://os.mbed.com/api/v3/platforms/?format=json"
4644

4745
DEFAULT_PLATFORM_DB = {
4846
u'daplink': {
@@ -364,14 +362,6 @@ def _overwrite_or_open(db):
364362
return {}
365363

366364

367-
class RemotePlatformDataException(Exception):
368-
pass
369-
370-
371-
class DatabaseUpdateException(Exception):
372-
pass
373-
374-
375365
class PlatformDatabase(object):
376366
"""Represents a union of multiple platform database files.
377367
Handles inter-process synchronization of database files.
@@ -492,102 +482,3 @@ def remove(self, id, permanent=False, device_type='daplink', verbose_data=False)
492482
self._update_db()
493483

494484
return _modify_data_format(removed, verbose_data)
495-
496-
def update_from_web(self, url=DEFAULT_UPDATE_URL):
497-
"""Update entries in the platform database from the API on the Mbed website.
498-
499-
If the update fails to contact the provided url, a `ConnectionError` will
500-
be raised (part of the `requests` library). If it fails during the web
501-
request or JSON parsing, a `RemotePlatformDataException` is raised.
502-
If it fails during the write to the filesystem, a `DatabaseUpdateException`
503-
is raised.
504-
505-
Returns dictionary of all updates that occurred in the following format:
506-
{
507-
"new": {
508-
"<target id>": "<platform name>",
509-
...
510-
},
511-
"updated": {
512-
"<target id>": {
513-
"old": "<previous platform name>",
514-
"new": "<new platform name>"
515-
}
516-
}
517-
}
518-
"""
519-
r = requests.get(url)
520-
try:
521-
platform_data = r.json()
522-
except ValueError as e:
523-
logger.debug("Failed to retrieve platform data from os.mbed.com.")
524-
logger.debug(e)
525-
logger.debug("Received the following response:")
526-
logger.debug("Status code: %d", r.status_code)
527-
logger.debug(r.text)
528-
raise RemotePlatformDataException()
529-
530-
try:
531-
"""Expected JSON format is as follows:
532-
[
533-
{
534-
"productcode": "1234",
535-
"logicalboard": {
536-
"name": "boardname"
537-
}
538-
},
539-
...
540-
]
541-
"""
542-
web_platforms = {
543-
v['productcode']: v['logicalboard']['name'].upper() for v in platform_data
544-
}
545-
except KeyError as e:
546-
logger.debug("Platform data had unexpected format.")
547-
logger.debug(e)
548-
logger.debug("Received platform data:")
549-
logger.debug(platform_data)
550-
raise RemotePlatformDataException()
551-
552-
current_platforms = {
553-
k: v
554-
for (k, v) in self.items()
555-
}
556-
557-
web_platform_keys = set(web_platforms.keys())
558-
current_platform_keys = set(current_platforms.keys())
559-
560-
new_platform_ids = web_platform_keys - current_platform_keys
561-
new_platforms = {
562-
id: web_platforms[id]
563-
for id in new_platform_ids
564-
}
565-
566-
shared_platform_ids = web_platform_keys.intersection(current_platform_keys)
567-
568-
updated_platforms_history = {
569-
id: {
570-
"old": current_platforms[id],
571-
"new": web_platforms[id]
572-
}
573-
for id in shared_platform_ids if web_platforms[id] != current_platforms[id]
574-
}
575-
576-
updated_platforms = {
577-
id: platform["new"]
578-
for id, platform in updated_platforms_history.items()
579-
}
580-
581-
all_updates = new_platforms.copy()
582-
all_updates.update(updated_platforms)
583-
584-
for id, platform_name in all_updates.items():
585-
self.add(id, platform_name)
586-
587-
if not self._update_db():
588-
raise DatabaseUpdateException()
589-
590-
return {
591-
"new": new_platforms,
592-
"updated": updated_platforms_history
593-
}

test/platform_database.py

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
import json
2525
from mock import patch, MagicMock, DEFAULT
2626
from io import StringIO
27-
from requests.exceptions import ConnectionError
2827

2928
from mbed_lstools.platform_database import PlatformDatabase, DEFAULT_PLATFORM_DB,\
30-
LOCAL_PLATFORM_DATABASE, RemotePlatformDataException, DatabaseUpdateException
29+
LOCAL_PLATFORM_DATABASE
3130

3231
try:
3332
unicode
@@ -150,79 +149,6 @@ def test_simplify_verbose_data(self):
150149
self.assertEqual(self.pdb.get('1337', verbose_data=True), platform_data)
151150
self.assertEqual(self.pdb.get('1337'), platform_data['platform_name'])
152151

153-
def test_update_from_web(self):
154-
with patch("requests.get") as _get:
155-
_result = MagicMock()
156-
_result.configure_mock(**{
157-
'json.return_value': [
158-
{
159-
'productcode': '1337',
160-
'logicalboard': {
161-
'name': 'dummy_board'
162-
}
163-
}
164-
]
165-
})
166-
_get.return_value = _result
167-
168-
self.pdb.update_from_web()
169-
_result.json.assert_called_once()
170-
self.assertEqual(self.pdb.get('1337'), 'DUMMY_BOARD')
171-
172-
def test_update_from_web_json_error(self):
173-
with patch("requests.get") as _get:
174-
_result = MagicMock()
175-
_result.configure_mock(**{
176-
'json.side_effect': ValueError
177-
})
178-
_get.return_value = _result
179-
180-
exception_raised = False
181-
try:
182-
self.pdb.update_from_web()
183-
except RemotePlatformDataException:
184-
exception_raised = True
185-
186-
self.assertTrue(exception_raised)
187-
188-
def test_update_from_web_request_error(self):
189-
with patch("requests.get") as _get:
190-
_get.side_effect = ConnectionError
191-
192-
exception_raised = False
193-
try:
194-
self.pdb.update_from_web()
195-
except ConnectionError:
196-
exception_raised = True
197-
198-
self.assertTrue(exception_raised)
199-
200-
def test_update_from_web_update_error(self):
201-
with patch("requests.get") as _get,\
202-
patch.object(PlatformDatabase, "_update_db") as _update_db:
203-
_result = MagicMock()
204-
_result.configure_mock(**{
205-
'json.return_value': [
206-
{
207-
'productcode': '1337',
208-
'logicalboard': {
209-
'name': 'dummy_board'
210-
}
211-
}
212-
]
213-
})
214-
_get.return_value = _result
215-
_update_db.return_value = False
216-
217-
exception_raised = False
218-
try:
219-
self.pdb.update_from_web()
220-
except DatabaseUpdateException:
221-
exception_raised = True
222-
223-
_update_db.assert_called_once()
224-
self.assertTrue(exception_raised)
225-
226152
class OverriddenPlatformDatabaseTests(unittest.TestCase):
227153
""" Test that for one database overriding another
228154
"""

0 commit comments

Comments
 (0)