| 1 |
# try_server.py - Interact with Try server |
| 2 |
# |
| 3 |
# Copyright 2019 Gregory Szorc <[email protected]> |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
# no-check-code because Python 3 native. |
| 9 |
|
| 10 |
import base64 |
| 11 |
import json |
| 12 |
import os |
| 13 |
import subprocess |
| 14 |
import tempfile |
| 15 |
|
| 16 |
from .aws import AWSConnection |
| 17 |
|
| 18 |
LAMBDA_FUNCTION = "ci-try-server-upload" |
| 19 |
|
| 20 |
|
| 21 |
def trigger_try(c: AWSConnection, rev="."): |
| 22 |
"""Trigger a new Try run.""" |
| 23 |
lambda_client = c.session.client("lambda") |
| 24 |
|
| 25 |
cset, bundle = generate_bundle(rev=rev) |
| 26 |
|
| 27 |
payload = { |
| 28 |
"bundle": base64.b64encode(bundle).decode("utf-8"), |
| 29 |
"node": cset["node"], |
| 30 |
"branch": cset["branch"], |
| 31 |
"user": cset["user"], |
| 32 |
"message": cset["desc"], |
| 33 |
} |
| 34 |
|
| 35 |
print("resolved revision:") |
| 36 |
print("node: %s" % cset["node"]) |
| 37 |
print("branch: %s" % cset["branch"]) |
| 38 |
print("user: %s" % cset["user"]) |
| 39 |
print("desc: %s" % cset["desc"].splitlines()[0]) |
| 40 |
print() |
| 41 |
|
| 42 |
print("sending to Try...") |
| 43 |
res = lambda_client.invoke( |
| 44 |
FunctionName=LAMBDA_FUNCTION, |
| 45 |
InvocationType="RequestResponse", |
| 46 |
Payload=json.dumps(payload).encode("utf-8"), |
| 47 |
) |
| 48 |
|
| 49 |
body = json.load(res["Payload"]) |
| 50 |
for message in body: |
| 51 |
print("remote: %s" % message) |
| 52 |
|
| 53 |
|
| 54 |
def generate_bundle(rev="."): |
| 55 |
"""Generate a bundle suitable for use by the Try service. |
| 56 |
|
| 57 |
Returns a tuple of revision metadata and raw Mercurial bundle data. |
| 58 |
""" |
| 59 |
# `hg bundle` doesn't support streaming to stdout. So we use a temporary |
| 60 |
# file. |
| 61 |
path = None |
| 62 |
try: |
| 63 |
fd, path = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg") |
| 64 |
os.close(fd) |
| 65 |
|
| 66 |
args = [ |
| 67 |
"hg", |
| 68 |
"bundle", |
| 69 |
"--type", |
| 70 |
"gzip-v2", |
| 71 |
"--base", |
| 72 |
"public()", |
| 73 |
"--rev", |
| 74 |
rev, |
| 75 |
path, |
| 76 |
] |
| 77 |
|
| 78 |
print("generating bundle...") |
| 79 |
subprocess.run(args, check=True) |
| 80 |
|
| 81 |
with open(path, "rb") as fh: |
| 82 |
bundle_data = fh.read() |
| 83 |
|
| 84 |
finally: |
| 85 |
if path: |
| 86 |
os.unlink(path) |
| 87 |
|
| 88 |
args = [ |
| 89 |
"hg", |
| 90 |
"log", |
| 91 |
"-r", |
| 92 |
rev, |
| 93 |
# We have to upload as JSON, so it won't matter if we emit binary |
| 94 |
# since we need to normalize to UTF-8. |
| 95 |
"-T", |
| 96 |
"json", |
| 97 |
] |
| 98 |
res = subprocess.run(args, check=True, capture_output=True) |
| 99 |
return json.loads(res.stdout)[0], bundle_data |