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