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