Use tmpdir while processing

This commit is contained in:
nolash 2021-06-02 16:49:53 +02:00
parent fa658f3fab
commit c47490705c
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -7,6 +7,7 @@ import uuid
import sys import sys
import tempfile import tempfile
import stat import stat
import shutil
# external imports # external imports
import csv import csv
@ -98,11 +99,11 @@ gas_oracle = OverrideGasOracle(limit=config.get('_GAS_LIMIT'), conn=rpc)
chain_spec = ChainSpec.from_chain_str(config.get('SESSION_CHAIN_SPEC')) chain_spec = ChainSpec.from_chain_str(config.get('SESSION_CHAIN_SPEC'))
try: try:
os.makedirs(config.get('_OUTPUT_DIR')) os.stat(config.get('_OUTPUT_DIR'))
logg.info('output dir {} created'.format(config.get('_OUTPUT_DIR')))
except FileExistsError:
sys.stderr.write('output directory {} already exists\n'.format(config.get('_OUTPUT_DIR'))) sys.stderr.write('output directory {} already exists\n'.format(config.get('_OUTPUT_DIR')))
sys.exit(1) sys.exit(1)
except FileNotFoundError:
pass
class InvalidData(Exception): class InvalidData(Exception):
@ -133,6 +134,7 @@ def validate(row):
def main(): def main():
tmp_out = tempfile.mkdtemp() tmp_out = tempfile.mkdtemp()
logg.debug('using tmp dir {}'.format(tmp_out))
f = open(config.get('_INPUT_FILE'), 'r') f = open(config.get('_INPUT_FILE'), 'r')
cr = csv.reader(f) cr = csv.reader(f)
@ -144,18 +146,22 @@ def main():
token = validate(row) token = validate(row)
except InvalidData as e: except InvalidData as e:
sys.stderr.write(str(e) + ' in line {}\n'.format(i)) sys.stderr.write(str(e) + ' in line {}\n'.format(i))
shutil.rmtree(tmp_out)
sys.exit(1) sys.exit(1)
multiplier = 10 ** token.decimals multiplier = 10 ** token.decimals
value = int(multiplier * float(row[2])) value = int(multiplier * float(row[2]))
(tx_hash_hex, o) = erc20.transfer(row[1], signer_address, row[0], value, tx_format=TxFormat.RLP_SIGNED) (tx_hash_hex, o) = erc20.transfer(row[1], signer_address, row[0], value, tx_format=TxFormat.RLP_SIGNED)
fname = '{}_{}'.format(i, row[0]) fname = '{}_{}'.format(i, row[0])
fpath = os.path.join(config.get('_OUTPUT_DIR'), fname) fpath = os.path.join(tmp_out, fname)
f = open(fpath, 'x') f = open(fpath, 'x')
f.write(o) f.write(o)
f.close() f.close()
logg.info('tx {}: {} ({} * 10^{}) {} {} -> {}'.format(tx_hash_hex, value, row[2], token.decimals, token.symbol, signer_address, row[0])) logg.info('tx {}: {} ({} * 10^{}) {} {} -> {}'.format(tx_hash_hex, value, row[2], token.decimals, token.symbol, signer_address, row[0]))
i += 1 i += 1
f.close() f.close()
shutil.copytree(tmp_out, config.get('_OUTPUT_DIR'))
logg.debug('files moved from tmp dir {} to {}'.format(tmp_out, config.get('_OUTPUT_DIR')))
shutil.rmtree(tmp_out)
if __name__ == '__main__': if __name__ == '__main__':