[cli] Add dylibs check when downloading the cli

pull/2/head
Daniel Van Der Maden 5 years ago
parent 65e865e7f0
commit 964bfab536
  1. 65
      pyhmy/cli.py

@ -57,6 +57,7 @@ import requests
from .util import get_bls_build_variables, get_gopath from .util import get_bls_build_variables, get_gopath
_dylibs = {"libbls384_256.dylib", "libcrypto.1.0.0.dylib", "libgmp.10.dylib", "libgmpxx.4.dylib", "libmcl.dylib"}
_accounts = {} # Internal accounts keystore, make sure to sync when needed. _accounts = {} # Internal accounts keystore, make sure to sync when needed.
_account_keystore_path = "~/.hmy/account-keys" # Internal path to account keystore, will match the current binary. _account_keystore_path = "~/.hmy/account-keys" # Internal path to account keystore, will match the current binary.
_binary_path = "hmy" # Internal binary path. _binary_path = "hmy" # Internal binary path.
@ -321,37 +322,45 @@ def download(path="./bin/hmy", replace=True, verbose=True):
:returns the environment to run the saved CLI binary. :returns the environment to run the saved CLI binary.
""" """
path = os.path.realpath(path) path = os.path.realpath(path)
assert not os.path.isdir(path), f"path `{path}` must specify a file, NOT a directory." parent_dir = Path(path).parent
if os.path.exists(path) and not replace: assert os.path.isfile(path), f"path `{path}` must specify a file."
return
old_cwd = os.getcwd() if not os.path.exists(path) or replace:
os.makedirs(Path(path).parent, exist_ok=True) old_cwd = os.getcwd()
os.chdir(Path(path).parent) os.makedirs(parent_dir, exist_ok=True)
cwd = os.path.realpath(os.getcwd()) os.chdir(parent_dir)
hmy_script_path = os.path.join(parent_dir, "hmy.sh")
hmy_script_path = os.path.join(cwd, "hmy.sh") with open(hmy_script_path, 'w') as f:
with open(hmy_script_path, 'w') as f: f.write(requests.get("https://raw.githubusercontent.com/harmony-one/go-sdk/master/scripts/hmy.sh")
f.write(requests.get("https://raw.githubusercontent.com/harmony-one/go-sdk/master/scripts/hmy.sh") .content.decode())
.content.decode()) os.chmod(hmy_script_path, os.stat(hmy_script_path).st_mode | stat.S_IEXEC)
os.chmod(hmy_script_path, os.stat(hmy_script_path).st_mode | stat.S_IEXEC) same_name_file = False
if os.path.exists(os.path.join(cwd, "hmy")): # Save same name file. if os.path.exists(os.path.join(parent_dir, "hmy")) and Path(path).name != "hmy": # Save same name file.
os.rename(os.path.join(cwd, "hmy"), os.path.join(cwd, ".hmy_tmp")) same_name_file = True
if verbose: os.rename(os.path.join(parent_dir, "hmy"), os.path.join(parent_dir, ".hmy_tmp"))
subprocess.call([hmy_script_path, '-d']) if verbose:
else: subprocess.call([hmy_script_path, '-d'])
subprocess.call([hmy_script_path, '-d'], stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT) else:
os.rename(os.path.join(cwd, "hmy"), path) subprocess.call([hmy_script_path, '-d'], stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT)
if os.path.exists(os.path.join(cwd, ".hmy_tmp")): os.rename(os.path.join(parent_dir, "hmy"), path)
os.rename(os.path.join(cwd, ".hmy_tmp"), os.path.join(cwd, "hmy")) if same_name_file:
if verbose: os.rename(os.path.join(parent_dir, ".hmy_tmp"), os.path.join(parent_dir, "hmy"))
print(f"Saved harmony binary to: `{path}`") if verbose:
print(f"Saved harmony binary to: `{path}`")
os.chdir(old_cwd)
env = os.environ.copy() env = os.environ.copy()
if sys.platform.startswith("linux"): files_in_parent_dir = set(os.listdir(parent_dir))
env["LD_LIBRARY_PATH"] = cwd if files_in_parent_dir.intersection(_dylibs) == _dylibs:
if sys.platform.startswith("linux"):
env["LD_LIBRARY_PATH"] = parent_dir
else:
env["DYLD_FALLBACK_LIBRARY_PATH"] = parent_dir
elif os.path.exists(f"{get_gopath()}/src/github.com/harmony-one/bls") \
and os.path.exists(f"{get_gopath()}/src/github.com/harmony-one/mcl"):
env.update(get_bls_build_variables)
else: else:
env["DYLD_FALLBACK_LIBRARY_PATH"] = cwd raise RuntimeWarning(f"Could not get environment for downloaded hmy CLI at `{path}`")
os.chdir(old_cwd)
return env return env

Loading…
Cancel
Save