blob: 7a06826e1fe9c2c8603b9077aef70626472ec415 [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__)
16UPLOADER = 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):
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 Ikuta87172072022-04-26 23:35:31 +000033
Mike Frysinger124bb8e2023-09-06 05:48:55 +000034 if config['version'] == VERSION:
35 config['countdown'] = max(0, config['countdown'] - 1)
36 return config
Takuto Ikuta9af233a2018-11-29 03:53:53 +000037
Mike Frysinger124bb8e2023-09-06 05:48:55 +000038 return {
39 'is-googler': ninjalog_uploader.IsGoogler(),
40 'countdown': 10,
41 'version': VERSION,
42 }
Takuto Ikuta9af233a2018-11-29 03:53:53 +000043
44
45def SaveConfig(config):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000046 with open(CONFIG, 'w') as f:
47 json.dump(config, f)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000048
49
50def ShowMessage(countdown):
Philipp Wollermannaf369d82023-09-22 06:40:54 +000051 allowlisted = '\n'.join(
Mike Frysinger124bb8e2023-09-06 05:48:55 +000052 [' * %s' % config for config in ninjalog_uploader.ALLOWLISTED_CONFIGS])
53 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
Philipp Wollermannaf369d82023-09-22 06:40:54 +000081""" % (allowlisted, countdown, __file__, __file__,
Raul Tambre80ee78e2019-05-06 22:41:05 +000082 os.path.abspath(os.path.join(THIS_DIR, "ninjalog.README.md"))))
Takuto Ikuta9af233a2018-11-29 03:53:53 +000083
84
85def main():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000086 config = LoadConfig()
Takuto Ikuta9af233a2018-11-29 03:53:53 +000087
Mike Frysinger124bb8e2023-09-06 05:48:55 +000088 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 Ikuta9af233a2018-11-29 03:53:53 +000094
Mike Frysinger124bb8e2023-09-06 05:48:55 +000095 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 Ikuta9af233a2018-11-29 03:53:53 +0000100
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000101 if 'opt-in' in config and not config['opt-in']:
102 # Upload is opted out.
103 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000104
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000105 if not config.get("is-googler", False):
106 # Not googler.
107 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000108
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000109 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 Ikuta9af233a2018-11-29 03:53:53 +0000115
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000116 if len(sys.argv) == 1:
117 # dry-run for debugging.
118 print("upload ninjalog dry-run")
119 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000120
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000121 # Run upload script without wait.
122 devnull = open(os.devnull, "w")
Philipp Wollermannaf369d82023-09-22 06:40:54 +0000123 creationflags = 0
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000124 if platform.system() == 'Windows':
Philipp Wollermannaf369d82023-09-22 06:40:54 +0000125 creationflags = subprocess.CREATE_NEW_PROCESS_GROUP
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000126 subprocess2.Popen([sys.executable, UPLOADER] + sys.argv[1:],
127 stdout=devnull,
128 stderr=devnull,
Philipp Wollermannaf369d82023-09-22 06:40:54 +0000129 creationflags=creationflags)
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000130
131
132if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000133 sys.exit(main())