def do_pager(text): pagers = ["less","more"] shell = False from os import environ # Hack for MS Windows command line (i.e. non CygWin) environment # When 'shell' is true, Windows aborts the calling program if executable # not found. # When 'shell' is false, an exception is raised, invoking the fallback # 'print' instead of the pager. # We risk assuming that "more" will always be available on a stock # Windows installation. if sys.platform.startswith("win") and 'HOME' not in environ: shell = True pagers = ["more"] if 'PAGER' in environ and environ['PAGER'] != pagers[0]: pagers = [environ['PAGER']] + pagers for pager in pagers: end = "" if pager == "less" else "\n(end of text)\n" try: from subprocess import Popen, PIPE, STDOUT p = Popen([pager], stdin=PIPE, shell=shell) except: pass else: p.communicate(text+end+"\n") msg_r("\r") break else: Msg(text+end)
def usr_rand(self,num_chars): if opt.usr_random: self.interactive() self.send('\n') else: rand_chars = list(getrandstr(num_chars,no_space=True)) vmsg_r('SEND ') while rand_chars: ch = rand_chars.pop(0) msg_r(yellow(ch)+' ' if opt.verbose else '+') ret = self.expect('left: ',ch,delay=0.005) self.expect('ENTER to continue: ','\n')
def usr_rand(self, num_chars): if opt.usr_random: self.interactive() self.send('\n') else: rand_chars = list(getrandstr(num_chars, no_space=True)) vmsg_r('SEND ') while rand_chars: ch = rand_chars.pop(0) msg_r(yellow(ch) + ' ' if opt.verbose else '+') ret = self.expect('left: ', ch, delay=0.005) self.expect('ENTER to continue: ', '\n')
def _get_keypress_unix_raw(prompt="",immed_chars="",prehold_protect=None): msg_r(prompt) fd = sys.stdin.fileno() old = termios.tcgetattr(fd) tty.setcbreak(fd) ch = sys.stdin.read(1) termios.tcsetattr(fd, termios.TCSADRAIN, old) return ch
def expect(self,s,t='',delay=None,regex=False,nonl=False,silent=False): delay = delay or (0,0.3)[bool(opt.buf_keypress)] if not silent: if opt.verbose: msg_r('EXPECT ' + yellow(str(s))) elif not opt.exact_output: msg_r('+') try: if s == '': ret = 0 else: f = (self.p.expect_exact,self.p.expect)[bool(regex)] ret = f(s,self.timeout) except pexpect.TIMEOUT: if opt.debug_pexpect: raise m1 = red('\nERROR. Expect {!r} timed out. Exiting\n'.format(s)) m2 = 'before: [{}]\n'.format(self.p.before) m3 = 'sent value: [{}]'.format(self.sent_value) if self.sent_value != None else '' rdie(1,m1+m2+m3) debug_pexpect_msg(self.p) if opt.verbose and type(s) != str: msg_r(' ==> {} '.format(ret)) if ret == -1: rdie(1,'Error. Expect returned {}'.format(ret)) else: if t == '': if not nonl and not silent: vmsg('') else: self.send(t,delay,s) return ret
def _get_keypress_mswin(prompt="",immed_chars="",prehold_protect=True): msg_r(prompt) timeout = float(0.5) while True: if msvcrt.kbhit(): ch = msvcrt.getch() if ord(ch) == 3: raise KeyboardInterrupt if immed_chars == "ALL" or ch in immed_chars: return ch if immed_chars == "ALL_EXCEPT_ENTER" and not ch in "\n\r": return ch hit_time = time.time() while True: if msvcrt.kbhit(): break if float(time.time() - hit_time) > timeout: return ch
def _get_keypress_unix(prompt="",immed_chars="",prehold_protect=True): msg_r(prompt) timeout = float(0.3) fd = sys.stdin.fileno() old = termios.tcgetattr(fd) tty.setcbreak(fd) while True: # Protect against held-down key before read() key = select([sys.stdin], [], [], timeout)[0] ch = sys.stdin.read(1) if prehold_protect: if key: continue if immed_chars == "ALL" or ch in immed_chars: break if immed_chars == "ALL_EXCEPT_ENTER" and not ch in "\n\r": break # Protect against long keypress key = select([sys.stdin], [], [], timeout)[0] if not key: break termios.tcsetattr(fd, termios.TCSADRAIN, old) return ch
def run_test(self,name,ut): msg_r('Testing MnemonicEntry methods...') from mmgen.mn_entry import mn_entry msg_r('\nTesting computed wordlist constants...') usl = {} for wl_id in self.vectors: for j,k in (('uniq_ss_len','usl'),('shortest_word','sw'),('longest_word','lw')): a = getattr(mn_entry(wl_id),j) b = self.vectors[wl_id][k] assert a == b, '{}:{} {} != {}'.format(wl_id,j,a,b) msg('OK') msg_r('Testing idx()...') qmsg('') junk = 'a g z aa gg zz aaa ggg zzz aaaa gggg zzzz aaaaaaaaaaaaaa gggggggggggggg zzzzzzzzzzzzzz' for wl_id in self.vectors: m = mn_entry(wl_id) qmsg('Wordlist: '+wl_id) for entry_mode in ('full','short'): for a,word in enumerate(m.wl): b = m.idx(word,entry_mode) assert a == b, '{} != {} ({!r} - entry mode: {!r})'.format(a,b,word,entry_mode) a = None for word in junk.split(): b = m.idx(word,entry_mode) assert a == b, '{} != {} ({!r} - entry mode: {!r})'.format(a,b,word,entry_mode) if 'idx_minimal' in self.vectors[wl_id]: for vec in self.vectors[wl_id]['idx_minimal']: chk = vec[1] b = m.idx(vec[0],'minimal') if chk is False: assert b is None, (b,None) elif chk is None: assert type(b) == tuple, (type(b),tuple) elif type(chk) is int: assert b == chk, (b,chk) msg('OK') return True
def expect(self, s, t='', delay=None, regex=False, nonl=False, silent=False): delay = delay or (0, 0.3)[bool(opt.buf_keypress)] if not silent: if opt.verbose: msg_r('EXPECT ' + yellow(str(s))) elif not opt.exact_output: msg_r('+') try: if s == '': ret = 0 else: f = (self.p.expect_exact, self.p.expect)[bool(regex)] ret = f(s, self.timeout) except pexpect.TIMEOUT: if opt.debug_pexpect: raise m1 = red('\nERROR. Expect {!r} timed out. Exiting\n'.format(s)) m2 = 'before: [{}]\n'.format(self.p.before) m3 = 'sent value: [{}]'.format( self.sent_value) if self.sent_value != None else '' rdie(1, m1 + m2 + m3) debug_pexpect_msg(self.p) if opt.verbose and type(s) != str: msg_r(' ==> {} '.format(ret)) if ret == -1: rdie(1, 'Error. Expect returned {}'.format(ret)) else: if t == '': if not nonl and not silent: vmsg('') else: self.send(t, delay, s) return ret
elif len(cmd_args) > 1: opt.opts.usage(opts_data) if opt.system: sys.path.pop(0) from mmgen.addr import test_for_keyconv if not test_for_keyconv(silent=True): msg( "To run this test, you must install 'keyconv' from the vanitygen package.") sys.exit(1) msg(green("Comparing {}'s internally generated addresses against output of 'keyconv'").format(g.proj_name)) from subprocess import check_output for i in range(1,rounds+1): msg_r("\rRound %s/%s " % (i,rounds)) sec = hexlify(os.urandom(32)) wif = hextowif(sec) a = privnum2addr(int(sec,16)) vmsg("\nkey: %s\naddr: %s\n" % (wif,a)) b = check_output(["keyconv", wif]).split()[1] if a != b: msg_r(red("\nERROR: Addresses do not match!")) msg(""" sec key: {} WIF key: {} {pnm}: {} keyconv: {} """.format(sec,wif,a,b,pnm=g.proj_name).rstrip()) sys.exit(3)
def _get_keypress_mswin_raw(prompt="",immed_chars="",prehold_protect=None): msg_r(prompt) ch = msvcrt.getch() if ord(ch) == 3: raise KeyboardInterrupt return ch
def opt_display(key,val='',beg="For selected",end=":\n"): s = "%s=%s" % (fmt_opt(key),val) if val else fmt_opt(key) msg_r("%s option '%s'%s" % (beg,s,end))