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__) |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 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): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 23 | with open(CONFIG, "r") as f: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 24 | try: |
| 25 | config = json.load(f) |
| 26 | except Exception: |
| 27 | # Set default value when failed to load config. |
| 28 | config = { |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 29 | "is-googler": ninjalog_uploader.IsGoogler(), |
| 30 | "countdown": 10, |
| 31 | "version": VERSION, |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 32 | } |
Takuto Ikuta | 8717207 | 2022-04-26 23:35:31 +0000 | [diff] [blame] | 33 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 34 | if config["version"] == VERSION: |
| 35 | config["countdown"] = max(0, config["countdown"] - 1) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 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 { |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 39 | "is-googler": ninjalog_uploader.IsGoogler(), |
| 40 | "countdown": 10, |
| 41 | "version": VERSION, |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 42 | } |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def SaveConfig(config): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 46 | with open(CONFIG, "w") as f: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 47 | json.dump(config, f) |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def ShowMessage(countdown): |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 51 | allowlisted = "\n".join( |
| 52 | [" * %s" % config for config in ninjalog_uploader.ALLOWLISTED_CONFIGS]) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 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 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 81 | """ % ( |
| 82 | allowlisted, |
| 83 | countdown, |
| 84 | __file__, |
| 85 | __file__, |
| 86 | os.path.abspath(os.path.join(THIS_DIR, "ninjalog.README.md")), |
| 87 | )) |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | def main(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 91 | config = LoadConfig() |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 92 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 93 | if len(sys.argv) == 2 and sys.argv[1] == "opt-in": |
| 94 | config["opt-in"] = True |
| 95 | config["countdown"] = 0 |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 96 | SaveConfig(config) |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 97 | print("ninjalog upload is opted in.") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 98 | return 0 |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 99 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 100 | if len(sys.argv) == 2 and sys.argv[1] == "opt-out": |
| 101 | config["opt-in"] = False |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 102 | SaveConfig(config) |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 103 | print("ninjalog upload is opted out.") |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 104 | return 0 |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 105 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 106 | if "opt-in" in config and not config["opt-in"]: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 107 | # Upload is opted out. |
| 108 | return 0 |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 109 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 110 | if not config.get("is-googler", False): |
| 111 | # Not googler. |
| 112 | return 0 |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 113 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 114 | if config.get("countdown", 0) > 0: |
| 115 | # Need to show message. |
| 116 | ShowMessage(config["countdown"]) |
| 117 | # Only save config if something has meaningfully changed. |
| 118 | SaveConfig(config) |
| 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 | if len(sys.argv) == 1: |
| 122 | # dry-run for debugging. |
| 123 | print("upload ninjalog dry-run") |
| 124 | return 0 |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 125 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 126 | # Run upload script without wait. |
| 127 | devnull = open(os.devnull, "w") |
Philipp Wollermann | af369d8 | 2023-09-22 06:40:54 +0000 | [diff] [blame] | 128 | creationflags = 0 |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 129 | if platform.system() == "Windows": |
Philipp Wollermann | af369d8 | 2023-09-22 06:40:54 +0000 | [diff] [blame] | 130 | creationflags = subprocess.CREATE_NEW_PROCESS_GROUP |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 131 | subprocess2.Popen( |
| 132 | [sys.executable, UPLOADER] + sys.argv[1:], |
| 133 | stdout=devnull, |
| 134 | stderr=devnull, |
| 135 | creationflags=creationflags, |
| 136 | ) |
Takuto Ikuta | 9af233a | 2018-11-29 03:53:53 +0000 | [diff] [blame] | 137 | |
| 138 | |
Takuto Ikuta | df3e577 | 2023-11-16 07:14:49 +0000 | [diff] [blame] | 139 | if __name__ == "__main__": |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 140 | sys.exit(main()) |