1*33f37583SAndroid Build Coastguard Worker#!/usr/bin/env python 2*33f37583SAndroid Build Coastguard Worker# 3*33f37583SAndroid Build Coastguard Worker# Copyright (C) 2018 The Android Open Source Project 4*33f37583SAndroid Build Coastguard Worker# 5*33f37583SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*33f37583SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*33f37583SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*33f37583SAndroid Build Coastguard Worker# 9*33f37583SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*33f37583SAndroid Build Coastguard Worker# 11*33f37583SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*33f37583SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*33f37583SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*33f37583SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*33f37583SAndroid Build Coastguard Worker# limitations under the License. 16*33f37583SAndroid Build Coastguard Worker 17*33f37583SAndroid Build Coastguard Workerimport apex_manifest_pb2 18*33f37583SAndroid Build Coastguard Workerfrom google.protobuf import message 19*33f37583SAndroid Build Coastguard Workerfrom google.protobuf.json_format import MessageToJson 20*33f37583SAndroid Build Coastguard Workerimport zipfile 21*33f37583SAndroid Build Coastguard Worker 22*33f37583SAndroid Build Coastguard Workerclass ApexManifestError(Exception): 23*33f37583SAndroid Build Coastguard Worker 24*33f37583SAndroid Build Coastguard Worker def __init__(self, errmessage): 25*33f37583SAndroid Build Coastguard Worker # Apex Manifest parse error (extra fields) or if required fields not present 26*33f37583SAndroid Build Coastguard Worker self.errmessage = errmessage 27*33f37583SAndroid Build Coastguard Worker 28*33f37583SAndroid Build Coastguard Worker 29*33f37583SAndroid Build Coastguard Workerdef ValidateApexManifest(manifest_pb): 30*33f37583SAndroid Build Coastguard Worker # Checking required fields 31*33f37583SAndroid Build Coastguard Worker if manifest_pb.name == "": 32*33f37583SAndroid Build Coastguard Worker raise ApexManifestError("'name' field is required.") 33*33f37583SAndroid Build Coastguard Worker if manifest_pb.version == 0: 34*33f37583SAndroid Build Coastguard Worker raise ApexManifestError("'version' field is required.") 35*33f37583SAndroid Build Coastguard Worker if manifest_pb.noCode and (manifest_pb.preInstallHook or 36*33f37583SAndroid Build Coastguard Worker manifest_pb.postInstallHook): 37*33f37583SAndroid Build Coastguard Worker raise ApexManifestError( 38*33f37583SAndroid Build Coastguard Worker "'noCode' can't be true when either preInstallHook or postInstallHook is set" 39*33f37583SAndroid Build Coastguard Worker ) 40*33f37583SAndroid Build Coastguard Worker 41*33f37583SAndroid Build Coastguard Workerdef ParseApexManifest(file): 42*33f37583SAndroid Build Coastguard Worker try: 43*33f37583SAndroid Build Coastguard Worker with open(file, "rb") as f: 44*33f37583SAndroid Build Coastguard Worker manifest_pb = apex_manifest_pb2.ApexManifest() 45*33f37583SAndroid Build Coastguard Worker manifest_pb.ParseFromString(f.read()) 46*33f37583SAndroid Build Coastguard Worker return manifest_pb 47*33f37583SAndroid Build Coastguard Worker except message.DecodeError as err: 48*33f37583SAndroid Build Coastguard Worker raise ApexManifestError(err) 49*33f37583SAndroid Build Coastguard Worker 50*33f37583SAndroid Build Coastguard Workerdef fromApex(apexFilePath): 51*33f37583SAndroid Build Coastguard Worker with zipfile.ZipFile(apexFilePath, 'r') as apexFile: 52*33f37583SAndroid Build Coastguard Worker with apexFile.open('apex_manifest.pb') as manifestFile: 53*33f37583SAndroid Build Coastguard Worker manifest = apex_manifest_pb2.ApexManifest() 54*33f37583SAndroid Build Coastguard Worker manifest.ParseFromString(manifestFile.read()) 55*33f37583SAndroid Build Coastguard Worker return manifest 56*33f37583SAndroid Build Coastguard Worker 57*33f37583SAndroid Build Coastguard Workerdef toJsonString(manifest): 58*33f37583SAndroid Build Coastguard Worker return MessageToJson(manifest, indent=2) 59