forked from bazelbuild/intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_version_txt.py
executable file
·134 lines (107 loc) · 4.15 KB
/
api_version_txt.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
#!/usr/bin/python3
#
# Copyright 2019 Google LLC
#
# 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.
"""Produces a api_version.txt file with the plugin API version."""
import argparse
import json
import re
from xml.dom.minidom import parseString # pylint: disable=g-importing-member
import zipfile
parser = argparse.ArgumentParser()
parser.add_argument(
"--application_info_json",
help="A json file containing the product information details.",
required=True,)
parser.add_argument(
"--check_eap",
help="If true, the build number will be checked and marked with `EAP` if it is an EAP version.",
default=False,
action="store_true")
def _is_valid_build_number(build_number):
"""Validates the build number.
Args:
build_number: The build number as text.
Returns:
true if the build number is valid and throws ValueError otherwise.
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 True
def _parse_app_info_json(application_info_json, check_eap):
"""Extracts the build number from application_info_json.
Args:
application_info_json: The name of the json file to extract the build number
from.
check_eap: If the returned build number should be checked and marked as EAP
if it is or not.
Raises:
ValueError: if the build number is invalid or it cannot be extracted.
"""
with open(application_info_json) as jf:
data = json.loads(jf.read())
build_number = data["productCode"] + "-" + data["buildNumber"]
if _is_valid_build_number(build_number):
if check_eap:
if "versionSuffix" in data and (data["versionSuffix"] == "EAP" or
data["versionSuffix"] == "Beta"):
print(build_number + " EAP")
return
print(build_number)
def _parse_app_info_jar(application_info_jar, application_info_file):
"""Extracts the build number from application_info_file which is inside application_info_jar.
Args:
application_info_jar: The name of the jar file.
application_info_file: The name of the application info file.
Raises:
ValueError: if the build number is invalid or it cannot be extracted.
"""
with open(application_info_file) as f:
application_info_file = f.read().strip()
with zipfile.ZipFile(application_info_jar, "r") as zf:
try:
data = zf.read(application_info_file)
except:
raise ValueError("Could not read application info file: " +
application_info_file)
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
try:
api_version_attr = attrs["apiVersion"]
except KeyError:
api_version_attr = attrs["number"]
if not api_version_attr:
raise ValueError("Could not find api version in application info")
if _is_valid_build_number(api_version_attr.value):
print(api_version_attr.value) # pylint: disable=superfluous-parens
def main():
args = parser.parse_args()
application_info_json = args.application_info_json
if application_info_json.endswith(".json"):
_parse_app_info_json(application_info_json, args.check_eap)
else:
raise ValueError(
"Invalid application_info_json: {}, only accepts .json files"
.format(application_info_json))
if __name__ == "__main__":
main()