| """Produces a api_version.txt file with the plugin API version. |
| """ |
| |
| import argparse |
| import re |
| from xml.dom.minidom import parseString |
| import zipfile |
| |
| parser = argparse.ArgumentParser() |
| |
| parser.add_argument( |
| "--application_info_jar", |
| help="The jar file containing the application info xml", |
| required=True,) |
| parser.add_argument( |
| "--application_info_name", |
| help="A .txt file containing the application info xml name", |
| required=True,) |
| |
| |
| def _parse_build_number(build_number): |
| """Parses the build number. |
| |
| Args: |
| build_number: The build number as text. |
| Returns: |
| build_number, build_number_without_product_code. |
| Raises: |
| ValueError: if the build number is invalid. |
| """ |
| match = re.match(r"^([A-Z]+-)?([0-9]+)((\.[0-9]+)*)", build_number) |
| if match is None: |
| raise ValueError("Invalid build number: " + build_number) |
| |
| return match.group(1) + match.group(2) + match.group(3) |
| |
| |
| def main(): |
| |
| args = parser.parse_args() |
| |
| with open(args.application_info_name) as f: |
| application_info_name = f.read().strip() |
| |
| with zipfile.ZipFile(args.application_info_jar, "r") as zf: |
| try: |
| data = zf.read(application_info_name) |
| except: |
| raise ValueError("Could not read application info file: " + |
| application_info_name) |
| component = parseString(data) |
| |
| build_elements = component.getElementsByTagName("build") |
| if not build_elements: |
| raise ValueError("Could not find <build> element.") |
| if len(build_elements) > 1: |
| raise ValueError("Ambiguous <build> element.") |
| build_element = build_elements[0] |
| |
| attrs = build_element.attributes |
| if attrs.has_key("apiVersion"): |
| api_version_attr = attrs.get("apiVersion") |
| else: |
| api_version_attr = attrs.get("number") |
| |
| if not api_version_attr: |
| raise ValueError("Could not find api version in application info") |
| |
| api_version = _parse_build_number(api_version_attr.value) |
| print api_version |
| |
| |
| if __name__ == "__main__": |
| main() |