Skip to content

Commit 6eabc52

Browse files
shahmishalahoppen
authored andcommitted
Update the log timer to use context manager
1 parent 5e5ee21 commit 6eabc52

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):
@@ -769,13 +768,11 @@ def _execute_merged_host_lipo_core_action(self):
769768
self._execute_action("merged-hosts-lipo-core")
770769

771770
def _execute_action(self, action_name):
772-
log_time('start', action_name)
773-
t_start = time.time()
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)
778-
log_time('end', action_name, time.time() - t_start)
771+
with log_time_in_scope(action_name):
772+
shell.call_without_sleeping(
773+
[BUILD_SCRIPT_IMPL_PATH] + self.impl_args +
774+
["--only-execute", action_name],
775+
env=self.impl_env, echo=self.args.verbose_build)
779776

780777
def execute_product_build_steps(self, product_class, host_target):
781778
product_source = product_class.product_source_name()
@@ -794,24 +791,18 @@ def execute_product_build_steps(self, product_class, host_target):
794791
if product.should_clean(host_target):
795792
log_message = "Cleaning %s" % product_name
796793
print("--- {} ---".format(log_message))
797-
t_start = time.time()
798-
log_time('start', log_message)
799-
product.clean(host_target)
800-
log_time('end', log_message, time.time() - t_start)
794+
with log_time_in_scope(log_message):
795+
product.clean(host_target)
801796
if product.should_build(host_target):
802797
log_message = "Building %s" % product_name
803798
print("--- {} ---".format(log_message))
804-
t_start = time.time()
805-
log_time('start', log_message, '0')
806-
product.build(host_target)
807-
log_time('end', log_message, time.time() - t_start)
799+
with log_time_in_scope(log_message):
800+
product.build(host_target)
808801
if product.should_test(host_target):
809802
log_message = "Running tests for %s" % product_name
810803
print("--- {} ---".format(log_message))
811-
t_start = time.time()
812-
log_time('start', log_message)
813-
product.test(host_target)
814-
log_time('end', log_message, time.time() - t_start)
804+
with log_time_in_scope(log_message):
805+
product.test(host_target)
815806
print("--- Finished tests for %s ---" % product_name)
816807
# Install the product if it should be installed specifically, or
817808
# if it should be built and `install_all` is set to True.
@@ -823,7 +814,5 @@ def execute_product_build_steps(self, product_class, host_target):
823814
not product.is_ignore_install_all_product()):
824815
log_message = "Installing %s" % product_name
825816
print("--- {} ---".format(log_message))
826-
t_start = time.time()
827-
log_time('start', log_message)
828-
product.install(host_target)
829-
log_time('end', log_message, time.time() - t_start)
817+
with log_time_in_scope(log_message):
818+
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)