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