diff options
author | Cassidy Burden <cburden@google.com> | 2016-06-14 16:19:18 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-06-15 11:00:50 -0700 |
commit | 242c6cbf1a3c7d1d09cde13281d8bb36d7ef6001 (patch) | |
tree | 0f0566be3dd0ad326fd7eee649d100604392ecea /binary_search_tool/binary_search_state.py | |
parent | 9c665a01b55ee4e672b29ff352989a640eac084f (diff) | |
download | toolchain-utils-242c6cbf1a3c7d1d09cde13281d8bb36d7ef6001.tar.gz |
Fix binary search tool's temp file creation
Fix the binary search tool so that temp files are created properly. This
means that temp files made for the switch scripts should be automatically
deleted once the switch script completes.
TEST=New unit test for temp file deletion
Change-Id: I5d36bfa07e274cd79965f182d85d6a7ea3006c85
Reviewed-on: https://chrome-internal-review.googlesource.com/264735
Commit-Ready: Cassidy Burden <cburden@google.com>
Tested-by: Cassidy Burden <cburden@google.com>
Reviewed-by: Caroline Tice <cmtice@google.com>
Diffstat (limited to 'binary_search_tool/binary_search_state.py')
-rwxr-xr-x | binary_search_tool/binary_search_state.py | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/binary_search_tool/binary_search_state.py b/binary_search_tool/binary_search_state.py index ecb3dd7e..7803cacb 100755 --- a/binary_search_tool/binary_search_state.py +++ b/binary_search_tool/binary_search_state.py @@ -103,15 +103,16 @@ class BinarySearchState(object): def RunSwitchScript(self, switch_script, item_list): if self.file_args: - temp_file = tempfile.mktemp() - f = open(temp_file, 'wb') - f.write('\n'.join(item_list)) - f.close() - command = '%s %s' % (switch_script, temp_file) + with tempfile.NamedTemporaryFile() as f: + f.write('\n'.join(item_list)) + f.flush() + command = '%s %s' % (switch_script, f.name) + ret, _, _ = self.ce.RunCommandWExceptionCleanup( + command, print_to_console=self.verbose) else: command = '%s %s' % (switch_script, ' '.join(item_list)) - ret, _, _ = self.ce.RunCommandWExceptionCleanup( - command, print_to_console=self.verbose) + ret, _, _ = self.ce.RunCommandWExceptionCleanup( + command, print_to_console=self.verbose) assert ret == 0, 'Switch script %s returned %d' % (switch_script, ret) def TestScript(self): @@ -301,9 +302,9 @@ class BinarySearchState(object): class MockBinarySearchState(BinarySearchState): """Mock class for BinarySearchState.""" - def __init__(self): + def __init__(self, **kwargs): # Initialize all arguments to None - kwargs = { + default_kwargs = { 'get_initial_items': 'echo "1"', 'switch_to_good': None, 'switch_to_bad': None, @@ -317,7 +318,8 @@ class MockBinarySearchState(BinarySearchState): 'file_args': None, 'verbose': None } - super(MockBinarySearchState, self).__init__(**kwargs) + default_kwargs.update(kwargs) + super(MockBinarySearchState, self).__init__(**default_kwargs) def _CanonicalizeScript(script_name): |