blob: 9bdae9e6832346694039fddbc86048fa731ed994 [file] [log] [blame]
Edward Lemur32e3d1e2018-07-12 00:54:051#!/usr/bin/env python
2# Copyright (c) 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
Edward Lemurc87d45b2018-07-26 17:43:116from __future__ import print_function
7
Edward Lemur32e3d1e2018-07-12 00:54:058import scm
9import subprocess2
10import sys
11
12from third_party import colorama
13
14
Edward Lemur48836262018-10-18 02:08:0615# Current version of metrics recording.
16# When we add new metrics, the version number will be increased, we display the
17# user what has changed, and ask the user to agree again.
18CURRENT_VERSION = 0
19
Edward Lemur5ba1e9c2018-07-23 18:19:0220APP_URL = 'https://cit-cli-metrics.appspot.com'
21
Edward Lemur48836262018-10-18 02:08:0622EMPTY_LINE = (
23 '* *'
24)
Edward Lemur32e3d1e2018-07-12 00:54:0525NOTICE_COUNTDOWN_HEADER = (
26 '*****************************************************\n'
27 '* METRICS COLLECTION WILL START IN %2d EXECUTIONS *'
28)
29NOTICE_COLLECTION_HEADER = (
30 '*****************************************************\n'
31 '* METRICS COLLECTION IS TAKING PLACE *'
32)
Edward Lemur48836262018-10-18 02:08:0633NOTICE_VERSION_CHANGE_HEADER = (
34 '*****************************************************\n'
35 '* WE ARE COLLECTING ADDITIONAL METRICS *'
36)
Edward Lemur32e3d1e2018-07-12 00:54:0537NOTICE_FOOTER = (
Edward Lemur32e3d1e2018-07-12 00:54:0538 '* For more information, and for how to disable this *\n'
39 '* message, please see metrics.README.md in your *\n'
40 '* depot_tools checkout. *\n'
41 '*****************************************************\n'
42)
43
Edward Lemur48836262018-10-18 02:08:0644CHANGE_NOTICE = {
45 # No changes for version 0
46 0: '',
47}
48
49
Edward Lemur40764b02018-07-20 18:50:2950KNOWN_PROJECT_URLS = {
51 'https://chrome-internal.googlesource.com/chrome/ios_internal',
52 'https://chrome-internal.googlesource.com/infra/infra_internal',
53 'https://chromium.googlesource.com/breakpad/breakpad',
54 'https://chromium.googlesource.com/chromium/src',
55 'https://chromium.googlesource.com/chromium/tools/depot_tools',
56 'https://chromium.googlesource.com/crashpad/crashpad',
57 'https://chromium.googlesource.com/external/gyp',
58 'https://chromium.googlesource.com/external/naclports',
59 'https://chromium.googlesource.com/infra/goma/client',
60 'https://chromium.googlesource.com/infra/infra',
61 'https://chromium.googlesource.com/native_client/',
62 'https://chromium.googlesource.com/syzygy',
63 'https://chromium.googlesource.com/v8/v8',
64 'https://dart.googlesource.com/sdk',
65 'https://pdfium.googlesource.com/pdfium',
66 'https://skia.googlesource.com/buildbot',
67 'https://skia.googlesource.com/skia',
68 'https://webrtc.googlesource.com/src',
69}
70
Edward Lemur32e3d1e2018-07-12 00:54:0571
72def get_python_version():
73 """Return the python version in the major.minor.micro format."""
74 return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
75
76
77def return_code_from_exception(exception):
78 """Returns the exit code that would result of raising the exception."""
79 if exception is None:
80 return 0
81 if isinstance(exception[1], SystemExit):
82 return exception[1].code
83 return 1
84
85
86def seconds_to_weeks(duration):
87 """Transform a |duration| from seconds to weeks approximately.
88
89 Drops the lowest 19 bits of the integer representation, which ammounts to
90 about 6 days.
91 """
92 return int(duration) >> 19
93
94
95def get_repo_timestamp(path_to_repo):
96 """Get an approximate timestamp for the upstream of |path_to_repo|.
97
98 Returns the top two bits of the timestamp of the HEAD for the upstream of the
99 branch path_to_repo is checked out at.
100 """
101 # Get the upstream for the current branch. If we're not in a branch, fallback
102 # to HEAD.
103 try:
104 upstream = scm.GIT.GetUpstreamBranch(path_to_repo)
105 except subprocess2.CalledProcessError:
106 upstream = 'HEAD'
107
108 # Get the timestamp of the HEAD for the upstream of the current branch.
109 p = subprocess2.Popen(
110 ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
111 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
112 stdout, _ = p.communicate()
113
114 # If there was an error, give up.
115 if p.returncode != 0:
116 return None
117
118 # Get the age of the checkout in weeks.
119 return seconds_to_weeks(stdout.strip())
120
121
122def print_notice(countdown):
123 """Print a notice to let the user know the status of metrics collection."""
124 colorama.init()
Edward Lemur48836262018-10-18 02:08:06125 print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
Edward Lemur32e3d1e2018-07-12 00:54:05126 if countdown:
Edward Lemurc87d45b2018-07-26 17:43:11127 print(NOTICE_COUNTDOWN_HEADER % countdown, file=sys.stderr)
Edward Lemur32e3d1e2018-07-12 00:54:05128 else:
Edward Lemurc87d45b2018-07-26 17:43:11129 print(NOTICE_COLLECTION_HEADER, file=sys.stderr)
Edward Lemur48836262018-10-18 02:08:06130 print(EMPTY_LINE, file=sys.stderr)
Edward Lemurc87d45b2018-07-26 17:43:11131 print(NOTICE_FOOTER + colorama.Style.RESET_ALL, file=sys.stderr)
Edward Lemur48836262018-10-18 02:08:06132
133
134def print_version_change(config_version):
135 """Print a notice to let the user know we are collecting more metrics."""
136 colorama.init()
137 print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='')
138 print(NOTICE_VERSION_CHANGE_HEADER, file=sys.stderr)
139 print(EMPTY_LINE, file=sys.stderr)
140 for version in range(config_version + 1, CURRENT_VERSION + 1):
141 print(CHANGE_NOTICE[version], file=sys.stderr)
142 print(EMPTY_LINE, file=sys.stderr)