Skip to content

Commit 06a4040

Browse files
committed
Update the log timer to use context manager
1 parent d184322 commit 06a4040

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

utils/swift_build_support/swift_build_support/build_script_invocation.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import os
1414
import pipes
1515
import platform
16-
import time
1716

1817
from build_swift.build_swift import argparse
1918
from build_swift.build_swift.constants import BUILD_SCRIPT_IMPL_PATH
@@ -38,7 +37,7 @@
3837
from swift_build_support.swift_build_support.utils \
3938
import exit_rejecting_arguments
4039
from swift_build_support.swift_build_support.utils import fatal_error
41-
from swift_build_support.swift_build_support.utils import log_time
40+
from swift_build_support.swift_build_support.utils import log_time_in_scope
4241

4342

4443
class BuildScriptInvocation(object):
@@ -771,13 +770,11 @@ def _execute_merged_host_lipo_core_action(self):
771770
self._execute_action("merged-hosts-lipo-core")
772771

773772
def _execute_action(self, action_name):
774-
log_time('start', action_name)
775-
t_start = time.time()
776-
shell.call_without_sleeping(
777-
[BUILD_SCRIPT_IMPL_PATH] + self.impl_args +
778-
["--only-execute", action_name],
779-
env=self.impl_env, echo=self.args.verbose_build)
780-
log_time('end', action_name, time.time() - t_start)
773+
with log_time_in_scope(action_name):
774+
shell.call_without_sleeping(
775+
[BUILD_SCRIPT_IMPL_PATH] + self.impl_args +
776+
["--only-execute", action_name],
777+
env=self.impl_env, echo=self.args.verbose_build)
781778

782779
def execute_product_build_steps(self, product_class, host_target):
783780
product_source = product_class.product_source_name()
@@ -796,24 +793,18 @@ def execute_product_build_steps(self, product_class, host_target):
796793
if product.should_clean(host_target):
797794
log_message = "Cleaning %s" % product_name
798795
print("--- {} ---".format(log_message))
799-
t_start = time.time()
800-
log_time('start', log_message)
801-
product.clean(host_target)
802-
log_time('end', log_message, time.time() - t_start)
796+
with log_time_in_scope(log_message):
797+
product.clean(host_target)
803798
if product.should_build(host_target):
804799
log_message = "Building %s" % product_name
805800
print("--- {} ---".format(log_message))
806-
t_start = time.time()
807-
log_time('start', log_message, '0')
808-
product.build(host_target)
809-
log_time('end', log_message, time.time() - t_start)
801+
with log_time_in_scope(log_message):
802+
product.build(host_target)
810803
if product.should_test(host_target):
811804
log_message = "Running tests for %s" % product_name
812805
print("--- {} ---".format(log_message))
813-
t_start = time.time()
814-
log_time('start', log_message)
815-
product.test(host_target)
816-
log_time('end', log_message, time.time() - t_start)
806+
with log_time_in_scope(log_message):
807+
product.test(host_target)
817808
print("--- Finished tests for %s ---" % product_name)
818809
# Install the product if it should be installed specifically, or
819810
# if it should be built and `install_all` is set to True.
@@ -825,7 +816,5 @@ def execute_product_build_steps(self, product_class, host_target):
825816
not product.is_ignore_install_all_product()):
826817
log_message = "Installing %s" % product_name
827818
print("--- {} ---".format(log_message))
828-
t_start = time.time()
829-
log_time('start', log_message)
830-
product.install(host_target)
831-
log_time('end', log_message, time.time() - t_start)
819+
with log_time_in_scope(log_message):
820+
product.install(host_target)

utils/swift_build_support/swift_build_support/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
from __future__ import absolute_import, print_function, unicode_literals
1414

15+
import contextlib
1516
import json
1617
import os
1718
import sys
19+
import time
1820

1921

2022
from build_swift.build_swift.constants import SWIFT_BUILD_ROOT
@@ -59,6 +61,16 @@ def log_time(event, command, duration=0):
5961
f.close()
6062

6163

64+
@contextlib.contextmanager
65+
def log_time_in_scope(action_name):
66+
log_time('start', action_name)
67+
t_start = time.time()
68+
try:
69+
yield
70+
finally:
71+
log_time('end', action_name, time.time() - t_start)
72+
73+
6274
def log_analyzer():
6375
"""
6476
Analyze .build_script_log and provide a summary of the time execution.

0 commit comments

Comments
 (0)