Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 | [diff] [blame] | 1 | #!/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 Lemur | c87d45b | 2018-07-26 17:43:11 | [diff] [blame] | 6 | from __future__ import print_function |
| 7 | |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 | [diff] [blame] | 8 | import scm |
| 9 | import subprocess2 |
| 10 | import sys |
| 11 | |
| 12 | from third_party import colorama |
| 13 | |
| 14 | |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 | [diff] [blame^] | 15 | # 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. |
| 18 | CURRENT_VERSION = 0 |
| 19 | |
Edward Lemur | 5ba1e9c | 2018-07-23 18:19:02 | [diff] [blame] | 20 | APP_URL = 'https://cit-cli-metrics.appspot.com' |
| 21 | |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 | [diff] [blame^] | 22 | EMPTY_LINE = ( |
| 23 | '* *' |
| 24 | ) |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 | [diff] [blame] | 25 | NOTICE_COUNTDOWN_HEADER = ( |
| 26 | '*****************************************************\n' |
| 27 | '* METRICS COLLECTION WILL START IN %2d EXECUTIONS *' |
| 28 | ) |
| 29 | NOTICE_COLLECTION_HEADER = ( |
| 30 | '*****************************************************\n' |
| 31 | '* METRICS COLLECTION IS TAKING PLACE *' |
| 32 | ) |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 | [diff] [blame^] | 33 | NOTICE_VERSION_CHANGE_HEADER = ( |
| 34 | '*****************************************************\n' |
| 35 | '* WE ARE COLLECTING ADDITIONAL METRICS *' |
| 36 | ) |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 | [diff] [blame] | 37 | NOTICE_FOOTER = ( |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 | [diff] [blame] | 38 | '* 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 Lemur | 4883626 | 2018-10-18 02:08:06 | [diff] [blame^] | 44 | CHANGE_NOTICE = { |
| 45 | # No changes for version 0 |
| 46 | 0: '', |
| 47 | } |
| 48 | |
| 49 | |
Edward Lemur | 40764b0 | 2018-07-20 18:50:29 | [diff] [blame] | 50 | KNOWN_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 Lemur | 32e3d1e | 2018-07-12 00:54:05 | [diff] [blame] | 71 | |
| 72 | def 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 | |
| 77 | def 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 | |
| 86 | def 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 | |
| 95 | def 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 | |
| 122 | def print_notice(countdown): |
| 123 | """Print a notice to let the user know the status of metrics collection.""" |
| 124 | colorama.init() |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 | [diff] [blame^] | 125 | print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='') |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 | [diff] [blame] | 126 | if countdown: |
Edward Lemur | c87d45b | 2018-07-26 17:43:11 | [diff] [blame] | 127 | print(NOTICE_COUNTDOWN_HEADER % countdown, file=sys.stderr) |
Edward Lemur | 32e3d1e | 2018-07-12 00:54:05 | [diff] [blame] | 128 | else: |
Edward Lemur | c87d45b | 2018-07-26 17:43:11 | [diff] [blame] | 129 | print(NOTICE_COLLECTION_HEADER, file=sys.stderr) |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 | [diff] [blame^] | 130 | print(EMPTY_LINE, file=sys.stderr) |
Edward Lemur | c87d45b | 2018-07-26 17:43:11 | [diff] [blame] | 131 | print(NOTICE_FOOTER + colorama.Style.RESET_ALL, file=sys.stderr) |
Edward Lemur | 4883626 | 2018-10-18 02:08:06 | [diff] [blame^] | 132 | |
| 133 | |
| 134 | def 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) |