2019-04-23 12:21:21 +00:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
|
2019-09-24 10:52:05 +00:00
|
|
|
from taskgraph.util.memoize import memoize
|
2019-04-23 12:21:21 +00:00
|
|
|
|
2019-05-27 13:00:44 +00:00
|
|
|
|
2020-07-22 13:07:28 +00:00
|
|
|
def get_variant(build_type):
|
2019-09-24 14:18:45 +00:00
|
|
|
all_variants = _fetch_all_variants()
|
|
|
|
matching_variants = [
|
2022-03-11 23:23:38 +00:00
|
|
|
variant for variant in all_variants if variant["build_type"] == build_type
|
2019-09-24 14:18:45 +00:00
|
|
|
]
|
|
|
|
number_of_matching_variants = len(matching_variants)
|
|
|
|
if number_of_matching_variants == 0:
|
2022-03-11 23:23:38 +00:00
|
|
|
raise ValueError('No variant found for build type "{}"'.format(build_type))
|
2019-09-24 14:18:45 +00:00
|
|
|
elif number_of_matching_variants > 1:
|
2022-03-11 23:23:38 +00:00
|
|
|
raise ValueError(
|
|
|
|
'Too many variants found for build type "{}"": {}'.format(
|
|
|
|
build_type, matching_variants
|
|
|
|
)
|
|
|
|
)
|
2019-09-24 14:18:45 +00:00
|
|
|
|
|
|
|
return matching_variants.pop()
|
|
|
|
|
|
|
|
|
|
|
|
@memoize
|
|
|
|
def _fetch_all_variants():
|
2022-03-11 23:23:38 +00:00
|
|
|
output = _run_gradle_process("printVariants")
|
|
|
|
content = _extract_content_from_command_output(output, prefix="variants: ")
|
2019-09-24 10:52:05 +00:00
|
|
|
return json.loads(content)
|
2019-04-23 12:21:21 +00:00
|
|
|
|
|
|
|
|
2019-08-21 15:32:01 +00:00
|
|
|
def _run_gradle_process(gradle_command, **kwargs):
|
|
|
|
gradle_properties = [
|
2022-03-11 23:23:38 +00:00
|
|
|
"-P{property_name}={value}".format(property_name=property_name, value=value)
|
2021-06-09 19:52:28 +00:00
|
|
|
for property_name, value in kwargs.items()
|
2019-08-21 15:32:01 +00:00
|
|
|
]
|
2019-04-23 12:21:21 +00:00
|
|
|
|
2022-03-11 23:23:38 +00:00
|
|
|
process = subprocess.Popen(
|
|
|
|
["./gradlew", "--no-daemon", "--quiet", gradle_command] + gradle_properties,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
universal_newlines=True,
|
|
|
|
)
|
2019-04-23 12:21:21 +00:00
|
|
|
output, err = process.communicate()
|
|
|
|
exit_code = process.wait()
|
|
|
|
|
2021-06-09 19:52:28 +00:00
|
|
|
if exit_code != 0:
|
2019-09-24 10:52:05 +00:00
|
|
|
raise RuntimeError("Gradle command returned error: {}".format(exit_code))
|
2019-04-23 12:21:21 +00:00
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_content_from_command_output(output, prefix):
|
2022-03-11 23:23:38 +00:00
|
|
|
variants_line = [line for line in output.split("\n") if line.startswith(prefix)][0]
|
|
|
|
return variants_line.split(" ", 1)[1]
|