mirror of
https://github.com/clearml/wexpect-venv
synced 2025-02-23 12:57:33 +00:00
[REF] refactor start of console: now starts as python module.
This commit is contained in:
parent
4b512294ee
commit
c35949704a
wexpect
56
wexpect/__main__.py
Normal file
56
wexpect/__main__.py
Normal file
@ -0,0 +1,56 @@
|
||||
import argparse
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import wexpect.console_reader as console_reader
|
||||
import wexpect.wexpect_util as wexpect_util
|
||||
|
||||
|
||||
logger = logging.getLogger('wexpect')
|
||||
logger.info('Hello')
|
||||
|
||||
def str2bool(v):
|
||||
if isinstance(v, bool):
|
||||
return v
|
||||
if v.lower() in ('yes', 'true', 't', 'y', '1'):
|
||||
return True
|
||||
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
|
||||
return False
|
||||
else:
|
||||
raise argparse.ArgumentTypeError('Boolean value expected.')
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Wexpect: executable automation for Windows.')
|
||||
|
||||
parser.add_argument('--console_reader_class', type=str,
|
||||
help='Class name of the console reader class.')
|
||||
|
||||
parser.add_argument('command', type=str, nargs='+', help='Command to be run with its arguments')
|
||||
parser.add_argument('--host_pid', type=int, help='Host process process ID')
|
||||
parser.add_argument('--codepage', type=str, help='Codepage')
|
||||
parser.add_argument('--window_size_x', type=int, help='Width of the console window', default=80)
|
||||
parser.add_argument('--window_size_y', type=int, help='Height of the console window', default=25)
|
||||
parser.add_argument('--buffer_size_x', type=int, help='Width of the console buffer', default=80)
|
||||
parser.add_argument('--buffer_size_y', type=int, help='Height of the console buffer',
|
||||
default=16000)
|
||||
parser.add_argument('--local_echo', type=str, help='Echo sent characters', default=True)
|
||||
parser.add_argument('--interact', type=str, help='Show console window', default=False)
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.console_reader_class == 'ConsoleReaderSocket':
|
||||
conole_reader_class = console_reader.ConsoleReaderSocket
|
||||
elif args.console_reader_class == 'ConsoleReaderPipe':
|
||||
conole_reader_class = console_reader.ConsoleReaderPipe
|
||||
|
||||
command = wexpect_util.join_args(args.command)
|
||||
|
||||
cons = conole_reader_class(
|
||||
path=command, host_pid=args.host_pid, codepage=args.codepage,
|
||||
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=str2bool(args.local_echo), interact=str2bool(args.interact))
|
||||
|
||||
sys.exit(cons.child_exitstatus)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -87,6 +87,7 @@ from .wexpect_util import ExceptionPexpect
|
||||
from .wexpect_util import EOF
|
||||
from .wexpect_util import TIMEOUT
|
||||
from .wexpect_util import split_command_line
|
||||
from .wexpect_util import join_args
|
||||
from .wexpect_util import init_logger
|
||||
from .wexpect_util import EOF_CHAR
|
||||
from .wexpect_util import SIGNAL_CHARS
|
||||
@ -353,7 +354,7 @@ class SpawnBase:
|
||||
if getattr(sys, 'frozen', False) else
|
||||
os.path.abspath(__file__))
|
||||
spath = [os.path.dirname(dirname)]
|
||||
pyargs = ['-c']
|
||||
pyargs = ['-m']
|
||||
if getattr(sys, 'frozen', False):
|
||||
# If we are running 'frozen', add library.zip and lib\library.zip to sys.path
|
||||
# py2exe: Needs appropriate 'zipfile' option in setup script and 'bundle_files' 3
|
||||
@ -367,7 +368,12 @@ class SpawnBase:
|
||||
os.path.basename(os.path.splitext(sys.executable)[0])))
|
||||
pyargs.insert(0, '-S') # skip 'import site'
|
||||
|
||||
if getattr(sys, 'frozen', False):
|
||||
self.run_coverage = False
|
||||
|
||||
if self.run_coverage:
|
||||
python_executable = 'coverage'
|
||||
pyargs = ['run', '--parallel-mode', '-c']
|
||||
elif getattr(sys, 'frozen', False):
|
||||
python_executable = os.path.join(dirname, 'python.exe')
|
||||
else:
|
||||
python_executable = os.path.join(os.path.dirname(sys.executable), 'python.exe')
|
||||
@ -380,27 +386,33 @@ class SpawnBase:
|
||||
'codepage': self.codepage
|
||||
}
|
||||
)
|
||||
console_class_parameters_kv_pairs = [f'{k}={v}' for k, v in self.console_class_parameters.items()]
|
||||
console_class_parameters_str = ', '.join(console_class_parameters_kv_pairs)
|
||||
console_class_parameters_kv_pairs = [
|
||||
f'--{k}={v}' for k, v in self.console_class_parameters.items()
|
||||
]
|
||||
console_class_parameters_str = ' '.join(console_class_parameters_kv_pairs)
|
||||
|
||||
child_class_initializator = f"cons = wexpect.{self.console_class_name}(wexpect.join_args({args}), {console_class_parameters_str});"
|
||||
args_str = join_args(args)
|
||||
child_class_initializator = (
|
||||
f"wexpect --console_reader_class {self.console_class_name}"
|
||||
f" {console_class_parameters_str} -- {args_str}"
|
||||
)
|
||||
|
||||
commandLine = '"%s" %s "%s"' % (python_executable,
|
||||
commandLine = '"%s" %s %s' % (python_executable,
|
||||
' '.join(pyargs),
|
||||
"import sys;"
|
||||
f"sys.path = {spath} + sys.path;"
|
||||
"import wexpect;"
|
||||
"import time;"
|
||||
"wexpect.console_reader.logger.info('loggerStart.');"
|
||||
f"{child_class_initializator}"
|
||||
"wexpect.console_reader.logger.info(f'Console finished2. {cons.child_exitstatus}');"
|
||||
"sys.exit(cons.child_exitstatus)"
|
||||
child_class_initializator
|
||||
)
|
||||
|
||||
logger.info(f'Console starter command:{commandLine}')
|
||||
|
||||
environ = os.environ
|
||||
python_path = environ.get('PYTHONPATH', '')
|
||||
spath = ';'.join(spath)
|
||||
environ['PYTHONPATH'] = f'{spath};{python_path}'
|
||||
|
||||
_, _, self.console_pid, __otid = win32process.CreateProcess(
|
||||
None, commandLine, None, None, False, win32process.CREATE_NEW_CONSOLE, None, self.cwd, si)
|
||||
None, commandLine, None, None, False, win32process.CREATE_NEW_CONSOLE, environ,
|
||||
self.cwd, si
|
||||
)
|
||||
|
||||
def get_console_process(self, force=False):
|
||||
if force or self.console_process is None:
|
||||
|
Loading…
Reference in New Issue
Block a user