Пример #1
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
Пример #2
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.")
Пример #3
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.")
Пример #4
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.')
Пример #5
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.')
Пример #6
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)
Пример #7
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'])
Пример #8
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)
Пример #9
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"])
Пример #10
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"])
Пример #11
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'])
Пример #12
0
    def startVMCLIapp(self, isofile):
        self.createAPISession()
        self.stopVMCLIapp()
        print(self.host + ' Starting VMCLI Service with ' + isofile)
        time.sleep(1)
        if 'win' in sys.platform:
            cwd = os.getcwd()
            # cmd = 'sc start VMCLI_' + self.host + ' -r [' + self.host + ']:443 -u ' + self.username + ' -p ' + self.password + ' -c "' + cwd + '\\' + isofile + '"'
            cmd = "VMCLI.exe -r [" + self.host + "]:443 -u " + self.username + " -p " + self.password + " -c " + cwd + "/" + isofile
            cmd = cmd.replace("\\", "/")
            count = 0
            while count < 10:
                try:
                    # output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
                    # print(self.host + ' ' + output)
                    session = PopenSpawn(cmd,
                                         timeout=30,
                                         encoding='utf-8',
                                         searchwindowsize=100)
                    self.VMCLISession = session
                    session.expect('Starting CD redirection', timeout=30)
                except:
                    pass
                time.sleep(30)
                if self.statusVMCLIapp():
                    break
                else:
                    print(self.host + " VMCLI Failed to start. Attempt #" +
                          str(count))
                    session.kill(signal.CTRL_C_EVENT)
                    count += 1
        else:
            self.startTunnel(['443', '5120'])
            cmd = 'VMCLIEXE -r [' + self.host.split('%')[0].split(
                'fe80'
            )[1] + ']:443 -u ' + self.username + ' -p ' + self.password + ' -c ' + isofile
            session = self.spawn(cmd, encoding='utf-8')
            try:
                session.expect('CD redirection in progress')
                self.VMCLISession = session
                time.sleep(1)
                print(self.host + ' ' + ' CD redirection in progress')
            except:
                print(self.host + 'VMCLI Failed to start')

        if not self.statusVMCLIapp():
            print(self.host + ' VMCLI Failed to start')
        else:
            print(self.host + ' CD redirection in progress')
        self.destroyAPISession()
Пример #13
0
def send(filepath):
    command = f'scp {filepath} {user}@{host}:{dst}'
    print(command)
    child = PopenSpawn(command)
    i = child.expect([f"{user}@{host}'s password:"******"{user}@{host}'s password: "******"*****@*****.**'s password:")
    child.sendline(pwd)
    child.expect(pexpect.EOF, timeout=None)

    stdout = child.before.decode()
    cmd_output = stdout.split('\r\n')
    for data in cmd_output:
        print('Output:', data)
Пример #14
0
class SerialDevice():
    process = None

    def __init__(self, port, prompt):
        self.port = port
        self.prompt = prompt

    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!')

    def close(self):
        self.process.write('\x03')
        self.process.wait()

    def run(self, command, timeout=3):
        self.process.sendline(command)
        self.process.expect(self.prompt, timeout=timeout)
        output = self.process.before.decode('utf-8')
        print('\n<serial output>\ncommand:', output)
        return output
Пример #15
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)
Пример #16
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
Пример #17
0
def apksigner(jar_path, key_path, alias):
    """使用apksigner签名

    Returns:
        type: None
    """
    cmd = ('apksigner sign --ks {} '
           '--ks-key-alias {} {}').format(key_path, alias, jar_path)
    child = PopenSpawn(cmd)
    result = child.expect('Keystore password for signer #1:')
    child.sendline(PWD)
    child.wait()
    os.remove(key_path)
Пример #18
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)
Пример #19
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)
Пример #20
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)
Пример #21
0
 def test_expect_basic(self):
     p = PopenSpawn('cat', timeout=5)
     p.sendline(b'Hello')
     p.sendline(b'there')
     p.sendline(b'Mr. Python')
     p.expect(b'Hello')
     p.expect(b'there')
     p.expect(b'Mr. Python')
     p.sendeof()
     p.expect(pexpect.EOF)
Пример #22
0
 def test_expect_basic(self):
     p = PopenSpawn("cat", timeout=5)
     p.sendline(b"Hello")
     p.sendline(b"there")
     p.sendline(b"Mr. Python")
     p.expect(b"Hello")
     p.expect(b"there")
     p.expect(b"Mr. Python")
     p.sendeof()
     p.expect(pexpect.EOF)
Пример #23
0
 def test_expect_basic(self):
     p = PopenSpawn('cat', timeout=5)
     p.sendline(b'Hello')
     p.sendline(b'there')
     p.sendline(b'Mr. Python')
     p.expect(b'Hello')
     p.expect(b'there')
     p.expect(b'Mr. Python')
     p.sendeof()
     p.expect(pexpect.EOF)
Пример #24
0
 def test_expect_basic(self):
     p = PopenSpawn("cat", timeout=5)
     p.sendline(b"Hello")
     p.sendline(b"there")
     p.sendline(b"Mr. Python")
     p.expect(b"Hello")
     p.expect(b"there")
     p.expect(b"Mr. Python")
     p.sendeof()
     p.expect(pexpect.EOF)
Пример #25
0
def jarsigner(jar_path, key_path, alias):
    """使用jarsigner签名

    Args:
        flag (bool): 是否兼容Android 4.2以下

    Returns:
        type: None

    """
    # 不支持Android 4.2 以下
    cmd = 'jarsigner -keystore {} {} {}'.format(key_path, jar_path, alias)
    child = PopenSpawn(cmd)
    result = child.expect('Enter Passphrase for keystore:')
    child.sendline(PWD)
    child.wait()
    os.remove(key_path)
class Scoutfish:
    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 = ""

    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
Пример #27
0
 def test_timeout_none(self):
     p = PopenSpawn("echo abcdef", timeout=None)
     p.expect("abc")
     p.expect_exact("def")
     p.expect(pexpect.EOF)
Пример #28
0
def genarate_key(key_name='debug.keystore', alias='release'):
    """使用keytool生成随机证书

    Returns:
        type: 无

    """
    cmd = ('keytool -genkeypair -keystore {} '
           '-alias {} -validity 3000').format(key_name, alias)
    child = PopenSpawn(cmd)

    result = child.expect('Enter keystore password:'******'Enter keystore password:'******'Re-enter new password:'******'Re-enter new password:'******']:')
    # print('What is your first and last name?\r\n  [Unknown]:')
    child.sendline(random_str())

    child.expect(']:')
    # print('What is the name of your organizational unit?\r\n  [Unknown]:')
    child.sendline(random_str())

    child.expect(']:')
    # print('What is the name of your organization?\r\n  [Unknown]:')
    child.sendline(random_str())

    child.expect(']:')
    # print('What is the name of your City or Locality?\r\n  [Unknown]:')
    child.sendline(random_str())

    child.expect(']:')
    # print('What is the name of your State or Province?\r\n  [Unknown]:')
    child.sendline(random_str())

    child.expect(']:')
    # print('What is the two-letter country code for this unit?\r\n  [Unknown]:')
    child.sendline(random_str())

    child.expect(']:')
    print(child.before[5:-15].decode(), end=' ')
    child.sendline('yes')

    child.expect('Enter key password for')
    # print('Enter key password for <release>\r\n\t(RETURN if same as keystore password):')
    child.sendline(PWD)

    child.expect('password:'******'Re - enter new password:')
    child.sendline(PWD)
    child.wait()

    # print(PWD)

    return (key_name, alias)
Пример #29
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,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:
					sys.stderr.write(clr1(u'Executing {}{}'.format(clr2(cmd_str),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:
				self.p = PopenSpawn(cmd_str.encode('utf8'))
			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 and not opt.coverage:
			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:
			rdie(1,'test.py: spawned program exited with value {}'.format(ret))
		if s == t:
			if not skip_ok: ok()
		else:
			fs = 'ERROR: recoded data:\n{}\ndiffers from original data:\n{}'
			rdie(3,fs.format(repr(t),repr(s)))

	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):
		fs = 'Generating encryption key from OS random data plus {}user-supplied entropy'
		my_expect(self.p,fs.format(('','saved ')[saved]))

	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 {}: '.format(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 {}passphrase for {}.*?: '.format(pwtype,desc),
				passphrase+'\n',regex=True)

	def hash_preset(self,desc,preset=''):
		my_expect(self.p,'Enter hash preset for {}'.format(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 = '{} written to file '.format(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("'").decode('utf8')
			return outfile
# 			else:
# 				ret = my_expect(self.p,s1)
		self.expect(self.NL,nonl=True)
		outfile = self.p.before.strip().strip("'").decode('utf8')
		if opt.debug_pexpect: rmsg('Outfile [{}]'.format(outfile))
		vmsg(u'{} 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 tx_view(self,view=None):
		repl = { 'terse':'t', 'full':'v' }[view] if view else 'n'
		my_expect(self.p,r'View .*?transaction.*? \(y\)es, \(N\)o, pager \(v\)iew.*?: ',repl,regex=True)
		if repl == 't':
			my_expect(self.p,r'any key to continue: ','\n')

	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
		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 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()
# -*- encoding: utf-8 -*-
import re

from pexpect.popen_spawn import PopenSpawn

login_test = PopenSpawn(['py', '-3', 'login_test.py'], )

login_test.expect('Username: '******'input username')
login_test.sendline('user')

login_test.expect('Password: '******'input password')
login_test.sendline('pass')

VERIFICATION_CODE_REGEX = re.compile(rb'Input verification code \((\d{4})\): ')
login_test.expect(VERIFICATION_CODE_REGEX)
print('input verification code', login_test.match.group(1))
login_test.sendline(login_test.match.group(1))

try:
    login_test.expect('>>>')
    login_test.sendline('print(1 + 1)')
    login_test.sendline('exit()')
except:
    print('unmatched output:', login_test.before)
finally:
    print('final output:', login_test.read())
Пример #31
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)
Пример #32
0
import os
from pexpect.popen_spawn import PopenSpawn

print(os.getcwd())
os.chdir(os.getcwd() + "\check_rebuild")
print(os.getcwd())

chlid = PopenSpawn("air")
chlid.expect

a = chlid.expect("running", timeout=300)
if a == 0:
    with open("main.go", "a") as f:
        f.write("\n\n")
else:
    exit(0)

a = chlid.expect("running", timeout=300)
if a == 0:
    print("::set-output name=value::PASS")
else:
    print("::set-output name=value::FAIL")
    exit(0)
Пример #33
0
 def test_timeout_none(self):
     p = PopenSpawn('echo abcdef', timeout=None)
     p.expect('abc')
     p.expect_exact('def')
     p.expect(pexpect.EOF)
Пример #34
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
Пример #35
0
 def test_timeout_none(self):
     p = PopenSpawn("echo abcdef", timeout=None)
     p.expect("abc")
     p.expect_exact("def")
     p.expect(pexpect.EOF)
Пример #36
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()
Пример #37
0
 def test_timeout_none(self):
     p = PopenSpawn('echo abcdef', timeout=None)
     p.expect('abc')
     p.expect_exact('def')
     p.expect(pexpect.EOF)
Пример #38
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
Пример #39
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'

    data_dir = os.path.join('test', 'data_dir')
    add_spawn_args = ' '.join([
        '{} {}'.format('--' + k.replace('_', '-'),
                       getattr(opt, k) if getattr(opt, k) != True else '')
        for k in ('testnet', 'rpc_host', 'rpc_port', 'regtest', 'coin')
        if getattr(opt, k)
    ]).split()
    add_spawn_args += ['--data-dir', data_dir]

    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

    def ok(self, exit_val=0):
        ret = self.p.wait()
        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 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()
Пример #40
0
                    action="store_true",
                    help="measure photodiode BEFORE temperature")
args = parser.parse_args()

# configure logging
logfile = open("laser-test.log", "w")

# spawn the shell process
child = PopenSpawn("python -u ./wasatch-shell.py --log-level debug",
                   logfile=logfile,
                   timeout=10,
                   maxread=65535,
                   encoding='utf-8')

# confirm the script launches correctly
child.expect("wasatch-shell version")
child.expect(prompt)

# open the spectrometer
child.sendline("open")
try:
    child.expect(success)
    child.expect(prompt)
except pexpect.exceptions.TIMEOUT:
    print("ERROR: No spectrometers found")
    sys.exit(1)

print("Successfully enumerated spectrometer")

# used by photodiode to READ laser power in mW
child.sendline("has_linearity_coeffs")
Пример #41
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)
Пример #42
0
# initialize test
################################################################################

# configure logging
logfile = open("load-test.log", "w")
logfile.write("settings: outer_loop %d, inner_loop %d, seed %s\n" %
              (args.outer_loop, args.inner_loop, args.seed))
random.seed(args.seed)

# spawn the shell process
child = PopenSpawn("python -u ./wasatch-shell.py --log-level debug",
                   logfile=logfile,
                   timeout=5)

# confirm the script launches correctly
child.expect("wasatch-shell version")
child.expect(prompt)

# open the spectrometer
child.sendline("open")
try:
    child.expect(success)
    child.expect(prompt)
except pexpect.exceptions.TIMEOUT:
    print "ERROR: No spectrometers found"
    sys.exit(1)

print "Successfully enumerated spectrometer"

child.sendline("has_linearity_coeffs")
child.expect(prompt)
Пример #43
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
Пример #44
0
class SerAPI:
    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

    def set_timeout(self, timeout):
        self.proc.timeout = timeout

    def get_timeout(self):
        return proc.timeout

    def send(self, cmd):
        'Send a command to SerAPI and retrieve the responses'
        #print(cmd)
        assert '\n' not in cmd
        self.proc.sendline(cmd)
        try:
            self.proc.expect([
                '\(Answer \d+ Ack\)\x00.*\(Answer \d+ Completed\)\x00',
                '\(Answer \d+ Ack\)\x00.*\(Answer \d+\(CoqExn.*\)\x00'
            ])
        except pexpect.TIMEOUT as ex:
            print(self.proc.before)
            raise CoqTimeout
        raw_responses = self.proc.after
        #print(raw_responses)
        ack_num = int(
            re.search(r'^\(Answer (?P<num>\d+)', raw_responses)['num'])
        for num in re.findall(r'(?<=\(Answer) \d+', raw_responses):
            assert int(num) == ack_num
        responses = []
        msg_str = []
        for item in raw_responses.split('\x00'):
            item = item.strip()
            if item == '':
                continue
            if not item.startswith('(Feedback') and not item.startswith(
                    '(Answer'):
                m = re.search(r'\(Feedback|\(Answer', item)
                if m is None:
                    continue
                item = item[m.span()[0]:]
                assert item.endswith(')')
            parsed_item = sexpdata.loads(item, nil=None, true=None)
            if 'CoqExn' in item:  # an error occured in Coq
                assert parsed_item[2][0] == Symbol('CoqExn')
                raise CoqExn(sexpdata.dumps(parsed_item[2][4]),
                             sexpdata.dumps(parsed_item[2]))
            if item.startswith('(Feedback'):  # ignore Feedback for now
                try:
                    msg = parsed_item[1][3][1]
                    if isinstance(msg,
                                  list) and msg != [] and msg[0] == Symbol(
                                      'Message'):
                        msg_sexp, _ = self.send(
                            '(Print ((pp_format PpStr)) (CoqPp %s))' %
                            sexpdata.dumps(msg[3]))
                        msg_str.extend(
                            [symbol2str(x[1]) for x in msg_sexp[1][2][1]])
                except IndexError:
                    pass
                continue
            responses.append(parsed_item)
        msg_str = '\n'.join(msg_str)
        return responses, raw_responses

    def send_add(self, cmd, return_ast):
        'Send a (Add () "XXX") command to SerAPI, return the state id and optionally the AST'
        responses, raw_responses = self.send('(Add () "%s")' % escape(cmd))
        state_ids = [
            int(sid) for sid in ADDED_STATE_PATTERN.findall(raw_responses)
        ]
        state_id = state_ids[-1]
        if self.states_stack != []:
            self.states_stack[-1].append(state_id)
        if return_ast:
            if cmd not in self.ast_cache:
                self.ast_cache[cmd] = self.query_ast(cmd)
            ast = self.ast_cache[cmd]
        else:
            ast = None
        return state_id, ast

    def query_ast(self, cmd):
        'Query the AST of the vernac command just added'
        responses, _ = self.send('(Parse () "%s")' % escape(cmd))
        ast = responses[1][2][1][0]
        assert ast[0] == Symbol('CoqAst')
        return ast

    def query_library(self, lib):
        responses, _ = self.send('(Query () (LocateLibrary "%s"))' % lib)
        physical_path = symbol2str(responses[1][2][1][0][3])
        return physical_path

    def query_qualid(self, qualid):
        responses, _ = self.send('(Query () (Locate "%s"))' % qualid)
        if responses[1][2][1] == [] and qualid.startswith('SerTop.'):
            qualid = qualid[len('SerTop.'):]
            responses, _ = self.send('(Query () (Locate "%s"))' % qualid)
        assert len(responses[1][2][1]) == 1
        short_responses = responses[1][2][1][0][1][0][1]
        assert short_responses[1][0] == Symbol('DirPath')
        short_ident = '.'.join(
            [symbol2str(x[1]) for x in short_responses[1][1][::-1]] +
            [symbol2str(short_responses[2][1])])
        return short_ident

    def query_env(self, current_file):
        'Query the global environment'
        responses, _ = self.send('(Query () Env)')
        env = responses[1][2][1][0]

        # store the constants
        constants = []
        for const in env[1][0][1][0][1]:
            # identifier
            qualid = print_mod_path(const[0][1]) + '.' + \
                '.'.join([symbol2str(x[1]) for x in const[0][2][1][::-1]] + [symbol2str(const[0][3][1])])
            if qualid.startswith('SerTop.'):
                logical_path = 'SerTop'
                physical_path = current_file
            else:
                logical_path = mod_path_file(const[0][1])
                assert qualid.startswith(logical_path)
                physical_path = os.path.relpath(
                    self.query_library(logical_path))
            physical_path += ':' + qualid[len(logical_path) + 1:]
            short_ident = self.query_qualid(qualid)
            # term
            assert const[1][0][1][0] == Symbol('const_body')
            if const[1][0][1][1][0] == Symbol('Undef'):  # delaration
                opaque = None
                term = None
            elif const[1][0][1][1][0] == Symbol(
                    'Def'):  # transparent definition
                opaque = False
                term = None
            else:
                assert const[1][0][1][1][0] == Symbol(
                    'OpaqueDef')  # opaque definition
                opaque = True
                term = None
            # type
            assert const[1][0][2][0] == Symbol('const_type')
            type_sexp = sexpdata.dumps(const[1][0][2][1])
            type = self.print_constr(type_sexp)
            sort = self.query_type(type_sexp, return_str=True)
            constants.append({
                'physical_path': physical_path,
                'short_ident': short_ident,
                'qualid': qualid,
                'term': term,
                'type': type,
                'sort': sort,
                'opaque': opaque,
                'sexp': sexpdata.dumps(const[1][0][2][1])
            })

        # store the inductives
        inductives = []
        for induct in env[1][0][1][1][1]:
            # identifier
            qualid = print_mod_path(induct[0][1]) + '.' + \
                '.'.join([symbol2str(x[1]) for x in induct[0][2][1][::-1]] + [symbol2str(induct[0][3][1])])
            short_ident = self.query_qualid(qualid)
            if qualid.startswith('SerTop.'):
                logical_path = 'SerTop'
                physical_path = current_file
            else:
                logical_path = mod_path_file(induct[0][1])
                physical_path = os.path.relpath(
                    self.query_library(logical_path))
            assert qualid.startswith(logical_path)
            physical_path += ':' + qualid[len(logical_path) + 1:]
            # blocks
            blocks = []
            for blk in induct[1][0][0][1]:
                blk_qualid = '.'.join(
                    qualid.split('.')[:-1] + [symbol2str(blk[0][1][1])])
                blk_short_ident = self.query_qualid(blk_qualid)
                # constructors
                constructors = []
                for c_name, c_type in zip(blk[3][1], blk[4][1]):
                    c_name = symbol2str(c_name[1])
                    c_type = self.print_constr(sexpdata.dumps(c_type))
                    #if c_type is not None:
                    #    c_type = UNBOUND_REL_PATTERN.sub(short_ident, c_type)
                    constructors.append((c_name, c_type))
                blocks.append({
                    'short_ident': blk_short_ident,
                    'qualid': blk_qualid,
                    'constructors': constructors
                })

            inductives.append({
                'physical_path':
                physical_path,
                'blocks':
                blocks,
                'is_record':
                induct[1][0][1][1] != Symbol('NotRecord'),
                'sexp':
                sexpdata.dumps(induct)
            })

        return constants, inductives

    def query_goals(self):
        'Retrieve a list of open goals'
        responses, _ = self.send('(Query () Goals)')
        assert responses[1][2][0] == Symbol('ObjList')
        if responses[1][2][1] == []:  #  no goals
            return [], [], [], []
        else:
            assert len(responses[1][2][1]) == 1

            def store_goals(goals_sexp):
                goals = []
                for g in goals_sexp:
                    hypotheses = []
                    for h in g[2][1]:
                        h_sexp = sexpdata.dumps(h[2])
                        hypotheses.append({
                            'idents':
                            [symbol2str(ident[1]) for ident in h[0][::-1]],
                            'term': [
                                None if t == [] else self.print_constr(
                                    sexpdata.dumps(t)) for t in h[1]
                            ],
                            'type':
                            self.print_constr(h_sexp),
                            'sexp':
                            h_sexp
                        })

                    type_sexp = sexpdata.dumps(g[1][1])
                    goals.append({
                        'id': int(g[0][1]),
                        'type': self.print_constr(type_sexp),
                        'sexp': type_sexp,
                        'hypotheses': hypotheses[::-1]
                    })
                return goals

            fg_goals = store_goals(responses[1][2][1][0][1][0][1])
            bg_goals = store_goals(
                list(
                    chain.from_iterable(
                        chain.from_iterable(responses[1][2][1][0][1][1][1]))))
            shelved_goals = store_goals(responses[1][2][1][0][1][2][1])
            given_up_goals = store_goals(responses[1][2][1][0][1][3][1])
            return fg_goals, bg_goals, shelved_goals, given_up_goals

    def has_open_goals(self):
        responses, _ = self.send('(Query () Goals)')
        assert responses[1][2][0] == Symbol('ObjList')
        return responses[1][2][1] != []

    def print_constr(self, sexp_str):
        if not hasattr(self, 'constr_cache'):
            self.constr_cache = {}
        if sexp_str not in self.constr_cache:
            try:
                responses, _ = self.send(
                    '(Print ((pp_format PpStr)) (CoqConstr %s))' % sexp_str)
                self.constr_cache[sexp_str] = normalize_spaces(
                    symbol2str(responses[1][2][1][0][1]))
            except CoqExn as ex:
                if ex.err_msg == 'Not_found':
                    return None
                else:
                    raise ex
            except TypeError as ex:
                self.constr_cache[sexp_str] = normalize_spaces(
                    symbol2str(responses[0][2][1][0][1]))
        return self.constr_cache[sexp_str]

    def query_vernac(self, cmd):
        return self.send('(Query () (Vernac "%s"))' % escape(cmd))

    def query_type(self, term_sexp, return_str=False):
        try:
            responses, _ = self.send('(Query () (Type %s))' % term_sexp)
        except CoqExn as ex:
            if ex.err_msg == 'Not_found':
                return None
            else:
                raise ex
        assert responses[1][2][1][0][0] == Symbol('CoqConstr')
        type_sexp = responses[1][2][1][0][1]
        if return_str:
            return self.print_constr(sexpdata.dumps(type_sexp))
        else:
            return type_sexp

    def execute(self, cmd, return_ast=False):
        'Execute a vernac command'
        state_id, ast = self.send_add(cmd, return_ast)
        responses, _ = self.send('(Exec %d)' % state_id)
        return responses, sexpdata.dumps(ast)

    def push(self):
        'push a new frame on the state stack (a checkpoint), which can be used to roll back to the current state'
        self.states_stack.append([])

    def cancel(self, states):
        self.send('(Cancel (%s))' % ' '.join([str(s) for s in states]))

    def pull(self):
        'remove a checkpoint created by push'
        states = self.states_stack.pop()
        self.states_stack[-1].extend(states)

    def pop(self):
        'rollback to a checkpoint created by push'
        self.cancel(self.states_stack.pop())

    def clean(self):
        self.proc.sendeof()
        self.proc.wait()
        self.dead = True

    def shutdown(self):
        self.proc.kill(signal.SIGKILL)
        self.dead = True

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.clean()