From 3cd31858f4c3b3fe0915e72e327468cb76a8ee95 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 6 May 2024 13:10:23 -0400 Subject: [PATCH 1/3] Add command_trace.py from firebase-ios-sdk --- scripts/lib/command_trace.py | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 scripts/lib/command_trace.py diff --git a/scripts/lib/command_trace.py b/scripts/lib/command_trace.py new file mode 100644 index 0000000..c443af8 --- /dev/null +++ b/scripts/lib/command_trace.py @@ -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 From 01ace250152a5239c2af4f8e73092a3d06ebf684 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 6 May 2024 13:14:00 -0400 Subject: [PATCH 2/3] Add terminal.py from firebase-ios-sdk --- scripts/lib/terminal.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 scripts/lib/terminal.py diff --git a/scripts/lib/terminal.py b/scripts/lib/terminal.py new file mode 100644 index 0000000..cbf6c16 --- /dev/null +++ b/scripts/lib/terminal.py @@ -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 From 5604437501077033eb034d286e1d96aa03b17bd5 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 6 May 2024 13:38:12 -0400 Subject: [PATCH 3/3] Default to project name `generative-ai-swift` --- scripts/xcresult_logs.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/xcresult_logs.py b/scripts/xcresult_logs.py index 656ec7d..d3078c4 100755 --- a/scripts/xcresult_logs.py +++ b/scripts/xcresult_logs.py @@ -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)