-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
271 lines (251 loc) · 10.5 KB
/
Copy pathapp.py
File metadata and controls
271 lines (251 loc) · 10.5 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
import sys
from time import sleep
import inquirer
import cma
import config
import exportStructure
import importStructure
import exportContent
import importContent
import login
def restructureOrgs(auth):
'''
Restructuring the org payload to something easier
'''
orgDict = {}
for org in auth['user']['organizations']:
if org['enabled']: # No point in adding disabled orgs to this variable
orgDict[org['name']] = {
'uid': org['uid'],
}
if 'is_owner' in org:
orgDict[org['name']]['isOwner'] = True
else:
orgDict[org['name']]['isOwner'] = False
return orgDict
def restructureExportStacks(stacks):
stackDict = {}
for stack in stacks['stacks']:
stackDict[stack['name']] = {
'org': stack['org_uid'],
'uid': stack['api_key'],
'masterLocale': stack['master_locale']
}
return stackDict
def restructureCreatedStack(stack):
stackDict = {
'org': stack['stack']['org_uid'],
'uid': stack['stack']['api_key'],
'masterLocale': stack['stack']['master_locale']
}
return stackDict
def findStack(orgs, authToken, region, action='EXPORT'):
'''
Choosing the org and finding the stack to either export from or import to
'''
try:
orgList = []
for name, value in orgs.items():
if value['isOwner']:
name = name + ' (You are the owner)'
orgList.append(name)
orgList = sorted(orgList)
orgList.append('Cancel and Exit')
chooseOrg = [
inquirer.List('chosenOrg',
message="{}Choose Organization to work on ({}){}".format(config.BOLD, action, config.END),
choices=orgList,
),
]
orgName = inquirer.prompt(chooseOrg)['chosenOrg'].replace(' (You are the owner)', '')
if orgName == 'Cancel and Exit':
return None, None
orgUid = orgs[orgName]['uid']
stacks = cma.getAllStacks(cma.constructAuthTokenHeader(authToken), orgUid, region)
stacks = restructureExportStacks(stacks)
if action == 'EXPORT' or action == 'IMPORT CONTENT':
stackList = []
elif action == 'IMPORT':
stackList = ['Create a new stack']
unsortedList = []
for name, _ in stacks.items():
unsortedList.append(name)
stackList = stackList + sorted(unsortedList) + ['Cancel and Exit']
chooseStack = [
inquirer.List('chosenStack',
message="{}Choose Stack to work on ({}){}".format(config.BOLD, action, config.END),
choices=stackList,
),
]
stackName = inquirer.prompt(chooseStack)['chosenStack']
if stackName == 'Create a new stack':
config.logging.info('{}New stack to be created for import{}'.format(config.BOLD, config.END))
stackName, stack = importStructure.createNewStack(authToken, orgUid, region)
if stackName:
return stackName, restructureCreatedStack(stack)
return None, None
elif stackName == 'Cancel and Exit':
return None, None
return stackName, stacks[stackName]
except TypeError:
exitProgram()
def initiateExportStackStructure(organizations, token, region):
'''
Exporting stack structure initation
'''
exportedStackName, exportedStack = findStack(organizations, token, region, 'EXPORT') # Choosing the org and stack to export from
if not exportedStackName:
return False
masterLocale = exportedStack['masterLocale']
config.logging.info('Chosen stack export structure from: {} ({}), with master locale {}'.format(exportedStackName, exportedStack['uid'], masterLocale))
folder = config.createFolder(exportedStackName)
config.checkDir(config.dataRootFolder + config.stackRootFolder + folder)
folder = {
'name': folder,
'fullPath': config.dataRootFolder + config.stackRootFolder + folder
}
config.logging.info('Stack structure will be exported to ' + folder['fullPath'])
exportStructure.exportStack(exportedStack['uid'], token, region, folder) # Exporting the stack
info = {
'stack': exportedStack,
'stackName': exportedStackName,
'apiKey': exportedStack['uid'],
'folder': folder,
'masterLocale': masterLocale,
'region': region,
}
return info
def initiateExportAll(organizations, token, region):
'''
Export All initation
'''
stackStructureExportInfo = initiateExportStackStructure(organizations, token, region) # Begin stack structure export
if not stackStructureExportInfo:
return None
contentExportInfo = exportContent.whatContentToExport(stackStructureExportInfo) # Choose what entries and assets to export
if not contentExportInfo:
return None
exportContent.iniateExportContent(stackStructureExportInfo, contentExportInfo, token)
config.addToExportReport('stackStructureExportInfo', stackStructureExportInfo, stackStructureExportInfo['folder']['fullPath'])
config.addToExportReport('contentExportInfo', contentExportInfo, stackStructureExportInfo['folder']['fullPath'])
config.logging.info('Generating Export Report.')
config.structureReport(stackStructureExportInfo['folder']['fullPath'])
config.logging.info('Finished Report: {}'.format(stackStructureExportInfo['folder']['fullPath'] + config.exportReportFile))
return True
def initiateImportStackStructure(organizations, t, r):
'''
Initating Stack Structure Import
'''
folder = exportStructure.chooseFolder()
_, importedStack = findStack(organizations, t, r, 'IMPORT') # Choosing the org and stack to import to
if not importedStack:
return False
importStructure.importStack(importedStack, t, r, folder)
return folder, importedStack
def initiateImportStackContent(token, folder, importedStack, exportReport, region):
'''
Initating Stack Content Import
'''
folder = exportReport['stackStructureExportInfo']['folder']['fullPath']
importContent.whatToImport(token, folder, importedStack, exportReport, region)
return None
# print(folder)
# masterLocale = importedStack['masterLocale']
# apiKey = importedStack['uid']
# whatToImport = importContent.whatToImport(folder, exportReport)
def whatToExport(organizations, token, region):
'''
Listing up export options
'''
exportAnswer = None
while exportAnswer != 'Go Back':
chooseAction = [
inquirer.List('chosenAction',
message="{}Choose Action to perform{}".format(config.BOLD, config.END),
choices=['Export Stack Structure and Content', 'Export Stack Structure', 'Go Back'],
),
]
exportAnswer = inquirer.prompt(chooseAction)['chosenAction']
if exportAnswer == 'Export Stack Structure and Content':
initiateExportAll(organizations, token, region)
elif exportAnswer == 'Export Stack Structure':
initiateExportStackStructure(organizations, token, region)
return None
def whatToImport(organizations, token, region):
'''
Listing up import options
'''
try:
chooseAction = [
inquirer.List('chosenAction',
message="{}Choose Action to perform:{}".format(config.BOLD, config.END),
choices=['Import Stack Structure', 'Import Stack Structure and Content', 'Import Only Content', 'Go Back'],
),
]
importAnswer = inquirer.prompt(chooseAction)['chosenAction']
if importAnswer == 'Import Stack Structure':
initiateImportStackStructure(organizations, token, region)
elif importAnswer == 'Import Stack Structure and Content':
folder, importedStack = initiateImportStackStructure(organizations, token, region)
exportReport = importContent.readExportReport(folder)
initiateImportStackContent(token, folder, importedStack, exportReport, region)
elif importAnswer == 'Import Only Content':
folder, exportReport = importContent.findImportContent(organizations, token, region)
_, importedStack = findStack(organizations, token, region, 'IMPORT CONTENT')
initiateImportStackContent(token, folder, importedStack, exportReport, region)
elif importAnswer == 'Go Back':
return None
return None
except TypeError:
exitProgram()
def exitProgram():
sleep(0.3)
config.logging.info('Exiting...')
sleep(0.3)
sys.exit()
def startupQuestion():
try:
action = [
inquirer.List('action',
message="{}Choose Action to perform:{}".format(config.BOLD, config.END),
choices=['Export', 'Import', 'Exit'],
),
]
answer = inquirer.prompt(action)['action']
return answer
except TypeError:
exitProgram()
if __name__ == '__main__':
'''
Everything starts here
'''
try:
print('''
{yellow}Export/Import Stack Structure and/or Content.{end}
{cyan}- Exports a Stack Structure, to the local disk.{end}
{blue}- Export Entries/Assets, to the local disk.{end}
{cyan}- Imports a Stack Structure, from the local disk.{end}
{blue}- Imports Entries/Assets, from the local disk.{end}
{bold}First! Answer a few questions.{end}
'''.format(yellow=config.YELLOW, cyan=config.CYAN, blue=config.BLUE, bold=config.BOLD, end=config.END))
'''
Login starts
'''
region, userInfo, liveUserInfo, token = login.startup()
config.logging.info('Logged in as: {}'.format(userInfo['username']))
orgs = restructureOrgs(liveUserInfo) # Making the org output simpler
'''
Login finished - Lets ask the user what he/she wants to do
'''
config.checkDir(config.dataRootFolder)
config.checkDir(config.dataRootFolder + config.stackRootFolder)
startupAction = None
while startupAction != 'Exit':
startupAction = startupQuestion()
if startupAction == 'Export':
whatToExport(orgs, token, region)
elif startupAction == 'Import':
whatToImport(orgs, token, region)
exitProgram()
except KeyboardInterrupt:
exitProgram()