Exemplo n.º 1
0
 def __init__(self, engine=''):
     if not engine:
         engine = './scoutfish'
     self.p = PopenSpawn(engine, encoding="utf-8")
     self.wait_ready()
     self.pgn = ''
     self.db = ''
Exemplo n.º 2
0
 def __init__(self, engine=''):
     if not engine:
         engine = './parser'
     self.engine = engine
     self.p = PopenSpawn(engine, encoding="utf-8")
     self.pgn = ''
     self.db = ''
Exemplo n.º 3
0
def getIPv4Interfaces():
    cmd = "netsh interface ip show config"
    session = PopenSpawn(cmd)
    output = session.read(200000)
    lines = output.splitlines()
    intname = None
    intjson = {}
    outputjson = {}
    for line in lines:
        line = line.decode("utf-8")
        splited = line.split(': ')
        if "Configuration for interface " in line:
            intname = line.split("Configuration for interface ")[-1].replace(
                '"', "")
            # print('Found ' + intname)
            intjson = {}
            continue
        elif "" == line:
            if intname is not None:
                outputjson.update({intname: intjson})
            intname = None
            intjson = {}
            continue

        if intname:
            intjson.update({splited[0].strip(): splited[-1].strip()})

    return outputjson
Exemplo n.º 4
0
    class ProcessIO(IO):
        def __init__(self):
            IO.__init__(self)
            self.proc = None
            self.buffer_size = 1

        def __del__(self):
            if self.proc is not None:
                self.proc.kill(signal.CTRL_BREAK_EVENT)

        def run(self, exe):
            #self.proc = Popen([exe], stdout=PIPE, stdin=PIPE, bufsize=self.buffer_size, shell=True)
            self.proc = PopenSpawn(exe)
            

        def send(self, s):
            #self.proc.stdin.write(s)
            self.proc.write(s)
            
        def recv(self, size):
            #return self.proc.stdout.read(size)
            return self.proc.read(size)

        def recvuntil(self, pred):
            self.proc.expect(pred)
            return self.proc.before
Exemplo n.º 5
0
    def __init__(self, timeout, debug=False):
        'Initialize the SerAPI subprocess'
        self.debug = debug
        try:
            self.proc = PopenSpawn('sertop --implicit --omit_loc --print0',
                                   encoding='utf-8',
                                   timeout=timeout,
                                   maxread=10000000)
        except FileNotFoundError:
            log(
                'Please make sure the "sertop" program is in the PATH.\nYou may have to run "eval $(opam env)".',
                'ERROR')
            sys.exit(1)
        self.proc.expect_exact(
            '(Feedback((doc_id 0)(span_id 1)(route 0)(contents Processed)))\0')
        self.send('Noop')
        self.states_stack = []

        # global printing options
        self.execute('Unset Printing Notations.')
        self.execute('Unset Printing Wildcard.')
        self.execute('Set Printing Coercions.')
        self.execute('Unset Printing Allow Match Default Clause.')
        self.execute('Unset Printing Factorizable Match Patterns.')
        self.execute('Unset Printing Compact Contexts.')
        self.execute('Set Printing Implicit.')
        self.execute('Set Printing Depth 999999.')
        self.execute('Unset Printing Records.')

        # initialize the state stack
        self.push()

        self.ast_cache = {}
        self.dead = False
Exemplo n.º 6
0
    def __enter__(self):
        from pexpect.popen_spawn import PopenSpawn

        cwd = os.getcwd()
        env = os.environ.copy()
        env['PATH'] = self.activator.pathsep_join(
            self.activator.path_conversion(
                concatv(
                    self.activator._get_path_dirs(join(cwd, 'shell')),
                    (dirname(sys.executable), ),
                    self.activator._get_starting_path_list(),
                )))
        env['PYTHONPATH'] = CONDA_PACKAGE_ROOT
        env = {str(k): str(v) for k, v in iteritems(env)}

        p = PopenSpawn(self.shell_name,
                       timeout=6,
                       maxread=2000,
                       searchwindowsize=None,
                       logfile=sys.stdout,
                       cwd=cwd,
                       env=env,
                       encoding=None,
                       codec_errors='strict')
        if self.init_command:
            p.sendline(self.init_command)
        self.p = p
        return self
 def __init__(self, engine=""):
     if not engine:
         engine = "./scoutfish"
     self.p = PopenSpawn(engine, timeout=TIME_OUT_SECOND, encoding="utf-8")
     self.wait_ready()
     self.pgn = ""
     self.db = ""
Exemplo n.º 8
0
    def __init__(self, args, no_output=False):

        if opt.direct_exec:
            msg('')
            from subprocess import call, check_output
            f = (call, check_output)[bool(no_output)]
            ret = f([args[0]] + args[1:])
            if f == call and ret != 0:
                die(
                    1,
                    red('ERROR: process returned a non-zero exit status ({})'.
                        format(ret)))
        else:
            if opt.pexpect_spawn:
                self.p = pexpect.spawn(args[0], args[1:], encoding='utf8')
                self.p.delaybeforesend = 0
            else:
                self.p = PopenSpawn(args, encoding='utf8')
#				self.p.delaybeforesend = 0 # TODO: try this here too

            if opt.exact_output: self.p.logfile = sys.stdout

        self.req_exit_val = 0
        self.skip_ok = False
        self.timeout = int(opt.pexpect_timeout or 0) or (60, 5)[bool(
            opt.debug_pexpect)]
        self.sent_value = None
Exemplo n.º 9
0
    def __init__(self, args, no_output=False):

        if opt.direct_exec:
            msg('')
            from subprocess import run, DEVNULL
            run([args[0]] + args[1:],
                check=True,
                stdout=DEVNULL if no_output else None)
        else:
            timeout = int(opt.pexpect_timeout or 0) or (60, 5)[bool(
                opt.debug_pexpect)]
            if opt.pexpect_spawn:
                self.p = pexpect.spawn(args[0],
                                       args[1:],
                                       encoding='utf8',
                                       timeout=timeout)
                self.p.delaybeforesend = 0
            else:
                self.p = PopenSpawn(args, encoding='utf8', timeout=timeout)
#				self.p.delaybeforesend = 0 # TODO: try this here too

            if opt.exact_output: self.p.logfile = sys.stdout

        self.req_exit_val = 0
        self.skip_ok = False
        self.sent_value = None
Exemplo n.º 10
0
	def __init__(self,name,mmgen_cmd,cmd_args,desc,no_output=False,passthru_args=[],msg_only=False,no_msg=False):
		cmd_args = ['--{}{}'.format(k.replace('_','-'),
			'='+getattr(opt,k) if getattr(opt,k) != True else ''
			) for k in passthru_args if getattr(opt,k)] \
			+ ['--data-dir='+data_dir] + cmd_args

		if g.platform == 'win': cmd,args = 'python',[mmgen_cmd]+cmd_args
		else:                   cmd,args = mmgen_cmd,cmd_args

		for i in args:
			if type(i) not in (str,unicode):
				m1 = 'Error: missing input files in cmd line?:'
				m2 = '\nName: {}\nCmd: {}\nCmd args: {}'
				die(2,(m1+m2).format(name,cmd,args))

		if opt.popen_spawn:
			args = [u'{q}{}{q}'.format(a,q="'" if ' ' in a else '') for a in args]

		cmd_str = u'{} {}'.format(cmd,u' '.join(args)).replace('\\','/')
		if opt.coverage:
			fs = 'python -m trace --count --coverdir={} --file={} {c}'
			cmd_str = fs.format(*init_coverage(),c=cmd_str)

		if opt.log:
			log_fd.write(cmd_str+'\n')

		if not no_msg:
			if opt.verbose or opt.print_cmdline or opt.exact_output:
				clr1,clr2,eol = ((green,cyan,'\n'),(nocolor,nocolor,' '))[bool(opt.print_cmdline)]
				sys.stderr.write(green('Testing: {}\n'.format(desc)))
				if not msg_only:
					s = repr(cmd_str) if g.platform == 'win' else cmd_str
					sys.stderr.write(clr1(u'Executing {}{}'.format(clr2(s),eol)))
			else:
				m = 'Testing {}: '.format(desc)
				msg_r(m)

		if msg_only: return

		if opt.direct_exec:
			msg('')
			from subprocess import call,check_output
			f = (call,check_output)[bool(no_output)]
			ret = f([cmd] + args)
			if f == call and ret != 0:
				die(1,red('ERROR: process returned a non-zero exit status ({})'.format(ret)))
		else:
			if opt.traceback:
				cmd,args = g.traceback_cmd,[cmd]+args
				cmd_str = g.traceback_cmd + ' ' + cmd_str
#			Msg('\ncmd_str: {}'.format(cmd_str))
			if opt.popen_spawn:
				# PopenSpawn() requires cmd string to be bytes.  However, it autoconverts unicode
				# input to bytes, though this behavior seems to be undocumented.  Setting 'encoding'
				# to 'UTF-8' will cause pexpect to reject non-unicode string input.
				self.p = PopenSpawn(cmd_str.encode('utf8'))
			else:
				self.p = pexpect.spawn(cmd,args)
			if opt.exact_output: self.p.logfile = sys.stdout
Exemplo n.º 11
0
def ping(host):
    # For Windows, IPv6 neighbors can be discovered by sending a link-local packet across the whole L2 network.
    # Response time should be <1ms since the toolkit needs to physically be near the nodes.
    session = PopenSpawn('ping -w 1 -n 8 ' + host)
    output = session.read(2000)
    output = output.decode('utf-8')
    print(output)
    return output
Exemplo n.º 12
0
 def test_unexpected_eof(self):
     p = PopenSpawn("ls -l /bin")
     try:
         p.expect("_Z_XY_XZ")  # Probably never see this in ls output.
     except pexpect.EOF:
         pass
     else:
         self.fail("Expected an EOF exception.")
Exemplo n.º 13
0
 def test_expect_eof(self):
     the_old_way = subprocess.Popen(args=["ls", "-l", "/bin"], stdout=subprocess.PIPE).communicate()[0].rstrip()
     p = PopenSpawn("ls -l /bin")
     # This basically tells it to read everything. Same as pexpect.run()
     # function.
     p.expect(pexpect.EOF)
     the_new_way = p.before.rstrip()
     assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)
Exemplo n.º 14
0
def pingIPv4(host):
    session = PopenSpawn('ping -n 1 ' + str(host))
    output = session.read(2000)
    output = output.decode('utf-8')
    if "100% loss" in output or "timed out" in output or "unreachable" in output:
        return None
    else:
        return str(host)
Exemplo n.º 15
0
 def test_unexpected_eof(self):
     p = PopenSpawn('ls -l /bin')
     try:
         p.expect('_Z_XY_XZ')  # Probably never see this in ls output.
     except pexpect.EOF:
         pass
     else:
         self.fail('Expected an EOF exception.')
Exemplo n.º 16
0
 def test_unexpected_eof(self):
     p = PopenSpawn("ls -l /bin")
     try:
         p.expect("_Z_XY_XZ")  # Probably never see this in ls output.
     except pexpect.EOF:
         pass
     else:
         self.fail("Expected an EOF exception.")
Exemplo n.º 17
0
 def test_unexpected_eof(self):
     p = PopenSpawn('ls -l /bin')
     try:
         p.expect('_Z_XY_XZ')  # Probably never see this in ls output.
     except pexpect.EOF:
         pass
     else:
         self.fail('Expected an EOF exception.')
Exemplo n.º 18
0
def spawn(command,
          args=[],
          timeout=30,
          maxread=2000,
          searchwindowsize=None,
          logfile=None,
          cwd=None,
          env=None,
          ignore_sighup=True,
          echo=True,
          encoding='utf-8',
          **kwargs):
    '''This is the main entry point for Pexpect. Use this functio to start
    and control child applications.

    See https://github.com/pexpect/pexpect/blob/master/pexpect/pty_spawn.py
    for more information.
    '''
    codec_errors = kwargs.get('codec_errors', kwargs.get('errors', 'strict'))
    if pty is None:
        command = shlex.split(command, posix=os.name == 'posix')
        command += args
        child = PopenSpawn(command,
                           timeout=timeout,
                           maxread=maxread,
                           searchwindowsize=searchwindowsize,
                           logfile=logfile,
                           cwd=cwd,
                           env=env,
                           encoding=encoding,
                           codec_errors=codec_errors)
        child.echo = echo
    else:
        try:
            # Signal handlers are inherited by forked processes, and we can't easily
            # reset it from the subprocess. Since kernelapp ignores SIGINT except in
            # message handlers, we need to temporarily reset the SIGINT handler here
            # so that the child and its children are interruptible.
            try:
                sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
            except ValueError:
                # Only Main Thread can handle signals
                sig = None
            child = pty_spawn(command,
                              args=args,
                              timeout=timeout,
                              maxread=maxread,
                              searchwindowsize=searchwindowsize,
                              logfile=logfile,
                              cwd=cwd,
                              env=env,
                              encoding=encoding,
                              codec_errors=codec_errors)
        finally:
            if sig:
                signal.signal(signal.SIGINT, sig)
    return child
Exemplo n.º 19
0
    def run_file(self, q):
        outputs = []

        for test in [str(t) for t in q.test_cases]:
            p = PopenSpawn(f"python {self.get_path(q.file)}")
            p.sendline(test)
            outputs.append(str(p.read().decode('utf-8')))

        return outputs
Exemplo n.º 20
0
    def start(self,user,password):
#523680852782251507
        try:
            #self.pianobarInstance = pexpect.spawn ('pianobar')
            self.pianobarInstance = PopenSpawn ('cmd')
            #self.pianobarInstance.expect('#   -\d\d:\d\d/\d\d:\d\d')
            self.started = True
        except Exception as e:
            print (e.strerror)
Exemplo n.º 21
0
 def test_bad_arg(self):
     p = PopenSpawn("cat")
     with self.assertRaisesRegexp(TypeError, ".*must be one of"):
         p.expect(1)
     with self.assertRaisesRegexp(TypeError, ".*must be one of"):
         p.expect([1, b"2"])
     with self.assertRaisesRegexp(TypeError, ".*must be one of"):
         p.expect_exact(1)
     with self.assertRaisesRegexp(TypeError, ".*must be one of"):
         p.expect_exact([1, b"2"])
Exemplo n.º 22
0
 def test_expect_eof(self):
     the_old_way = subprocess.Popen(
         args=['ls', '-l',
               '/bin'], stdout=subprocess.PIPE).communicate()[0].rstrip()
     p = PopenSpawn('ls -l /bin')
     # This basically tells it to read everything. Same as pexpect.run()
     # function.
     p.expect(pexpect.EOF)
     the_new_way = p.before.rstrip()
     assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)
Exemplo n.º 23
0
 def test_bad_arg(self):
     p = PopenSpawn('cat')
     with self.assertRaisesRegexp(TypeError, '.*must be one of'):
         p.expect(1)
     with self.assertRaisesRegexp(TypeError, '.*must be one of'):
         p.expect([1, b'2'])
     with self.assertRaisesRegexp(TypeError, '.*must be one of'):
         p.expect_exact(1)
     with self.assertRaisesRegexp(TypeError, '.*must be one of'):
         p.expect_exact([1, b'2'])
Exemplo n.º 24
0
 def __init__(self, times=5):
     # 执行进程
     self.process = PopenSpawn('cmd', timeout=10)
     # 日志
     self.logFile = open("logfile.txt", 'wb')
     self.process.logfile = self.logFile
     # 功能序列
     self.d = self.reverse_dict()
     self.seq = ('login', 'register')
     # 测试次数
     self.times = times
Exemplo n.º 25
0
 def test_expect(self):
     the_old_way = subprocess.Popen(args=["ls", "-l", "/bin"], stdout=subprocess.PIPE).communicate()[0].rstrip()
     p = PopenSpawn("ls -l /bin")
     the_new_way = b""
     while 1:
         i = p.expect([b"\n", pexpect.EOF])
         the_new_way = the_new_way + p.before
         if i == 1:
             break
         the_new_way += b"\n"
     the_new_way = the_new_way.rstrip()
     assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)
Exemplo n.º 26
0
	def __init__(self,name,mmgen_cmd,cmd_args,desc,no_output=False,passthru_args=[],msg_only=False):
		cmd_args = ['--{}{}'.format(k.replace('_','-'),
			'='+getattr(opt,k) if getattr(opt,k) != True else ''
			) for k in passthru_args if getattr(opt,k)] \
			+ ['--data-dir='+os.path.join('test','data_dir')] + cmd_args

		if g.platform == 'win': cmd,args = 'python',[mmgen_cmd]+cmd_args
		else:                   cmd,args = mmgen_cmd,cmd_args

		for i in args:
			if type(i) not in (str,unicode):
				m1 = 'Error: missing input files in cmd line?:'
				m2 = '\nName: {}\nCmd: {}\nCmd args: {}'
				die(2,(m1+m2).format(name,cmd,args))

		if opt.popen_spawn:
			args = [(a,"'{}'".format(a))[' ' in a] for a in args]

		cmd_str = '{} {}'.format(cmd,' '.join(args)).replace('\\','/')

		if opt.log:
			log_fd.write(cmd_str+'\n')

		if opt.verbose or opt.print_cmdline or opt.exact_output:
			clr1,clr2,eol = ((green,cyan,'\n'),(nocolor,nocolor,' '))[bool(opt.print_cmdline)]
			sys.stderr.write(green('Testing: {}\n'.format(desc)))
			if not msg_only:
				sys.stderr.write(clr1('Executing {}{}'.format(clr2(cmd_str),eol)))
		else:
			m = 'Testing %s: ' % desc
			msg_r(m)

		if msg_only: return

		if opt.direct_exec:
			msg('')
			from subprocess import call,check_output
			f = (call,check_output)[bool(no_output)]
			ret = f([cmd] + args)
			if f == call and ret != 0:
				m = 'ERROR: process returned a non-zero exit status (%s)'
				die(1,red(m % ret))
		else:
			if opt.traceback:
				cmd,args = g.traceback_cmd,[cmd]+args
				cmd_str = g.traceback_cmd + ' ' + cmd_str
#			Msg('\ncmd_str: {}'.format(cmd_str))
			if opt.popen_spawn:
				self.p = PopenSpawn(cmd_str)
			else:
				self.p = pexpect.spawn(cmd,args)
			if opt.exact_output: self.p.logfile = sys.stdout
Exemplo n.º 27
0
 def test_expect(self):
     the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'],
                                    stdout=subprocess.PIPE).communicate()[0].rstrip()
     p = PopenSpawn('ls -l /bin')
     the_new_way = b''
     while 1:
         i = p.expect([b'\n', pexpect.EOF])
         the_new_way = the_new_way + p.before
         if i == 1:
             break
         the_new_way += b'\n'
     the_new_way = the_new_way.rstrip()
     assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)
Exemplo n.º 28
0
def something():
    child = PopenSpawn('ssh [email protected]')  # powershell
    print child.before, child.after
    i = child.expect('password')
    if i == 0:  # Timeout
        print 'ERROR!'
        print 'SSH could not login. Here is what SSH said:'
        print child.before, child.after
        return None
    if i == 1:
        child.sendline('Huqiang2018')
        print child.before.decode('gbk')
    return child
Exemplo n.º 29
0
def main():
  parser = GooeyParser(description='Package your Gooey applications into standalone executables')
  parser.add_argument(
    "program_name",
    metavar='Program Name',
    help='Destination name for the packaged executable'
  )
  parser.add_argument(
    "source_path",
    metavar="Program Source",
    help='The main source file of your program',
    widget="FileChooser"
  )

  parser.add_argument(
    "output_dir",
    metavar="Output Directory",
    help='Location to store the generated files',
    widget="DirChooser"
  )

  args = parser.parse_args()

  if not os.path.exists(args.source_path):
    raise IOError('{} does not appear to be a valid file path'.format(args.source_path))

  if not os.path.exists(args.output_dir):
    raise IOError('{} does not appear to be a valid directory'.format(args.output_dir))

  with open(os.path.join(local_path(), 'build_template'), 'r') as f:
    spec_details = f.read().format(program_name=args.program_name, source_path=args.source_path)

  fileno, path = tempfile.mkstemp(prefix='gooeybuild', suffix='.spec')
  with open(path, 'w') as f:
    f.write(spec_details)

  cmd = 'pyinstaller "{0}" --distpath="{1}"'.format(path, args.output_dir)
  print cmd
  from pexpect.popen_spawn import PopenSpawn
  child = PopenSpawn(cmd)
  child.logfile = sys.stdout
  child.wait()
  print dedent('''
  ___  _ _  ______                 _
 / _ \| | | |  _  \               | |
/ /_\ \ | | | | | |___  _ __   ___| |
|  _  | | | | | | / _ \| '_ \ / _ \ |
| | | | | | | |/ / (_) | | | |  __/_|
\_| |_/_|_| |___/ \___/|_| |_|\___(_)
  ''')
  print 'Wrote Executable file to {}'.format(args.output_dir)
Exemplo n.º 30
0
 def test_bad_arg(self):
     p = PopenSpawn('cat')
     with self.assertRaisesRegexp(TypeError, '.*must be one of'):
         p.expect(1)
     with self.assertRaisesRegexp(TypeError, '.*must be one of'):
         p.expect([1, b'2'])
     with self.assertRaisesRegexp(TypeError, '.*must be one of'):
         p.expect_exact(1)
     with self.assertRaisesRegexp(TypeError, '.*must be one of'):
         p.expect_exact([1, b'2'])
Exemplo n.º 31
0
 def test_bad_arg(self):
     p = PopenSpawn("cat")
     with self.assertRaisesRegexp(TypeError, ".*must be one of"):
         p.expect(1)
     with self.assertRaisesRegexp(TypeError, ".*must be one of"):
         p.expect([1, b"2"])
     with self.assertRaisesRegexp(TypeError, ".*must be one of"):
         p.expect_exact(1)
     with self.assertRaisesRegexp(TypeError, ".*must be one of"):
         p.expect_exact([1, b"2"])
Exemplo n.º 32
0
 def open(self):
     self.process = PopenSpawn(
         f'{sys.executable} -m pexpect_serial_terminal -p {self.port}')
     self.process.logfile_read = open("serial_log.txt", "ab+")
     self.process.sendline('')
     i = self.process.expect([r'.+ login:'******'ubuntu')
         self.process.expect('Password:'******'ubuntu')
         self.process.expect(self.prompt)
     elif i == 2:
         self.close()
         raise ValueError('cannot log in serial device!')
Exemplo n.º 33
0
 def test_expect(self):
     the_old_way = (subprocess.Popen(
         args=["ls", "-l",
               "/bin"], stdout=subprocess.PIPE).communicate()[0].rstrip())
     p = PopenSpawn("ls -l /bin")
     the_new_way = b""
     while 1:
         i = p.expect([b"\n", pexpect.EOF])
         the_new_way = the_new_way + p.before
         if i == 1:
             break
         the_new_way += b"\n"
     the_new_way = the_new_way.rstrip()
     assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)
Exemplo n.º 34
0
 def test_expect(self):
     the_old_way = subprocess.Popen(
         args=['ls', '-l',
               '/bin'], stdout=subprocess.PIPE).communicate()[0].rstrip()
     p = PopenSpawn('ls -l /bin')
     the_new_way = b''
     while 1:
         i = p.expect([b'\n', pexpect.EOF])
         the_new_way = the_new_way + p.before
         if i == 1:
             break
         the_new_way += b'\n'
     the_new_way = the_new_way.rstrip()
     assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)
Exemplo n.º 35
0
def getARPTable():
    cmd = "arp -a"
    session = PopenSpawn(cmd)
    output = session.read(20000)
    output = output.decode('utf-8')
    lines = output.splitlines()
    macjson = {}
    for line in lines:
        if "dynamic" in line:
            splited = line.split()
            mac = splited[1].strip().replace("-", "")
            mac = mac[0:4] + "." + mac[4:8] + "." + mac[8:12]
            macjson.update({splited[0].strip(): mac})
    return macjson
Exemplo n.º 36
0
    def __init__(self, name, mmgen_cmd, cmd_args, desc, no_output=False):

        cmd_args = self.add_spawn_args + cmd_args
        cmd = (('./', '')[bool(opt.system)] + mmgen_cmd,
               'python')[g.platform == 'win']
        args = (cmd_args, [mmgen_cmd] + cmd_args)[g.platform == 'win']

        for i in args:
            if type(i) not in (str, unicode):
                m1 = 'Error: missing input files in cmd line?:'
                m2 = '\nName: {}\nCmd: {}\nCmd args: {}'
                die(2, (m1 + m2).format(name, cmd, args))
        if opt.popen_spawn:
            args = [("'" + a + "'" if ' ' in a else a) for a in args]
        cmd_str = '{} {}'.format(cmd, ' '.join(args))
        if opt.popen_spawn:
            cmd_str = cmd_str.replace('\\', '/')

        if opt.log:
            log_fd.write(cmd_str + '\n')
        if opt.verbose or opt.print_cmdline or opt.exact_output:
            clr1, clr2, eol = ((green, cyan, '\n'),
                               (nocolor, nocolor,
                                ' '))[bool(opt.print_cmdline)]
            sys.stderr.write(green('Testing: {}\n'.format(desc)))
            sys.stderr.write(clr1('Executing {}{}'.format(clr2(cmd_str), eol)))
        else:
            m = 'Testing %s: ' % desc
            msg_r(m)

        if mmgen_cmd == '': return

        if opt.direct_exec:
            msg('')
            from subprocess import call, check_output
            f = (call, check_output)[bool(no_output)]
            ret = f([cmd] + args)
            if f == call and ret != 0:
                m = 'ERROR: process returned a non-zero exit status (%s)'
                die(1, red(m % ret))
        else:
            if opt.traceback:
                cmd, args = g.traceback_cmd, [cmd] + args
                cmd_str = g.traceback_cmd + ' ' + cmd_str
            if opt.popen_spawn:
                self.p = PopenSpawn(cmd_str)
            else:
                self.p = pexpect.spawn(cmd, args)
            if opt.exact_output: self.p.logfile = sys.stdout
Exemplo n.º 37
0
    def run(self, block=True, binary=False, cwd=None, env=None):
        """Runs the given command, with or without pexpect functionality enabled."""
        self.blocking = block

        # Use subprocess. 如果使用blocking 就是用subprocess
        if self.blocking:
            popen_kwargs = self._default_popen_kwargs.copy()
            popen_kwargs['universal_newlines'] = not binary
            if cwd:
                popen_kwargs['cwd'] = cwd
            if env:
                popen_kwargs['env'].update(env)
            s = subprocess.Popen(self._popen_args, **popen_kwargs)
        # Otherwise, use pexpect. # 否则使用pexpect
        else:
            pexpect_kwargs = self._default_pexpect_kwargs.copy()
            if binary:
                pexpect_kwargs['encoding'] = None
            if cwd:
                pexpect_kwargs['cwd'] = cwd
            if env:
                pexpect_kwargs['env'].update(env)
            # Enable Python subprocesses to work with expect functionality.
            pexpect_kwargs['env']['PYTHONUNBUFFERED'] = '1'
            s = PopenSpawn(self._popen_args, **pexpect_kwargs)
        self.subprocess = s
        self.was_run = True
Exemplo n.º 38
0
 def __init__(self, engine=''):
     if not engine:
         engine = './scoutfish'
     self.p = PopenSpawn(engine, encoding="utf-8")
     self.wait_ready()
     self.pgn = ''
     self.db = ''
Exemplo n.º 39
0
    def run(self, block=True, binary=False, cwd=None, env=None, shell=True):
        """Runs the given command, with or without pexpect functionality enabled."""
        self.blocking = block

        # Use subprocess.
        if self.blocking:
            popen_kwargs = self._default_popen_kwargs.copy()
            del popen_kwargs["stdin"]
            popen_kwargs["universal_newlines"] = not binary
            if cwd:
                popen_kwargs["cwd"] = cwd
            if env:
                popen_kwargs["env"].update(env)
            if not shell:
                popen_kwargs["shell"] = False
            s = subprocess.Popen(self._popen_args, **popen_kwargs)
        # Otherwise, use pexpect.
        else:
            pexpect_kwargs = self._default_pexpect_kwargs.copy()
            if binary:
                pexpect_kwargs["encoding"] = None
            if cwd:
                pexpect_kwargs["cwd"] = cwd
            if env:
                pexpect_kwargs["env"].update(env)
            # Enable Python subprocesses to work with expect functionality.
            pexpect_kwargs["env"]["PYTHONUNBUFFERED"] = "1"
            s = PopenSpawn(self._popen_args, **pexpect_kwargs)
        self.subprocess = s
        self.was_run = True
Exemplo n.º 40
0
    def test_expect_exact(self):
        the_old_way = subprocess.Popen(args=["ls", "-l", "/bin"], stdout=subprocess.PIPE).communicate()[0].rstrip()
        p = PopenSpawn("ls -l /bin")
        the_new_way = b""
        while 1:
            i = p.expect_exact([b"\n", pexpect.EOF])
            the_new_way = the_new_way + p.before
            if i == 1:
                break
            the_new_way += b"\n"
        the_new_way = the_new_way.rstrip()

        assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)
        p = PopenSpawn("echo hello.?world")
        i = p.expect_exact(b".?")
        self.assertEqual(p.before, b"hello")
        self.assertEqual(p.after, b".?")
Exemplo n.º 41
0
    def test_expect_exact(self):
        the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'],
                                       stdout=subprocess.PIPE).communicate()[0].rstrip()
        p = PopenSpawn('ls -l /bin')
        the_new_way = b''
        while 1:
            i = p.expect_exact([b'\n', pexpect.EOF])
            the_new_way = the_new_way + p.before
            if i == 1:
                break
            the_new_way += b'\n'
        the_new_way = the_new_way.rstrip()

        assert the_old_way == the_new_way, len(the_old_way) - len(the_new_way)
        p = PopenSpawn('echo hello.?world')
        i = p.expect_exact(b'.?')
        self.assertEqual(p.before, b'hello')
        self.assertEqual(p.after, b'.?')
Exemplo n.º 42
0
def spawn(command, args=[], timeout=30, maxread=2000,
          searchwindowsize=None, logfile=None, cwd=None, env=None,
          ignore_sighup=True, echo=True, encoding='utf-8', **kwargs):
    '''This is the main entry point for Pexpect. Use this functio to start
    and control child applications.

    See https://github.com/pexpect/pexpect/blob/master/pexpect/pty_spawn.py
    for more information.
    '''
    codec_errors = kwargs.get('codec_errors', kwargs.get('errors', 'strict'))
    if pty is None:
        command = shlex.split(command, posix=os.name == 'posix')
        command += args
        child = PopenSpawn(command, timeout=timeout, maxread=maxread,
                           searchwindowsize=searchwindowsize,
                           logfile=logfile, cwd=cwd, env=env,
                           encoding=encoding, codec_errors=codec_errors)
        child.echo = echo
    else:
        try:
            # Signal handlers are inherited by forked processes, and we can't easily
            # reset it from the subprocess. Since kernelapp ignores SIGINT except in
            # message handlers, we need to temporarily reset the SIGINT handler here
            # so that the child and its children are interruptible.
            try:
                sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
            except ValueError:
                # Only Main Thread can handle signals
                sig = None
            child = pty_spawn(command, args=args, timeout=timeout,
                              maxread=maxread,
                              searchwindowsize=searchwindowsize,
                              logfile=logfile, cwd=cwd, env=env,
                              encoding=encoding, codec_errors=codec_errors)
        finally:
            if sig:
                signal.signal(signal.SIGINT, sig)
    return child
Exemplo n.º 43
0
	def __init__(self,args,no_output=False):

		if opt.direct_exec:
			msg('')
			from subprocess import call,check_output
			f = (call,check_output)[bool(no_output)]
			ret = f([args[0]] + args[1:])
			if f == call and ret != 0:
				die(1,red('ERROR: process returned a non-zero exit status ({})'.format(ret)))
		else:
			if opt.pexpect_spawn:
				self.p = pexpect.spawn(args[0],args[1:],encoding='utf8')
				self.p.delaybeforesend = 0
			else:
				self.p = PopenSpawn(args,encoding='utf8')
#				self.p.delaybeforesend = 0 # TODO: try this here too

			if opt.exact_output: self.p.logfile = sys.stdout

		self.req_exit_val = 0
		self.skip_ok = False
		self.timeout = int(opt.pexpect_timeout or 0) or (60,5)[bool(opt.debug_pexpect)]
		self.sent_value = None
Exemplo n.º 44
0
 def test_crlf_encoding(self):
     p = PopenSpawn('echo alpha beta', encoding='utf-8')
     assert p.read() == 'alpha beta' + p.crlf
Exemplo n.º 45
0
class MMGenPexpect(object):

	NL = '\r\n'
	if g.platform == 'linux' and opt.popen_spawn:
		import atexit
		atexit.register(lambda: os.system('stty sane'))
		NL = '\n'

	def __init__(self,name,mmgen_cmd,cmd_args,desc,no_output=False,passthru_args=[],msg_only=False):
		cmd_args = ['--{}{}'.format(k.replace('_','-'),
			'='+getattr(opt,k) if getattr(opt,k) != True else ''
			) for k in passthru_args if getattr(opt,k)] \
			+ ['--data-dir='+os.path.join('test','data_dir')] + cmd_args

		if g.platform == 'win': cmd,args = 'python',[mmgen_cmd]+cmd_args
		else:                   cmd,args = mmgen_cmd,cmd_args

		for i in args:
			if type(i) not in (str,unicode):
				m1 = 'Error: missing input files in cmd line?:'
				m2 = '\nName: {}\nCmd: {}\nCmd args: {}'
				die(2,(m1+m2).format(name,cmd,args))

		if opt.popen_spawn:
			args = [(a,"'{}'".format(a))[' ' in a] for a in args]

		cmd_str = '{} {}'.format(cmd,' '.join(args)).replace('\\','/')

		if opt.log:
			log_fd.write(cmd_str+'\n')

		if opt.verbose or opt.print_cmdline or opt.exact_output:
			clr1,clr2,eol = ((green,cyan,'\n'),(nocolor,nocolor,' '))[bool(opt.print_cmdline)]
			sys.stderr.write(green('Testing: {}\n'.format(desc)))
			if not msg_only:
				sys.stderr.write(clr1('Executing {}{}'.format(clr2(cmd_str),eol)))
		else:
			m = 'Testing %s: ' % desc
			msg_r(m)

		if msg_only: return

		if opt.direct_exec:
			msg('')
			from subprocess import call,check_output
			f = (call,check_output)[bool(no_output)]
			ret = f([cmd] + args)
			if f == call and ret != 0:
				m = 'ERROR: process returned a non-zero exit status (%s)'
				die(1,red(m % ret))
		else:
			if opt.traceback:
				cmd,args = g.traceback_cmd,[cmd]+args
				cmd_str = g.traceback_cmd + ' ' + cmd_str
#			Msg('\ncmd_str: {}'.format(cmd_str))
			if opt.popen_spawn:
				self.p = PopenSpawn(cmd_str)
			else:
				self.p = pexpect.spawn(cmd,args)
			if opt.exact_output: self.p.logfile = sys.stdout

	def ok(self,exit_val=0):
		ret = self.p.wait()
#		Msg('expect: {} got: {}'.format(exit_val,ret))
		if ret != exit_val:
			die(1,red('test.py: spawned program exited with value {}'.format(ret)))
		if opt.profile: return
		if opt.verbose or opt.exact_output:
			sys.stderr.write(green('OK\n'))
		else: msg(' OK')

	def cmp_or_die(self,s,t,skip_ok=False,exit_val=0):
		ret = self.p.wait()
		if ret != exit_val:
			die(1,red('test.py: spawned program exited with value {}'.format(ret)))
		if s == t:
			if not skip_ok: ok()
		else:
			sys.stderr.write(red(
				'ERROR: recoded data:\n%s\ndiffers from original data:\n%s\n' %
					(repr(t),repr(s))))
			sys.exit(3)

	def license(self):
		if 'MMGEN_NO_LICENSE' in os.environ: return
		p = "'w' for conditions and warranty info, or 'c' to continue: "
		my_expect(self.p,p,'c')

	def label(self,label='Test Label'):
		p = 'Enter a wallet label, or hit ENTER for no label: '
		my_expect(self.p,p,label+'\n')

	def usr_rand_out(self,saved=False):
		m = '%suser-supplied entropy' % (('','saved ')[saved])
		my_expect(self.p,'Generating encryption key from OS random data plus ' + m)

	def usr_rand(self,num_chars):
		if opt.usr_random:
			self.interactive()
			my_send(self.p,'\n')
		else:
			rand_chars = list(getrandstr(num_chars,no_space=True))
			my_expect(self.p,'symbols left: ','x')
			try:
				vmsg_r('SEND ')
				while self.p.expect('left: ',0.1) == 0:
					ch = rand_chars.pop(0)
					msg_r(yellow(ch)+' ' if opt.verbose else '+')
					self.p.send(ch)
			except:
				vmsg('EOT')
			my_expect(self.p,'ENTER to continue: ','\n')

	def passphrase_new(self,desc,passphrase):
		my_expect(self.p,('Enter passphrase for %s: ' % desc), passphrase+'\n')
		my_expect(self.p,'Repeat passphrase: ', passphrase+'\n')

	def passphrase(self,desc,passphrase,pwtype=''):
		if pwtype: pwtype += ' '
		my_expect(self.p,('Enter %spassphrase for %s.*?: ' % (pwtype,desc)),
				passphrase+'\n',regex=True)

	def hash_preset(self,desc,preset=''):
		my_expect(self.p,('Enter hash preset for %s' % desc))
		my_expect(self.p,('or hit ENTER .*?:'), str(preset)+'\n',regex=True)

	def written_to_file(self,desc,overwrite_unlikely=False,query='Overwrite?  ',oo=False):
		s1 = '%s written to file ' % desc
		s2 = query + "Type uppercase 'YES' to confirm: "
		ret = my_expect(self.p,([s1,s2],s1)[overwrite_unlikely])
		if ret == 1:
			my_send(self.p,'YES\n')
#			if oo:
			outfile = self.expect_getend("Overwriting file '").rstrip("'")
			return outfile
# 			else:
# 				ret = my_expect(self.p,s1)
		self.expect(self.NL,nonl=True)
		outfile = self.p.before.strip().strip("'")
		if opt.debug_pexpect: msgred('Outfile [%s]' % outfile)
		vmsg('%s file: %s' % (desc,cyan(outfile.replace("'",''))))
		return outfile

	def no_overwrite(self):
		self.expect("Overwrite?  Type uppercase 'YES' to confirm: ",'\n')
		self.expect('Exiting at user request')

	def tx_view(self):
		my_expect(self.p,r'View .*?transaction.*? \(y\)es, \(N\)o, pager \(v\)iew.*?: ','\n',regex=True)

	def expect_getend(self,s,regex=False):
		ret = self.expect(s,regex=regex,nonl=True)
		debug_pexpect_msg(self.p)
#		end = self.readline().strip()
		# readline() of partial lines doesn't work with PopenSpawn, so do this instead:
		self.expect(self.NL,nonl=True,silent=True)
		debug_pexpect_msg(self.p)
		end = self.p.before
		vmsg(' ==> %s' % cyan(end))
		return end

	def interactive(self):
		return self.p.interact() # interact() not available with popen_spawn

	def kill(self,signal):
		return self.p.kill(signal)

	def logfile(self,arg):
		self.p.logfile = arg

	def expect(self,*args,**kwargs):
		return my_expect(self.p,*args,**kwargs)

	def send(self,*args,**kwargs):
		return my_send(self.p,*args,**kwargs)

# 	def readline(self):
# 		return self.p.readline()
# 	def readlines(self):
# 		return [l.rstrip()+'\n' for l in self.p.readlines()]

	def read(self,n=None):
		return self.p.read(n)

	def close(self):
		if not opt.popen_spawn:
			self.p.close()
Exemplo n.º 46
0
class Scoutfish:
    def __init__(self, engine=''):
        if not engine:
            engine = './scoutfish'
        self.p = PopenSpawn(engine, encoding="utf-8")
        self.wait_ready()
        self.pgn = ''
        self.db = ''

    def wait_ready(self):
        self.p.sendline('isready')
        self.p.expect(u'readyok')

    def open(self, pgn):
        '''Open a PGN file and create an index if not exsisting'''
        if not os.path.isfile(pgn):
            raise NameError("File {} does not exsist".format(pgn))
        pgn = os.path.normcase(pgn)
        self.pgn = pgn
        self.db = os.path.splitext(pgn)[0] + '.scout'
        if not os.path.isfile(self.db):
            result = self.make()
            self.db = result['DB file']

    def close(self):
        '''Terminate scoutfish. Not really needed: engine will terminate as
           soon as pipe is closed, i.e. when we exit.'''
        self.p.sendline('quit')
        self.p.expect(pexpect.EOF)
        self.pgn = ''
        self.db = ''

    def make(self):
        '''Make an index out of a pgn file. Normally called by open()'''
        if not self.pgn:
            raise NameError("Unknown DB, first open a PGN file")
        cmd = 'make ' + self.pgn
        self.p.sendline(cmd)
        self.wait_ready()
        s = '{' + self.p.before.split('{')[1]
        s = s.replace('\\', r'\\')  # Escape Windows's path delimiter
        result = json.loads(s)
        self.p.before = ''
        return result

    def setoption(self, name, value):
        '''Set an option value, like threads number'''
        cmd = "setoption name {} value {}".format(name, value)
        self.p.sendline(cmd)
        self.wait_ready()

    def scout(self, q):
        '''Run query defined by 'q' dict. Result will be a dict too'''
        if not self.db:
            raise NameError("Unknown DB, first open a PGN file")
        j = json.dumps(q)
        cmd = "scout {} {}".format(self.db, j)
        self.p.sendline(cmd)
        self.wait_ready()
        result = json.loads(self.p.before)
        self.p.before = ''
        return result

    def scout_raw(self, q):
        '''Run query defined by 'q' dict. Result will be full output'''
        if not self.db:
            raise NameError("Unknown DB, first open a PGN file")
        j = json.dumps(q)
        cmd = "scout {} {}".format(self.db, j)
        self.p.sendline(cmd)
        self.wait_ready()
        result = self.p.before
        self.p.before = ''
        return result

    def get_games(self, matches):
        '''Retrieve the PGN games specified in the offset list. Games are
           added to each list item with a 'pgn' key'''
        if not self.pgn:
            raise NameError("Unknown DB, first open a PGN file")
        with open(self.pgn, "rU") as f:
            for match in matches:
                f.seek(match['ofs'])
                game = ''
                for line in f:
                    if line.startswith('[Event "'):
                        if game:
                            break  # Second one, start of next game
                        else:
                            game = line  # First occurence
                    elif game:
                        game += line
                match['pgn'] = game.strip()
        return matches

    def get_header(self, pgn):
        '''Return a dict with just header information out of a pgn game. The
           pgn tags are supposed to be consecutive'''
        header = {}
        for line in pgn.splitlines():
            line = line.strip()
            if line.startswith('[') and line.endswith(']'):
                tag_match = PGN_HEADERS_REGEX.match(line)
                if tag_match:
                    header[tag_match.group(1)] = tag_match.group(2)
            else:
                break
        return header

    def get_game_headers(self, matches):
        '''Return a list of headers out of a list of pgn games. It is defined
           to be compatible with the return value of get_games()'''
        headers = []
        for match in matches:
            pgn = match['pgn']
            h = self.get_header(pgn)
            headers.append(h)
        return headers
Exemplo n.º 47
0
 def test_expect_exact_basic(self):
     p = PopenSpawn("cat", timeout=5)
     p.sendline(b"Hello")
     p.sendline(b"there")
     p.sendline(b"Mr. Python")
     p.expect_exact(b"Hello")
     p.expect_exact(b"there")
     p.expect_exact(b"Mr. Python")
     p.sendeof()
     p.expect_exact(pexpect.EOF)
Exemplo n.º 48
0
 def test_timeout_none(self):
     p = PopenSpawn("echo abcdef", timeout=None)
     p.expect("abc")
     p.expect_exact("def")
     p.expect(pexpect.EOF)
Exemplo n.º 49
0
 def __init__(self, engine=''):
     if not engine:
         engine = './parser'
     self.p = PopenSpawn(engine, encoding="utf-8")
     self.pgn = ''
     self.db = ''
Exemplo n.º 50
0
class Parser:
    def __init__(self, engine=''):
        if not engine:
            engine = './parser'
        self.p = PopenSpawn(engine, encoding="utf-8")
        self.pgn = ''
        self.db = ''

    def wait_ready(self):
        self.p.sendline('isready')
        self.p.expect(u'readyok')

    def open(self, pgn, full=True):
        '''Open a PGN file and create an index if not exsisting'''
        if not os.path.isfile(pgn):
            raise NameError("File {} does not exsist".format(pgn))
        pgn = os.path.normcase(pgn)
        self.pgn = pgn
        self.db = os.path.splitext(pgn)[0] + '.bin'
        if not os.path.isfile(self.db):
            result = self.make(full)
            self.db = result['Book file']

    def close(self):
        '''Terminate chess_db. Not really needed: engine will terminate as
           soon as pipe is closed, i.e. when we exit.'''
        self.p.sendline('quit')
        self.p.expect(pexpect.EOF)
        self.pgn = ''
        self.db = ''

    def make(self, full=True):
        '''Make an index out of a pgn file'''
        if not self.pgn:
            raise NameError("Unknown DB, first open a PGN file")
        cmd = 'book ' + self.pgn
        if full:
            cmd += ' full'
        self.p.sendline(cmd)
        self.wait_ready()
        s = '{' + self.p.before.split('{')[1]
        s = s.replace('\\', r'\\')  # Escape Windows's path delimiter
        result = json.loads(s)
        self.p.before = ''
        return result

    def find(self, fen, limit=10, skip=0):
        '''Find all games with positions equal to fen'''
        if not self.db:
            raise NameError("Unknown DB, first open a PGN file")
        cmd = "find {} limit {} skip {} {}".format(self.db, limit, skip, fen)
        self.p.sendline(cmd)
        self.wait_ready()
        result = json.loads(self.p.before)
        self.p.before = ''
        return result

    def get_games(self, list):
        '''Retrieve the PGN games specified in the offset list'''
        if not self.pgn:
            raise NameError("Unknown DB, first open a PGN file")
        pgn = []
        with open(self.pgn, "r") as f:
            for ofs in list:
                f.seek(ofs)
                game = ''
                for line in f:
                    if line.startswith('[Event "'):
                        if game:
                            break  # Second one, start of next game
                        else:
                            game = line  # First occurence
                    elif game:
                        game += line
                pgn.append(game.strip())
        return pgn

    def get_header(self, pgn):
        '''Return a dict with just header information out of a pgn game. The
           pgn tags are supposed to be consecutive'''
        header = {}
        for line in pgn.splitlines():
            line = line.strip()
            if line.startswith('[') and line.endswith(']'):
                tag_match = PGN_HEADERS_REGEX.match(line)
                if tag_match:
                    header[tag_match.group(1)] = tag_match.group(2)
                else:
                    break
        return header

    def get_game_headers(self, list):
        '''Return a list of headers out of a list of pgn games'''
        headers = []
        for pgn in list:
            h = self.get_header(pgn)
            headers.append(h)
        return headers
Exemplo n.º 51
0
 def test_timeout_none(self):
     p = PopenSpawn('echo abcdef', timeout=None)
     p.expect('abc')
     p.expect_exact('def')
     p.expect(pexpect.EOF)
Exemplo n.º 52
0
 def test_expect_timeout(self):
     p = PopenSpawn('cat', timeout=5)
     p.expect(pexpect.TIMEOUT)  # This tells it to wait for timeout.
     self.assertEqual(p.after, pexpect.TIMEOUT)
Exemplo n.º 53
0
 def test_crlf(self):
     p = PopenSpawn('echo alpha beta')
     assert p.read() == b'alpha beta' + p.crlf
Exemplo n.º 54
0
class MMGenPexpect(object):

	def __init__(self,args,no_output=False):

		if opt.direct_exec:
			msg('')
			from subprocess import call,check_output
			f = (call,check_output)[bool(no_output)]
			ret = f([args[0]] + args[1:])
			if f == call and ret != 0:
				die(1,red('ERROR: process returned a non-zero exit status ({})'.format(ret)))
		else:
			if opt.pexpect_spawn:
				self.p = pexpect.spawn(args[0],args[1:],encoding='utf8')
				self.p.delaybeforesend = 0
			else:
				self.p = PopenSpawn(args,encoding='utf8')
#				self.p.delaybeforesend = 0 # TODO: try this here too

			if opt.exact_output: self.p.logfile = sys.stdout

		self.req_exit_val = 0
		self.skip_ok = False
		self.timeout = int(opt.pexpect_timeout or 0) or (60,5)[bool(opt.debug_pexpect)]
		self.sent_value = None

	def do_decrypt_ka_data(self,hp,pw,desc='key-address data',check=True,have_yes_opt=False):
#		self.hash_preset(desc,hp)
		self.passphrase(desc,pw)
		if not have_yes_opt:
			self.expect('Check key-to-address validity? (y/N): ',('n','y')[check])

	def view_tx(self,view):
		self.expect('View.* transaction.*\? .*: ',view,regex=True)
		if view not in 'n\n':
			self.expect('to continue: ','\n')

	def do_comment(self,add_comment,has_label=False):
		p = ('Add a comment to transaction','Edit transaction comment')[has_label]
		self.expect('{}? (y/N): '.format(p),('n','y')[bool(add_comment)])
		if add_comment:
			self.expect('Comment: ',add_comment+'\n')

	def ok(self):
		ret = self.p.wait()
		if ret != self.req_exit_val and not opt.coverage:
			die(1,red('test.py: spawned program exited with value {}'.format(ret)))
		if opt.profile: return
		if not self.skip_ok:
			sys.stderr.write(green('OK\n') if opt.exact_output or opt.verbose else (' OK\n'))
		return self

	def license(self):
		if 'MMGEN_NO_LICENSE' in os.environ: return
		self.expect("'w' for conditions and warranty info, or 'c' to continue: ",'c')

	def label(self,label='Test Label (UTF-8) α'):
		self.expect('Enter a wallet label, or hit ENTER for no label: ',label+'\n')

	def usr_rand_out(self,saved=False):
		fs = 'Generating encryption key from OS random data plus {}user-supplied entropy'
		self.expect(fs.format(('','saved ')[saved]))

	def usr_rand(self,num_chars):
		if opt.usr_random:
			self.interactive()
			self.send('\n')
		else:
			rand_chars = list(getrandstr(num_chars,no_space=True))
			vmsg_r('SEND ')
			while rand_chars:
				ch = rand_chars.pop(0)
				msg_r(yellow(ch)+' ' if opt.verbose else '+')
				ret = self.expect('left: ',ch,delay=0.005)
			self.expect('ENTER to continue: ','\n')

	def passphrase_new(self,desc,passphrase):
		self.expect('Enter passphrase for {}: '.format(desc),passphrase+'\n')
		self.expect('Repeat passphrase: ',passphrase+'\n')

	def passphrase(self,desc,passphrase,pwtype=''):
		if pwtype: pwtype += ' '
		self.expect('Enter {}passphrase for {}.*?: '.format(pwtype,desc),passphrase+'\n',regex=True)

	def hash_preset(self,desc,preset=''):
		self.expect('Enter hash preset for {}'.format(desc))
		self.expect('or hit ENTER .*?:',str(preset)+'\n',regex=True)

	def written_to_file(self,desc,overwrite_unlikely=False,query='Overwrite?  ',oo=False):
		s1 = '{} written to file '.format(desc)
		s2 = query + "Type uppercase 'YES' to confirm: "
		ret = self.expect(([s1,s2],s1)[overwrite_unlikely])
		if ret == 1:
			self.send('YES\n')
			return self.expect_getend("Overwriting file '").rstrip("'")
		self.expect(NL,nonl=True)
		outfile = self.p.before.strip().strip("'")
		if opt.debug_pexpect:
			rmsg('Outfile [{}]'.format(outfile))
		vmsg('{} file: {}'.format(desc,cyan(outfile.replace("'",''))))
		return outfile

	def no_overwrite(self):
		self.expect("Overwrite?  Type uppercase 'YES' to confirm: ",'\n')
		self.expect('Exiting at user request')

	def expect_getend(self,s,regex=False):
		ret = self.expect(s,regex=regex,nonl=True)
		debug_pexpect_msg(self.p)
		# readline() of partial lines doesn't work with PopenSpawn, so do this instead:
		self.expect(NL,nonl=True,silent=True)
		debug_pexpect_msg(self.p)
		end = self.p.before.rstrip()
		if not g.debug:
			vmsg(' ==> {}'.format(cyan(end)))
		return end

	def interactive(self):
		return self.p.interact() # interact() not available with popen_spawn

	def kill(self,signal):
		return self.p.kill(signal)

	def expect(self,s,t='',delay=None,regex=False,nonl=False,silent=False):
		delay = delay or (0,0.3)[bool(opt.buf_keypress)]

		if not silent:
			if opt.verbose:
				msg_r('EXPECT ' + yellow(str(s)))
			elif not opt.exact_output: msg_r('+')

		try:
			if s == '':
				ret = 0
			else:
				f = (self.p.expect_exact,self.p.expect)[bool(regex)]
				ret = f(s,self.timeout)
		except pexpect.TIMEOUT:
			if opt.debug_pexpect: raise
			m1 = red('\nERROR.  Expect {!r} timed out.  Exiting\n'.format(s))
			m2 = 'before: [{}]\n'.format(self.p.before)
			m3 = 'sent value: [{}]'.format(self.sent_value) if self.sent_value != None else ''
			rdie(1,m1+m2+m3)

		debug_pexpect_msg(self.p)

		if opt.verbose and type(s) != str:
			msg_r(' ==> {} '.format(ret))

		if ret == -1:
			rdie(1,'Error.  Expect returned {}'.format(ret))
		else:
			if t == '':
				if not nonl and not silent: vmsg('')
			else:
				self.send(t,delay,s)
			return ret

	def send(self,t,delay=None,s=False):
		self.sent_value = None
		delay = delay or (0,0.3)[bool(opt.buf_keypress)]
		if delay: time.sleep(delay)
		ret = self.p.send(t) # returns num bytes written
		if ret:
			self.sent_value = t
		if delay: time.sleep(delay)
		if opt.verbose:
			ls = (' ','')[bool(opt.debug or not s)]
			es = ('  ','')[bool(s)]
			msg('{}SEND {}{}'.format(ls,es,yellow("'{}'".format(t.replace('\n',r'\n')))))
		return ret

	def read(self,n=-1):
		return self.p.read(n)

	def close(self):
		if opt.pexpect_spawn:
			self.p.close()
Exemplo n.º 55
0
 def test_expect_exact_basic(self):
     p = PopenSpawn('cat', timeout=5)
     p.sendline(b'Hello')
     p.sendline(b'there')
     p.sendline(b'Mr. Python')
     p.expect_exact(b'Hello')
     p.expect_exact(b'there')
     p.expect_exact(b'Mr. Python')
     p.sendeof()
     p.expect_exact(pexpect.EOF)