8000 Entry point to print test logs from JSON output. by jeffherman · Pull Request #684 · google/openhtf · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Entry point to print test logs from JSON output. #684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions openhtf/util/testcat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2017 Google Inc. All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Output a JSON-encoded log output to standard out.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be in contrib/ or bin/, not util/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setuptools.find_packages (used in setup.py) will only search for directories with __init__.py. Adding either contrib/ or bin/ to openhtf's package list will result in either contrib or bin being a top-level importable module. While it's unlikely that anyone would ever publish a bin or contrib pypi module, it's not clear that we should be co-opting this part of the namespace for openhtf-specific purposes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @jeffherman. Unless you want to force people to go to our GitHub page and download utilities separately, stuff like this should go somewhere inside the actual package.

However I am an advocate of using runnable modules (invoke with python -m) instead of declaring entry points for setuptools to wire up executable scripts. In my anecdotal experience, the entry points mechanism is more brittle and doesn't always work with all environments. Whereas if you've gotten as far as successfully making the packager itself importable, it's more unlikely runnable modules within will fail.


Takes OpenHTF JSON log outputs and prints out human-readable files.
"""

import argparse
import datetime
import json
import logging


ATTR_MAP = dict(
red='\033[91m',
green='\033[92m',
yellow='\033[93m',
blue='\033[94m',
purple='\033[095m',
cyan='\033[96m',
white='\033[97m')

LEVEL_MAP = {
logging.DEBUG:'cyan',
logging.INFO:'green',
logging.WARNING:'yellow',
logging.ERROR:'red',
logging.CRITICAL:'red'}


def main():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'test_json', help='Path of JSON to parse')
options = parser.parse_args()

test_output = json.load(open(options.test_json, 'r'))
for log_record in test_output['log_records']:
epoch = log_record['timestamp_millis'] / 1000.
time_str = (
datetime.datetime.fromtimestamp(epoch).strftime(
'%Y-%m-%d %H:%M:%S.%f'))
attr = ATTR_MAP[LEVEL_MAP[log_record['level']]]
for line in log_record['message'].splitlines():
print '{attr}{time_str} {source}:{lineno} {line}\033[0m'.format(
attr=attr,
time_str=time_str,
source=log_record['source'],
lineno=log_record['lineno'],
line=line.encode('utf-8'))


if __name__ == '__main__':
main()
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,9 @@ def run_tests(self):
'pytest>=2.9.2',
'pytest-cov>=2.2.1',
],
entry_points={
'console_scripts': [
'openhtf-testcat = openhtf.util.testcat:main'
]
}
)
0