blob: 6ce7dba27429016e283650b37ea44c2f9db564d8 [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 Ikutaa2e91db2020-06-09 11:21:59 +000026 config = json.load(f)
27 if config['version'] == VERSION:
28 config['countdown'] = max(0, config['countdown'] - 1)
29 return config
Takuto Ikuta9af233a2018-11-29 03:53:53 +000030
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000031 return {
Takuto Ikutaa6573312022-01-20 00:24:57 +000032 'is-googler': ninjalog_uploader.IsGoogler(),
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000033 'countdown': 10,
34 'version': VERSION,
35 }
Takuto Ikuta9af233a2018-11-29 03:53:53 +000036
37
38def SaveConfig(config):
Takuto Ikuta8a2e6a72021-02-26 04:24:49 +000039 with open(CONFIG, 'w') as f:
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000040 json.dump(config, f)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000041
42
43def ShowMessage(countdown):
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000044 whitelisted = '\n'.join(
Victor Hugo Vianna Silva787f2f02021-11-11 03:27:28 +000045 [' * %s' % config for config in ninjalog_uploader.ALLOWLISTED_CONFIGS])
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000046 print("""
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000047Your ninjalog will be uploaded to build stats server. The uploaded log will be
48used to analyze user side build performance.
Takuto Ikuta9af233a2018-11-29 03:53:53 +000049
50The following information will be uploaded with ninjalog.
51* OS (e.g. Win, Mac or Linux)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000052* number of cpu cores of building machine
Takuto Ikutac8069af2019-01-09 06:24:56 +000053* build targets (e.g. chrome, browser_tests)
54* parallelism passed by -j flag
55* following build configs
56%s
Takuto Ikuta9af233a2018-11-29 03:53:53 +000057
58Uploading ninjalog will be started after you run autoninja another %d time.
59
60If you don't want to upload ninjalog, please run following command.
61$ %s opt-out
62
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000063If you want to allow upload ninjalog from next autoninja run, please run the
64following command.
Takuto Ikuta9af233a2018-11-29 03:53:53 +000065$ %s opt-in
66
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000067If you have questions about this, please send mail to infra-dev@chromium.org
Takuto Ikuta9af233a2018-11-29 03:53:53 +000068
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000069You can find a more detailed explanation in
70%s
Takuto Ikutaa6573312022-01-20 00:24:57 +000071or
72https://chromium.googlesource.com/chromium/tools/depot_tools/+/main/ninjalog.README.md
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000073
Takuto Ikutac8069af2019-01-09 06:24:56 +000074""" % (whitelisted, countdown, __file__, __file__,
Raul Tambre80ee78e2019-05-06 22:41:05 +000075 os.path.abspath(os.path.join(THIS_DIR, "ninjalog.README.md"))))
Takuto Ikuta9af233a2018-11-29 03:53:53 +000076
77
78def main():
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000079 config = LoadConfig()
Takuto Ikuta9af233a2018-11-29 03:53:53 +000080
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000081 if len(sys.argv) == 2 and sys.argv[1] == 'opt-in':
82 config['opt-in'] = True
83 config['countdown'] = 0
84 SaveConfig(config)
85 print('ninjalog upload is opted in.')
86 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +000087
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000088 if len(sys.argv) == 2 and sys.argv[1] == 'opt-out':
89 config['opt-in'] = False
90 SaveConfig(config)
91 print('ninjalog upload is opted out.')
92 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +000093
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000094 if 'opt-in' in config and not config['opt-in']:
95 # Upload is opted out.
96 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +000097
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000098 if not config.get("is-googler", False):
99 # Not googler.
100 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000101
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000102 if config.get("countdown", 0) > 0:
103 # Need to show message.
104 ShowMessage(config["countdown"])
105 # Only save config if something has meaningfully changed.
106 SaveConfig(config)
107 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000108
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000109 if len(sys.argv) == 1:
110 # dry-run for debugging.
111 print("upload ninjalog dry-run")
112 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000113
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000114 # Run upload script without wait.
115 devnull = open(os.devnull, "w")
Takuto Ikuta59622a52020-06-11 05:36:31 +0000116 creationnflags = 0
117 if platform.system() == 'Windows':
118 creationnflags = subprocess.CREATE_NEW_PROCESS_GROUP
Takuto Ikuta84e43fa2021-01-19 02:51:50 +0000119 subprocess2.Popen([sys.executable, UPLOADER] + sys.argv[1:],
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000120 stdout=devnull,
Takuto Ikuta59622a52020-06-11 05:36:31 +0000121 stderr=devnull,
122 creationflags=creationnflags)
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000123
124
125if __name__ == '__main__':
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000126 sys.exit(main())