def _unpack_stub_universe_json(self, scratchdir, stub_universe_file):
stub_universe_json = json.loads(stub_universe_file.read().decode('utf-8'), object_pairs_hook=collections.OrderedDict)
# put package files into a subdir of scratchdir: avoids conflicts with reuse of scratchdir elsewhere
pkgdir = os.path.join(scratchdir, 'stub-universe-{}'.format(self._pkg_name))
os.makedirs(pkgdir)
if 'packages' not in stub_universe_json:
raise Exception('Expected "packages" key in root of stub universe JSON: {}'.format(
self._stub_universe_url))
if len(stub_universe_json['packages']) != 1:
raise Exception('Expected single "packages" entry in stub universe JSON: {}'.format(
self._stub_universe_url))
# note: we delete elements from package_json they're unpacked, as package_json is itself written to a file
package_json = stub_universe_json['packages'][0]
def extract_json_file(package_dict, name):
file_dict = package_dict.get(name)
if file_dict is not None:
del package_dict[name]
# ensure that the file has a trailing newline (json.dump() doesn't!)
with open(os.path.join(pkgdir, name + '.json'), 'w') as fileref:
content = json.dumps(file_dict, indent=2)
fileref.write(content)
if not content.endswith('\n'):
fileref.write('\n')
extract_json_file(package_json, 'command')
extract_json_file(package_json, 'config')
extract_json_file(package_json, 'resource')
marathon_json = package_json.get('marathon', {}).get('v2AppMustacheTemplate')
if marathon_json is not None:
del package_json['marathon']
with open(os.path.join(pkgdir, 'marathon.json.mustache'), 'w') as marathon_file:
marathon_file.write(base64.standard_b64decode(marathon_json).decode())
if 'releaseVersion' in package_json:
del package_json['releaseVersion']
with open(os.path.join(pkgdir, 'package.json'), 'w') as package_file:
json.dump(package_json, package_file, indent=2)
return pkgdir
评论列表
文章目录