4
4
from contextlib import contextmanager
5
5
import logging
6
6
import signal
7
- from typing import Optional , Sequence
7
+ from typing import Dict , Optional , Sequence
8
8
9
9
import libyang
10
10
@@ -166,6 +166,87 @@ def install_module(
166
166
valid_codes = valid_codes ,
167
167
)
168
168
169
+ def install_modules (
170
+ self ,
171
+ filepaths : Dict [str , Sequence [str ]],
172
+ searchdirs : Optional [Sequence [str ]] = None ,
173
+ ignore_already_exists = False ,
174
+ ) -> None :
175
+ """
176
+ Install new schemas (modules) into sysrepo in a batch.
177
+
178
+ :arg filepaths:
179
+ Dict of paths to the new schemas associated with a list of features to enable.
180
+ Can have either YANG or YIN extension/format.
181
+ :arg searchdirs:
182
+ Optional list of search directories for import schemas.
183
+ :arg ignore_already_exists:
184
+ Ignore error if module already exists in sysrepo.
185
+ """
186
+ schema_paths = tuple ([str2c (k ) for k in filepaths ] + [ffi .NULL ])
187
+
188
+ # We need to maintain a pointer to extension names in C
189
+ _ref = []
190
+ all_features = []
191
+ has_feature = False
192
+ for features in filepaths .values ():
193
+ features_cname = []
194
+ for f in features :
195
+ cname = str2c (f )
196
+ _ref .append (cname )
197
+ features_cname .append (cname )
198
+ has_feature = True
199
+
200
+ all_features .append (ffi .new ("char *[]" , tuple (features_cname + [ffi .NULL ])))
201
+
202
+ if has_feature :
203
+ all_features = tuple (all_features )
204
+ else :
205
+ all_features = ffi .NULL
206
+
207
+ if ignore_already_exists :
208
+ valid_codes = (lib .SR_ERR_OK , lib .SR_ERR_EXISTS )
209
+ else :
210
+ valid_codes = (lib .SR_ERR_OK ,)
211
+
212
+ if not searchdirs :
213
+ searchdirs = []
214
+
215
+ check_call (
216
+ lib .sr_install_modules ,
217
+ self .cdata ,
218
+ schema_paths ,
219
+ str2c (":" .join (searchdirs )),
220
+ all_features ,
221
+ valid_codes = valid_codes ,
222
+ )
223
+
224
+ def update_modules (
225
+ self ,
226
+ filepaths : Sequence [str ],
227
+ searchdirs : Optional [Sequence [str ]] = None ,
228
+ ) -> None :
229
+ """
230
+ Update schemas (modules) into sysrepo in a batch.
231
+
232
+ :arg filepaths:
233
+ Array of paths to the new schemas.
234
+ Can have either YANG or YIN extension/format.
235
+ :arg searchdirs:
236
+ Optional list of search directories for import schemas.
237
+ """
238
+ schema_paths = tuple ([str2c (path ) for path in filepaths ] + [ffi .NULL ])
239
+ if not searchdirs :
240
+ searchdirs = []
241
+
242
+ check_call (
243
+ lib .sr_update_modules ,
244
+ self .cdata ,
245
+ schema_paths ,
246
+ str2c (":" .join (searchdirs )),
247
+ valid_codes = (lib .SR_ERR_OK ,),
248
+ )
249
+
169
250
def remove_module (self , name : str , force : bool = False ) -> None :
170
251
"""
171
252
Remove an installed module from sysrepo. Deferred until there are no
@@ -176,6 +257,16 @@ def remove_module(self, name: str, force: bool = False) -> None:
176
257
"""
177
258
check_call (lib .sr_remove_module , self .cdata , str2c (name ), force )
178
259
260
+ def remove_modules (self , names : Sequence [str ], force : bool = False ) -> None :
261
+ """
262
+ Remove installed modules from sysrepo.
263
+
264
+ :arg str names:
265
+ Array of names of the modules to remove.
266
+ """
267
+ names = tuple ([str2c (n ) for n in names ] + [ffi .NULL ])
268
+ check_call (lib .sr_remove_modules , self .cdata , names , force )
269
+
179
270
def enable_module_feature (self , name : str , feature_name : str ) -> None :
180
271
"""
181
272
Enable a module feature.
0 commit comments