-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.py
More file actions
308 lines (262 loc) · 9.53 KB
/
main.py
File metadata and controls
308 lines (262 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#####################################################################
## Copyright (c) Autodesk, Inc. All rights reserved
## Written by Forge Partner Development
##
## Permission to use, copy, modify, and distribute this software in
## object code form for any purpose and without fee is hereby granted,
## provided that the above copyright notice appears in all copies and
## that both that copyright notice and the limited warranty and
## restricted rights notice below appear in all supporting
## documentation.
##
## AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
## AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
## MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
## DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
## UNINTERRUPTED OR ERROR FREE.
#####################################################################
import sys
import time
import os
import requests
import argparse
from pathlib import Path
import boto3
#print(requests.get('https://www.howsmyssl.com/a/check', verify=False).json()['tls_version'])
forge_da = __import__('forge_da')
config = __import__('config')
utility = __import__('utility')
#forge_da.createWorkItem_outputPersonalAWSBucket('2',{'bucketKey':'bucket_key','inputFileNameOSS':'file_name.rvt','jsonInput':{}} )
#create folders for report and output
script_path = os.getcwd()
new_abs_path = os.path.join(script_path, 'report')
if Path(new_abs_path).is_dir()==False:
os.mkdir(new_abs_path)
new_abs_path = os.path.join(script_path, 'output')
if Path(new_abs_path).is_dir()==False:
os.mkdir(new_abs_path)
appPackagePath = './plugins/bundles/'
modelfilePath = './sample files/'
#desired nick name
desired_nickname = config.desired_nickname
#Alias
alias = config.alias
# time out max 2when get workitem status
TIMEOUT = 10 * 60 # 10min
#default width
width = 100
#default height
height = 100
parser = argparse.ArgumentParser(description='Demo Workflow of Design Automation. Input Updated Height and Width:')
parser.add_argument('--height', required=True)
parser.add_argument('--width', required=True)
#read height and width from arguments
args = parser.parse_args()
width = args.width
height = args.height
### 0. get bundle
file_list=os.listdir(appPackagePath)
index = 0
for bundle in file_list:
print ('[' + str(index) + '] ' + bundle)
index+=1
variable = input('select bundle: ')
#app package zip name
zip_name = file_list[int(variable)]
print(zip_name)
#app bundle name
appboundle_name = os.path.splitext(zip_name)[0]
#activity name
activity_name = appboundle_name
file_list=os.listdir(modelfilePath)
index = 0
for demofile in file_list:
print ('[' + str(index) + '] ' + demofile)
index+=1
variable = input('select demo file: ')
#demo file name
demo_file_name = file_list[int(variable)]
print(demo_file_name)
### 1. get token
print('---Getting token...')
token = forge_da.getToken()
if token == None:
quit()
### 2. check nick name
print('---Check nick name...')
nickname = forge_da.getNickName()
print('nick name: ' + nickname)
if nickname == None:
quit()
if nickname == config.Forge_CLIENT_ID :
#set desired nick name
if forge_da.deleteNickName(nickname) == None:
quit()
if forge_da.setNickName(desired_nickname) == None:
quit()
nickname = forge_da.getNickName()
if nickname == None:
quit()
print('current nick name: ' + nickname)
### 3. list engines
print('---List engines...')
engines = forge_da.dumpEngines()
if engines == None:
quit()
index = 0
print('***engine list***')
for eng in engines:
print(' ['+str(index) + ']' + eng)
index+=1
variable = input('select product: ')
enginename = engines[int(variable)]
print(enginename)
#print(engineAttributes(engine))
### 4. create/update app bundle
print('---Creating App Bundle...')
qualifiedAppBundleId = '{0}.{1}+{2}'.format(nickname, appboundle_name, alias)
findApp = False
print('***appbundle list***')
appbubdles = forge_da.getDefinedAppbundles()
for app in appbubdles:
print(' '+ app)
if qualifiedAppBundleId == app:
findApp = True
if findApp :
print('---App bundle already defined:' +qualifiedAppBundleId)
print('---Creating new version of app bundle...')
newAppVersionRes = forge_da.createAppBundleVersion(enginename,appboundle_name)
if newAppVersionRes == None:
quit()
print('---Uploading bundle zip...')
uploadRes = forge_da.uploadZip(appPackagePath,appboundle_name,zip_name,newAppVersionRes['uploadParameters']['formData'] )
if uploadRes == None:
quit()
print('---Updating app bundle alias...')
alaisRes = forge_da.updateAppBundleAlias(appboundle_name,alias,newAppVersionRes['version'])
if alaisRes == None:
quit()
print('***New app bundle version is created...' + str(qualifiedAppBundleId) + ' version=' + str(newAppVersionRes['version']))
else:
print('---Creating new app bundle...')
newAppRes = forge_da.createAppBundle(enginename,appboundle_name)
print(newAppRes)
if newAppRes == None:
quit()
uploadRes = forge_da.uploadZip(appPackagePath,appboundle_name,zip_name,newAppRes['uploadParameters']['formData'] )
if uploadRes == None:
quit()
print('---Creating app bundle alias...')
alaisRes = forge_da.createAppBundleAlias(appboundle_name,alias,1)
if alaisRes == None:
quit()
print('***New app bundle is created...{0}'.format(qualifiedAppBundleId))
### 5. create activity
qualifiedActivityId = '{0}.{1}+{2}'.format(nickname, activity_name, alias)
findAct = False
print('***activity list***')
activities = forge_da.getDefinedActivities()
for act in activities:
print(' ' + act)
if qualifiedActivityId == act:
findAct = True
if findAct :
# as this activity points to a AppBundle "dev" alias (which points to the last version of the bundle)
# there is no need to update it (for this sample), but this may be extended for different contexts
print('***Activity already defined:' +qualifiedActivityId)
else:
print('---Creating activity...')
res = forge_da.createActivity(enginename,appboundle_name,qualifiedAppBundleId,activity_name)
if res == None:
quit()
print('---Creating activity alias...')
res = forge_da.createActivityAlias(activity_name,alias)
if res == None:
quit()
print('***New activity is created...{0}'.format(qualifiedActivityId))
### 6. prepare source file in Forge bucket
print('---Prepare source file in Forge bucket...')
bucketKey = nickname.lower() + "_designautomation"
res = forge_da.createBucket(bucketKey)
if res == None:
quit()
res = forge_da.uploadFileToBucket(modelfilePath,bucketKey,demo_file_name)
if res == None:
quit()
inputFileNameOSS = res['objectKey']
### 7. create workitem
print('---Create workitem...')
param={}
param['bucketKey'] = bucketKey
param['inputFileNameOSS'] = inputFileNameOSS
param['jsonInput'] ={}
param['jsonInput']['width'] = width
param['jsonInput']['height'] = height
res = forge_da.createWorkItem(qualifiedActivityId,param)
if res == None:
quit()
#work item ID
workItemId = res['id']
#url of output file on Forge OSS bucket
ossOutputUrl = res['ossOutputUrl']
outputFileNameOSS = res['outputFileNameOSS']
### 8. check workitem status and download result/report
status = 'pending'
print('---work item running!')
start_time = time.time()
while status == 'pending' or status == 'inprogress':
statusRes = forge_da.getWorkItemStatus(workItemId)
if statusRes == None:
quit()
status = statusRes['status']
if status != 'pending' and status != 'inprogress':
break
elapsed_time = time.time() - start_time
if elapsed_time > TIMEOUT:
print(' get work item status timeout!')
break
print('****work item status <outour to Forge OSS bucket>:' + status)
### 9. get output file or log file
reportUrl = statusRes['reportUrl']
if status == 'success':
# #successful job. download report and the output file from Forge OSS bucket
utility.downloadResults({'workItemId':workItemId,'report':{'url':reportUrl},
'output':{'url':ossOutputUrl,'header':{'Authorization': config.token},'fileName':outputFileNameOSS}})
else:
#failed job. download the report file
utility.downloadResults({'workItemId':workItemId,'report':{'url':reportUrl},'output':None})
# #other user scenarios ###
## output to personal AWS bucket
print('---Create workItem, output to personal AWS bucket...')
inputparam={}
inputparam['bucketKey'] = bucketKey
inputparam['inputFileNameOSS'] = inputFileNameOSS
inputparam['jsonInput'] ={}
inputparam['jsonInput']['width'] = width
inputparam['jsonInput']['height'] = height
res = forge_da.createWorkItem_outputPersonalAWSBucket(qualifiedActivityId,inputparam)
if res == None:
quit()
#work item ID
workItemId = res['id']
# ###check workitem status and download result/report
status = 'pending'
print('---work item running!')
start_time = time.time()
while status == 'pending' or status == 'inprogress':
statusRes = forge_da.getWorkItemStatus(workItemId)
if statusRes == None:
quit()
status = statusRes['status']
if status != 'pending' and status != 'inprogress':
break
elapsed_time = time.time() - start_time
if elapsed_time > TIMEOUT:
print(' get work item status timeout!')
break
print('****work item status <output to personal AWS bucket>:' + status)
# ### get log file only as the output has been uploaded to AWS bucket
# try with AWS SDK to download it needed.
reportUrl = statusRes['reportUrl']
utility.downloadResults({'workItemId':workItemId,'report':{'url':reportUrl},'output':None})
##