summaryrefslogtreecommitdiffstats
path: root/dom/quota/scripts/utils.py
blob: d5322728a5f6bdfa0bcfab552a877f61b6dd2517 (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
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
# 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 datetime
import json

import requests


def readJSONFile(FileName):
    f = open(FileName, "r")
    p = json.load(f)
    f.close()
    return p


def writeJSONFile(FileName, Content):
    with open(FileName, "w") as outfile:
        json.dump(Content, outfile, indent=4)


def dateback(days):
    today = datetime.date.today()
    delta = datetime.timedelta(days)
    return today - delta


def lastweek():
    today = datetime.date.today()
    delta = datetime.timedelta(days=7)
    return today - delta


# Given a set of build ids, fetch the repository base URL for each id.
def fetchBuildRevisions(buildids):
    buildhub_url = "https://buildhub.moz.tools/api/search"
    delids = {}
    for bid in buildids:
        print("Fetching revision for build {}.".format(bid))
        body = {"size": 1, "query": {"term": {"build.id": bid}}}
        resp = requests.post(url=buildhub_url, json=body)
        hits = resp.json()["hits"]["hits"]
        if len(hits) > 0:
            buildids[bid] = (
                hits[0]["_source"]["source"]["repository"]
                + "/annotate/"
                + hits[0]["_source"]["source"]["revision"]
            )
        else:
            print("No revision for build.id {}".format(bid))
            delids[bid] = "x"
    for bid in delids:
        buildids.pop(bid)


def readExecutionFile(workdir):
    exefile = "{}/qmexecutions.json".format(workdir)
    try:
        return readJSONFile(exefile)
    except OSError:
        return []


def writeExecutionFile(workdir, executions):
    exefile = "{}/qmexecutions.json".format(workdir)
    try:
        writeJSONFile(exefile, executions)
    except OSError:
        print("Error writing execution record.")


def getLastRunFromExecutionFile(workdir):
    executions = readExecutionFile(workdir)
    if len(executions) > 0:
        return executions[len(executions) - 1]
    return {}


def updateLastRunToExecutionFile(workdir, run):
    executions = readExecutionFile(workdir)
    executions[len(executions) - 1] = run
    writeExecutionFile(workdir, executions)


def addNewRunToExecutionFile(workdir, run):
    executions = readExecutionFile(workdir)
    executions.append(run)
    writeExecutionFile(workdir, executions)