blob: f17a05363d4732051f7c91e5f7135dcd097d29a1 [file] [log] [blame]
Takuto Ikuta84e43fa2021-01-19 02:51:50 +00001#!/usr/bin/env python3
Takuto Ikuta9af233a2018-11-29 03:53:53 +00002# 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 Ikuta9af233a2018-11-29 03:53:53 +00006import json
Takuto Ikuta59622a52020-06-11 05:36:31 +00007import os
8import platform
9import subprocess
Takuto Ikuta9af233a2018-11-29 03:53:53 +000010import sys
11
Takuto Ikuta9af233a2018-11-29 03:53:53 +000012import ninjalog_uploader
Edward Lemur59a3b2f2020-01-14 01:50:50 +000013import subprocess2
Takuto Ikuta9af233a2018-11-29 03:53:53 +000014
15THIS_DIR = os.path.dirname(__file__)
Takuto Ikutadf3e5772023-11-16 07:14:49 +000016UPLOADER = os.path.join(THIS_DIR, "ninjalog_uploader.py")
17CONFIG = os.path.join(THIS_DIR, "ninjalog.cfg")
Takuto Ikutaa6573312022-01-20 00:24:57 +000018VERSION = 3
Takuto Ikuta9af233a2018-11-29 03:53:53 +000019
20
21def LoadConfig():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000022 if os.path.isfile(CONFIG):
Takuto Ikutadf3e5772023-11-16 07:14:49 +000023 with open(CONFIG, "r") as f:
Mike Frysinger124bb8e2023-09-06 05:48:55 +000024 try:
25 config = json.load(f)
26 except Exception:
27 # Set default value when failed to load config.
28 config = {
Takuto Ikutadf3e5772023-11-16 07:14:49 +000029 "is-googler": ninjalog_uploader.IsGoogler(),
30 "countdown": 10,
31 "version": VERSION,
Mike Frysinger124bb8e2023-09-06 05:48:55 +000032 }
Takuto Ikuta87172072022-04-26 23:35:31 +000033
Takuto Ikutadf3e5772023-11-16 07:14:49 +000034 if config["version"] == VERSION:
35 config["countdown"] = max(0, config["countdown"] - 1)
Mike Frysinger124bb8e2023-09-06 05:48:55 +000036 return config
Takuto Ikuta9af233a2018-11-29 03:53:53 +000037
Mike Frysinger124bb8e2023-09-06 05:48:55 +000038 return {
Takuto Ikutadf3e5772023-11-16 07:14:49 +000039 "is-googler": ninjalog_uploader.IsGoogler(),
40 "countdown": 10,
41 "version": VERSION,
Mike Frysinger124bb8e2023-09-06 05:48:55 +000042 }
Takuto Ikuta9af233a2018-11-29 03:53:53 +000043
44
45def SaveConfig(config):
Takuto Ikutadf3e5772023-11-16 07:14:49 +000046 with open(CONFIG, "w") as f:
Mike Frysinger124bb8e2023-09-06 05:48:55 +000047 json.dump(config, f)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000048
49
50def ShowMessage(countdown):
Takuto Ikutadf3e5772023-11-16 07:14:49 +000051 allowlisted = "\n".join(
52 [" * %s" % config for config in ninjalog_uploader.ALLOWLISTED_CONFIGS])
Mike Frysinger124bb8e2023-09-06 05:48:55 +000053 print("""
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000054Your ninjalog will be uploaded to build stats server. The uploaded log will be
55used to analyze user side build performance.
Takuto Ikuta9af233a2018-11-29 03:53:53 +000056
57The following information will be uploaded with ninjalog.
58* OS (e.g. Win, Mac or Linux)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000059* number of cpu cores of building machine
Takuto Ikutac8069af2019-01-09 06:24:56 +000060* build targets (e.g. chrome, browser_tests)
61* parallelism passed by -j flag
62* following build configs
63%s
Takuto Ikuta9af233a2018-11-29 03:53:53 +000064
Louis Romerocf9a7762022-05-20 08:10:36 +000065Uploading ninjalog will be started after you run autoninja another %d time(s).
Takuto Ikuta9af233a2018-11-29 03:53:53 +000066
67If you don't want to upload ninjalog, please run following command.
Takuto Ikuta653d7e62022-01-24 04:35:37 +000068$ python3 %s opt-out
Takuto Ikuta9af233a2018-11-29 03:53:53 +000069
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000070If you want to allow upload ninjalog from next autoninja run, please run the
71following command.
Takuto Ikuta653d7e62022-01-24 04:35:37 +000072$ python3 %s opt-in
Takuto Ikuta9af233a2018-11-29 03:53:53 +000073
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000074If you have questions about this, please send mail to infra-dev@chromium.org
Takuto Ikuta9af233a2018-11-29 03:53:53 +000075
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000076You can find a more detailed explanation in
77%s
Takuto Ikutaa6573312022-01-20 00:24:57 +000078or
79https://chromium.googlesource.com/chromium/tools/depot_tools/+/main/ninjalog.README.md
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000080
Takuto Ikutadf3e5772023-11-16 07:14:49 +000081""" % (
82 allowlisted,
83 countdown,
84 __file__,
85 __file__,
86 os.path.abspath(os.path.join(THIS_DIR, "ninjalog.README.md")),
87 ))
Takuto Ikuta9af233a2018-11-29 03:53:53 +000088
89
90def main():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000091 config = LoadConfig()
Takuto Ikuta9af233a2018-11-29 03:53:53 +000092
Takuto Ikutadf3e5772023-11-16 07:14:49 +000093 if len(sys.argv) == 2 and sys.argv[1] == "opt-in":
94 config["opt-in"] = True
95 config["countdown"] = 0
Mike Frysinger124bb8e2023-09-06 05:48:55 +000096 SaveConfig(config)
Takuto Ikutadf3e5772023-11-16 07:14:49 +000097 print("ninjalog upload is opted in.")
Mike Frysinger124bb8e2023-09-06 05:48:55 +000098 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +000099
Takuto Ikutadf3e5772023-11-16 07:14:49 +0000100 if len(sys.argv) == 2 and sys.argv[1] == "opt-out":
101 config["opt-in"] = False
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000102 SaveConfig(config)
Takuto Ikutadf3e5772023-11-16 07:14:49 +0000103 print("ninjalog upload is opted out.")
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000104 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000105
Takuto Ikutadf3e5772023-11-16 07:14:49 +0000106 if "opt-in" in config and not config["opt-in"]:
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000107 # Upload is opted out.
108 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000109
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000110 if not config.get("is-googler", False):
111 # Not googler.
112 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000113
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000114 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 Ikuta9af233a2018-11-29 03:53:53 +0000120
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000121 if len(sys.argv) == 1:
122 # dry-run for debugging.
123 print("upload ninjalog dry-run")
124 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000125
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000126 # Run upload script without wait.
127 devnull = open(os.devnull, "w")
Philipp Wollermannaf369d82023-09-22 06:40:54 +0000128 creationflags = 0
Takuto Ikutadf3e5772023-11-16 07:14:49 +0000129 if platform.system() == "Windows":
Philipp Wollermannaf369d82023-09-22 06:40:54 +0000130 creationflags = subprocess.CREATE_NEW_PROCESS_GROUP
Takuto Ikutadf3e5772023-11-16 07:14:49 +0000131 subprocess2.Popen(
132 [sys.executable, UPLOADER] + sys.argv[1:],
133 stdout=devnull,
134 stderr=devnull,
135 creationflags=creationflags,
136 )
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000137
138
Takuto Ikutadf3e5772023-11-16 07:14:49 +0000139if __name__ == "__main__":
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000140 sys.exit(main())