Skip to content

Commit 0f2add7

Browse files
authored
Merge pull request #10089 from bridadan/optional_use_of_cpm
Make the presence of cmsis-pack-manager package optional
2 parents fe59870 + f200c27 commit 0f2add7

File tree

1 file changed

+61
-18
lines changed

1 file changed

+61
-18
lines changed

tools/arm_pack_manager/__init__.py

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
from os.path import join, dirname
2020
from json import load, dump
2121
import warnings
22-
from cmsis_pack_manager import Cache as _Cache
22+
23+
try:
24+
from cmsis_pack_manager import Cache as _Cache
25+
_CPM_PRESENT = True
26+
except ImportError:
27+
_CPM_PRESENT = False
2328

2429
from tools.flash_algo import PackFlashAlgo
2530

@@ -60,10 +65,14 @@ class Cache(object):
6065
:type no_timeouts: bool
6166
"""
6267
def __init__(self, silent, no_timeouts):
63-
self._cache = _Cache(
64-
silent, no_timeouts,
65-
json_path=LocalPackDir, data_path=LocalPackDir
66-
)
68+
if _CPM_PRESENT:
69+
self._cache = _Cache(
70+
silent, no_timeouts,
71+
json_path=LocalPackDir, data_path=LocalPackDir
72+
)
73+
else:
74+
self._cache = None
75+
6776
try:
6877
self._legacy_names = load(open(LocalPackLegacyNames))
6978
except IOError:
@@ -101,17 +110,36 @@ def _get_sectors(self, device):
101110

102111
@property
103112
def index(self):
104-
return _CacheLookup(self._cache.index, self._legacy_names)
113+
if _CPM_PRESENT:
114+
return _CacheLookup(self._cache.index, self._legacy_names)
115+
else:
116+
local_index = load(open(LocalPackIndex))
117+
return _CacheLookup(local_index, self._legacy_names)
105118

106119
def cache_descriptors(self):
107-
self._cache.cache_descriptors
120+
if _CPM_PRESENT:
121+
return self._cache.cache_descriptors()
122+
else:
123+
print(
124+
'The Python package "cmsis-pack-manager" is not installed. '
125+
'To cache CMSIS Pack descriptors, please install this '
126+
'package with "pip install cmsis-pack-manager".'
127+
)
128+
return []
108129

109130
def cache_everything(self):
110-
self._cache.cache_everything()
111-
for name, device in self._cache.index.items():
112-
if name != "version":
113-
device["sectors"] = self._get_sectors(device)
114-
self.generate_index()
131+
if _CPM_PRESENT:
132+
self._cache.cache_everything()
133+
for name, device in self._cache.index.items():
134+
if name != "version":
135+
device["sectors"] = self._get_sectors(device)
136+
self.generate_index()
137+
else:
138+
print(
139+
'The Python package "cmsis-pack-manager" is not installed. '
140+
'To update the cache, please install this package with '
141+
'"pip install cmsis-pack-manager".'
142+
)
115143

116144
def get_svd_file(self, device_name):
117145
"""Retrieve the flash algorithm file for a particular part.
@@ -125,11 +153,26 @@ def get_svd_file(self, device_name):
125153
describes the flashing algorithm
126154
:rtype: ZipExtFile
127155
"""
128-
device = self.index[device_name]
129-
pack = self.pack_from_cache(device)
130-
return pack.open(device['debug'])
156+
if _CPM_PRESENT:
157+
device = self.index[device_name]
158+
pack = self._cache.pack_from_cache(device)
159+
return pack.open(device['debug'])
160+
else:
161+
print(
162+
'The Python package "cmsis-pack-manager" is not installed. '
163+
'To use SVD files, please install this package with '
164+
'"pip install cmsis-pack-manager".'
165+
)
166+
return None
131167

132168
def generate_index(self):
133-
with open(LocalPackIndex, "w+") as out:
134-
self._cache.index["version"] = "0.2.0"
135-
dump(self._cache.index, out, indent=4, sort_keys=True)
169+
if _CPM_PRESENT:
170+
with open(LocalPackIndex, "w+") as out:
171+
self._cache.index["version"] = "0.2.0"
172+
dump(self._cache.index, out, indent=4, sort_keys=True)
173+
else:
174+
print(
175+
'The Python package "cmsis-pack-manager" is not installed. '
176+
'To generate a CMSIS Pack index, please install this package with '
177+
'"pip install cmsis-pack-manager".'
178+
)

0 commit comments

Comments
 (0)