From 9ae198f0f6fd258e712acdabff26d7c12ff7b4f8 Mon Sep 17 00:00:00 2001 From: William Luke Date: Tue, 1 Mar 2022 17:06:01 +0300 Subject: [PATCH] chore: download contracts to tmp dir --- cic/cmd/wizard.py | 3 ++- cic/contract/helpers.py | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/cic/cmd/wizard.py b/cic/cmd/wizard.py index 3b6f6b7..3668c9f 100644 --- a/cic/cmd/wizard.py +++ b/cic/cmd/wizard.py @@ -56,7 +56,8 @@ def execute(config: Config, eargs: ExtraArgs): skip_gen = eargs.skip_gen skip_deploy = eargs.skip_deploy wallet_keystore = eargs.y - config.add(wallet_keystore, "WALLET_KEY_FILE", exists_ok=True) + if wallet_keystore: + config.add(wallet_keystore, "WALLET_KEY_FILE", exists_ok=True) if not skip_gen: contract = generate_contract(directory, [target], config, interactive=True) diff --git a/cic/contract/helpers.py b/cic/contract/helpers.py index 3ec71b5..93c41c5 100644 --- a/cic/contract/helpers.py +++ b/cic/contract/helpers.py @@ -5,6 +5,8 @@ import sys import json import requests import importlib +import tempfile +import hashlib # local imports from cic.writers import OutputWriter @@ -21,17 +23,24 @@ CONTRACTS = [ "url": "https://gitlab.com/cicnet/erc20-demurrage-token/-/raw/master/python/erc20_demurrage_token/data/DemurrageTokenSingleNocap", "name": "Demurrage Token Single No Cap", }, + { + "url":"https://gitlab.com/cicnet/erc20-demurrage-token/-/raw/lash/gas-safety-valve/python/erc20_demurrage_token/data/DemurrageTokenSingleNocap", + "name": "Demurrage Token Single No Cap (gas-safety-valve)", + } ] # Download File from Url -def download_file(url: str, directory: str, filename=None) -> (str, bytes): - os.makedirs(directory, exist_ok=True) +def download_file(url: str, filename=None) -> (str, bytes): + directory = tempfile.gettempdir() filename = filename if filename else url.split("/")[-1] - path = os.path.join(directory, filename) + hash_object = hashlib.md5(url.encode()) + path = os.path.join(directory, hash_object.hexdigest()) + log.debug(f"Downloading {filename} to {path}") if not os.path.exists(path): log.debug(f"Downloading {filename}") r = requests.get(url, allow_redirects=True) - open(path, "wb").write(r.content) + with open(path, "wb") as f: + f.write(r.content) return path return path @@ -50,15 +59,14 @@ def select_contract(): val = input("Select contract (C,0,1..): ") if val.isdigit() and int(val) < len(CONTRACTS): contract = CONTRACTS[int(val)] - directory = f"./contracts/{contract['name']}" - bin_path = os.path.abspath(download_file(contract["url"] + ".bin", directory)) - json_path = download_file(contract["url"] + ".json", directory) + bin_path = os.path.abspath(download_file(contract["url"] + ".bin")) + json_path = download_file(contract["url"] + ".json") elif val == "C": possible_bin_location = input("Enter a path or url to a contract.bin: ") if possible_bin_location.startswith('http'): # possible_bin_location is url - bin_path = download_file(possible_bin_location, directory) + bin_path = download_file(possible_bin_location) else: # possible_bin_location is path if os.path.exists(possible_bin_location):