Skip to content
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

Fix xcresult_logs.py script #162

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
79 changes: 79 additions & 0 deletions scripts/lib/command_trace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2019 Google
#
# 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.

import logging

from lib import terminal

_commands = logging.getLogger('commands')


def log(command_args):
"""Logs that a command has run.

Args:
command_args: A list of the command and its arguments.
"""
if _commands.isEnabledFor(logging.DEBUG):
columns = terminal.columns()

text = ' '.join(command_args)

# When just passing --trace, shorten output to the width of the current
# window. When running extra verbose don't shorten.
if not logging.root.isEnabledFor(logging.INFO):
if len(text) >= columns:
text = text[0:columns - 5] + ' ...'

_commands.debug('%s', text)


def add_arguments(parser):
"""Adds standard arguments to the given ArgumentParser."""
parser.add_argument('--trace', action='store_true',
help='show commands')
parser.add_argument('--verbose', '-v', action='count', default=0,
help='run verbosely')


def enable_tracing():
"""Enables tracing of command execution."""
_commands.setLevel(logging.DEBUG)


def setup(args):
"""Prepares for tracing/verbosity based on the given parsed arguments."""
level = logging.WARN

if args.trace:
enable_tracing()

if args.verbose >= 2:
level = logging.DEBUG
elif args.verbose >= 1:
level = logging.INFO

logging.basicConfig(format='%(message)s', level=level)


def parse_args(parser):
"""Shortcut that adds arguments, parses, and runs setup.

Returns:
The args result from parser.parse_args().
"""
add_arguments(parser)
args = parser.parse_args()
setup(args)
return args
38 changes: 38 additions & 0 deletions scripts/lib/terminal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2019 Google
#
# 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.

import subprocess
import threading


_lock = threading.Lock()
_columns = None


def columns():
"""Returns the number of columns in the terminal's display."""

global _columns
with _lock:
if _columns is None:
_columns = _find_terminal_columns()
return _columns


def _find_terminal_columns():
try:
result = subprocess.check_output(['tput', 'cols'])
return int(result.rstrip())
except subprocess.CalledProcessError:
return 80
4 changes: 4 additions & 0 deletions scripts/xcresult_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ def project_from_workspace_path(path):
if ext == '.xcworkspace':
_logger.debug('Using project %s from workspace %s', root, path)
return root
elif path == '.':
project = 'generative-ai-swift'
_logger.debug(f'Using project {project} from workspace {path}')
return project

raise ValueError('%s is not a valid workspace path' % path)

Expand Down
Loading