blob: fbfca3f296e3907e69613e4d0f08f0e84c874de2 [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 Ikutac8069af2019-01-09 06:24:56 +000020VERSION = 2
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 {
32 'is-googler':
33 ninjalog_uploader.IsGoogler('chromium-build-stats.appspot.com'),
34 'countdown': 10,
35 'version': VERSION,
36 }
Takuto Ikuta9af233a2018-11-29 03:53:53 +000037
38
39def SaveConfig(config):
Takuto Ikuta8a2e6a72021-02-26 04:24:49 +000040 with open(CONFIG, 'w') as f:
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000041 json.dump(config, f)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000042
43
44def ShowMessage(countdown):
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000045 whitelisted = '\n'.join(
Victor Hugo Vianna Silva787f2f02021-11-11 03:27:28 +000046 [' * %s' % config for config in ninjalog_uploader.ALLOWLISTED_CONFIGS])
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000047 print("""
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000048Your ninjalog will be uploaded to build stats server. The uploaded log will be
49used to analyze user side build performance.
Takuto Ikuta9af233a2018-11-29 03:53:53 +000050
51The following information will be uploaded with ninjalog.
52* OS (e.g. Win, Mac or Linux)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000053* number of cpu cores of building machine
Takuto Ikutac8069af2019-01-09 06:24:56 +000054* build targets (e.g. chrome, browser_tests)
55* parallelism passed by -j flag
56* following build configs
57%s
Takuto Ikuta9af233a2018-11-29 03:53:53 +000058
59Uploading ninjalog will be started after you run autoninja another %d time.
60
61If you don't want to upload ninjalog, please run following command.
62$ %s opt-out
63
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000064If you want to allow upload ninjalog from next autoninja run, please run the
65following command.
Takuto Ikuta9af233a2018-11-29 03:53:53 +000066$ %s opt-in
67
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000068If you have questions about this, please send mail to infra-dev@chromium.org
Takuto Ikuta9af233a2018-11-29 03:53:53 +000069
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000070You can find a more detailed explanation in
71%s
72
Takuto Ikutac8069af2019-01-09 06:24:56 +000073""" % (whitelisted, countdown, __file__, __file__,
Raul Tambre80ee78e2019-05-06 22:41:05 +000074 os.path.abspath(os.path.join(THIS_DIR, "ninjalog.README.md"))))
Takuto Ikuta9af233a2018-11-29 03:53:53 +000075
76
77def main():
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000078 config = LoadConfig()
Takuto Ikuta9af233a2018-11-29 03:53:53 +000079
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000080 if len(sys.argv) == 2 and sys.argv[1] == 'opt-in':
81 config['opt-in'] = True
82 config['countdown'] = 0
83 SaveConfig(config)
84 print('ninjalog upload is opted in.')
85 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +000086
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000087 if len(sys.argv) == 2 and sys.argv[1] == 'opt-out':
88 config['opt-in'] = False
89 SaveConfig(config)
90 print('ninjalog upload is opted out.')
91 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +000092
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000093 if 'opt-in' in config and not config['opt-in']:
94 # Upload is opted out.
95 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +000096
Takuto Ikutaa2e91db2020-06-09 11:21:59 +000097 if not config.get("is-googler", False):
98 # Not googler.
99 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000100
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000101 if config.get("countdown", 0) > 0:
102 # Need to show message.
103 ShowMessage(config["countdown"])
104 # Only save config if something has meaningfully changed.
105 SaveConfig(config)
106 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000107
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000108 if len(sys.argv) == 1:
109 # dry-run for debugging.
110 print("upload ninjalog dry-run")
111 return 0
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000112
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000113 # Run upload script without wait.
114 devnull = open(os.devnull, "w")
Takuto Ikuta59622a52020-06-11 05:36:31 +0000115 creationnflags = 0
116 if platform.system() == 'Windows':
117 creationnflags = subprocess.CREATE_NEW_PROCESS_GROUP
Takuto Ikuta84e43fa2021-01-19 02:51:50 +0000118 subprocess2.Popen([sys.executable, UPLOADER] + sys.argv[1:],
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000119 stdout=devnull,
Takuto Ikuta59622a52020-06-11 05:36:31 +0000120 stderr=devnull,
121 creationflags=creationnflags)
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000122
123
124if __name__ == '__main__':
Takuto Ikutaa2e91db2020-06-09 11:21:59 +0000125 sys.exit(main())