Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 1 | # Copyright (C) 2018 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | """Send notification email if new version is found. |
| 15 | |
| 16 | Example usage: |
| 17 | external_updater_notifier \ |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 18 | --history ~/updater/history \ |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 19 | --generate_change \ |
| 20 | --recipients xxx@xxx.xxx \ |
| 21 | googletest |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 22 | """ |
| 23 | |
Haibo Huang | 11c4a75 | 2019-01-31 15:07:03 -0800 | [diff] [blame] | 24 | from datetime import timedelta, datetime |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 25 | import argparse |
| 26 | import json |
| 27 | import os |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 28 | import re |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 29 | import subprocess |
| 30 | import time |
| 31 | |
Haibo Huang | 11c4a75 | 2019-01-31 15:07:03 -0800 | [diff] [blame] | 32 | import git_utils |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 33 | |
| 34 | def parse_args(): |
| 35 | """Parses commandline arguments.""" |
| 36 | |
| 37 | parser = argparse.ArgumentParser( |
| 38 | description='Check updates for third party projects in external/.') |
| 39 | parser.add_argument( |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 40 | '--history', |
| 41 | help='Path of history file. If doesn' |
| 42 | 't exist, a new one will be created.') |
| 43 | parser.add_argument( |
| 44 | '--recipients', |
| 45 | help='Comma separated recipients of notification email.') |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 46 | parser.add_argument( |
| 47 | '--generate_change', |
| 48 | help='If set, an upgrade change will be uploaded to Gerrit.', |
| 49 | action='store_true', required=False) |
| 50 | parser.add_argument( |
| 51 | 'paths', nargs='*', |
| 52 | help='Paths of the project.') |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 53 | |
| 54 | return parser.parse_args() |
| 55 | |
| 56 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 57 | CHANGE_URL_PATTERN = r'(https:\/\/[^\s]*android-review[^\s]*) Upgrade' |
| 58 | CHANGE_URL_RE = re.compile(CHANGE_URL_PATTERN) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 59 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 60 | |
| 61 | def _send_email(proj, latest_ver, recipient, upgrade_log): |
| 62 | print('Sending email for {}: {}'.format(proj, latest_ver)) |
| 63 | msg = "New version: {}".format(latest_ver) |
| 64 | match = CHANGE_URL_RE.search(upgrade_log) |
| 65 | if match is not None: |
| 66 | msg += '\n\nAn upgrade change is generated at:\n{}'.format( |
| 67 | match.group(1)) |
| 68 | |
| 69 | msg += '\n\n' |
| 70 | msg += upgrade_log |
| 71 | |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 72 | subprocess.run(['sendgmr', '--to=' + recipient, |
| 73 | '--subject=' + proj], check=True, |
| 74 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 75 | input=msg, encoding='ascii') |
| 76 | |
| 77 | |
Haibo Huang | 11c4a75 | 2019-01-31 15:07:03 -0800 | [diff] [blame] | 78 | NOTIFIED_TIME_KEY_NAME = 'latest_notified_time' |
| 79 | |
| 80 | |
| 81 | def _should_notify(latest_ver, proj_history): |
| 82 | if latest_ver in proj_history: |
| 83 | # Processed this version before. |
| 84 | return False |
| 85 | |
| 86 | timestamp = proj_history.get(NOTIFIED_TIME_KEY_NAME, 0) |
| 87 | time_diff = datetime.today() - datetime.fromtimestamp(timestamp) |
| 88 | if git_utils.is_commit(latest_ver) and time_diff <= timedelta(days=30): |
| 89 | return False |
| 90 | |
| 91 | return True |
| 92 | |
| 93 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 94 | def _process_results(args, history, results): |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 95 | for proj, res in results.items(): |
| 96 | if 'latest' not in res: |
| 97 | continue |
| 98 | latest_ver = res['latest'] |
| 99 | current_ver = res['current'] |
| 100 | if latest_ver == current_ver: |
| 101 | continue |
| 102 | proj_history = history.setdefault(proj, {}) |
Haibo Huang | 11c4a75 | 2019-01-31 15:07:03 -0800 | [diff] [blame] | 103 | if _should_notify(latest_ver, proj_history): |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 104 | upgrade_log = _upgrade(proj) if args.generate_change else "" |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 105 | try: |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 106 | _send_email(proj, latest_ver, args.recipients, upgrade_log) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 107 | proj_history[latest_ver] = int(time.time()) |
Haibo Huang | 11c4a75 | 2019-01-31 15:07:03 -0800 | [diff] [blame] | 108 | proj_history[NOTIFIED_TIME_KEY_NAME] = int(time.time()) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 109 | except subprocess.CalledProcessError as err: |
| 110 | msg = """Failed to send email for {} ({}). |
| 111 | stdout: {} |
| 112 | stderr: {}""".format(proj, latest_ver, err.stdout, err.stderr) |
| 113 | print(msg) |
| 114 | |
| 115 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 116 | RESULT_FILE_PATH = '/tmp/update_check_result.json' |
| 117 | |
| 118 | |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 119 | def send_notification(args): |
| 120 | """Compare results and send notification.""" |
| 121 | results = {} |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 122 | with open(RESULT_FILE_PATH, 'r') as f: |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 123 | results = json.load(f) |
| 124 | history = {} |
| 125 | try: |
| 126 | with open(args.history, 'r') as f: |
| 127 | history = json.load(f) |
| 128 | except FileNotFoundError: |
| 129 | pass |
| 130 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 131 | _process_results(args, history, results) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 132 | |
| 133 | with open(args.history, 'w') as f: |
| 134 | json.dump(history, f, sort_keys=True) |
| 135 | |
| 136 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 137 | def _upgrade(proj): |
| 138 | out = subprocess.run(['out/soong/host/linux-x86/bin/external_updater', |
| 139 | 'update', '--branch_and_commit', '--push_change', |
| 140 | proj], |
| 141 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 142 | cwd=os.environ['ANDROID_BUILD_TOP']) |
| 143 | stdout = out.stdout.decode('utf-8') |
| 144 | stderr = out.stderr.decode('utf-8') |
| 145 | return """ |
| 146 | ==================== |
| 147 | | Debug Info | |
| 148 | ==================== |
| 149 | -=-=-=-=stdout=-=-=-=- |
| 150 | {} |
| 151 | |
| 152 | -=-=-=-=stderr=-=-=-=- |
| 153 | {} |
| 154 | """.format(stdout, stderr) |
| 155 | |
| 156 | |
| 157 | def _check_updates(args): |
| 158 | subprocess.run(['out/soong/host/linux-x86/bin/external_updater', |
| 159 | 'check', |
| 160 | '--json_output', RESULT_FILE_PATH, |
| 161 | '--delay', '0'] + args.paths, |
| 162 | cwd=os.environ['ANDROID_BUILD_TOP']) |
| 163 | |
| 164 | |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 165 | def main(): |
| 166 | """The main entry.""" |
| 167 | |
| 168 | args = parse_args() |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 169 | _check_updates(args) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 170 | send_notification(args) |
| 171 | |
| 172 | |
| 173 | if __name__ == '__main__': |
| 174 | main() |