blob: c6158d69ee994d07858531c19371e938cb194aab [file] [log] [blame]
Ben Segall530d86d2023-05-29 16:11:00 +00001#!/usr/bin/env python3
2# Copyright 2023 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"""This script manages the counter for how many times developers should
6be notified before uploading reclient metrics."""
7
8import json
9import os
10import subprocess
11import sys
12
13THIS_DIR = os.path.dirname(__file__)
14CONFIG = os.path.join(THIS_DIR, 'reclient_metrics.cfg')
15VERSION = 1
16
17
18def default_config():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000019 return {
20 'is-googler': is_googler(),
21 'countdown': 10,
22 'version': VERSION,
23 }
Ben Segall530d86d2023-05-29 16:11:00 +000024
25
26def load_config():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000027 config = None
28 try:
29 with open(CONFIG) as f:
30 raw_config = json.load(f)
31 if raw_config['version'] == VERSION:
32 raw_config['countdown'] = max(0, raw_config['countdown'] - 1)
33 config = raw_config
34 except Exception:
35 pass
36 if not config:
37 config = default_config()
38 save_config(config)
39 return config
Ben Segall530d86d2023-05-29 16:11:00 +000040
41
42def save_config(config):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000043 with open(CONFIG, 'w') as f:
44 json.dump(config, f)
Ben Segall530d86d2023-05-29 16:11:00 +000045
46
47def show_message(config, ninja_out):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000048 print("""
Ben Segall530d86d2023-05-29 16:11:00 +000049Your reclient metrics will be uploaded to the chromium build metrics database. The uploaded metrics will be used to analyze user side build performance.
50
51We upload the contents of {ninja_out_abs}.
52This contains
53* Flags passed to reproxy
54 * Auth related flags are filtered out by reproxy
55* Start and end time of build tasks
56* Aggregated durations and counts of events during remote build actions
57* OS (e.g. Win, Mac or Linux)
58* Number of cpu cores and the amount of RAM of the building machine
59
60Uploading reclient metrics will be started after you run autoninja another {config_count} time(s).
61
62If you don't want to upload reclient metrics, please run following command.
63$ python3 {file_path} opt-out
64
65If you want to allow upload reclient metrics from next autoninja run, please run the
66following command.
67$ python3 {file_path} opt-in
68
69If you have questions about this, please send an email to foundry-x@google.com
70
71You can find a more detailed explanation in
72{metrics_readme_path}
73or
74https://chromium.googlesource.com/chromium/tools/depot_tools/+/main/reclient_metrics.README.md
75""".format(
Mike Frysinger124bb8e2023-09-06 05:48:55 +000076 ninja_out_abs=os.path.abspath(
77 os.path.join(ninja_out, ".reproxy_tmp", "logs", "rbe_metrics.txt")),
78 config_count=config.get("countdown", 0),
79 file_path=__file__,
80 metrics_readme_path=os.path.abspath(
81 os.path.join(THIS_DIR, "reclient_metrics.README.md")),
82 ))
Ben Segall530d86d2023-05-29 16:11:00 +000083
84
85def is_googler(config=None):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000086 """Check whether this user is Googler or not."""
87 if config is not None and 'is-googler' in config:
88 return config['is-googler']
89 # Use cipd auth-info to check for googler status as
90 # downloading rewrapper configs already requires cipd to be logged in
91 p = subprocess.run('cipd auth-info',
92 stdout=subprocess.PIPE,
93 stderr=subprocess.PIPE,
94 text=True,
95 shell=True)
96 if p.returncode != 0:
97 return False
98 lines = p.stdout.splitlines()
99 if len(lines) == 0:
100 return False
101 l = lines[0]
102 # |l| will be like 'Logged in as <user>@google.com.' for googlers.
103 return l.startswith('Logged in as ') and l.endswith('@google.com.')
Ben Segall530d86d2023-05-29 16:11:00 +0000104
105
106def check_status(ninja_out):
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000107 """Checks metrics collections status and shows notice to user if needed.
Ben Segall530d86d2023-05-29 16:11:00 +0000108
109 Returns True if metrics should be collected."""
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000110 config = load_config()
111 if not is_googler(config):
112 return False
113 if 'opt-in' in config:
114 return config['opt-in']
115 if config.get("countdown", 0) > 0:
116 show_message(config, ninja_out)
117 return False
118 return True
Ben Segall530d86d2023-05-29 16:11:00 +0000119
120
121def main(argv):
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000122 cfg = load_config()
Ben Segall530d86d2023-05-29 16:11:00 +0000123
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000124 if not is_googler(cfg):
125 save_config(cfg)
126 return 0
Ben Segall530d86d2023-05-29 16:11:00 +0000127
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000128 if len(argv) == 2 and argv[1] == 'opt-in':
129 cfg['opt-in'] = True
130 cfg['countdown'] = 0
131 save_config(cfg)
132 print('reclient metrics upload is opted in.')
133 return 0
Ben Segall530d86d2023-05-29 16:11:00 +0000134
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000135 if len(argv) == 2 and argv[1] == 'opt-out':
136 cfg['opt-in'] = False
137 save_config(cfg)
138 print('reclient metrics upload is opted out.')
139 return 0
Ben Segall530d86d2023-05-29 16:11:00 +0000140
141
142if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000143 sys.exit(main(sys.argv))