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 | 1c7284e | 2019-02-01 11:34:21 -0800 | [diff] [blame] | 53 | parser.add_argument( |
| 54 | '--all', action='store_true', |
| 55 | help='Checks all projects.') |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 56 | |
| 57 | return parser.parse_args() |
| 58 | |
| 59 | |
Haibo Huang | bdc8cde | 2019-11-08 16:29:39 -0800 | [diff] [blame] | 60 | def _get_android_top(): |
Haibo Huang | 081300e | 2019-11-11 14:50:17 -0800 | [diff] [blame] | 61 | return os.environ['ANDROID_BUILD_TOP'] |
Haibo Huang | bdc8cde | 2019-11-08 16:29:39 -0800 | [diff] [blame] | 62 | |
| 63 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 64 | CHANGE_URL_PATTERN = r'(https:\/\/[^\s]*android-review[^\s]*) Upgrade' |
| 65 | CHANGE_URL_RE = re.compile(CHANGE_URL_PATTERN) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 66 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 67 | |
Haibo Huang | bdc8cde | 2019-11-08 16:29:39 -0800 | [diff] [blame] | 68 | def _read_owner_file(proj): |
| 69 | owner_file = os.path.join(_get_android_top(), 'external', proj, 'OWNERS') |
| 70 | if not os.path.isfile(owner_file): |
| 71 | return None |
| 72 | with open(owner_file, 'r') as f: |
| 73 | return f.read().strip() |
| 74 | |
| 75 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 76 | def _send_email(proj, latest_ver, recipient, upgrade_log): |
| 77 | print('Sending email for {}: {}'.format(proj, latest_ver)) |
| 78 | msg = "New version: {}".format(latest_ver) |
| 79 | match = CHANGE_URL_RE.search(upgrade_log) |
| 80 | if match is not None: |
| 81 | msg += '\n\nAn upgrade change is generated at:\n{}'.format( |
| 82 | match.group(1)) |
| 83 | |
Haibo Huang | bdc8cde | 2019-11-08 16:29:39 -0800 | [diff] [blame] | 84 | owners = _read_owner_file(proj) |
| 85 | if owners: |
| 86 | msg += '\n\nOWNERS file: \n' |
| 87 | msg += owners |
| 88 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 89 | msg += '\n\n' |
| 90 | msg += upgrade_log |
| 91 | |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 92 | subprocess.run(['sendgmr', '--to=' + recipient, |
| 93 | '--subject=' + proj], check=True, |
| 94 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 95 | input=msg, encoding='ascii') |
| 96 | |
| 97 | |
Haibo Huang | 11c4a75 | 2019-01-31 15:07:03 -0800 | [diff] [blame] | 98 | NOTIFIED_TIME_KEY_NAME = 'latest_notified_time' |
| 99 | |
| 100 | |
| 101 | def _should_notify(latest_ver, proj_history): |
| 102 | if latest_ver in proj_history: |
| 103 | # Processed this version before. |
| 104 | return False |
| 105 | |
| 106 | timestamp = proj_history.get(NOTIFIED_TIME_KEY_NAME, 0) |
| 107 | time_diff = datetime.today() - datetime.fromtimestamp(timestamp) |
| 108 | if git_utils.is_commit(latest_ver) and time_diff <= timedelta(days=30): |
| 109 | return False |
| 110 | |
| 111 | return True |
| 112 | |
| 113 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 114 | def _process_results(args, history, results): |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 115 | for proj, res in results.items(): |
| 116 | if 'latest' not in res: |
| 117 | continue |
| 118 | latest_ver = res['latest'] |
| 119 | current_ver = res['current'] |
| 120 | if latest_ver == current_ver: |
| 121 | continue |
| 122 | proj_history = history.setdefault(proj, {}) |
Haibo Huang | 11c4a75 | 2019-01-31 15:07:03 -0800 | [diff] [blame] | 123 | if _should_notify(latest_ver, proj_history): |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 124 | upgrade_log = _upgrade(proj) if args.generate_change else "" |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 125 | try: |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 126 | _send_email(proj, latest_ver, args.recipients, upgrade_log) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 127 | proj_history[latest_ver] = int(time.time()) |
Haibo Huang | 11c4a75 | 2019-01-31 15:07:03 -0800 | [diff] [blame] | 128 | proj_history[NOTIFIED_TIME_KEY_NAME] = int(time.time()) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 129 | except subprocess.CalledProcessError as err: |
| 130 | msg = """Failed to send email for {} ({}). |
| 131 | stdout: {} |
| 132 | stderr: {}""".format(proj, latest_ver, err.stdout, err.stderr) |
| 133 | print(msg) |
| 134 | |
| 135 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 136 | RESULT_FILE_PATH = '/tmp/update_check_result.json' |
| 137 | |
| 138 | |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 139 | def send_notification(args): |
| 140 | """Compare results and send notification.""" |
| 141 | results = {} |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 142 | with open(RESULT_FILE_PATH, 'r') as f: |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 143 | results = json.load(f) |
| 144 | history = {} |
| 145 | try: |
| 146 | with open(args.history, 'r') as f: |
| 147 | history = json.load(f) |
| 148 | except FileNotFoundError: |
| 149 | pass |
| 150 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 151 | _process_results(args, history, results) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 152 | |
| 153 | with open(args.history, 'w') as f: |
Haibo Huang | 38dda43 | 2019-02-01 15:45:35 -0800 | [diff] [blame] | 154 | json.dump(history, f, sort_keys=True, indent=4) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 155 | |
| 156 | |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 157 | def _upgrade(proj): |
| 158 | out = subprocess.run(['out/soong/host/linux-x86/bin/external_updater', |
| 159 | 'update', '--branch_and_commit', '--push_change', |
| 160 | proj], |
| 161 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
Haibo Huang | bdc8cde | 2019-11-08 16:29:39 -0800 | [diff] [blame] | 162 | cwd=_get_android_top()) |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 163 | stdout = out.stdout.decode('utf-8') |
| 164 | stderr = out.stderr.decode('utf-8') |
| 165 | return """ |
| 166 | ==================== |
| 167 | | Debug Info | |
| 168 | ==================== |
| 169 | -=-=-=-=stdout=-=-=-=- |
| 170 | {} |
| 171 | |
| 172 | -=-=-=-=stderr=-=-=-=- |
| 173 | {} |
| 174 | """.format(stdout, stderr) |
| 175 | |
| 176 | |
| 177 | def _check_updates(args): |
Haibo Huang | 1c7284e | 2019-02-01 11:34:21 -0800 | [diff] [blame] | 178 | params = ['out/soong/host/linux-x86/bin/external_updater', |
| 179 | 'check', '--json_output', RESULT_FILE_PATH, |
Haibo Huang | 5c6c63e | 2019-02-01 11:44:48 -0800 | [diff] [blame] | 180 | '--delay', '30'] |
Haibo Huang | 1c7284e | 2019-02-01 11:34:21 -0800 | [diff] [blame] | 181 | if args.all: |
| 182 | params.append('--all') |
| 183 | else: |
| 184 | params += args.paths |
| 185 | |
Haibo Huang | 081300e | 2019-11-11 14:50:17 -0800 | [diff] [blame] | 186 | print(_get_android_top()) |
Haibo Huang | bdc8cde | 2019-11-08 16:29:39 -0800 | [diff] [blame] | 187 | subprocess.run(params, cwd=_get_android_top()) |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 188 | |
| 189 | |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 190 | def main(): |
| 191 | """The main entry.""" |
| 192 | |
| 193 | args = parse_args() |
Haibo Huang | 39287b1 | 2019-01-30 15:48:27 -0800 | [diff] [blame] | 194 | _check_updates(args) |
Haibo Huang | 39aaab6 | 2019-01-25 12:23:03 -0800 | [diff] [blame] | 195 | send_notification(args) |
| 196 | |
| 197 | |
| 198 | if __name__ == '__main__': |
| 199 | main() |