Skip to content

Commit 479f892

Browse files
committed
Update the log timer to use context manager
1 parent e44288a commit 479f892

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
@@ -36,7 +35,7 @@
3635
from swift_build_support.swift_build_support.utils \
3736
import exit_rejecting_arguments
3837
from swift_build_support.swift_build_support.utils import fatal_error
39-
from swift_build_support.swift_build_support.utils import log_time
38+
from swift_build_support.swift_build_support.utils import log_time_in_scope
4039

4140

4241
class BuildScriptInvocation(object):
@@ -781,13 +780,11 @@ def _execute_merged_host_lipo_core_action(self):
781780
self._execute_action("merged-hosts-lipo-core")
782781

783782
def _execute_action(self, action_name):
784-
log_time('start', action_name)
785-
t_start = time.time()
786-
shell.call_without_sleeping(
787-
[BUILD_SCRIPT_IMPL_PATH] + self.impl_args +
788-
["--only-execute", action_name],
789-
env=self.impl_env, echo=self.args.verbose_build)
790-
log_time('end', action_name, time.time() - t_start)
783+
with log_time_in_scope(action_name):
784+
shell.call_without_sleeping(
785+
[BUILD_SCRIPT_IMPL_PATH] + self.impl_args +
786+
["--only-execute", action_name],
787+
env=self.impl_env, echo=self.args.verbose_build)
791788

792789
def execute_product_build_steps(self, product_class, host_target):
793790
product_source = product_class.product_source_name()
@@ -806,24 +803,18 @@ def execute_product_build_steps(self, product_class, host_target):
806803
if product.should_clean(host_target):
807804
log_message = "Cleaning %s" % product_name
808805
print("--- {} ---".format(log_message))
809-
t_start = time.time()
810-
log_time('start', log_message)
811-
product.clean(host_target)
812-
log_time('end', log_message, time.time() - t_start)
806+
with log_time_in_scope(log_message):
807+
product.clean(host_target)
813808
if product.should_build(host_target):
814809
log_message = "Building %s" % product_name
815810
print("--- {} ---".format(log_message))
816-
t_start = time.time()
817-
log_time('start', log_message, '0')
818-
product.build(host_target)
819-
log_time('end', log_message, time.time() - t_start)
811+
with log_time_in_scope(log_message):
812+
product.build(host_target)
820813
if product.should_test(host_target):
821814
log_message = "Running tests for %s" % product_name
822815
print("--- {} ---".format(log_message))
823-
t_start = time.time()
824-
log_time('start', log_message)
825-
product.test(host_target)
826-
log_time('end', log_message, time.time() - t_start)
816+
with log_time_in_scope(log_message):
817+
product.test(host_target)
827818
print("--- Finished tests for %s ---" % product_name)
828819
# Install the product if it should be installed specifically, or
829820
# if it should be built and `install_all` is set to True.
@@ -835,7 +826,5 @@ def execute_product_build_steps(self, product_class, host_target):
835826
not product.is_ignore_install_all_product()):
836827
log_message = "Installing %s" % product_name
837828
print("--- {} ---".format(log_message))
838-
t_start = time.time()
839-
log_time('start', log_message)
840-
product.install(host_target)
841-
log_time('end', log_message, time.time() - t_start)
829+
with log_time_in_scope(log_message):
830+
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
@@ -10,9 +10,11 @@
1010
#
1111
# ===---------------------------------------------------------------------===#
1212

13+
import contextlib
1314
import json
1415
import os
1516
import sys
17+
import time
1618

1719

1820
from build_swift.build_swift.constants import SWIFT_BUILD_ROOT
@@ -57,6 +59,16 @@ def log_time(event, command, duration=0):
5759
f.close()
5860

5961

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

0 commit comments

Comments
 (0)