blob: baabeea7e0c4e639421548039f709f419ee888cc [file] [log] [blame]
Edward Lemur32e3d1e2018-07-12 00:54:05 +00001#!/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
6import scm
7import subprocess2
8import sys
9
10from third_party import colorama
11
12
13NOTICE_COUNTDOWN_HEADER = (
14 '*****************************************************\n'
15 '* METRICS COLLECTION WILL START IN %2d EXECUTIONS *'
16)
17NOTICE_COLLECTION_HEADER = (
18 '*****************************************************\n'
19 '* METRICS COLLECTION IS TAKING PLACE *'
20)
21NOTICE_FOOTER = (
22 '* *\n'
23 '* For more information, and for how to disable this *\n'
24 '* message, please see metrics.README.md in your *\n'
25 '* depot_tools checkout. *\n'
26 '*****************************************************\n'
27)
28
Edward Lemur40764b02018-07-20 18:50:29 +000029KNOWN_PROJECT_URLS = {
30 'https://chrome-internal.googlesource.com/chrome/ios_internal',
31 'https://chrome-internal.googlesource.com/infra/infra_internal',
32 'https://chromium.googlesource.com/breakpad/breakpad',
33 'https://chromium.googlesource.com/chromium/src',
34 'https://chromium.googlesource.com/chromium/tools/depot_tools',
35 'https://chromium.googlesource.com/crashpad/crashpad',
36 'https://chromium.googlesource.com/external/gyp',
37 'https://chromium.googlesource.com/external/naclports',
38 'https://chromium.googlesource.com/infra/goma/client',
39 'https://chromium.googlesource.com/infra/infra',
40 'https://chromium.googlesource.com/native_client/',
41 'https://chromium.googlesource.com/syzygy',
42 'https://chromium.googlesource.com/v8/v8',
43 'https://dart.googlesource.com/sdk',
44 'https://pdfium.googlesource.com/pdfium',
45 'https://skia.googlesource.com/buildbot',
46 'https://skia.googlesource.com/skia',
47 'https://webrtc.googlesource.com/src',
48}
49
Edward Lemur32e3d1e2018-07-12 00:54:05 +000050
51def get_python_version():
52 """Return the python version in the major.minor.micro format."""
53 return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info)
54
55
56def return_code_from_exception(exception):
57 """Returns the exit code that would result of raising the exception."""
58 if exception is None:
59 return 0
60 if isinstance(exception[1], SystemExit):
61 return exception[1].code
62 return 1
63
64
65def seconds_to_weeks(duration):
66 """Transform a |duration| from seconds to weeks approximately.
67
68 Drops the lowest 19 bits of the integer representation, which ammounts to
69 about 6 days.
70 """
71 return int(duration) >> 19
72
73
74def get_repo_timestamp(path_to_repo):
75 """Get an approximate timestamp for the upstream of |path_to_repo|.
76
77 Returns the top two bits of the timestamp of the HEAD for the upstream of the
78 branch path_to_repo is checked out at.
79 """
80 # Get the upstream for the current branch. If we're not in a branch, fallback
81 # to HEAD.
82 try:
83 upstream = scm.GIT.GetUpstreamBranch(path_to_repo)
84 except subprocess2.CalledProcessError:
85 upstream = 'HEAD'
86
87 # Get the timestamp of the HEAD for the upstream of the current branch.
88 p = subprocess2.Popen(
89 ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'],
90 stdout=subprocess2.PIPE, stderr=subprocess2.PIPE)
91 stdout, _ = p.communicate()
92
93 # If there was an error, give up.
94 if p.returncode != 0:
95 return None
96
97 # Get the age of the checkout in weeks.
98 return seconds_to_weeks(stdout.strip())
99
100
101def print_notice(countdown):
102 """Print a notice to let the user know the status of metrics collection."""
103 colorama.init()
104 print colorama.Fore.RED + '\033[1m'
105 if countdown:
106 print NOTICE_COUNTDOWN_HEADER % countdown
107 else:
108 print NOTICE_COLLECTION_HEADER
109 print NOTICE_FOOTER + colorama.Style.RESET_ALL