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 \ |
| 18 | --result_file ~/updater/new_result \ |
| 19 | --history ~/updater/history \ |
| 20 | --recipients xxx@xxx.xxx |
| 21 | """ |
| 22 | |
| 23 | import argparse |
| 24 | import json |
| 25 | import os |
| 26 | import subprocess |
| 27 | import time |
| 28 | |
| 29 | |
| 30 | def parse_args(): |
| 31 | """Parses commandline arguments.""" |
| 32 | |
| 33 | parser = argparse.ArgumentParser( |
| 34 | description='Check updates for third party projects in external/.') |
| 35 | parser.add_argument( |
| 36 | '--result_file', |
| 37 | help='Json check result generated by external updater.') |
| 38 | parser.add_argument( |
| 39 | '--history', |
| 40 | help='Path of history file. If doesn' |
| 41 | 't exist, a new one will be created.') |
| 42 | parser.add_argument( |
| 43 | '--recipients', |
| 44 | help='Comma separated recipients of notification email.') |
| 45 | |
| 46 | return parser.parse_args() |
| 47 | |
| 48 | |
| 49 | def _send_email(proj, latest_ver, recipient): |
| 50 | print('Sending email for {}: {}'.format(proj, latest_ver)) |
| 51 | msg = """New version: {} |
| 52 | |
| 53 | To upgrade: |
| 54 | tools/external_updater/updater.sh update {}""".format( |
| 55 | latest_ver, proj) |
| 56 | subprocess.run(['sendgmr', '--to=' + recipient, |
| 57 | '--subject=' + proj], check=True, |
| 58 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 59 | input=msg, encoding='ascii') |
| 60 | |
| 61 | |
| 62 | def _process_results(history, results, recipient): |
| 63 | for proj, res in results.items(): |
| 64 | if 'latest' not in res: |
| 65 | continue |
| 66 | latest_ver = res['latest'] |
| 67 | current_ver = res['current'] |
| 68 | if latest_ver == current_ver: |
| 69 | continue |
| 70 | proj_history = history.setdefault(proj, {}) |
| 71 | if latest_ver not in proj_history: |
| 72 | try: |
| 73 | _send_email(proj, latest_ver, recipient) |
| 74 | proj_history[latest_ver] = int(time.time()) |
| 75 | except subprocess.CalledProcessError as err: |
| 76 | msg = """Failed to send email for {} ({}). |
| 77 | stdout: {} |
| 78 | stderr: {}""".format(proj, latest_ver, err.stdout, err.stderr) |
| 79 | print(msg) |
| 80 | |
| 81 | |
| 82 | def send_notification(args): |
| 83 | """Compare results and send notification.""" |
| 84 | results = {} |
| 85 | with open(args.result_file, 'r') as f: |
| 86 | results = json.load(f) |
| 87 | history = {} |
| 88 | try: |
| 89 | with open(args.history, 'r') as f: |
| 90 | history = json.load(f) |
| 91 | except FileNotFoundError: |
| 92 | pass |
| 93 | |
| 94 | _process_results(history, results, args.recipients) |
| 95 | |
| 96 | with open(args.history, 'w') as f: |
| 97 | json.dump(history, f, sort_keys=True) |
| 98 | |
| 99 | |
| 100 | def main(): |
| 101 | """The main entry.""" |
| 102 | |
| 103 | args = parse_args() |
| 104 | send_notification(args) |
| 105 | |
| 106 | |
| 107 | if __name__ == '__main__': |
| 108 | main() |