tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

utils.py (2452B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import datetime
      6 import json
      7 
      8 import requests
      9 
     10 from qm_try_analysis.logging import error, info, warning
     11 
     12 
     13 def readJSONFile(FileName):
     14    f = open(FileName)
     15    p = json.load(f)
     16    f.close()
     17    return p
     18 
     19 
     20 def writeJSONFile(FileName, Content):
     21    with open(FileName, "w") as outfile:
     22        json.dump(Content, outfile, indent=4)
     23 
     24 
     25 def dateback(days):
     26    today = datetime.date.today()
     27    delta = datetime.timedelta(days)
     28    return today - delta
     29 
     30 
     31 def lastweek():
     32    today = datetime.date.today()
     33    delta = datetime.timedelta(days=7)
     34    return today - delta
     35 
     36 
     37 # Given a set of build ids, fetch the repository base URL for each id.
     38 def fetchBuildRevisions(buildids):
     39    buildhub_url = "https://buildhub.moz.tools/api/search"
     40    delids = {}
     41    for bid in buildids:
     42        info(f"Fetching revision for build {bid}.")
     43        body = {"size": 1, "query": {"term": {"build.id": bid}}}
     44        resp = requests.post(url=buildhub_url, json=body)
     45        hits = resp.json()["hits"]["hits"]
     46        if len(hits) > 0:
     47            buildids[bid] = (
     48                hits[0]["_source"]["source"]["repository"]
     49                + "/annotate/"
     50                + hits[0]["_source"]["source"]["revision"]
     51            )
     52        else:
     53            warning(f"No revision for build.id {bid}")
     54            delids[bid] = "x"
     55    for bid in delids:
     56        buildids.pop(bid)
     57 
     58 
     59 def readExecutionFile(workdir):
     60    exefile = f"{workdir}/qmexecutions.json"
     61    try:
     62        return readJSONFile(exefile)
     63    except OSError:
     64        return []
     65 
     66 
     67 def writeExecutionFile(workdir, executions):
     68    exefile = f"{workdir}/qmexecutions.json"
     69    try:
     70        writeJSONFile(exefile, executions)
     71    except OSError:
     72        error("Error writing execution record.")
     73 
     74 
     75 def getLastRunFromExecutionFile(workdir):
     76    executions = readExecutionFile(workdir)
     77    if len(executions) > 0:
     78        return executions[len(executions) - 1]
     79    return {}
     80 
     81 
     82 def updateLastRunToExecutionFile(workdir, run):
     83    executions = readExecutionFile(workdir)
     84    executions[len(executions) - 1] = run
     85    writeExecutionFile(workdir, executions)
     86 
     87 
     88 def addNewRunToExecutionFile(workdir, run):
     89    executions = readExecutionFile(workdir)
     90    executions.append(run)
     91    writeExecutionFile(workdir, executions)