12
12
from .._services .model_repository import ModelRepository as mr
13
13
from ..core import RestObj
14
14
15
+
15
16
def getZippedModel (model , gPath , project = None ):
16
- ''' Retrieve a zipped file containing all of the model contents or a specified
17
+ """ Retrieve a zipped file containing all of the model contents or a specified
17
18
model. The project argument is only needed if the model argument is not a valid
18
19
UUID or RestObj.
19
20
@@ -27,9 +28,9 @@ def getZippedModel(model, gPath, project=None):
27
28
project : string or RestObj, optional
28
29
Project identifier, which is required when only the model name is supplied. Default
29
30
is None.
30
- '''
31
- params = {' format' : ' zip' }
32
- modelZip = mr .get (' models/%s' % (model ), params = params , format_ = ' content' )
31
+ """
32
+ params = {" format" : " zip" }
33
+ modelZip = mr .get (" models/%s" % (model ), params = params , format_ = " content" )
33
34
modelName = mr .get_model (model ).name
34
35
# Check if the provided project variable is a REST object
35
36
if isinstance (project , RestObj ):
@@ -38,25 +39,32 @@ def getZippedModel(model, gPath, project=None):
38
39
projectName = mr .get_project (project ).name
39
40
# Check to see if project folder exists
40
41
if (Path (gPath ) / projectName ).exists ():
41
- #Check to see if model folder exists
42
+ # Check to see if model folder exists
42
43
if (Path (gPath ) / projectName / modelName ).exists ():
43
- with open (Path (gPath ) / projectName / modelName / (modelName + '.zip' ), 'wb' ) as zFile :
44
+ with open (
45
+ Path (gPath ) / projectName / modelName / (modelName + ".zip" ), "wb"
46
+ ) as zFile :
44
47
zFile .write (modelZip )
45
48
else :
46
49
newDir = Path (gPath ) / projectName / modelName
47
50
newDir .mkdir (parents = True , exist_ok = True )
48
- with open (Path (gPath ) / projectName / modelName / (modelName + '.zip' ), 'wb' ) as zFile :
51
+ with open (
52
+ Path (gPath ) / projectName / modelName / (modelName + ".zip" ), "wb"
53
+ ) as zFile :
49
54
zFile .write (modelZip )
50
55
else :
51
56
newDir = Path (gPath ) / projectName
52
57
newDir .mkdir (parents = True , exist_ok = True )
53
58
newDir = Path (gPath ) / projectName / modelName
54
59
newDir .mkdir (parents = True , exist_ok = True )
55
- with open (Path (gPath ) / (projectName + '/' + modelName + '.zip' ), 'wb' ) as zFile :
60
+ with open (
61
+ Path (gPath ) / (projectName + "/" + modelName + ".zip" ), "wb"
62
+ ) as zFile :
56
63
zFile .write (modelZip )
57
64
58
65
return modelName , projectName
59
66
67
+
60
68
def project_exists (response , project ):
61
69
"""Checks if project exists on SAS Viya. If the project does not exist, then a new
62
70
project is created or an error is raised.
@@ -93,7 +101,8 @@ def project_exists(response, project):
93
101
return response
94
102
else :
95
103
return response
96
-
104
+
105
+
97
106
def model_exists (project , name , force ):
98
107
"""Checks if model already exists and either raises an error or deletes the redundant model.
99
108
@@ -138,6 +147,8 @@ def model_exists(project, name, force):
138
147
project .name
139
148
)
140
149
)
150
+
151
+
141
152
class GitIntegrate :
142
153
@classmethod
143
154
def pullViyaModel (
@@ -146,14 +157,14 @@ def pullViyaModel(
146
157
gPath ,
147
158
project = None ,
148
159
):
149
- ''' Send an API request in order to pull a model from a project in
160
+ """ Send an API request in order to pull a model from a project in
150
161
SAS Model Manager in a zipped format. The contents of the zip file
151
162
include all files found in SAS Model Manager's model UI, except that
152
163
read-only json files are updated to match the current state of the model.
153
-
164
+
154
165
After pulling down the zipped model, unpack the file in the model folder.
155
166
Overwrites files with the same name.
156
-
167
+
157
168
If supplying a model name instead of model UUID, a project name or uuid must
158
169
be supplied as well. Models in the model repository are allowed duplicate
159
170
names, therefore we need a method of parsing the returned models.
@@ -166,7 +177,7 @@ def pullViyaModel(
166
177
Base directory of the git repository.
167
178
project : string or RestObj, optional
168
179
A string or JSON response representing the project the model exists in, default is None.
169
- '''
180
+ """
170
181
# Try to pull down the model assuming a UUID or RestObj is provided
171
182
try :
172
183
if isinstance (model , RestObj ):
@@ -190,24 +201,30 @@ def pullViyaModel(
190
201
try :
191
202
if model ["name" ] == model :
192
203
modelId = model .id
193
- modelName , projectName = getZippedModel (modelId , gPath , projectName )
204
+ modelName , projectName = getZippedModel (
205
+ modelId , gPath , projectName
206
+ )
194
207
except TypeError :
195
208
if projectModels ["name" ] == model :
196
209
modelId = projectModels .id
197
- modelName , projectName = getZippedModel (modelId , gPath , projectName )
198
-
210
+ modelName , projectName = getZippedModel (
211
+ modelId , gPath , projectName
212
+ )
213
+
199
214
# Unpack the pulled down zip model and overwrite any duplicate files
200
- mPath = Path (gPath ) / '{projectName}/{modelName}' .format (projectName = projectName , modelName = modelName )
201
- with zipfile .ZipFile (str (mPath / (modelName + '.zip' )), mode = 'r' ) as zFile :
215
+ mPath = Path (gPath ) / "{projectName}/{modelName}" .format (
216
+ projectName = projectName , modelName = modelName
217
+ )
218
+ with zipfile .ZipFile (str (mPath / (modelName + ".zip" )), mode = "r" ) as zFile :
202
219
zFile .extractall (str (mPath ))
203
-
220
+
204
221
# Delete the zip model objects in the directory to minimize confusion when uploading back to SAS Model Manager
205
- for zipFile in mPath .glob (' *.zip' ):
222
+ for zipFile in mPath .glob (" *.zip" ):
206
223
zipFile .unlink ()
207
-
224
+
208
225
@classmethod
209
226
def pushGitModel (cls , gPath , modelName = None , projectName = None ):
210
- ''' Push a single model in the git repository up to SAS Model Manager. This function
227
+ """ Push a single model in the git repository up to SAS Model Manager. This function
211
228
creates an archive of all files in the directory and imports the zipped model.
212
229
213
230
Parameters
@@ -218,29 +235,31 @@ def pushGitModel(cls, gPath, modelName=None, projectName=None):
218
235
Name of model to be imported, by default None
219
236
projectName : string, optional
220
237
Name of project the model is imported from, by default None
221
- '''
238
+ """
222
239
if modelName is None and projectName is None :
223
240
modelDir = gPath
224
241
modelName = modelDir .name
225
242
projectName = modelDir .parent .name
226
243
else :
227
- modelDir = Path (gPath ) / (projectName + '/' + modelName )
228
- for zipFile in modelDir .glob (' *.zip' ):
229
- zipFile .unlink ()
244
+ modelDir = Path (gPath ) / (projectName + "/" + modelName )
245
+ for zipFile in modelDir .glob (" *.zip" ):
246
+ zipFile .unlink ()
230
247
fileNames = []
231
- fileNames .extend (sorted (Path (modelDir ).glob ('*' )))
232
- with zipfile .ZipFile (str (modelDir / (modelDir .name + '.zip' )), mode = 'w' ) as zFile :
248
+ fileNames .extend (sorted (Path (modelDir ).glob ("*" )))
249
+ with zipfile .ZipFile (
250
+ str (modelDir / (modelDir .name + ".zip" )), mode = "w"
251
+ ) as zFile :
233
252
for file in fileNames :
234
253
zFile .write (str (file ), arcname = file .name )
235
- with open (modelDir / (modelDir .name + ' .zip' ), 'rb' ) as zFile :
254
+ with open (modelDir / (modelDir .name + " .zip" ), "rb" ) as zFile :
236
255
zipIOFile = io .BytesIO (zFile .read ())
237
256
# Check if model with same name already exists in project. Delete if it exists.
238
257
model_exists (projectName , modelName , True )
239
258
mr .import_model_from_zip (modelName , projectName , zipIOFile )
240
-
259
+
241
260
@classmethod
242
- def gitRepoPush (cls , gPath , commitMessage , branch = ' origin' ):
243
- ''' Create a new commit with new files, then push changes from the local repository to a remote
261
+ def gitRepoPush (cls , gPath , commitMessage , branch = " origin" ):
262
+ """ Create a new commit with new files, then push changes from the local repository to a remote
244
263
branch. The default remote branch is origin.
245
264
246
265
Parameters
@@ -251,32 +270,32 @@ def gitRepoPush(cls, gPath, commitMessage, branch='origin'):
251
270
Commit message for the new commit
252
271
branch : str, optional
253
272
Branch name for the remote repository, by default 'origin'
254
- '''
273
+ """
255
274
repo = Repo (gPath )
256
275
repo .git .add (all = True )
257
276
repo .index .commit (commitMessage )
258
277
pushBranch = repo .remote (name = branch )
259
278
pushBranch .push ()
260
-
279
+
261
280
@classmethod
262
- def gitRepoPull (cls , gPath , branch = ' origin' ):
263
- ''' Pull down any changes from a remote branch of the git repository. The default branch is
264
- origin.
281
+ def gitRepoPull (cls , gPath , branch = " origin" ):
282
+ """ Pull down any changes from a remote branch of the git repository. The default branch is
283
+ origin.
265
284
266
285
Parameters
267
286
----------
268
287
gPath : string or Path
269
288
Base directory of the git repository.
270
289
branch : string
271
290
Branch name for the remote repository, by default 'origin'
272
- '''
291
+ """
273
292
repo = Repo (gPath )
274
293
pullBranch = repo .remote (name = branch )
275
294
pullBranch .pull ()
276
-
295
+
277
296
@classmethod
278
297
def pullGitProject (cls , gPath , project = None ):
279
- ''' Using a user provided project name, search for the project in the specified git repository,
298
+ """ Using a user provided project name, search for the project in the specified git repository,
280
299
check if the project already exists on SAS Model Manager (create a new project if it does not),
281
300
then upload each model found in the git project to SAS Model Manager
282
301
@@ -286,33 +305,40 @@ def pullGitProject(cls, gPath, project=None):
286
305
Base directory of the git repository or the project directory.
287
306
project : string or RestObj
288
307
Project name, UUID, or JSON response from SAS Model Manager.
289
- '''
308
+ """
290
309
# Check to see if provided project argument is a valid project on SAS Model Manager
291
310
projectResponse = mr .get_project (project )
292
311
project = project_exists (projectResponse , project )
293
312
projectName = project .name
294
-
313
+
295
314
# Check if project exists in git path and produce an error if it does not
296
315
pPath = Path (gPath ) / projectName
297
316
if pPath .exists ():
298
- models = [x for x in pPath .glob ('*' ) if x .is_dir ()]
317
+ models = [x for x in pPath .glob ("*" ) if x .is_dir ()]
299
318
if len (models ) == 0 :
300
- print ('No models were found in project {}.' .format (projectName ))
301
- print ('{numModels} models were found in project {projectName}.' .format (numModels = len (models ), projectName = projectName ))
319
+ print ("No models were found in project {}." .format (projectName ))
320
+ print (
321
+ "{numModels} models were found in project {projectName}." .format (
322
+ numModels = len (models ), projectName = projectName
323
+ )
324
+ )
302
325
else :
303
- raise FileNotFoundError ('No directory with the name {} was found in the specified git path.' .format (project ))
304
-
326
+ raise FileNotFoundError (
327
+ "No directory with the name {} was found in the specified git path." .format (
328
+ project
329
+ )
330
+ )
331
+
305
332
# Loop through paths of models and upload each to SAS Model Manager
306
333
for model in models :
307
334
# Remove any extra zip objects in the directory
308
- for zipFile in model .glob (' *.zip' ):
335
+ for zipFile in model .glob (" *.zip" ):
309
336
zipFile .unlink ()
310
337
cls .pushGitModel (model )
311
-
312
-
338
+
313
339
@classmethod
314
340
def pullMMProject (cls , gPath , project ):
315
- ''' Following the user provided project argument, pull down all models from the
341
+ """ Following the user provided project argument, pull down all models from the
316
342
corresponding SAS Model Manager project into the mapped git directories.
317
343
318
344
Parameters
@@ -321,32 +347,34 @@ def pullMMProject(cls, gPath, project):
321
347
Base directory of the git repository.
322
348
project : string or RestObj
323
349
The name or id of the model project, or a RestObj representation of the project.
324
- '''
350
+ """
325
351
# Check to see if provided project argument is a valid project on SAS Model Manager
326
352
projectResponse = mr .get_project (project )
327
353
project = project_exists (projectResponse , project )
328
354
projectName = project .name
329
-
355
+
330
356
# Check if project exists in git path and create it if it does not
331
357
pPath = Path (gPath ) / projectName
332
358
if not pPath .exists ():
333
359
Path (pPath ).mkdir (parents = True , exist_ok = True )
334
-
360
+
335
361
# Return a list of model names from SAS Model Manager project
336
- modelResponse = mr .get (' projects/{}/models' .format (project .id ))
362
+ modelResponse = mr .get (" projects/{}/models" .format (project .id ))
337
363
if modelResponse == []:
338
- raise FileNotFoundError ('No models were found in the specified project. A new project folder ' +
339
- 'has been created if it did not already exist within the git repository.' )
364
+ raise FileNotFoundError (
365
+ "No models were found in the specified project. A new project folder "
366
+ + "has been created if it did not already exist within the git repository."
367
+ )
340
368
modelNames = []
341
369
modelId = []
342
370
for model in modelResponse :
343
371
modelNames .append (model .name )
344
372
modelId .append (model .id )
345
-
373
+
346
374
# For each model, search for an appropriate model directory in the project directory and pull down the model
347
375
for name , id in zip (modelNames , modelId ):
348
376
mPath = pPath / name
349
377
# If the model directory does not exist, create one in the project directory
350
378
if not mPath .exists ():
351
379
Path (mPath ).mkdir (parents = True , exist_ok = True )
352
- cls .pullViyaModel (id , mPath .parents [1 ])
380
+ cls .pullViyaModel (id , mPath .parents [1 ])
0 commit comments