blob: 72288860161d029b09654110a38be95767a09c6a [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')
18VERSION = 1
19
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):
43 print """
44Your ninjalog will be uploaded to build stats server. Uploaded log will be used
45to analyze user side build performance.
46
47The following information will be uploaded with ninjalog.
48* OS (e.g. Win, Mac or Linux)
49* build directory (e.g. /home/foo/chromium/src/out/Release)
50* hostname
51* number of cpu cores of building machine
52* cmdline passed to ninja (e.g. ninja -C out/Default -j1024 chrome)
53* build config (e.g. use_goma=true, is_component_build=true, etc)
54
55Uploading ninjalog will be started after you run autoninja another %d time.
56
57If you don't want to upload ninjalog, please run following command.
58$ %s opt-out
59
60If you allow upload ninjalog from next autoninja run, please run the following
61command.
62$ %s opt-in
63
64If you have question about this, please send mail to infra-dev@chromium.org
65
66""" % (countdown, __file__, __file__)
67
68
69def main():
70 config = LoadConfig()
71
72 if len(sys.argv) == 2 and sys.argv[1] == 'opt-in':
73 config['opt-in'] = True
74 config['countdown'] = 0
75 SaveConfig(config)
76 print('ninjalog upload is opted in.')
77 return 0
78
79 if len(sys.argv) == 2 and sys.argv[1] == 'opt-out':
80 config['opt-in'] = False
81 SaveConfig(config)
82 print('ninjalog upload is opted out.')
83 return 0
84
85 SaveConfig(config)
86
87 if 'opt-in' in config and not config['opt-in']:
88 # Upload is opted out.
89 return 0
90
91 if not config.get("is-googler", False):
92 # Not googler.
93 return 0
94
95 if config.get("countdown", 0) > 0:
96 # Need to show message.
97 ShowMessage(config["countdown"])
98 return 0
99
100 if len(sys.argv) == 1:
101 # dry-run for debugging.
102 print("upload ninjalog dry-run")
103 return 0
104
105 # Run upload script without wait.
106 devnull = open(os.devnull, "w")
107 subprocess.Popen([sys.executable, UPLOADER] + sys.argv[1:],
108 stdout=devnull, stderr=devnull)
109
110
111if __name__ == '__main__':
112 sys.exit(main())