blob: 3b1d132f9ceade1475462420ed4e48a9aeef5bf6 [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
Raul Tambre80ee78e2019-05-06 22:41:05 +00006from __future__ import print_function
7
Takuto Ikuta9af233a2018-11-29 03:53:53 +00008import json
Takuto Ikuta59622a52020-06-11 05:36:31 +00009import os
10import platform
11import subprocess
Takuto Ikuta9af233a2018-11-29 03:53:53 +000012import sys
13
Takuto Ikuta9af233a2018-11-29 03:53:53 +000014import ninjalog_uploader
Edward Lemur59a3b2f2020-01-14 01:50:50 +000015import subprocess2
Takuto Ikuta9af233a2018-11-29 03:53:53 +000016
17THIS_DIR = os.path.dirname(__file__)
18UPLOADER = os.path.join(THIS_DIR, 'ninjalog_uploader.py')
19CONFIG = os.path.join(THIS_DIR, 'ninjalog.cfg')
Takuto Ikutaa6573312022-01-20 00:24:57 +000020VERSION = 3
Takuto Ikuta9af233a2018-11-29 03:53:53 +000021
22
23def LoadConfig():
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000024 if os.path.isfile(CONFIG):
Takuto Ikuta8a2e6a72021-02-26 04:24:49 +000025 with open(CONFIG, 'r') as f:
Takuto Ikuta87172072022-04-26 23:35:31 +000026 try:
27 config = json.load(f)
28 except Exception:
29 # Set default value when failed to load config.
30 config = {
31 'is-googler': ninjalog_uploader.IsGoogler(),
32 'countdown': 10,
33 'version': VERSION,
34 }
35
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000036 if config['version'] == VERSION:
37 config['countdown'] = max(0, config['countdown'] - 1)
38 return config
Takuto Ikuta9af233a2018-11-29 03:53:53 +000039
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000040 return {
Takuto Ikutaa6573312022-01-20 00:24:57 +000041 'is-googler': ninjalog_uploader.IsGoogler(),
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000042 'countdown': 10,
43 'version': VERSION,
44 }
Takuto Ikuta9af233a2018-11-29 03:53:53 +000045
46
47def SaveConfig(config):
Takuto Ikuta8a2e6a72021-02-26 04:24:49 +000048 with open(CONFIG, 'w') as f:
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000049 json.dump(config, f)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000050
51
52def ShowMessage(countdown):
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000053 whitelisted = '\n'.join(
Victor Hugo Vianna Silva787f2f02021-11-11 03:27:28 +000054 [' * %s' % config for config in ninjalog_uploader.ALLOWLISTED_CONFIGS])
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000055 print("""
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000056Your ninjalog will be uploaded to build stats server. The uploaded log will be
57used to analyze user side build performance.
Takuto Ikuta9af233a2018-11-29 03:53:53 +000058
59The following information will be uploaded with ninjalog.
60* OS (e.g. Win, Mac or Linux)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000061* number of cpu cores of building machine
Takuto Ikutac8069af2019-01-09 06:24:56 +000062* build targets (e.g. chrome, browser_tests)
63* parallelism passed by -j flag
64* following build configs
65%s
Takuto Ikuta9af233a2018-11-29 03:53:53 +000066
67Uploading ninjalog will be started after you run autoninja another %d time.
68
69If you don't want to upload ninjalog, please run following command.
Takuto Ikuta653d7e62022-01-24 04:35:37 +000070$ python3 %s opt-out
Takuto Ikuta9af233a2018-11-29 03:53:53 +000071
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000072If you want to allow upload ninjalog from next autoninja run, please run the
73following command.
Takuto Ikuta653d7e62022-01-24 04:35:37 +000074$ python3 %s opt-in
Takuto Ikuta9af233a2018-11-29 03:53:53 +000075
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000076If you have questions about this, please send mail to infra-dev@chromium.org
Takuto Ikuta9af233a2018-11-29 03:53:53 +000077
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000078You can find a more detailed explanation in
79%s
Takuto Ikutaa6573312022-01-20 00:24:57 +000080or
81https://chromium.googlesource.com/chromium/tools/depot_tools/+/main/ninjalog.README.md
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000082
Takuto Ikutac8069af2019-01-09 06:24:56 +000083""" % (whitelisted, countdown, __file__, __file__,
Raul Tambre80ee78e2019-05-06 22:41:05 +000084 os.path.abspath(os.path.join(THIS_DIR, "ninjalog.README.md"))))
Takuto Ikuta9af233a2018-11-29 03:53:53 +000085
86
87def main():
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000088 config = LoadConfig()
Takuto Ikuta9af233a2018-11-29 03:53:53 +000089
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000090 if len(sys.argv) == 2 and sys.argv[1] == 'opt-in':
91 config['opt-in'] = True
92 config['countdown'] = 0
93 SaveConfig(config)
94 print('ninjalog upload is opted in.')
95 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +000096
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000097 if len(sys.argv) == 2 and sys.argv[1] == 'opt-out':
98 config['opt-in'] = False
99 SaveConfig(config)
100 print('ninjalog upload is opted out.')
101 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000102
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000103 if 'opt-in' in config and not config['opt-in']:
104 # Upload is opted out.
105 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000106
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000107 if not config.get("is-googler", False):
108 # Not googler.
109 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000110
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000111 if config.get("countdown", 0) > 0:
112 # Need to show message.
113 ShowMessage(config["countdown"])
114 # Only save config if something has meaningfully changed.
115 SaveConfig(config)
116 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000117
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000118 if len(sys.argv) == 1:
119 # dry-run for debugging.
120 print("upload ninjalog dry-run")
121 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000122
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000123 # Run upload script without wait.
124 devnull = open(os.devnull, "w")
Takuto Ikuta59622a52020-06-11 05:36:31 +0000125 creationnflags = 0
126 if platform.system() == 'Windows':
127 creationnflags = subprocess.CREATE_NEW_PROCESS_GROUP
Takuto Ikuta84e43fa2021-01-19 02:51:50 +0000128 subprocess2.Popen([sys.executable, UPLOADER] + sys.argv[1:],
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000129 stdout=devnull,
Takuto Ikuta59622a52020-06-11 05:36:31 +0000130 stderr=devnull,
131 creationflags=creationnflags)
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000132
133
134if __name__ == '__main__':
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000135 sys.exit(main())