[Updater] Generates change and include in the email
Test: Run locally and check email
Change-Id: I7d67d138cab20a387b2bf5010d8f802113f8bed7
diff --git a/notifier.py b/notifier.py
index d33dc65..982354c 100644
--- a/notifier.py
+++ b/notifier.py
@@ -15,14 +15,16 @@
Example usage:
external_updater_notifier \
- --result_file ~/updater/new_result \
--history ~/updater/history \
- --recipients xxx@xxx.xxx
+ --generate_change \
+ --recipients xxx@xxx.xxx \
+ googletest
"""
import argparse
import json
import os
+import re
import subprocess
import time
@@ -33,33 +35,45 @@
parser = argparse.ArgumentParser(
description='Check updates for third party projects in external/.')
parser.add_argument(
- '--result_file',
- help='Json check result generated by external updater.')
- parser.add_argument(
'--history',
help='Path of history file. If doesn'
't exist, a new one will be created.')
parser.add_argument(
'--recipients',
help='Comma separated recipients of notification email.')
+ parser.add_argument(
+ '--generate_change',
+ help='If set, an upgrade change will be uploaded to Gerrit.',
+ action='store_true', required=False)
+ parser.add_argument(
+ 'paths', nargs='*',
+ help='Paths of the project.')
return parser.parse_args()
-def _send_email(proj, latest_ver, recipient):
- print('Sending email for {}: {}'.format(proj, latest_ver))
- msg = """New version: {}
+CHANGE_URL_PATTERN = r'(https:\/\/[^\s]*android-review[^\s]*) Upgrade'
+CHANGE_URL_RE = re.compile(CHANGE_URL_PATTERN)
-To upgrade:
- tools/external_updater/updater.sh update {}""".format(
- latest_ver, proj)
+
+def _send_email(proj, latest_ver, recipient, upgrade_log):
+ print('Sending email for {}: {}'.format(proj, latest_ver))
+ msg = "New version: {}".format(latest_ver)
+ match = CHANGE_URL_RE.search(upgrade_log)
+ if match is not None:
+ msg += '\n\nAn upgrade change is generated at:\n{}'.format(
+ match.group(1))
+
+ msg += '\n\n'
+ msg += upgrade_log
+
subprocess.run(['sendgmr', '--to=' + recipient,
'--subject=' + proj], check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
input=msg, encoding='ascii')
-def _process_results(history, results, recipient):
+def _process_results(args, history, results):
for proj, res in results.items():
if 'latest' not in res:
continue
@@ -69,8 +83,9 @@
continue
proj_history = history.setdefault(proj, {})
if latest_ver not in proj_history:
+ upgrade_log = _upgrade(proj) if args.generate_change else ""
try:
- _send_email(proj, latest_ver, recipient)
+ _send_email(proj, latest_ver, args.recipients, upgrade_log)
proj_history[latest_ver] = int(time.time())
except subprocess.CalledProcessError as err:
msg = """Failed to send email for {} ({}).
@@ -79,10 +94,13 @@
print(msg)
+RESULT_FILE_PATH = '/tmp/update_check_result.json'
+
+
def send_notification(args):
"""Compare results and send notification."""
results = {}
- with open(args.result_file, 'r') as f:
+ with open(RESULT_FILE_PATH, 'r') as f:
results = json.load(f)
history = {}
try:
@@ -91,16 +109,45 @@
except FileNotFoundError:
pass
- _process_results(history, results, args.recipients)
+ _process_results(args, history, results)
with open(args.history, 'w') as f:
json.dump(history, f, sort_keys=True)
+def _upgrade(proj):
+ out = subprocess.run(['out/soong/host/linux-x86/bin/external_updater',
+ 'update', '--branch_and_commit', '--push_change',
+ proj],
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ cwd=os.environ['ANDROID_BUILD_TOP'])
+ stdout = out.stdout.decode('utf-8')
+ stderr = out.stderr.decode('utf-8')
+ return """
+====================
+| Debug Info |
+====================
+-=-=-=-=stdout=-=-=-=-
+{}
+
+-=-=-=-=stderr=-=-=-=-
+{}
+""".format(stdout, stderr)
+
+
+def _check_updates(args):
+ subprocess.run(['out/soong/host/linux-x86/bin/external_updater',
+ 'check',
+ '--json_output', RESULT_FILE_PATH,
+ '--delay', '0'] + args.paths,
+ cwd=os.environ['ANDROID_BUILD_TOP'])
+
+
def main():
"""The main entry."""
args = parse_args()
+ _check_updates(args)
send_notification(args)