aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/project/utils.py
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2020-03-30 00:13:20 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2020-03-30 00:53:14 +0300
commit8a8941b853ba5ccecc65cf55222c6c129e92d675 (patch)
treed3a251cdfe4be68914f43103876d908930314bb4 /project/utils.py
parentproject.boost: first-class MinGW-w64 support (diff)
downloadcmake-common-8a8941b853ba5ccecc65cf55222c6c129e92d675.tar.gz
cmake-common-8a8941b853ba5ccecc65cf55222c6c129e92d675.zip
project: minor-ish refactoring
Diffstat (limited to 'project/utils.py')
-rw-r--r--project/utils.py35
1 files changed, 33 insertions, 2 deletions
diff --git a/project/utils.py b/project/utils.py
index 8c57f59..68327af 100644
--- a/project/utils.py
+++ b/project/utils.py
@@ -7,6 +7,7 @@ from contextlib import contextmanager
import logging
import os.path
import subprocess
+import tempfile
def normalize_path(s):
@@ -41,5 +42,35 @@ def run(cmd_line):
return subprocess.run(cmd_line, check=True)
-def run_cmake(cmake_args):
- return run(['cmake'] + cmake_args)
+@contextmanager
+def delete_on_error(path):
+ try:
+ yield
+ except:
+ logging.info('Removing temporary file: %s', path)
+ os.remove(path)
+ raise
+
+
+@contextmanager
+def delete(path):
+ try:
+ yield
+ finally:
+ logging.info('Removing temporary file: %s', path)
+ os.remove(path)
+
+
+@contextmanager
+def temp_file(contents, **kwargs):
+ '''Make NamedTemporaryFile usable on Windows.
+
+ It can't be opened a second time on Windows, hence this silliness.
+ '''
+ tmp = tempfile.NamedTemporaryFile(delete=False, **kwargs)
+ with tmp as file, delete_on_error(file.name):
+ path = file.name
+ logging.info('Writing temporary file: %s', path)
+ file.write(contents)
+ with delete(path):
+ yield path