def write_to_file(self): d = self.ssdata self._format() compare_or_die(d.target_data_len, 'target data length', len(self.fmt_data), 'length of formatted ' + self.desc) k = ('output', 'input')[self.op == 'pwchg_new'] fn, d.hincog_offset = self._get_hincog_params(k) if opt.outdir and not os.path.dirname(fn): fn = os.path.join(opt.outdir, fn) check_offset = True try: os.stat(fn) except: if keypress_confirm( "Requested file '{}' does not exist. Create?".format(fn), default_yes=True): min_fsize = d.target_data_len + d.hincog_offset msg(self.msg['choose_file_size'].format(min_fsize)) while True: fsize = parse_bytespec(my_raw_input('Enter file size: ')) if fsize >= min_fsize: break msg('File size must be an integer no less than {}'.format( min_fsize)) from mmgen.tool import MMGenToolCmd MMGenToolCmd().rand2file(fn, str(fsize)) check_offset = False else: die(1, 'Exiting at user request') from mmgen.filename import Filename f = Filename(fn, ftype=type(self), write=True) dmsg('{} data len {}, offset {}'.format(capfirst(self.desc), d.target_data_len, d.hincog_offset)) if check_offset: self._check_valid_offset(f, 'write') if not opt.quiet: confirm_or_raise('', "alter file '{}'".format(f.name)) flgs = os.O_RDWR | os.O_BINARY if g.platform == 'win' else os.O_RDWR fh = os.open(f.name, flgs) os.lseek(fh, int(d.hincog_offset), os.SEEK_SET) os.write(fh, self.fmt_data) os.close(fh) msg("{} written to file '{}' at offset {}".format( capfirst(self.desc), f.name, d.hincog_offset))
def token_transfer_ops(self, op, amt=1000): self.spawn('', msg_only=True) sid = dfl_sid from mmgen.tool import MMGenToolCmd usr_mmaddrs = ['{}:E:{}'.format(sid, i) for i in (11, 21)] usr_addrs = [ MMGenToolCmd().gen_addr(addr, dfl_words_file) for addr in usr_mmaddrs ] self._rpc_init() from mmgen.altcoins.eth.contract import Token from mmgen.altcoins.eth.tx import EthereumMMGenTX as etx def do_transfer(): for i in range(2): tk = Token( self.read_from_tmpfile('token_addr{}'.format(i + 1)).strip()) imsg_r('\n' + tk.info()) imsg('dev token balance (pre-send): {}'.format( tk.balance(dfl_addr))) imsg('Sending {} {} to address {} ({})'.format( amt, g.coin, usr_addrs[i], usr_mmaddrs[i])) from mmgen.obj import ETHAmt txid = tk.transfer(dfl_addr, usr_addrs[i], amt, dfl_privkey, start_gas=ETHAmt(60000, 'wei'), gasPrice=ETHAmt(8, 'Gwei')) assert etx.get_exec_status( txid, True) != 0, 'Transfer of token funds failed. Aborting' def show_bals(): for i in range(2): tk = Token( self.read_from_tmpfile('token_addr{}'.format(i + 1)).strip()) imsg('Token: {}'.format(tk.symbol())) imsg('dev token balance: {}'.format(tk.balance(dfl_addr))) imsg('usr token balance: {} ({} {})'.format( tk.balance(usr_addrs[i]), usr_mmaddrs[i], usr_addrs[i])) silence() if op == 'show_bals': show_bals() elif op == 'do_transfer': do_transfer() end_silence() return 'ok'
def tool_rand2file(self): outfile = os.path.join(self.tmpdir, 'rand2file.out') from mmgen.tool import MMGenToolCmd tu = MMGenToolCmd() for nbytes in ('1', '1023', '1K', '1048575', '1M', '1048577', '123M'): t = self.spawn('mmgen-tool', [ '-d', self.tmpdir, '-r0', 'rand2file', 'rand2file.out', nbytes ], extra_desc='({} byte{})'.format( nbytes, suf(tu.bytespec(nbytes)))) t.expect('random data written to file') t.read() t.p.wait() t.ok() t.skip_ok = True return t