use generated random pipe_file_name instead of pid

The process pid is differs from the host and the child's point of view.
See #26 for more details. This, random generated pipe_file_name, whill solve this issue.
This commit is contained in:
Benedek Racz 2020-05-31 13:37:48 +02:00
parent c8977d8460
commit 850eece43b
4 changed files with 28 additions and 6 deletions

View File

@ -30,6 +30,9 @@ def main():
parser.add_argument('--port', type=int, help= parser.add_argument('--port', type=int, help=
"If the console reader class is SpawnSocket, this option specifies the " "If the console reader class is SpawnSocket, this option specifies the "
"socket's port.", default=False) "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: try:
args = parser.parse_args() args = parser.parse_args()
@ -48,7 +51,7 @@ def main():
command = wexpect_util.join_args(args.command) command = wexpect_util.join_args(args.command)
cons = conole_reader_class( 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, 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, 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)) local_echo=wexpect_util.str2bool(args.local_echo), interact=wexpect_util.str2bool(args.interact))

View File

@ -515,8 +515,12 @@ class ConsoleReaderPipe(ConsoleReaderBase):
else: else:
end_time = time.time() + timeout end_time = time.time() + timeout
pipe_name = 'wexpect_{}'.format(self.console_pid) try:
pipe_full_path = r'\\.\pipe\{}'.format(pipe_name) 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) logger.info('Start pipe server: %s', pipe_full_path)
self.pipe = win32pipe.CreateNamedPipe( self.pipe = win32pipe.CreateNamedPipe(
pipe_full_path, pipe_full_path,

View File

@ -30,6 +30,7 @@ from .wexpect_util import join_args
from .wexpect_util import init_logger from .wexpect_util import init_logger
from .wexpect_util import EOF_CHAR from .wexpect_util import EOF_CHAR
from .wexpect_util import SIGNAL_CHARS from .wexpect_util import SIGNAL_CHARS
from .wexpect_util import generate_id
logger = logging.getLogger('wexpect') logger = logging.getLogger('wexpect')
@ -870,7 +871,8 @@ class SpawnPipe(SpawnBase):
**kwargs): **kwargs):
self.pipe = None self.pipe = None
self.console_class_name = 'ConsoleReaderPipe' 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__( super().__init__(
command=command, args=args, timeout=timeout, maxread=maxread, command=command, args=args, timeout=timeout, maxread=maxread,
@ -889,8 +891,7 @@ class SpawnPipe(SpawnBase):
else: else:
end_time = time.time() + timeout end_time = time.time() + timeout
pipe_name = 'wexpect_{}'.format(self.console_pid) pipe_full_path = r'\\.\pipe\{}'.format(self.pipe_file_name)
pipe_full_path = r'\\.\pipe\{}'.format(pipe_name)
logger.debug(f'Trying to connect to pipe: {pipe_full_path}') logger.debug(f'Trying to connect to pipe: {pipe_full_path}')
while True: while True:
if end_time < time.time(): if end_time < time.time():

View File

@ -14,6 +14,8 @@ import sys
import os import os
import logging import logging
import signal import signal
import string
import random
# platform does not define VEOF so assume CTRL-D # platform does not define VEOF so assume CTRL-D
EOF_CHAR = b'\x04' EOF_CHAR = b'\x04'
@ -27,7 +29,19 @@ SPAM = 5
logging.addLevelName(SPAM, "SPAM") 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): def str2bool(v):
'''Help parsing boolean values with argparse
From: https://stackoverflow.com/a/43357954/2506522
'''
if isinstance(v, bool): if isinstance(v, bool):
return v return v
if v.lower() in ('yes', 'true', 't', 'y', '1'): if v.lower() in ('yes', 'true', 't', 'y', '1'):