blob: 8cdb5d93dbef97043d2b86a1fff7e806beb46dfb [file] [log] [blame]
Edward Lemur59a3b2f2020-01-14 01:50:50 +00001#!/usr/bin/env vpython
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 os
Takuto Ikuta9af233a2018-11-29 03:53:53 +00009import json
10import 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 Ikutac8069af2019-01-09 06:24:56 +000018VERSION = 2
Takuto Ikuta9af233a2018-11-29 03:53:53 +000019
20
21def LoadConfig():
22 if os.path.isfile(CONFIG):
23 with open(CONFIG, 'rb') as f:
24 config = json.load(f)
25 if config['version'] == VERSION:
Takuto Ikuta65ace122019-03-22 03:57:17 +000026 config['countdown'] = max(0, config['countdown'] - 1)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000027 return config
28
29 return {
30 'is-googler': ninjalog_uploader.IsGoogler(
31 'chromium-build-stats.appspot.com'),
32 'countdown': 10,
33 'version': VERSION,
34 }
35
36
37def SaveConfig(config):
38 with open(CONFIG, 'wb') as f:
39 json.dump(config, f)
40
41
42def ShowMessage(countdown):
Takuto Ikutac8069af2019-01-09 06:24:56 +000043 whitelisted = '\n'.join([' * %s' % config for config in
44 ninjalog_uploader.WHITELISTED_CONFIGS])
Raul Tambre80ee78e2019-05-06 22:41:05 +000045 print("""
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000046Your ninjalog will be uploaded to build stats server. The uploaded log will be
47used to analyze user side build performance.
Takuto Ikuta9af233a2018-11-29 03:53:53 +000048
49The following information will be uploaded with ninjalog.
50* OS (e.g. Win, Mac or Linux)
Takuto Ikuta9af233a2018-11-29 03:53:53 +000051* number of cpu cores of building machine
Takuto Ikutac8069af2019-01-09 06:24:56 +000052* build targets (e.g. chrome, browser_tests)
53* parallelism passed by -j flag
54* following build configs
55%s
Takuto Ikuta9af233a2018-11-29 03:53:53 +000056
57Uploading ninjalog will be started after you run autoninja another %d time.
58
59If you don't want to upload ninjalog, please run following command.
60$ %s opt-out
61
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000062If you want to allow upload ninjalog from next autoninja run, please run the
63following command.
Takuto Ikuta9af233a2018-11-29 03:53:53 +000064$ %s opt-in
65
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000066If you have questions about this, please send mail to infra-dev@chromium.org
Takuto Ikuta9af233a2018-11-29 03:53:53 +000067
Takuto Ikuta540ba9d2018-12-19 22:24:08 +000068You can find a more detailed explanation in
69%s
70
Takuto Ikutac8069af2019-01-09 06:24:56 +000071""" % (whitelisted, countdown, __file__, __file__,
Raul Tambre80ee78e2019-05-06 22:41:05 +000072 os.path.abspath(os.path.join(THIS_DIR, "ninjalog.README.md"))))
Takuto Ikuta9af233a2018-11-29 03:53:53 +000073
74
75def main():
76 config = LoadConfig()
77
78 if len(sys.argv) == 2 and sys.argv[1] == 'opt-in':
79 config['opt-in'] = True
80 config['countdown'] = 0
81 SaveConfig(config)
82 print('ninjalog upload is opted in.')
83 return 0
84
85 if len(sys.argv) == 2 and sys.argv[1] == 'opt-out':
86 config['opt-in'] = False
87 SaveConfig(config)
88 print('ninjalog upload is opted out.')
89 return 0
90
Takuto Ikuta9af233a2018-11-29 03:53:53 +000091 if 'opt-in' in config and not config['opt-in']:
92 # Upload is opted out.
93 return 0
94
95 if not config.get("is-googler", False):
96 # Not googler.
97 return 0
98
99 if config.get("countdown", 0) > 0:
100 # Need to show message.
101 ShowMessage(config["countdown"])
Bruce Dawsoncf9613f2019-03-20 03:57:35 +0000102 # Only save config if something has meaningfully changed.
103 SaveConfig(config)
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000104 return 0
105
106 if len(sys.argv) == 1:
107 # dry-run for debugging.
108 print("upload ninjalog dry-run")
109 return 0
110
111 # Run upload script without wait.
112 devnull = open(os.devnull, "w")
Edward Lemur59a3b2f2020-01-14 01:50:50 +0000113 subprocess2.Popen(['vpython', UPLOADER] + sys.argv[1:],
114 stdout=devnull, stderr=devnull)
Takuto Ikuta9af233a2018-11-29 03:53:53 +0000115
116
117if __name__ == '__main__':
118 sys.exit(main())