blob: 406aa002652c7c9e4ad436ce4cef097015642a7c [file] [log] [blame]
Takuto Ikuta9af233a2018-11-29 03:53:53 +00001#!/usr/bin/env python
2# 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
6import os
7import subprocess
8import json
9import sys
10
11from third_party import httplib2
12
13import ninjalog_uploader
14
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:
26 config['countdown'] -= 1
27 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])
Takuto Ikuta9af233a2018-11-29 03:53:53 +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__,
Takuto Ikuta540ba9d2018-12-19 22:24:08 +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
91 SaveConfig(config)
92
93 if 'opt-in' in config and not config['opt-in']:
94 # Upload is opted out.
95 return 0
96
97 if not config.get("is-googler", False):
98 # Not googler.
99 return 0
100
101 if config.get("countdown", 0) > 0:
102 # Need to show message.
103 ShowMessage(config["countdown"])
104 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")
113 subprocess.Popen([sys.executable, UPLOADER] + sys.argv[1:],
114 stdout=devnull, stderr=devnull)
115
116
117if __name__ == '__main__':
118 sys.exit(main())