-
Notifications
You must be signed in to change notification settings - Fork 62
/
generatejson.py
executable file
·292 lines (252 loc) · 10.1 KB
/
generatejson.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Generate Json file for installapplications
# Usage: python generatejson.py --item \
# item-name='A name' \
# item-path='A path' \
# item-stage='A stage' \
# item-type='A type' \
# item-url='A url' \
# script-do-not-wait='A boolean' \
# pkg-skip-if='A string' \
# retries='An integer' \
# retrywait='An integer' \
# --base-url URL \
# --output PATH
#
# --item can be used unlimited times
# If you do do not specify an item-url, one will be generated as
# base-url/stage/file-name-of-item
# Future plan for this tool is to add AWS S3 integration for auto-upload
import hashlib
import json
import argparse
import os
import subprocess
import tempfile
from xml.dom import minidom
def gethash(filename):
hash_function = hashlib.sha256()
if not os.path.isfile(filename):
print('FILE NOT FOUND - CHECK YOUR PATH')
return 'FILE NOT FOUND - CHECK YOUR PATH'
fileref = open(filename, 'rb')
while 1:
chunk = fileref.read(2**16)
if not chunk:
break
hash_function.update(chunk)
fileref.close()
return hash_function.hexdigest()
def getpkginfopath(filename):
'''Extracts the package BOM with xar'''
cmd = ['/usr/bin/xar', '-tf', filename]
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(bom, err) = proc.communicate()
bom = bom.strip().split(b'\n')
if proc.returncode == 0:
for entry in bom:
if entry.startswith(b'PackageInfo'):
return entry
elif entry.endswith(b'.pkg/PackageInfo'):
return entry
else:
print("Error: %s while extracting BOM for %s" % (err, filename))
def extractpkginfo(filename):
'''Takes input of a file path and returns a file path to the
extracted PackageInfo file.'''
cwd = os.getcwd()
if not os.path.isfile(filename):
return
else:
tmpFolder = tempfile.mkdtemp()
os.chdir(tmpFolder)
# need to get path from BOM
pkgInfoPath = getpkginfopath(filename).decode('utf-8')
extractedPkgInfoPath = os.path.join(tmpFolder, pkgInfoPath)
cmd = ['/usr/bin/xar', '-xf', filename, pkgInfoPath]
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = proc.communicate()
os.chdir(cwd)
return extractedPkgInfoPath
def getpkginfo(filename):
'''Takes input of a file path and returns strings of the
package identifier and version from PackageInfo.'''
if not os.path.isfile(filename):
return "", ""
else:
pkgInfoPath = extractpkginfo(os.path.abspath(filename))
dom = minidom.parse(pkgInfoPath)
pkgRefs = dom.getElementsByTagName('pkg-info')
for ref in pkgRefs:
pkgId = ref.attributes['identifier'].value
pkgVersion = ref.attributes['version'].value
return pkgId, pkgVersion
def build_item_dict(itemsToProcess, base_url):
'''Takes a dict of item and the base_url where root dir is hosted
itemsToProcess = [
{
'item-name': '',
'item-path': '',
'item-stage': '',
'item-type': '',
'item-url': '',
'script-do-not-wait': '',
'pkg-skip-if': '',
'retries': int,
'retrywait': int
},
...
]
returns a dict that can be dumped to bootstrap.json'''
# Create our stages now so InstallApplications won't blow up
stages = {
'preflight': [],
'setupassistant': [],
'userland': []
}
# Process each item in the order they were passed in
for item in itemsToProcess:
itemJson = {}
# Get the file extension of the file
fileExt = os.path.splitext(item['item-path'])[1]
# Get the file name of the file
fileName = os.path.basename(item['item-path'])
# Get the full path of the file
filePath = item['item-path']
# Determine the type of item to process - for scripts, default to
# rootscript
if fileExt in ('.py', '.sh', '.rb', '.php'):
try:
itemJson['type'] = itemType = item['item-type']
except KeyError:
itemJson['type'] = itemType = 'rootscript'
elif fileExt == '.pkg':
itemJson['type'] = itemType = 'package'
else:
print('Could not determine package type for item or unsupported: \
%s' % str(item))
exit(1)
if itemType not in ('package', 'rootscript', 'userscript'):
print('item-type malformed: %s' % str(item['item-type']))
exit(1)
# Determine the stage of the item to process - default to userland
try:
if item['item-stage'] in ('preflight', 'setupassistant',
'userland'):
itemStage = item['item-stage']
pass
else:
print('item-stage malformed: %s' % str(item['item-stage']))
exit(1)
except KeyError:
itemStage = 'userland'
# Determine the url of the item to process - defaults to
# baseurl/stage/filename
try:
itemJson['url'] = item['item-url']
except KeyError:
itemJson['url'] = '%s/%s/%s' % (base_url, itemStage, fileName)
# Determine the name of the item to process - defaults to the filename
if not item['item-name']:
itemJson['name'] = fileName
else:
itemJson['name'] = item['item-name']
# Determine the hash of the item to process - SHA256
itemJson['hash'] = gethash(filePath)
# Add information for scripts and packages
if itemType in ('rootscript', 'userscript'):
if itemType == 'userscript':
# Pass the userscripts folder path
itemJson['file'] = '/Library/'\
'installapplications/userscripts/%s' % fileName
else:
itemJson['file'] = '/Library/'\
'installapplications/%s' % fileName
# Check crappy way of doing booleans
try:
if item['script-do-not-wait'] in ('true', 'True', '1',
'false', 'False', '0'):
# If True, pass the key to the item
if item['script-do-not-wait'] in ('true', 'True', '1'):
itemJson['donotwait'] = True
else:
print(
'script-do-not-wait malformed: %s ' %
str(item['script-do-not-wait'])
)
exit(1)
except:
itemJson['donotwait'] = False
# If packages, we need the version and packageid
elif itemType == 'package':
(pkgId, pkgVersion) = getpkginfo(filePath)
itemJson['file'] = '/Library/'\
'installapplications/%s' % fileName
itemJson['packageid'] = pkgId
itemJson['version'] = pkgVersion
try:
# handle skipping based on architecture
if item['pkg-skip-if'] not in ('false', 'False', '0'):
# Add the key to the item
if item['pkg-skip-if'] in ('intel', 'x86_64',
'apple_silicon', 'arm64'):
itemJson['skip_if'] = item['pkg-skip-if']
except:
pass
# Add retries and retry wait if they're set. Cast to int because
# argparse defaults to these being strings.
if item['retries'] is not None:
itemJson['retries'] = int(item['retries'])
if item['retrywait'] is not None:
itemJson['retrywait'] = int(item['retrywait'])
# Append the info to the appropriate stage
stages[itemStage].append(itemJson)
return stages
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--base-url', default=None, action='store',
help='Base URL to where root dir is hosted')
parser.add_argument('--output', default=None, action='store',
help='Required: Output directory to save json')
parser.add_argument('--item', default=None, action='append', nargs=9,
metavar=(
'item-name', 'item-path', 'item-stage',
'item-type', 'item-url', 'script-do-not-wait',
'pkg-skip-if', 'retries', 'retrywait'),
help='Required: Options for item. All items are \
required. Scripts default to rootscript and stage \
Scripts default to rootscript and stage defaults to userland')
args = parser.parse_args()
# Bail if we don't have one item, the base url and the output dir
if not args.item or not args.base_url or not args.output:
parser.print_help()
exit(1)
# Let's first loop through the items and convert everything to key value
# pairs
itemsToProcess = []
for item in args.item:
processedItem = {}
for itemOption in item:
values = itemOption.split('=', 1)
processedItem[values[0]] = values[1]
itemsToProcess.append(processedItem)
# Create our stages now so InstallApplications won't blow up
stages = build_item_dict(itemsToProcess=itemsToProcess, base_url=args.base_url)
# Saving the json file to the output directory path
savePath = os.path.join(args.output, 'bootstrap.json')
# Sort the primary keys, but not the sub keys, so things are in the correct
# order
try:
with open(savePath, 'w') as outFile:
json.dump(stages, outFile, sort_keys=True, indent=2)
except IOError:
print('[Error] Not a valid directory: %s' % savePath)
exit(1)
print('Json saved to %s' % savePath)
if __name__ == '__main__':
main()