Commit 98a7e803 authored by Samuel Huang's avatar Samuel Huang Committed by Commit Bot

[Metrics] Fix box drawing on Windows, and refactor code.

The Unicode box drawing turn out malformed on Windows (even with
fix_encoding.py). This CL refactors the box drawing code, and makes
Windows box drawing use regular ASCII characters. Details:
* Add print_red_boxed_text() to programatically print box aroun given
  list og strings.
  * Unicode box drawing characters are escaped, removing the use of
    non-ASCII characters in source code.
* Convert message source to generators for greater flexibilty, and
  remove the need for manual horizontal padding.
* Add proper use colorama with Style.BRIGNT and Style.RESET_ALL.
* For Windows, revert to ASCII characters for box drawing.

Change-Id: I71a728db480709b4376d538aaecd701f9197e7ac
Reviewed-on: https://chromium-review.googlesource.com/c/1461249
Commit-Queue: Samuel Huang <huangs@chromium.org>
Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
parent b1f865da
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2018 The Chromium Authors. All rights reserved. # Copyright (c) 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
...@@ -22,43 +21,35 @@ CURRENT_VERSION = 1 ...@@ -22,43 +21,35 @@ CURRENT_VERSION = 1
APP_URL = 'https://cit-cli-metrics.appspot.com' APP_URL = 'https://cit-cli-metrics.appspot.com'
EMPTY_LINE = ( def get_notice_countdown_header(countdown):
'┃ ┃' if countdown == 0:
) yield ' METRICS COLLECTION IS TAKING PLACE'
NOTICE_COUNTDOWN_HEADER = ( else:
'┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n' yield ' METRICS COLLECTION WILL START IN %d EXECUTIONS' % countdown
'┃ METRICS COLLECTION WILL START IN %2d EXECUTIONS ┃'
) def get_notice_version_change_header():
NOTICE_COLLECTION_HEADER = ( yield ' WE ARE COLLECTING ADDITIONAL METRICS'
'┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n' yield ''
'┃ METRICS COLLECTION IS TAKING PLACE ┃' yield ' Please review the changes and opt-in again.'
)
NOTICE_VERSION_CHANGE_HEADER = ( def get_notice_footer():
'┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n' yield 'To suppress this message opt in or out using:'
'┃ WE ARE COLLECTING ADDITIONAL METRICS ┃\n' yield '$ gclient metrics [--opt-in] [--opt-out]'
'┃ ┃\n' yield 'For more information please see metrics.README.md'
'┃ Please review the changes and opt-in again. ┃' yield 'in your depot_tools checkout or visit'
) yield 'https://goo.gl/yNpRDV.'
NOTICE_FOOTER = (
'┃ To suppress this message opt in or out using: ┃\n' def get_change_notice(version):
'┃ $ gclient metrics [--opt-in] [--opt-out] ┃\n' if version == 0:
'┃ For more information please see metrics.README.md ┃\n' pass # No changes for version 0
'┃ in your depot_tools checkout or visit ┃\n' elif version == 1:
'┃ https://goo.gl/yNpRDV. ┃\n' yield 'We want to collect the Git version.'
'┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n' yield 'We want to collect information about the HTTP'
) yield 'requests that depot_tools makes, and the git and'
yield 'cipd commands it executes.'
CHANGE_NOTICE = { yield ''
# No changes for version 0 yield 'We only collect known strings to make sure we'
0: '', yield 'don\'t record PII.'
1: ('┃ We want to collect the Git version. ┃\n'
'┃ We want to collect information about the HTTP ┃\n'
'┃ requests that depot_tools makes, and the git and ┃\n'
'┃ cipd commands it executes. ┃\n'
'┃ ┃\n'
'┃ We only collect known strings to make sure we ┃\n'
'┃ don\'t record PII. ┃')
}
KNOWN_PROJECT_URLS = { KNOWN_PROJECT_URLS = {
...@@ -286,25 +277,31 @@ def get_repo_timestamp(path_to_repo): ...@@ -286,25 +277,31 @@ def get_repo_timestamp(path_to_repo):
# Get the age of the checkout in weeks. # Get the age of the checkout in weeks.
return seconds_to_weeks(stdout.strip()) return seconds_to_weeks(stdout.strip())
def print_red_boxed_text(out, min_width, lines):
def print_notice(countdown):
"""Print a notice to let the user know the status of metrics collection."""
colorama.init() colorama.init()
print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='') if sys.platform == 'win32':
if countdown: [EW, NS, SE, SW, NE, NW] = list('=|++++')
print(NOTICE_COUNTDOWN_HEADER % countdown, file=sys.stderr)
else: else:
print(NOTICE_COLLECTION_HEADER, file=sys.stderr) [EW, NS, SE, SW, NE, NW] = list(u'\u2501\u2503\u250F\u2513\u2517\u251B')
print(EMPTY_LINE, file=sys.stderr) out(colorama.Fore.RED + colorama.Style.BRIGHT)
print(NOTICE_FOOTER + colorama.Style.RESET_ALL, file=sys.stderr) width = max(min_width, max(len(line) for line in lines))
out(SE + EW * (width + 2) + SW + '\n')
for line in lines:
out('%s %-*s %s\n' % (NS, width, line, NS))
out(NE + EW * (width + 2) + NW + '\n')
out(colorama.Style.RESET_ALL)
def print_notice(countdown):
"""Print a notice to let the user know the status of metrics collection."""
lines = list(get_notice_countdown_header(countdown))
lines.append('')
lines += list(get_notice_footer())
print_red_boxed_text(sys.stderr.write, 49, lines)
def print_version_change(config_version): def print_version_change(config_version):
"""Print a notice to let the user know we are collecting more metrics.""" """Print a notice to let the user know we are collecting more metrics."""
colorama.init() lines = list(get_notice_version_change_header())
print(colorama.Fore.RED + '\033[1m', file=sys.stderr, end='') for version in xrange(config_version + 1, CURRENT_VERSION + 1):
print(NOTICE_VERSION_CHANGE_HEADER, file=sys.stderr) lines.append('')
print(EMPTY_LINE, file=sys.stderr) lines += list(get_change_notice(version))
for version in range(config_version + 1, CURRENT_VERSION + 1): print_red_boxed_text(sys.stderr.write, 49, lines)
print(CHANGE_NOTICE[version], file=sys.stderr)
print(EMPTY_LINE, file=sys.stderr)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment