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