Edward Lemur | 59a3b2f | 2020-01-14 01:50:50 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env vpython |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 2 | # Copyright 2018 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 6 | from __future__ import print_function |
| 7 | |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 8 | import os |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 9 | import json |
| 10 | import sys |
| 11 | |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 12 | import ninjalog_uploader |
Edward Lemur | 59a3b2f | 2020-01-14 01:50:50 +0000 | [diff] [blame^] | 13 | import subprocess2 |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 14 | |
| 15 | THIS_DIR = os.path.dirname(__file__) |
| 16 | UPLOADER = os.path.join(THIS_DIR, 'ninjalog_uploader.py') |
| 17 | CONFIG = os.path.join(THIS_DIR, 'ninjalog.cfg') |
Takuto Ikuta | c8069af | 2019-01-09 06:24:56 +0000 | [diff] [blame] | 18 | VERSION = 2 |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | def LoadConfig(): |
| 22 | if os.path.isfile(CONFIG): |
| 23 | with open(CONFIG, 'rb') as f: |
| 24 | config = json.load(f) |
| 25 | if config['version'] == VERSION: |
Takuto Ikuta | 65ace12 | 2019-03-22 03:57:17 +0000 | [diff] [blame] | 26 | config['countdown'] = max(0, config['countdown'] - 1) |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 27 | return config |
| 28 | |
| 29 | return { |
| 30 | 'is-googler': ninjalog_uploader.IsGoogler( |
| 31 | 'chromium-build-stats.appspot.com'), |
| 32 | 'countdown': 10, |
| 33 | 'version': VERSION, |
| 34 | } |
| 35 | |
| 36 | |
| 37 | def SaveConfig(config): |
| 38 | with open(CONFIG, 'wb') as f: |
| 39 | json.dump(config, f) |
| 40 | |
| 41 | |
| 42 | def ShowMessage(countdown): |
Takuto Ikuta | c8069af | 2019-01-09 06:24:56 +0000 | [diff] [blame] | 43 | whitelisted = '\n'.join([' * %s' % config for config in |
| 44 | ninjalog_uploader.WHITELISTED_CONFIGS]) |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 45 | print(""" |
Takuto Ikuta | 540ba9d | 2018-12-19 22:24:08 +0000 | [diff] [blame] | 46 | Your ninjalog will be uploaded to build stats server. The uploaded log will be |
| 47 | used to analyze user side build performance. |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 48 | |
| 49 | The following information will be uploaded with ninjalog. |
| 50 | * OS (e.g. Win, Mac or Linux) |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 51 | * number of cpu cores of building machine |
Takuto Ikuta | c8069af | 2019-01-09 06:24:56 +0000 | [diff] [blame] | 52 | * build targets (e.g. chrome, browser_tests) |
| 53 | * parallelism passed by -j flag |
| 54 | * following build configs |
| 55 | %s |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 56 | |
| 57 | Uploading ninjalog will be started after you run autoninja another %d time. |
| 58 | |
| 59 | If you don't want to upload ninjalog, please run following command. |
| 60 | $ %s opt-out |
| 61 | |
Takuto Ikuta | 540ba9d | 2018-12-19 22:24:08 +0000 | [diff] [blame] | 62 | If you want to allow upload ninjalog from next autoninja run, please run the |
| 63 | following command. |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 64 | $ %s opt-in |
| 65 | |
Takuto Ikuta | 540ba9d | 2018-12-19 22:24:08 +0000 | [diff] [blame] | 66 | If you have questions about this, please send mail to infra-dev@chromium.org |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 67 | |
Takuto Ikuta | 540ba9d | 2018-12-19 22:24:08 +0000 | [diff] [blame] | 68 | You can find a more detailed explanation in |
| 69 | %s |
| 70 | |
Takuto Ikuta | c8069af | 2019-01-09 06:24:56 +0000 | [diff] [blame] | 71 | """ % (whitelisted, countdown, __file__, __file__, |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 72 | os.path.abspath(os.path.join(THIS_DIR, "ninjalog.README.md")))) |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | def main(): |
| 76 | config = LoadConfig() |
| 77 | |
| 78 | if len(sys.argv) == 2 and sys.argv[1] == 'opt-in': |
| 79 | config['opt-in'] = True |
| 80 | config['countdown'] = 0 |
| 81 | SaveConfig(config) |
| 82 | print('ninjalog upload is opted in.') |
| 83 | return 0 |
| 84 | |
| 85 | if len(sys.argv) == 2 and sys.argv[1] == 'opt-out': |
| 86 | config['opt-in'] = False |
| 87 | SaveConfig(config) |
| 88 | print('ninjalog upload is opted out.') |
| 89 | return 0 |
| 90 | |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 91 | if 'opt-in' in config and not config['opt-in']: |
| 92 | # Upload is opted out. |
| 93 | return 0 |
| 94 | |
| 95 | if not config.get("is-googler", False): |
| 96 | # Not googler. |
| 97 | return 0 |
| 98 | |
| 99 | if config.get("countdown", 0) > 0: |
| 100 | # Need to show message. |
| 101 | ShowMessage(config["countdown"]) |
Bruce Dawson | cf9613f | 2019-03-20 03:57:35 +0000 | [diff] [blame] | 102 | # Only save config if something has meaningfully changed. |
| 103 | SaveConfig(config) |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 104 | return 0 |
| 105 | |
| 106 | if len(sys.argv) == 1: |
| 107 | # dry-run for debugging. |
| 108 | print("upload ninjalog dry-run") |
| 109 | return 0 |
| 110 | |
| 111 | # Run upload script without wait. |
| 112 | devnull = open(os.devnull, "w") |
Edward Lemur | 59a3b2f | 2020-01-14 01:50:50 +0000 | [diff] [blame^] | 113 | subprocess2.Popen(['vpython', UPLOADER] + sys.argv[1:], |
| 114 | stdout=devnull, stderr=devnull) |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | if __name__ == '__main__': |
| 118 | sys.exit(main()) |