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 | |
| 13 | NOTICE_COUNTDOWN_HEADER = ( |
| 14 | '*****************************************************\n' |
| 15 | '* METRICS COLLECTION WILL START IN %2d EXECUTIONS *' |
| 16 | ) |
| 17 | NOTICE_COLLECTION_HEADER = ( |
| 18 | '*****************************************************\n' |
| 19 | '* METRICS COLLECTION IS TAKING PLACE *' |
| 20 | ) |
| 21 | NOTICE_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 | |
| 29 | |
| 30 | def get_python_version(): |
| 31 | """Return the python version in the major.minor.micro format.""" |
| 32 | return '{v.major}.{v.minor}.{v.micro}'.format(v=sys.version_info) |
| 33 | |
| 34 | |
| 35 | def return_code_from_exception(exception): |
| 36 | """Returns the exit code that would result of raising the exception.""" |
| 37 | if exception is None: |
| 38 | return 0 |
| 39 | if isinstance(exception[1], SystemExit): |
| 40 | return exception[1].code |
| 41 | return 1 |
| 42 | |
| 43 | |
| 44 | def seconds_to_weeks(duration): |
| 45 | """Transform a |duration| from seconds to weeks approximately. |
| 46 | |
| 47 | Drops the lowest 19 bits of the integer representation, which ammounts to |
| 48 | about 6 days. |
| 49 | """ |
| 50 | return int(duration) >> 19 |
| 51 | |
| 52 | |
| 53 | def get_repo_timestamp(path_to_repo): |
| 54 | """Get an approximate timestamp for the upstream of |path_to_repo|. |
| 55 | |
| 56 | Returns the top two bits of the timestamp of the HEAD for the upstream of the |
| 57 | branch path_to_repo is checked out at. |
| 58 | """ |
| 59 | # Get the upstream for the current branch. If we're not in a branch, fallback |
| 60 | # to HEAD. |
| 61 | try: |
| 62 | upstream = scm.GIT.GetUpstreamBranch(path_to_repo) |
| 63 | except subprocess2.CalledProcessError: |
| 64 | upstream = 'HEAD' |
| 65 | |
| 66 | # Get the timestamp of the HEAD for the upstream of the current branch. |
| 67 | p = subprocess2.Popen( |
| 68 | ['git', '-C', path_to_repo, 'log', '-n1', upstream, '--format=%at'], |
| 69 | stdout=subprocess2.PIPE, stderr=subprocess2.PIPE) |
| 70 | stdout, _ = p.communicate() |
| 71 | |
| 72 | # If there was an error, give up. |
| 73 | if p.returncode != 0: |
| 74 | return None |
| 75 | |
| 76 | # Get the age of the checkout in weeks. |
| 77 | return seconds_to_weeks(stdout.strip()) |
| 78 | |
| 79 | |
| 80 | def print_notice(countdown): |
| 81 | """Print a notice to let the user know the status of metrics collection.""" |
| 82 | colorama.init() |
| 83 | print colorama.Fore.RED + '\033[1m' |
| 84 | if countdown: |
| 85 | print NOTICE_COUNTDOWN_HEADER % countdown |
| 86 | else: |
| 87 | print NOTICE_COLLECTION_HEADER |
| 88 | print NOTICE_FOOTER + colorama.Style.RESET_ALL |