You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by tv...@apache.org on 2021/02/04 07:53:17 UTC

[buildstream] 02/06: utils: add _TempTextBuffer for avoiding tmp files

This is an automated email from the ASF dual-hosted git repository.

tvb pushed a commit to branch aevri/win32_temptext
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 1ea866dcfa6f8eb54c2fad85f2b9ec902c198274
Author: Angelos Evripiotis <je...@bloomberg.net>
AuthorDate: Thu Jun 6 16:23:59 2019 +0100

    utils: add _TempTextBuffer for avoiding tmp files
    
    Add a convenience context manager for temporary text buffers, backed by
    StringIO. This can be used instead of temporary files in some cases,
    saving on file I/O and compatibility concerns.
---
 src/buildstream/utils.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py
index f509ce9..79cbbde 100644
--- a/src/buildstream/utils.py
+++ b/src/buildstream/utils.py
@@ -24,6 +24,8 @@ Utilities
 import calendar
 import errno
 import hashlib
+import io
+import locale
 import os
 import re
 import shutil
@@ -1278,6 +1280,53 @@ def _search_upward_for_files(directory, filenames):
         directory = parent_dir
 
 
+# A context manager for temporary text buffers.
+#
+# This can be used instead of temporary files in some cases, saving on file I/O
+# and compatibility concerns.
+#
+# The 'stream' member can be used in a similar way to an open text file.
+#
+# The getbuffer() method returns a bytes object that may be used with e.g.
+# cas.add_object().
+#
+# Note that the format of bytes written in the buffer are the same as
+# `open("tmp", "w")`:
+#
+# - The line separator is `os.linesep`.
+# - The encoding is `locale.getpreferredencoding()`.
+#
+class _TempTextBuffer():
+
+    def __init__(self):
+        self.stream = None
+
+    def __enter__(self):
+        self.stream = io.StringIO(newline=os.linesep)
+        return self
+
+    def __exit__(self, *_):
+        self.stream.close()
+        self.stream = None
+
+    # get_bytes_copy()
+    #
+    # Returns a copy of the encoded bytes of the text written to the buffer.
+    #
+    # Returns:
+    #    (bytes): The encoded bytes of the text written to this buffer.
+    #
+    def get_bytes_copy(self):
+        if self.stream is None:
+            raise Exception(
+                "Must be used as a context manager in a 'with' statement.")
+        strval = self.stream.getvalue()
+        bytestring = strval.encode(
+            locale.getpreferredencoding(do_setlocale=False)
+        )
+        return bytestring
+
+
 # _deterministic_umask()
 #
 # Context managed to apply a umask to a section that may be affected by a users