From 6c044dfcc82f05324d0c20c786239dbf35754e93 Mon Sep 17 00:00:00 2001 From: Stefan Zarembinski Date: Thu, 2 May 2019 11:27:54 +0200 Subject: [PATCH] all tested --- eosfactory/core/config.py | 103 +++++++++++++++++++++----------------- eosfactory/core/teos.py | 101 +++++++++---------------------------- eosfactory/core/utils.py | 85 +++++++++++++++++++++++++++++-- 3 files changed, 162 insertions(+), 127 deletions(-) diff --git a/eosfactory/core/config.py b/eosfactory/core/config.py index 39543374..4dbf7457 100644 --- a/eosfactory/core/config.py +++ b/eosfactory/core/config.py @@ -26,7 +26,6 @@ CONFIG_JSON = "config.json" CONTRACTS_DIR = "contracts/" TEMPLATE_DIR = ("TEMPLATE_DIR", "templates/contracts") -EOSIO_CPP_DIR = "/usr/opt/eosio.cdt/0.0.0/" eosfactory_data_ = ("EOSFACTORY_DATA_DIR", [os.path.expandvars("${HOME}/.local/" + EOSFACTORY_DIR),\ @@ -58,7 +57,8 @@ ["nodeos","/usr/bin/nodeos", "/usr/local/bin/nodeos"]) eosio_cpp_ = ("EOSIO_CPP", ["eosio-cpp", "/usr/bin/eosio-cpp", "/usr/local/bin/eosio-cpp"]) -eosio_cpp_dir_ = ("EOSIO_CPP_DIR", [EOSIO_CPP_DIR]) +eosio_cdt_root_ = ("EOSIO_CDT_ROOT", + ["/usr/opt/eosio.cdt/0.0.0/", "/usr/local/Cellar/eosio.cdt/0.0.0"]) eosio_cpp_includes_ = ( "EOSIO_CPP_INCLUDES", [["include", "include/libcxx", "include/eosiolib/core", \ @@ -207,23 +207,23 @@ def set(contract_workspace_dir): def config_dir(): - dir = os.path.join(eosfactory_data(), CONFIG_DIR) - if not os.path.exists(dir): + path = os.path.join(eosfactory_data(), CONFIG_DIR) + if not os.path.exists(path): raise errors.Error(''' Cannot find the configuration directory {} - '''.format(dir), translate=False) - return dir + '''.format(path), translate=False) + return path def template_dir(): - dir = os.path.join(eosfactory_data(), TEMPLATE_DIR[1]) - if not os.path.exists(dir): + path = os.path.join(eosfactory_data(), TEMPLATE_DIR[1]) + if not os.path.exists(path): raise errors.Error(''' Cannot find the template directory {} - '''.format(dir), translate=False) - return dir + '''.format(path), translate=False) + return path def eoside_includes_dir(): @@ -233,12 +233,12 @@ def eoside_includes_dir(): *INCLUDE* entry in the *config.json* file, see :func:`.current_config`. ''' - dir = includes_[1] - if not os.path.isabs(dir): - dir = os.path.join(eosfactory_data(), includes_[1]) - if not os.path.exists(dir): - dir = None - return dir + path = includes_[1] + if not os.path.isabs(path): + path = os.path.join(eosfactory_data(), includes_[1]) + if not os.path.exists(path): + path = None + return path def eoside_libs_dir(): @@ -248,12 +248,12 @@ def eoside_libs_dir(): *LIBS* entry in the *config.json* file, see :func:`.current_config`. ''' - dir = libs_[1] - if not os.path.isabs(dir): - dir = os.path.join(eosfactory_data(), libs_[1]) - if not os.path.exists(dir): - dir = None - return dir + path = libs_[1] + if not os.path.isabs(path): + path = os.path.join(eosfactory_data(), libs_[1]) + if not os.path.exists(path): + path = None + return path def contract_workspace_dir(dont_set_workspace=False): @@ -390,9 +390,9 @@ def nodeos_config_dir(): *LOCAL_NODE_CONFIG_DIR* entry in the *config.json* file, see :func:`.current_config`. ''' - dir = first_valid_path(config_dir_, raise_error=False) - if dir: - return dir + path = first_valid_path(config_dir_, raise_error=False) + if path: + return path return config_dir() @@ -587,13 +587,18 @@ def eosio_cpp_version(): return ["", EOSIO_CDT_VERSION] -def eosio_cpp_dir(): +def eosio_cdt_root(): '''The path to the *eosio-cpp* installation directory. The setting may be changed with *EOSIO_CPP* entry in the *config.json* file, see :func:`.current_config`. ''' + # find /usr -wholename "*/eosio.cdt/1.6.1" + config_json = config_map() + if eosio_cdt_root_[0] in config_json and config_json[eosio_cdt_root_[0]]: + return config_json[eosio_cdt_root_[0]] + eosio_cpp_version_ = eosio_cpp_version() if not eosio_cpp_version_: raise errors.Error( @@ -602,20 +607,25 @@ def eosio_cpp_dir(): ''') version_pattern = re.compile(".+/eosio\.cdt/(\d\.\d\.\d)/$") - dir = eosio_cpp_dir_[1][0] - if not version_pattern.match(dir): - raise errors.Error( - ''' -The assumed pattern -{} -does not match the directory template 'core.config.EOSIO_CPP_DIR' -{} - '''.format(version_pattern, EOSIO_CPP_DIR), translate=False - ) - dir = dir.replace( - re.findall(version_pattern, dir)[0], eosio_cpp_version_[0]) + tested = [] + for path in eosio_cdt_root_[1]: + tested.append(path) + if version_pattern.match(path): + path = path.replace( + re.findall(version_pattern, path)[0], eosio_cpp_version_[0]) + if(os.path.exists(path)): + return path - return dir + msg = "Cannot determine the installation directory of 'eosio-cdt. Tried:" + for path in tested: + msg = '''{} + {} + '''.format(msg, path) + msg = '''{} +Define it in the config file +{} + '''.format(msg, config_file()) + raise errors.Error(msg, translate=False) def eosio_cpp_includes(): @@ -625,9 +635,9 @@ def eosio_cpp_includes(): file, see :func:`.current_config`. ''' list = [] - dir = eosio_cpp_dir() + path = eosio_cdt_root() for include in eosio_cpp_includes_[1][0]: - list.append(dir + include) + list.append(path + include) return list @@ -1064,7 +1074,7 @@ def update_eosio_cpp_includes(c_cpp_properties_path, root=""): if re.findall(dir_pattern, c_cpp_properties): new = c_cpp_properties.replace(re.findall( - dir_pattern, c_cpp_properties)[0], eosio_cpp_dir()) + dir_pattern, c_cpp_properties)[0], eosio_cdt_root()) if not new == c_cpp_properties: with open(c_cpp_properties_path,'w') as f: f.write(new) @@ -1082,12 +1092,13 @@ def installation_dependencies(config_map): '''Verify whether 'eosio' and 'eosio.cpp' packages are properly installed. ''' eosio_version_ = config_map["EOSIO_VERSION"] - if eosio_version_: + if eosio_version_ and eosio_version_[0]: if len(eosio_version_) > 1: print('''NOTE! The version of the installed 'eosio' package is {} while EOSFactory was tested with version {} - '''.format(eosio_version_[0], eosio_version_[1])) + '''.format( + eosio_version_[0], eosio_version_[1])) else: print('''Cannot determine the version of the installed 'eosio' package as 'nodeos' does not response. ''') @@ -1184,9 +1195,9 @@ def current_config(contract_dir=None, dont_set_workspace=False): except: map[eosio_cpp_[0]] = None try: - map[eosio_cpp_dir_[0]] = eosio_cpp_dir() + map[eosio_cdt_root_[0]] = eosio_cdt_root() except: - map[eosio_cpp_dir_[0]] = None + map[eosio_cdt_root_[0]] = None try: map[eosio_cpp_includes_[0]] = eosio_cpp_includes() except: diff --git a/eosfactory/core/teos.py b/eosfactory/core/teos.py index 6507ae60..c3588e7f 100644 --- a/eosfactory/core/teos.py +++ b/eosfactory/core/teos.py @@ -269,8 +269,8 @@ def build( print("######## command line sent to eosio-cpp:") print(" ".join(command_line)) - eosio_cpp(command_line, build_dir) - + utils.long_process(command_line, build_dir, is_verbose=True, + prompt="eosio-cpp") if not compile_only: if "wasm" in target_path: logger.TRACE(''' @@ -502,107 +502,54 @@ def get_pid(name=None): return [int(pid) for pid in stdout.split()] -def eosio_cpp(command_line, build_dir): - stop = False - PERIOD = 2 - def thread_function(name): - while True: - print(".", end="", flush=True) - time.sleep(PERIOD) - if stop: - break - - cwd = os.path.join(build_dir, "cwd") - if os.path.exists(cwd): - try: - shutil.rmtree(cwd) - except Exception as e: - raise errors.Error(''' -Cannot remove the directory {}. -error message: -============== -{} - '''.format(cwd, str(e))) - os.mkdir(cwd) - - threading.Thread(target=thread_function, args=(1,)).start() - p = subprocess.run( - command_line, - cwd=cwd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - - stop = True - time.sleep(PERIOD) - print() - stdout = p.stdout.decode("ISO-8859-1") - stderr = p.stderr.decode("ISO-8859-1") - returncode = p.returncode - print(stdout) - shutil.rmtree(cwd) - - if returncode: - raise errors.Error(''' -command line: -============= -{} - -error message: -============== -{} - '''.format(" ".join(command_line), stderr)) - - return returncode - - def get_target_dir(source_dir): - dir = os.path.join(source_dir, "build") - if os.path.exists(dir): - return dir + path = os.path.join(source_dir, "build") + if os.path.exists(path): + return path - dir = os.path.join(source_dir, "..", "build") - if os.path.exists(dir): - return dir + path = os.path.join(source_dir, "..", "build") + if os.path.exists(path): + return path try: - os.mkdir(dir) + os.mkdir(path) except Exception as e: raise errors.Error(str(e)) - return dir + return path def get_recardian_dir(source_dir): - dir = os.path.join(source_dir, "..", "ricardian") - if os.path.exists(dir): - return dir + path = os.path.join(source_dir, "..", "ricardian") + if os.path.exists(path): + return path - dir = os.path.join(source_dir, "ricardian") - if not os.path.exists(dir): + path = os.path.join(source_dir, "ricardian") + if not os.path.exists(path): try: - os.mkdir(dir) + os.mkdir(path) except Exception as e: raise errors.Error(str(e)) - return dir + return path def get_include_dir(source_dir): - dir = os.path.join(source_dir, "..", "include") - if os.path.exists(dir): - return dir + path = os.path.join(source_dir, "..", "include") + if os.path.exists(path): + return path - dir = os.path.join(source_dir, "include") - if not os.path.exists(dir): + path = os.path.join(source_dir, "include") + if not os.path.exists(path): try: - os.mkdir(dir) + os.mkdir(path) except Exception as e: raise errors.Error(str(e)) - return dir + return path def args(clear=False): diff --git a/eosfactory/core/utils.py b/eosfactory/core/utils.py index bf18bf9b..f1604aee 100644 --- a/eosfactory/core/utils.py +++ b/eosfactory/core/utils.py @@ -1,3 +1,9 @@ +import time +import os +import shutil +import threading +import subprocess + import eosfactory.core.errors as errors def wslMapLinuxWindows(path, back_slash=True): @@ -24,9 +30,7 @@ def heredoc(message): return message -def spawn( - command_line, error_message='', shell=False, raise_exception=True): - import subprocess +def spawn(command_line, error_message='', shell=False, raise_exception=True): stdout = None stderr = None try: @@ -74,4 +78,77 @@ def is_windows_ubuntu(): def which(file_path): - return spawn("which {}".format(file_path), shell=True) \ No newline at end of file + return spawn("which {}".format(file_path), shell=True) + + +def long_process(command_line, build_dir=None, is_verbose=True, prompt=None, + shell=False): + stop = False + PERIOD = 2 + + def thread_function(): + if prompt: + print("{}: ".format(prompt), end="", flush=True) + while True: + print(".", end="", flush=True) + time.sleep(PERIOD) + if stop: + break + + cwd = None + if build_dir: + cwd = os.path.join(build_dir, "cwd") + if os.path.exists(cwd): + try: + shutil.rmtree(cwd) + except Exception as e: + raise errors.Error(''' +Cannot remove the directory {}. +error message: +============== +{} + '''.format(cwd, str(e))) + os.mkdir(cwd) + + threading.Thread(target=thread_function).start() + p = subprocess.run( + command_line, + cwd=cwd, + shell=shell, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + stop = True + time.sleep(PERIOD) + print() + stdout = p.stdout.decode("ISO-8859-1") + stderr = p.stderr.decode("ISO-8859-1") + returncode = p.returncode + if is_verbose: + print(stdout) + if cwd: + shutil.rmtree(cwd) + + if returncode: + raise errors.Error(''' +command line: +============= +{} + +error message: +============== +{} + '''.format(" ".join(command_line), stderr)) + + return p + + +def locate(start_dir, partial_path): + cl = ["find", start_dir, "-wholename", '"*{}"'.format(partial_path)] + p = long_process(" ".join(cl), None, False, + "locating '{}'".format(partial_path), True) + stdout = p.stdout.decode("ISO-8859-1") + if stdout: + return stdout.strip() + + return "" \ No newline at end of file