summaryrefslogtreecommitdiffstats
path: root/testing/performance/hooks_android_startup.py
blob: 706dff1c0a48b38b856370d9fc186b65c4f15254 (plain)
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
# 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 pathlib
import re
import subprocess
from datetime import datetime, timedelta

import requests
from mozperftest.system.android_startup import (
    BASE_URL_DICT,
    DATETIME_FORMAT,
    KEY_ARCHITECTURE,
    KEY_COMMIT,
    KEY_DATETIME,
    KEY_NAME,
    KEY_PRODUCT,
)
from mozversioncontrol import get_repository_object

HTTP_200_OKAY = 200


def before_iterations(kw):
    product = kw["AndroidStartUp_product"]
    architecture = "arm64-v8a"
    if product == "geckoview_example":
        architecture = "aarch64"
    if get_repository_object("").name == "git":
        commit_info = subprocess.getoutput("git log --max-count 1")
    else:
        commit_info = subprocess.getoutput("hg log -l 1")
    commit_date = re.search(r"[Dd]ate:\s+([:\s\w]+)\s+", str(commit_info)).group(1)
    download_date = (
        datetime.strptime(commit_date, "%a %b %d %H:%M:%S %Y") - timedelta(days=1)
    ).strftime(DATETIME_FORMAT)

    nightly_url = BASE_URL_DICT[product].format(
        date=download_date, architecture=architecture
    )
    filename = f"{product}_nightly_{architecture}.apk"
    print("Fetching {}...".format(filename), end="", flush=True)
    download_apk_as_date(nightly_url, download_date, filename)
    print(f"Downloaded {product} for date: {download_date}")

    kw["apk_metadata"] = {
        KEY_NAME: filename,
        KEY_DATETIME: download_date,
        KEY_COMMIT: "",
        KEY_ARCHITECTURE: architecture,
        KEY_PRODUCT: product,
    }


def download_apk_as_date(nightly_url, download_date_string, filename):
    apk = requests.get(nightly_url)
    if apk.status_code != HTTP_200_OKAY:
        raise Exception(
            f"Something went wrong downloading the apk check to make sure you have entered"
            f" a date that is valid and that the apk for the date you have requested("
            f"{download_date_string}) is available and that the URL({nightly_url}) is also "
            f"valid"
        )
    pathlib.Path(filename).write_bytes(apk.content)