blob: ca4dcf65e7bfa5a8770d9d3bc15a359892df8fd2 [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():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000024 if os.path.isfile(CONFIG):
25 with open(CONFIG, 'r') as f:
26 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 }
Takuto Ikuta87172072022-04-26 23:35:31 +000035
Mike Frysinger124bb8e2023-09-06 05:48:55 +000036 if config['version'] == VERSION:
37 config['countdown'] = max(0, config['countdown'] - 1)
38 return config
Takuto Ikuta9af233a2018-11-29 03:53:53 +000039
Mike Frysinger124bb8e2023-09-06 05:48:55 +000040 return {
41 'is-googler': ninjalog_uploader.IsGoogler(),
42 'countdown': 10,
43 'version': VERSION,
44 }
Takuto Ikuta9af233a2018-11-29 03:53:53 +000045
46
47def SaveConfig(config):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000048 with open(CONFIG, 'w') as f:
49 json.dump(config, f)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000050
51
52def ShowMessage(countdown):
Philipp Wollermannaf369d82023-09-22 06:40:54 +000053 allowlisted = '\n'.join(
Mike Frysinger124bb8e2023-09-06 05:48:55 +000054 [' * %s' % config for config in ninjalog_uploader.ALLOWLISTED_CONFIGS])
55 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
Louis Romerocf9a7762022-05-20 08:10:36 +000067Uploading ninjalog will be started after you run autoninja another %d time(s).
Takuto Ikuta9af233a2018-11-29 03:53:53 +000068
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
Philipp Wollermannaf369d82023-09-22 06:40:54 +000083""" % (allowlisted, 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():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000088 config = LoadConfig()
Takuto Ikuta9af233a2018-11-29 03:53:53 +000089
Mike Frysinger124bb8e2023-09-06 05:48:55 +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
Mike Frysinger124bb8e2023-09-06 05:48:55 +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
Mike Frysinger124bb8e2023-09-06 05:48:55 +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
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000107 if not config.get("is-googler", False):
108 # Not googler.
109 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000110
Mike Frysinger124bb8e2023-09-06 05:48:55 +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
Mike Frysinger124bb8e2023-09-06 05:48:55 +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
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000123 # Run upload script without wait.
124 devnull = open(os.devnull, "w")
Philipp Wollermannaf369d82023-09-22 06:40:54 +0000125 creationflags = 0
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000126 if platform.system() == 'Windows':
Philipp Wollermannaf369d82023-09-22 06:40:54 +0000127 creationflags = subprocess.CREATE_NEW_PROCESS_GROUP
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000128 subprocess2.Popen([sys.executable, UPLOADER] + sys.argv[1:],
129 stdout=devnull,
130 stderr=devnull,
Philipp Wollermannaf369d82023-09-22 06:40:54 +0000131 creationflags=creationflags)
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000132
133
134if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000135 sys.exit(main())