[Notes] [Git][BuildStream/buildstream][master] 4 commits: _stream.py: Preserve stdout FD across checkout-to-stdout



Title: GitLab

Benjamin Schubert pushed to branch master at BuildStream / buildstream

Commits:

3 changed files:

Changes:

  • buildstream/_stream.py
    ... ... @@ -423,9 +423,16 @@ class Stream():
    423 423
                     else:
    
    424 424
                         if location == '-':
    
    425 425
                             with target.timed_activity("Creating tarball"):
    
    426
    -                            with os.fdopen(sys.stdout.fileno(), 'wb') as fo:
    
    427
    -                                with tarfile.open(fileobj=fo, mode="w|") as tf:
    
    428
    -                                    sandbox_vroot.export_to_tar(tf, '.')
    
    426
    +                            # Save the stdout FD to restore later
    
    427
    +                            saved_fd = os.dup(sys.stdout.fileno())
    
    428
    +                            try:
    
    429
    +                                with os.fdopen(sys.stdout.fileno(), 'wb') as fo:
    
    430
    +                                    with tarfile.open(fileobj=fo, mode="w|") as tf:
    
    431
    +                                        sandbox_vroot.export_to_tar(tf, '.')
    
    432
    +                            finally:
    
    433
    +                                # No matter what, restore stdout for further use
    
    434
    +                                os.dup2(saved_fd, sys.stdout.fileno())
    
    435
    +                                os.close(saved_fd)
    
    429 436
                         else:
    
    430 437
                             with target.timed_activity("Creating tarball '{}'"
    
    431 438
                                                        .format(location)):
    

  • tests/frontend/buildcheckout.py
    ... ... @@ -128,7 +128,6 @@ def test_build_checkout_tarball(datafiles, cli):
    128 128
         assert os.path.join('.', 'usr', 'include', 'pony.h') in tar.getnames()
    
    129 129
     
    
    130 130
     
    
    131
    -@pytest.mark.skip(reason="Capturing the binary output is causing a stacktrace")
    
    132 131
     @pytest.mark.datafiles(DATA_DIR)
    
    133 132
     def test_build_checkout_tarball_stdout(datafiles, cli):
    
    134 133
         project = os.path.join(datafiles.dirname, datafiles.basename)
    
    ... ... @@ -143,7 +142,7 @@ def test_build_checkout_tarball_stdout(datafiles, cli):
    143 142
     
    
    144 143
         checkout_args = ['checkout', '--tar', 'target.bst', '-']
    
    145 144
     
    
    146
    -    result = cli.run(project=project, args=checkout_args)
    
    145
    +    result = cli.run(project=project, args=checkout_args, binary_capture=True)
    
    147 146
         result.assert_success()
    
    148 147
     
    
    149 148
         with open(tarball, 'wb') as f:
    

  • tests/testutils/runcli.py
    ... ... @@ -17,7 +17,7 @@ import pytest
    17 17
     # CliRunner convenience API (click.testing module) does not support
    
    18 18
     # separation of stdout/stderr.
    
    19 19
     #
    
    20
    -from _pytest.capture import MultiCapture, FDCapture
    
    20
    +from _pytest.capture import MultiCapture, FDCapture, FDCaptureBinary
    
    21 21
     
    
    22 22
     # Import the main cli entrypoint
    
    23 23
     from buildstream._frontend import cli as bst_cli
    
    ... ... @@ -234,9 +234,10 @@ class Cli():
    234 234
         #    silent (bool): Whether to pass --no-verbose
    
    235 235
         #    env (dict): Environment variables to temporarily set during the test
    
    236 236
         #    args (list): A list of arguments to pass buildstream
    
    237
    +    #    binary_capture (bool): Whether to capture the stdout/stderr as binary
    
    237 238
         #
    
    238 239
         def run(self, configure=True, project=None, silent=False, env=None,
    
    239
    -            cwd=None, options=None, args=None):
    
    240
    +            cwd=None, options=None, args=None, binary_capture=False):
    
    240 241
             if args is None:
    
    241 242
                 args = []
    
    242 243
             if options is None:
    
    ... ... @@ -278,7 +279,7 @@ class Cli():
    278 279
                 except ValueError:
    
    279 280
                     sys.__stdout__ = open('/dev/stdout', 'w')
    
    280 281
     
    
    281
    -            result = self.invoke(bst_cli, bst_args)
    
    282
    +            result = self.invoke(bst_cli, bst_args, binary_capture=binary_capture)
    
    282 283
     
    
    283 284
             # Some informative stdout we can observe when anything fails
    
    284 285
             if self.verbose:
    
    ... ... @@ -295,7 +296,7 @@ class Cli():
    295 296
     
    
    296 297
             return result
    
    297 298
     
    
    298
    -    def invoke(self, cli, args=None, color=False, **extra):
    
    299
    +    def invoke(self, cli, args=None, color=False, binary_capture=False, **extra):
    
    299 300
             exc_info = None
    
    300 301
             exception = None
    
    301 302
             exit_code = 0
    
    ... ... @@ -305,8 +306,8 @@ class Cli():
    305 306
             old_stdin = sys.stdin
    
    306 307
             with open(os.devnull) as devnull:
    
    307 308
                 sys.stdin = devnull
    
    308
    -
    
    309
    -            capture = MultiCapture(out=True, err=True, in_=False, Capture=FDCapture)
    
    309
    +            capture_kind = FDCaptureBinary if binary_capture else FDCapture
    
    310
    +            capture = MultiCapture(out=True, err=True, in_=False, Capture=capture_kind)
    
    310 311
                 capture.start_capturing()
    
    311 312
     
    
    312 313
                 try:
    



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]