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
class Simulate: 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 # self.choice = Backend.CHOICE # 功能与对应输入字符的映射(反转后台类对象字典) @staticmethod def reverse_dict(): d = Backend.func_dict return dict(zip(d.values(), d.keys())) def common(self, choice): # 等待特定字符出现 self.process.expect_exact("请输入您想使用的功能") # 命令行输入 self.process.send(f'{choice}\n') self.process.expect_exact("请输入用户名") # 使用 faker 制造测试数据 fake = Faker('zh_CN') self.process.send(fake.phone_number() + '\n') self.process.expect_exact("请输入密码") self.process.send(fake.password(length=random.randint(6, 16)) + '\n') # 模拟登录 def login(self): self.common(self.d['login']) def register(self): self.common(self.d['register']) def exit(self): self.process.expect_exact("请输入您想使用的功能") self.process.send('q\n') # 不忘文件关闭 self.logFile.close() def run(self): # 执行 homework, 开始测试 self.process.sendline('python mini_system.py') # 输入 1 使用登录功能;输入 2 使用注册功能;输入 q 退出程序 # File "K:\Anaconda\lib\site-packages\pexpect\spawnbase.py", line 138, in _coerce_expect_string # return s.encode('ascii') for i in range(5): # 执行注册或登录测试 exec(f'self.{random.choice(self.seq)}()') self.exit()
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
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)
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)
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
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)
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)
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
def set_pass_password2(entry_name, password): import sys from pexpect.popen_spawn import PopenSpawn command = 'pass insert --force {k}'.format(k=entry_name) # child = pexpect.spawn(command, encoding='utf-8', ignore_sighup=True) child = PopenSpawn(command, encoding='utf-8') child.logfile = sys.stdout print(child.read()) # child.expect("Enter password") child.sendline(password) # child.expect("Retype password") print(child.read()) child.sendline(password + 'a') print(child.readline()) print(child.exitstatus, child.signalstatus)
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 ServerHandler: def __init__(self, start_script): # self.timer = ServerTimer() self.jvm_args = (open(start_script, 'r')).read() if self.jvm_args is not None: sys.stdout.write( f"Detected Java arguments from {start_script}: \n{self.jvm_args}\n" ) else: sys.stdout.write("Start script not found, quitting...\n") exit() self.start_triggers = [ '[Server thread/INFO]: Done (', '[Server thread/INFO] [minecraft/DedicatedServer]: Done (', 'For help, type "help"' ] self.status = False # Start JVM instance with provided arguments def instantiate(self): try: self.server = PopenSpawn(self.jvm_args) except Exception as e: sys.stderr.write(str(e)) # Listen on the JVM for triggers def listen(self): while self.server.proc.poll() is None: line = self.server.proc.stdout.read() print(line) # if 'For help, type "help"' in i.decode('utf-8'): # sys.stdout.write('{} [Python Wrapper] [INFO]: Server detected online\n'.format(timestamp())) # self.status = True # self.execute('say test!') # for trigger in self.start_triggers: # if trigger.encode('utf-8') in self.server.proc.stdout.readline(): # sys.stdout.write('{} [Python Wrapper] [INFO]: Server detected online\n'.format(timestamp())) # self.status = True # self.execute('say test!') # break if line == '' and self.server.proc.poll() != None: break if line != '': sys.stdout.write(line.decode('utf-8')) sys.stdout.flush() # if self.status: # self.timer_start() # self.run_timer() # if self.status: # self.timer.start_timestamp = self.timer.get_timestamp() def timer_start(self): self.start_timestamp = int(time.time()) def spawn_timer_daemon(self): self.timer_daemon = threading.Thread(target=self.run_timer, name='timer', daemon=True) self.timer_daemon.start() def run_timer(self): self.server.sendline('list') def update_pipe(self): return (self.server.proc.stdout.readline()).decode('utf-8') def execute(self, cmd): self.server.sendline(cmd)
import traceback import pexpect from pexpect.popen_spawn import PopenSpawn try: child = PopenSpawn('cmd') child.sendline('ping') child.sendline('exit') child.expect(pexpect.EOF) out = child.before.decode('gbk') print out except pexpect.EOF: traceback.print_exc()
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)
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
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
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()
def launch_wasabi(wallet, launch_path='', destination='', keepalive=True, pwd=''): """ Launch Wasabi daemon. Return pexpect child if successful or return False if Wasabi didn't started. Wasabi daemon's documentation: https://docs.wasabiwallet.io/using-wasabi/Daemon.html#headless-wasabi-daemon """ command = '' # If no launch_path provided, assume deb package is installed. if not launch_path: command += 'wassabee mix' # If launch_path is provided, if WalletWasabi.Gui in it, # assume source code. elif 'WalletWasabi.Gui' in launch_path: command += 'dotnet run --mix' # If launch_path is provided, if WalletWasabi.Gui not in it, # assume targz. else: command += './wassabee mix' command += ' --wallet:{}'.format(wallet) if destination: command += ' --destination:{}'.format(destination) if keepalive: command += ' --keepalive' print('Starting Wasabi') if launch_path: try: wasabi_proc = PopenSpawn(command, cwd=launch_path) except FileNotFoundError as e: raise FileNotFoundError( str(e) + ', check that your ' + '"launch_path" setting is correct') else: wasabi_proc = PopenSpawn(command) index = wasabi_proc.expect_exact( ['Password:'******'selected wallet does not exist', TIMEOUT, EOF], timeout=30) if index == 0: wasabi_proc.sendline(pwd) elif index == 1: raise MyExceptions.WalletMissing(name + '.json wallet does not exist') elif index == 2: raise MyExceptions.ProcessTimeout('Wasabi process TIMEOUT') elif index == 3: raise EOFError('Pexpect EOF') index = wasabi_proc.expect_exact( ['Correct password', 'Wrong password', TIMEOUT, EOF], timeout=30) if index == 0: print('Wasabi daemon started') elif index == 1: wasabi_proc.kill(SIGTERM) wasabi_proc.wait() raise MyExceptions.WrongPassword('Wrong password') elif index == 2: raise MyExceptions.ProcessTimeout('Wasabi process TIMEOUT') elif index == 3: raise EOFError('Pexpect EOF') index = wasabi_proc.expect_exact(['Starting Wallet', TIMEOUT, EOF], timeout=30) if index == 0: print('Wallet starting') elif index == 1: raise MyExceptions.ProcessTimeout('Wasabi process TIMEOUT') elif index == 2: raise EOFError('Pexpect EOF') if is_wasabi_running(): print('Wasabi started') return wasabi_proc else: return False
class WeatherSimulation(threading.Thread): def __init__(self, id, path, options, weather, redo=False, deactivate_window_mode=True, bin_folder=os.path.join( os.getcwd(), "3rdparty", "weather-particle-simulator", "{}_{}".format(bin_platform, bin_bitness))): threading.Thread.__init__(self) self.id = id self.simtime = 0. # Time (s) self.simdur = 0. # Duration (s) self.options = options if "preset" in self.options: self.preset = self.options["preset"] else: self.preset = None self.path = path self.weather = weather self.deactivate_window_mode = deactivate_window_mode self.redo = redo self.bin_folder = bin_folder self.output_dir = os.path.join(self.path, weather["weather"], "{}mm".format(weather["fallrate"])) print("Create thread", self.output_dir) self.assert_validity() def assert_validity(self): if self.preset is not None: assert self.preset[0] != "kitti" or self.preset[1] in [ "0000", "0032", "0056", "0071", "0117" ], "Kitti preset is invalid" assert self.preset[0] != "cityscapes" or self.preset[1] in [ "0000" ], "Cityscapes preset is invalid" def _print(self, *argv): print("\r #{} ".format(self.id), end='') print.__call__(*argv) def interact(self, wait_for, send_str): # print('wait for: {}'.format(wait_for)) self.child.expect(wait_for) # print('send: {}'.format(send_str)) self.child.sendline(send_str.encode('ascii')) def interact_step_menu(self, menu): self.interact('Steps: What do you want to do \?', menu) def set_sim_steps_times(self, start, dur, last): self.interact_step_menu('2') self.interact('Enter new duration', '{}'.format(start)) self.interact_step_menu('3') self.interact('Enter new duration', '{}'.format(dur)) self.interact_step_menu('4') self.interact('Enter new duration', '{}'.format(last)) def set_sim_steps_camera_focal(self, values): self.interact_step_menu('12') self.interact('What do you want to do \?', '{}'.format(3)) self.interact('Separator', '{}'.format(";")) self.interact('Enter all steps values', '{}'.format(";".join(["{}".format(v) for v in values]))) self.interact('Continue \?', '{}'.format("y")) def set_sim_steps_camera_exposure(self, values): self.interact_step_menu('13') self.interact('What do you want to do \?', '{}'.format(3)) self.interact('Separator', '{}'.format(";")) self.interact('Enter all steps values', '{}'.format(";".join(["{}".format(v) for v in values]))) self.interact('Continue \?', '{}'.format("y")) def set_sim_steps_camera_motion(self, values): self.interact_step_menu('18') self.interact('What do you want to do \?', '{}'.format(3)) self.interact('Separator', '{}'.format(";")) self.interact('Enter all steps values', '{}'.format(";".join(["{}".format(v) for v in values]))) self.interact('Continue \?', '{}'.format("y")) def set_sim_steps_rain_fallrate(self, values): self.interact_step_menu('41') self.interact('What do you want to do \?', '{}'.format(3)) self.interact('Separator', '{}'.format(";")) self.interact('Enter all steps values', '{}'.format(";".join(["{}".format(v) for v in values]))) self.interact('Continue \?', '{}'.format("y")) def interact_main_menu(self, menu): self.interact('What do you want to do \?', menu) def set_sim_Duration(self, val): self.interact_main_menu('6') self.interact('Enter new duration', '{}'.format(val)) def set_sim_Hz(self, val): self.interact_main_menu('7') self.interact('Enter new frequency', '{}'.format(val)) def set_sim_ParticlesDetectionLatencyFrames(self, val): self.interact_main_menu('61') self.interact('Enter new particles detection latency', '{}'.format(val)) def set_sim_ParticlesDetectionErrorMargin(self, val): self.interact_main_menu('62') self.interact('Enter new particles detection error', '{}'.format(val)) def set_sim_Camera0_Hz(self, val): self.interact_main_menu('10') self.interact('Enter new frequency', '{}'.format(val)) def set_sim_Camera0_ViewMatrixIC(self, pos, lookat, up): self.interact_main_menu('15') self.interact('Enter new IC pos x', '{}'.format(pos[0])) self.interact('Enter new IC pos y', '{}'.format(pos[1])) self.interact('Enter new IC pos z', '{}'.format(pos[2])) self.interact('Enter new IC lookat x', '{}'.format(lookat[0])) self.interact('Enter new IC lookat y', '{}'.format(lookat[1])) self.interact('Enter new IC lookat z', '{}'.format(lookat[2])) self.interact('Enter new IC up x', '{}'.format(up[0])) self.interact('Enter new IC up y', '{}'.format(up[1])) self.interact('Enter new IC up z', '{}'.format(up[2])) def set_sim_Camera0_CcdSpecs(self, width, height, pixsize): self.interact_main_menu('11') self.interact('Camera 0 CCD pxl size', '{}'.format(pixsize)) self.interact('Camera 0 CCD width', '{}'.format(width)) self.interact('Camera 0 CCD height', '{}'.format(height)) def set_sim_Camera0_Focal(self, val): self.interact_main_menu('12') self.interact('Enter new focal', '{}'.format(val)) def set_sim_Camera0_Resolution(self, width, height): self.interact_main_menu('14') self.interact('Camera 0 Resolution WIDTH', '{}'.format(width)) self.interact('Camera 0 Resolution HEIGHT', '{}'.format(height)) def set_sim_Camera0_ExposureTime(self, val): self.interact_main_menu('13') self.interact('Enter new exposure time', '{}'.format(val)) def set_sim_Camera0_VisibilityMappingAuto(self): self.interact_main_menu('17') self.interact('Enter new visibility mapping MIN', '{}'.format(0)) self.interact('Enter new visibility mapping MAX', '{}'.format(0)) def set_sim_Camera0_MotionSpeedIC(self, val): self.interact_main_menu('18') self.interact('Enter new initial motion speed', '{}'.format(val)) def set_sim_Projector0_Hz(self, val): self.interact_main_menu('21') assert (val == "AHL_HZ_MAX") # No need to do anything def set_sim_Projector0_Res(self, width, height): self.interact_main_menu('22') # No need to do anything def set_sim_Projector0_MinPixelOverlay(self, val): self.interact_main_menu('24') self.interact('Enter new minimum pixel overlay', '{}'.format(val)) def set_sim_Projector0_DbgSaveLightmap(self, val): while True: self.interact_main_menu('28') obs = self.child.expect([ "Projector 0 save light maps \(OFF\)", "Projector 0 save light maps \(ON\)" ]) if (val and obs == 1) or (not val and obs == 0): break def set_sim_Stats_Active(self, val): while True: self.interact_main_menu('70') obs = self.child.expect([ "Output simulation stats \(OFF\)", "Output simulation stats \(ON\)" ]) if (val and obs == 1) or (not val and obs == 0): break def set_sim_Stats_StatsLevel(self, val): while True: self.interact_main_menu('72') obs = self.child.expect( ["Stats level \(HIERARCHY\)", "Stats level \(NO HIERARCHY\)"]) if (val == "HIERARCHY" and obs == 0) or (val == "NO HIERARCHY" and obs == 1): break def set_sim_Stats_StartTime(self, val): self.interact_main_menu('71') self.interact('Enter start time', '{}'.format(val)) def apply_options(self): # Series of hacks to avoid warning due to incompatible parameters self.set_sim_Camera0_CcdSpecs(99999, 99999, 1.0) # Apply settings self.set_sim_Duration(stats_start_time * 1000 + self.options["sim_duration"] * 1000) self.set_sim_Hz(str(self.options["sim_hz"])) self.set_sim_ParticlesDetectionLatencyFrames(0) self.set_sim_ParticlesDetectionErrorMargin(0) # Camera parameters self.set_sim_Camera0_Hz(self.options["cam_hz"]) self.set_sim_Camera0_ViewMatrixIC(self.options["cam_pos"], self.options["cam_lookat"], self.options["cam_up"]) self.set_sim_Camera0_Resolution( self.options["cam_WH"][0], self.options["cam_WH"][1]) # Order matters self.set_sim_Camera0_CcdSpecs( self.options["cam_CCD_WH"][0], self.options["cam_CCD_WH"][1], self.options["cam_CCD_pixsize"]) # Order matters self.set_sim_Camera0_Focal(self.options["cam_focal"]) self.set_sim_Camera0_ExposureTime(self.options["cam_exposure"]) self.set_sim_Camera0_VisibilityMappingAuto() # self.set_sim_Camera0_motionSpeedIC = 30 # Projector parameters self.set_sim_Projector0_Hz("AHL_HZ_MAX") self.set_sim_Projector0_Res(self.options["cam_WH"][0], self.options["cam_WH"][1]) self.set_sim_Projector0_MinPixelOverlay(50) self.set_sim_Projector0_DbgSaveLightmap(True) # Debug # parameters # self.set_sim_Render()->Active(false) # self.set_sim_Render()->Hz(200) # self.set_sim_Render()->DbgCameraManip(true) # self.set_sim_Render()->DbgCameraManipViewMatrix(self.set_sim_Camera0_Pos(), self.set_sim_Camera0_Lookat(), self.set_sim_Camera( # 0)->Up()) self.set_sim_Stats_Active(True) self.set_sim_Stats_StartTime("{}".format(stats_start_time * 1000)) self.set_sim_Stats_StatsLevel("HIERARCHY") def run(self): weather = self.weather os.makedirs(self.output_dir, exist_ok=True) if not self.redo: files = os.listdir(self.output_dir) results_computed = np.any(["camera0.xml" in f for f in files]) if results_computed: self._print("Simulation file exits {}, next!".format( self.output_dir)) return # Save options as json dumps try: fp = open(os.path.join(self.output_dir, "sim_options.json"), 'w') options_ = { k: v.tolist() if isinstance(v, np.ndarray) else v for k, v in self.options.items() } # Convert to native types if "sim_steps" in options_: options_["sim_steps"] = { k: v.tolist() if isinstance(v, np.ndarray) else v for k, v in options_["sim_steps"].items() } json.dump(options_, fp) fp.close() except Exception as e: print(e) print("Failed saving JSON {}... Not crucial, continuing") log_path = os.path.join(self.output_dir, 'automate_log.txt') log_fp = open(log_path, 'a+') # self._print(self.output_dir) self.child = PopenSpawn(os.path.join(self.bin_folder, 'AHLSimulation'), cwd=self.output_dir, logfile=logwriter(log_fp)) try: self._print("In main menu") self.interact_main_menu('9') self.interact('Set the seed for random generator', '0') if self.preset is None: self._print("Apply options:", self.options) self.apply_options() else: self._print("Apply preset (ignoring options):", self.preset[:2]) if self.preset[0] in sequence_code: self.interact_main_menu('99') time.sleep(0.5) self._print("Setting system") self.child.expect('Which system to run ?') seq_code = sequence_code[self.preset[0]] + self.preset[1] self._print(' System code: ', seq_code) self.child.sendline(seq_code.encode('ascii')) elif "nuscenes" in self.preset[0].lower(): self.interact_main_menu('99') time.sleep(0.5) self._print("Setting system") self.child.expect('Which system to run ?') if "2Hz" in self.preset[0]: seq_code = '1000' else: seq_code = '100' self._print(' System code: ', seq_code) self.child.sendline(seq_code.encode('ascii')) else: raise NotImplementedError( "No settings for this set {}".format(self.preset[0])) # Deactivate windows AND save light map option if self.deactivate_window_mode: self._print("In main menu") self.interact_main_menu('28') time.sleep(0.5) self._print(" Save light map") # Deactivate rain particles self._print("Deactivating rain particles") self.interact_main_menu('410') self.child.expect('410. Rain \(OFF\)') if weather["weather"] == "rain": self._print("Activating rain particles") # Activate rain particles self.interact_main_menu('410') self.child.expect('410. Rain \(ON\)') self._print("Setting rain fallrate") # Set rain fallrate self.interact_main_menu('414') self.child.expect('Enter new Rain fall rate') code = str(weather["fallrate"]) self.child.sendline(code.encode('ascii')) self._print("In main menu") _steps_menu = False if self.preset is None: assert self.options["sim_mode"] in ["normal", "steps"] # Actions in main menu if self.options["sim_mode"] == "steps": assert ( "sim_steps" in self.options and type(self.options["sim_steps"]) == dict and len(self.options["sim_steps"].keys()) != 0 ), "[ERROR]: sim_steps must be provided and be a non-empty dictionary." assert np.all( [ type(v) == list or type(v) == np.ndarray for k, v in self.options["sim_steps"].items() ] ), "[ERROR]: values in sim_steps should all be list/arrays" # Apply initial cam speed if "cam_motion" in self.options["sim_steps"]: speedIC = self.options["sim_steps"]["cam_motion"][0] self.set_sim_Camera0_MotionSpeedIC(speedIC) # Actions in step menu if self.options["sim_mode"] == "steps": # Enter step menu self.interact_main_menu('102') _steps_menu = True # Set step durations step_dur = 1. / self.options["cam_hz"] self.set_sim_steps_times(stats_start_time * 1000, step_dur * 1000, step_dur * 1000) # Attempt to compute the total simulation time max_steps = max( [len(v) for k, v in self.options["sim_steps"].items()]) self.simdur = stats_start_time + (1 + max_steps) * step_dur if "cam_motion" in self.options["sim_steps"]: self._print(" Apply {} steps cam motion: {}".format( len(self.options["sim_steps"]["cam_motion"]), ";".join([ "{}".format(v) for v in self.options["sim_steps"]["cam_motion"] ]))) self.set_sim_steps_camera_motion( self.options["sim_steps"]["cam_motion"]) if "cam_focal" in self.options["sim_steps"]: self._print(" Apply {} steps cam focal: {}".format( len(self.options["sim_steps"]["cam_focal"]), ";".join([ "{}".format(v) for v in self.options["sim_steps"]["cam_focal"] ]))) self.set_sim_steps_camera_focal( self.options["sim_steps"]["cam_focal"]) if "cam_exposure" in self.options["sim_steps"]: self._print(" Apply {} steps cam exposure: {}".format( len(self.options["sim_steps"]["cam_exposure"]), ";".join([ "{}".format(v) for v in self.options["sim_steps"]["cam_exposure"] ]))) self.set_sim_steps_camera_exposure( self.options["sim_steps"]["cam_exposure"]) if "rain_fallrate" in self.options["sim_steps"]: self._print(" Apply {} steps rain fallrate: {}".format( len(self.options["sim_steps"]["rain_fallrate"]), ";".join([ "{}".format(v) for v in self.options["sim_steps"]["rain_fallrate"] ]))) self.set_sim_steps_rain_fallrate( self.options["sim_steps"]["rain_fallrate"]) if self.options["sim_mode"] == "normal": self.simdur = stats_start_time + self.options[ "sim_duration"] else: # Enter step menu self.interact_main_menu('102') _steps_menu = True # self._print("Going to step menu") # self.interact_main_menu('102') # if "nuscenes" in preset[0].lower(): # self.child.expect('Steps: What do you want to do \?') # self._print("In Step menu") # self.child.sendline('18'.encode('ascii')) # self._print("Camera 0 motion speed choices") # self.child.expect("What do you want to do") # self.child.sendline('3'.encode('ascii')) # self._print(" Camera 0 motion speed choices (3 -> all at once)") # # speed = np.linalg.norm(np.array(preset[3]), axis=1) / 1000 * 3600 / (np.array(preset[4]) * 1e-6) # self.child.expect("Separator") # self.child.sendline(';'.encode('ascii')) # self.child.expect("Enter all steps values") # self._print(" Camera 0 motion speeds (min, max): ({}, {})".format(np.min(speed), np.max(speed))) # self.child.sendline(';'.join([str(s) for s in speed.tolist()]).encode('ascii')) # self.child.expect("Continue") # self.child.sendline('y'.encode('ascii')) if _steps_menu: self._print("In Step menu") self._print("Starting simulation") self.interact_step_menu('1') index = None while index != 1: index = self.child.expect([ '[0-9]+:[0-9]+:[0-9]+\.[0-9]* +\(p#[0-9]*\)', '\[Simulation stopped\]' ], timeout=None) if index == 0: self.simtime += 0.5 # Each time a time is displayed the simulation advanced of 0.5 seconds self._print("Simulation stopped") time.sleep(5.) # Wait for the "Press any key to continue" self.child.sendline(b'\n') if _steps_menu: self.child.expect('Steps: What do you want to do \?') self._print("In Step menu") self._print("Going to main menu") self.child.sendline('0'.encode('ascii')) _steps_menu = False self._print("In main menu") self._print("Stopping process") self.child.expect('What do you want to do \?') self.child.sendline('0'.encode('ascii')) self.child.expect('Press any key to continue . . .') self.child.sendline(b'\n') self.child.expect('Press any key to continue . . .') self.child.sendline(b'\n') except ExceptionPexpect as e: self._print( "ERROR occured. Check the content of the log file to solve: {}" .format(log_path)) self._print(e) self._print( "If starting python from an IDE, check LD_LIBRARY_PATH environment is accessible (https://youtrack.jetbrains.com/issue/PY-29580)" ) self.child.kill(signal.SIGINT) exit() except Exception as e: # Might happens at the end of the simulation, if the process closes slightly before last key strike self._print(e) pass # Kill process in case it's not dead (cruel world) try: self.child.wait() self.child.kill(signal.SIGINT) except Exception: pass log_fp.close()
import traceback import pexpect from pexpect.popen_spawn import PopenSpawn try: child = PopenSpawn('ssh [email protected]') # powershellz child.sendline('Huqiang2018') print child.before, child.after i = child.expect([pexpect.TIMEOUT, 'password: '******'ERROR!' print 'SSH could not login. Here is what SSH said:' print child.before, child.after if i == 1: child.sendline('Huqiang2018') child.sendline('ls -l') print child.before, child.after except pexpect.EOF: traceback.print_exec()
parser = argparse.ArgumentParser(description="One-Shot measurement") parser.add_argument("--integration-time-ms", type=int, default=100) parser.add_argument("--scans-to-average", type=int, default=1) parser.add_argument("--wavelength", type=float, default=None) parser.add_argument("--wavenumber", type=float, default=None) parser.add_argument("--laser", action="store_true", help="fire the laser") args = parser.parse_args() # initialize test logfile = open("one-shot.log", "w") child = PopenSpawn("python -u ./wasatch-shell.py --log-level debug", logfile=logfile, timeout=max_timeout_sec, maxread=65535, encoding='utf-8') 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) # configure the measurement child.sendline("set_integration_time_ms %d" % args.integration_time_ms) child.expect(prompt) child.sendline("set_scans_to_average %d" % args.scans_to_average) child.expect(prompt) if args.wavelength is not None:
# -*- 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())
class Pandora(object): def __init__(self): self.started = False 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) def partnerLogin(self): partnerLoginData = { "username": "******", "password": "******", "deviceModel": "pre", "version": "5" } self.partnerLoginResponse = requests.post('https://tuner.pandora.com/services/json/?method=auth.partnerLogin',json = partnerLoginData) def userLogin(self,user,password): partnerJson = json.loads(self.partnerLoginResponse.text) partnerResult = partnerJson['result'] self.syncTime = partnerResult['syncTime'] self.partnerAuthToken = partnerResult['partnerAuthToken'] self.partnerId = partnerResult['partnerId'] userLoginData = { "loginType": "user", "username": user, "password": password, "partnerAuthToken": self.partnerAuthToken, "includePandoraOneInfo":True, "includeAdAttributes":True, "includeSubscriptionExpiration":True, "includeStationArtUrl":True, "returnStationList":True, "returnGenreStations":True, "syncTime": self.syncTime } self.userLoginResponse = requests.post('https://tuner.pandora.com/services/json/?method=auth.userLogin&partner_id='+self.partnerId,json = userLoginData) def retrieveStations(self): userJson = json.loads(self.userLoginResponse.text) userResult = userJson['result'] self.userId = userResult['userId'] self.userAuthToken = userResult['userAuthToken'] stationData = { "userAuthToken": self.userAuthToken, "syncTime": self.syncTime } self.stationResponse = requests.post('https://tuner.pandora.com/services/json/?method=user.getStationList&partner_id='+self.partnerId+'&user_id='+self.userId, json = stationData) def retrievePlaylist(self): stationJson = json.loads(self.stationResponse.text) stationResult = stationJson['result'] self.stationName = stationResult['stations'][0]['stationName'] self.stationToken = stationResult['stations'][0]['stationToken'] playlistData = { "userAuthToken": self.userAuthToken, "additionalAudioUrl": "HTTP_32_AACPLUS_ADTS,HTTP_64_AACPLUS_ADTS", "syncTime": self.syncTime, "stationToken": self.stationToken } self.playlistResponse = requests.post('https://tuner.pandora.com/services/json/?method=station.getPlaylist&partner_id='+self.partnerId+'&user_id='+self.userId, json = playlistData) def playSong(self): playlistJson = json.loads(self.playlistResponse.text) playlistResult = playlistJson['result'] self.song = playlistResult['items'][0]['audioUrlMap']['highQuality']['audioUrl'] def play(self): self.pianobarInstance.sendline('p') def pause(self): self.pianobarInstance.sendline('p') def stop(self): self.pianobarInstance.sendline('q') self.started = False def skip(self): self.pianobarInstance.sendline('n') def read(self): self.output = self.pianobarInstance.readline()
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
# 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") child.expect(prompt) has_linearity_coeffs = re.match("1", child.before) # used to SET laser power in mW
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) has_linearity_coeffs = re.match("1", child.before) ################################################################################ # run the test iterations
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) return len(states) def pop(self): "rollback to a checkpoint created by push" self.cancel(self.states_stack.pop()) def pop_n(self, cnt): states = [] for i in range(cnt): states.append(self.states_stack[-1].pop()) self.cancel(states) 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()