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