diff options
author | Dario Freni <dariofreni@google.com> | 2018-12-27 12:44:01 +0000 |
---|---|---|
committer | Abhijeet Kaur <abkaur@google.com> | 2018-12-28 11:05:42 +0000 |
commit | adbee5d0c204cfcf9ddfcc7ace3dddf0225f576e (patch) | |
tree | 0179d7f84cca389ec73d2786d8fbf94cb35c601f /apexer/apex_manifest.py | |
parent | 9c3defa5622638e9192a4fe5050728d0cfe0d210 (diff) |
Revert "Revert "Use protobuf as schema for JSON APEX manifest""
This reverts commit 0fe26b1e6832d7fe6c19e728c21fdc0e9be2ed44.
Reason for revert: Testing the breakage.
Bug: 122067734
Test: Import error not deterministically reproducible. 'embedded_launcher: true' may remove the import error as suggested in the bug discussion thread.
Change-Id: Iefd8c6c05c7bcf83a51aeed141eaaca436eed3cc
Diffstat (limited to 'apexer/apex_manifest.py')
-rw-r--r-- | apexer/apex_manifest.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/apexer/apex_manifest.py b/apexer/apex_manifest.py new file mode 100644 index 0000000..8c7eca4 --- /dev/null +++ b/apexer/apex_manifest.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# +# Copyright (C) 2018 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import manifest_schema_pb2 +from google.protobuf.json_format import Parse +from google.protobuf.json_format import ParseError + +class ApexManifest: + # Default values + package_name = "" + version_number = 0 + pre_install_hook = "" + def __init__(self, manifest_json): + self.package_name = manifest_json["name"] + self.version_number = manifest_json["version"] + if('pre_install_hook' in manifest_json): + self.pre_install_hook = manifest_json["pre_install_hook"] + +class ApexManifestError(Exception): + def __init__(self, errmessage): + # Apex Manifest parse error (extra fields) or if required fields not present + self.errmessage = errmessage + +def ValidateApexManifest(manifest_raw): + try: + manifest_json = json.loads(manifest_raw) + # TODO: The version of protobuf library present in the Android tree at the time of writing + # doesn't support the json_name field name. Proto converts underscore field names to + # camelCase. To use protobuf with "pre_install_hook" field name, converting to camelCase + # explicitly. b/121546801 + # Convert field names to camelCase + for field, value in manifest_json.items(): + manifest_json[to_camel_case(field)] = manifest_json.pop(field) + manifest_pb = Parse(json.dumps(manifest_json), manifest_schema_pb2.ManifestSchema()) + except (ParseError, ValueError) as err: + raise ApexManifestError(err) + # Checking required fields + if manifest_pb.name == "": + raise ApexManifestError("'name' field is required.") + if manifest_pb.version == 0: + raise ApexManifestError("'version' field is required.") + return ApexManifest(manifest_json) + +def to_camel_case(snake_str): + components = snake_str.split('_') + return components[0] + ''.join(x.title() for x in components[1:])
\ No newline at end of file |