diff --git a/wexpect/__main__.py b/wexpect/__main__.py index 13e858f..abc8eb5 100644 --- a/wexpect/__main__.py +++ b/wexpect/__main__.py @@ -30,6 +30,9 @@ def main(): parser.add_argument('--port', type=int, help= "If the console reader class is SpawnSocket, this option specifies the " "socket's port.", default=False) + parser.add_argument('--pipe_file_name', type=str, help= + "If the console reader class is SpawnPipe, this option specifies the " + "pipe's name.", default=None) try: args = parser.parse_args() @@ -48,7 +51,7 @@ def main(): command = wexpect_util.join_args(args.command) cons = conole_reader_class( - path=command, host_pid=args.host_pid, codepage=args.codepage, port=args.port, + path=command, host_pid=args.host_pid, codepage=args.codepage, port=args.port, pipe_file_name=args.pipe_file_name, window_size_x=args.window_size_x, window_size_y=args.window_size_y, buffer_size_x=args.buffer_size_x, buffer_size_y=args.buffer_size_y, local_echo=wexpect_util.str2bool(args.local_echo), interact=wexpect_util.str2bool(args.interact)) diff --git a/wexpect/console_reader.py b/wexpect/console_reader.py index 781b657..8689e76 100644 --- a/wexpect/console_reader.py +++ b/wexpect/console_reader.py @@ -515,8 +515,12 @@ class ConsoleReaderPipe(ConsoleReaderBase): else: end_time = time.time() + timeout - pipe_name = 'wexpect_{}'.format(self.console_pid) - pipe_full_path = r'\\.\pipe\{}'.format(pipe_name) + try: + self.pipe_name = kwargs['pipe_file_name'] + except KeyError: + self.pipe_name = 'wexpect_{}'.format(self.console_pid) + + pipe_full_path = r'\\.\pipe\{}'.format(self.pipe_name) logger.info('Start pipe server: %s', pipe_full_path) self.pipe = win32pipe.CreateNamedPipe( pipe_full_path, diff --git a/wexpect/host.py b/wexpect/host.py index f512269..9874324 100644 --- a/wexpect/host.py +++ b/wexpect/host.py @@ -30,6 +30,7 @@ from .wexpect_util import join_args from .wexpect_util import init_logger from .wexpect_util import EOF_CHAR from .wexpect_util import SIGNAL_CHARS +from .wexpect_util import generate_id logger = logging.getLogger('wexpect') @@ -870,7 +871,8 @@ class SpawnPipe(SpawnBase): **kwargs): self.pipe = None self.console_class_name = 'ConsoleReaderPipe' - self.console_class_parameters = {} + self.pipe_file_name = f'wexpect_{generate_id()}' + self.console_class_parameters = {'pipe_file_name': self.pipe_file_name} super().__init__( command=command, args=args, timeout=timeout, maxread=maxread, @@ -889,8 +891,7 @@ class SpawnPipe(SpawnBase): else: end_time = time.time() + timeout - pipe_name = 'wexpect_{}'.format(self.console_pid) - pipe_full_path = r'\\.\pipe\{}'.format(pipe_name) + pipe_full_path = r'\\.\pipe\{}'.format(self.pipe_file_name) logger.debug(f'Trying to connect to pipe: {pipe_full_path}') while True: if end_time < time.time(): diff --git a/wexpect/wexpect_util.py b/wexpect/wexpect_util.py index 7987c2d..20dae5b 100644 --- a/wexpect/wexpect_util.py +++ b/wexpect/wexpect_util.py @@ -14,6 +14,8 @@ import sys import os import logging import signal +import string +import random # platform does not define VEOF so assume CTRL-D EOF_CHAR = b'\x04' @@ -27,7 +29,19 @@ SPAM = 5 logging.addLevelName(SPAM, "SPAM") +def generate_id(size=6, chars=string.ascii_uppercase + string.digits): + '''Generates random string, to use as ID. + Using random string as pipe's filename gives a workaround to #26 + From: https://stackoverflow.com/a/2257449/2506522 + ''' + return ''.join(random.choice(chars) for _ in range(size)) + + def str2bool(v): + '''Help parsing boolean values with argparse + From: https://stackoverflow.com/a/43357954/2506522 + + ''' if isinstance(v, bool): return v if v.lower() in ('yes', 'true', 't', 'y', '1'):