Fix issues working with stdin when piping to Python.

Review Request #13555 — Created Feb. 18, 2024 and submitted — Latest diff uploaded

Information

rbinstall
master

Reviewers

We hit an issue before where piping the installer to Python would give
us a useless stdin. I had thought this was fixed, but I've still hit
errors with the new code here.

First off, in the cases I've tested locally, stdin isn't coming up as a
TTY, which just means none of this worked. In my past tests, it did.
This might be dependent on Python in some way. I'm not sure.

The solution here is to re-open stdin as a TTY. This gives us back full
input from the terminal, and seems to be the standard practice (though
time will tell if there are gotchas).

Second, querying for terminal info could fail. We now check for
exceptions here and just fall back to an empty result.

Tested running the installer manually, via curl | python, and via
cat | python on both macOS and Linux (over SSH). Verified I was able
to run the installer, provide input that the installer could work with,
and that the terminal queries continued to work (including detecting light
backgrounds).

Diff Revision 1 (Latest)

Commits

First Last Summary ID Author
Fix issues working with stdin when piping to Python.
We hit an issue before where piping the installer to Python would give us a useless stdin. I had thought this was fixed, but I've still hit errors with the new code here. First off, in the cases I've tested locally, stdin isn't coming up as a TTY, which just means none of this worked. In my past tests, it did. This might be dependent on Python in some way. I'm not sure. The solution here is to re-open stdin as a TTY. This gives us back full input from the terminal, and seems to be the standard practice (though time will tell if there are gotchas). Second, querying for terminal info could fail. We now check for exceptions here and just fall back to an empty result.
0e0d08f012478d39f21d643d8e9b0489ffc7def6 Christian Hammond

Files

rbinstall/ui.py
scripts/templates/bootstrap.py
rbinstall/ui.py
Revision 6dc3402ddb81c4e3f87293015d2a33144287157e New Change
314 lines
def query_terminal(
315
        The resulting data from the terminal query.
315
        The resulting data from the terminal query.
316
    """
316
    """
317
    if termios is None or tty is None or not os.isatty(sys.stdout.fileno()):
317
    if termios is None or tty is None or not os.isatty(sys.stdout.fileno()):
318
        return ''
318
        return ''
319

    
   
319

   

    
   
320
    try:
320
    fd = sys.stdin.fileno()
321
>>>>    fd = sys.stdin.fileno()
321
    old_settings = termios.tcgetattr(fd)
322
>>>>    old_settings = termios.tcgetattr(fd)
322

    
   
323

   
323
    result = ''
324
>>>>    result = ''
324

    
   
325

   
325
    try:
326
>>>>    try:
326
        tty.setraw(fd)
327
>>>>        tty.setraw(fd)
327
        sys.stdout.write(ansi_code)
328
>>>>        sys.stdout.write(ansi_code)
328
        sys.stdout.flush()
329
>>>>        sys.stdout.flush()
329

    
   
330

   
330
        for i in range(max_len):
331
>>>>        for i in range(max_len):
331
            c = sys.stdin.read(1)
332
>>>>            c = sys.stdin.read(1)
332

    
   
333

   
333
            if c == terminator or not c:
334
>>>>            if c == terminator or not c:
334
                break
335
>>>>                break
335

    
   
336

   
336
            result += c
337
>>>>            result += c
337
    finally:
338
>>>>    finally:
338
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
339
>>>>        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

    
   
340
    except Exception:

    
   
341
        # Something went wrong, so just return an empty string.

    
   
342
        result = ''
339

    
   
343

   
340
    return result
344
    return result
341

    
   
345

   
342

    
   
346

   
343
def print_header(
347
def print_header(
265 lines
scripts/templates/bootstrap.py
Loading...