def get_testnet(alias, testnet=None, reset=False): if not alias and not testnet: return Testnet(reset=reset) if alias: mapping = get_mapping() if alias in mapping: return Testnet(mapping[alias]["url"], mapping[alias]["account_name"], mapping[alias]["owner_key"], mapping[alias]["active_key"]) elif alias == "JUNGLE": return JUNGLE elif alias == "KYLIN": return KYLIN else: logger.ERROR(''' Testnet ``{}`` is not defined in the testnet mapping. '''.format(alias)) elif testnet: return Testnet(testnet[0], testnet[1], testnet[2], testnet[3]) logger.ERROR(''' Cannot determine testnet. ''')
def __init__(self, url=None, account_name=None, owner_key=None, active_key=None, reset=False): if not url: if reset: manager.reset(verbosity=[logger.Verbosity.ERROR]) else: manager.resume(verbosity=[logger.Verbosity.ERROR]) import eosfactory.shell.account as account eosio = account.Eosio("account_master") setup.is_local_address = True account_name = eosio.name owner_key = eosio.owner_key.key_private active_key = eosio.active_key.key_private if not account_name or not owner_key or not active_key: logger.ERROR(''' If the ``url`` is set, the ``account_name`` and keys have to be set, as well. ''') self.url = url self.account_name = account_name self.owner_key = owner_key self.active_key = active_key
def wsl_root(): '''The root directory of the Windows WSL, or empty string if not Windows. The root directory of the Ubuntu file system, owned by the installation, if any, of the Windows Subsystem Linux (WSL). It may be changed with *WSL_ROOT* entry in the *config.json* file, see :func:`.current_config`. ''' if not utils.is_windows_ubuntu(): return "" wsl_root_sh = "wsl_root.sh" wsl_root_sh = os.path.join(eosfactory_data(), wsl_root_sh) if wsl_root_[1][0] is None: path = "" path, error = utils.spawn([wsl_root_sh, path], raise_exception=False) if error: while True: if not os.path.exists(wsl_root_sh): path = "" logger.ERROR(''' Cannot find the bash command: '{}' The intelisense feature of Visual Studio Code will be disabled. '''.format(wsl_root_sh), translate=False) break path = input( logger.error(''' Error message is {} Cannot find the root of the WSL file system which was tried to be '{}' Please, find the path in your computer and enter it. Enter nothing, if you do not care about having efficient the intelisense of Visual Studio Code. '''.format(error, path), translate=False) + "\n<<< ") if not path: break path, error = utils.spawn([wsl_root_sh, path], raise_exception=False) if not error: break path = path.replace("\\", "/") path = path.replace(path[0:2], path[0:2].lower()) wsl_root_[1][0] = path return wsl_root_[1][0]
def get_testnet(name=None, testnet=None, reset=False): '''Return a testnet. Args: name (str): If set, the testnet name, otherwise the local testnet is returned. testnet (tuple): The tuple (<url> <name> <owner key> <active key>) representing a :class:`.Testnet` object, returned if the *name* argument is not set. reset (bool): If both the *name* and *testnet* arguments are not set, determine whether the local node is to be reset. Returns: :class:`.Testnet`: a testnet ''' if not name and not testnet: return Testnet(reset=reset) if name: mapping = manager.read_map(TESTNET_FILE) if name in mapping: return Testnet(mapping[name]["url"], mapping[name]["account_name"], mapping[name]["owner_key"], mapping[name]["active_key"], mapping[name]["name"]) elif name == "JUNGLE": return JUNGLE elif name == "KYLIN": return KYLIN else: logger.ERROR(''' Testnet *{}* is not defined in the testnet mapping. '''.format(name)) elif testnet: return Testnet(testnet[0], testnet[1], testnet[2], testnet[3]) logger.ERROR(''' Cannot determine testnet. ''')
def create_ignore_list_file(contract_dir, is_log_creation=False): ignore_file = os.path.join(contract_dir, config.IGNORE_FILE) if not os.path.exists(ignore_file): if is_log_creation: logger.ERROR(''' There is not any '{}' file in the project directory {} Creating a default one. '''.format(config.IGNORE_FILE, contract_dir)) with open(ignore_file, "w+") as f: f.write("\n".join(config.IGNORE_LIST)) return ignore_file
def read_map(file_name, text_editor="nano"): '''Return json account map Attempt to open the account map file named ``setup.account_map``, located in the wallet directory ``config.keosd_wallet_dir()``, to return its json contents. If the file does not exist, return an empty json. If the file is corrupted, offer editing the file with the ``nano`` linux editor. Return ``None`` if the the offer is rejected. ''' wallet_dir_ = config.keosd_wallet_dir() path = os.path.join(wallet_dir_, file_name) while True: try: # whether the setup map file exists: with open(path, "r") as input_file: return json.load(input_file) except Exception as e: if isinstance(e, FileNotFoundError): return {} else: logger.ERROR(''' The json file {} is misformed. The error message is: {} Do you want to edit the file? '''.format(str(path), str(e)), translate=False) answer = input("y/n <<< ") if answer == "y": utils.spawn([text_editor, path]) continue else: raise errors.Error(''' Use the function 'manager.edit_account_map()' to edit the file. ''', translate=False)
def on_nodeos_error(clear=False): ERROR_WAIT_TIME = 5 NOT_ERROR = [ "exit shutdown", "configuration items in the config.ini file are redundantly", ] node_stop() args_ = args(clear) args_.insert(0, config.node_exe()) command_line = " ".join(args_) logger.ERROR(''' The local 'nodeos' failed to start few times in sequence. Perhaps, something is wrong with configuration of the system. See the command line issued: ''') print("\n{}\n".format(command_line)) print(''' Now, see the result of execution of the command line: ''') def runInThread(): proc = subprocess.Popen( " ".join(args_), stdin=subprocess.DEVNULL, stdout=std_out_handle, stderr=subprocess.PIPE, shell=True) out, err = proc.communicate() err_msg = err.decode("ISO-8859-1") not_error = False if err_msg: for item in NOT_ERROR: if item in err_msg: not_error = True break if not_error: print( ''' Just another hang incident of the 'nodeos' executable.''') if clear: print( ''' Rerun the script. ''') else: print( ''' Rerun the script with 'nodeos' restarted. ''') else: print(err_msg) thread = threading.Thread(target=runInThread) thread.start() # Wait for the nodeos process to crash for i in (0, int(ERROR_WAIT_TIME)): print(".", end="", flush=True) time.sleep(ERROR_WAIT_TIME) print() # Kill the process: it is stuck, or it is running well. node_stop() exit()
def project_from_template(project_name, template=None, workspace_dir=None, c_cpp_prop_path=None, remove_existing=False, open_vscode=False, throw_exists=False, verbosity=None): '''Given the project name and template name, create a smart contract project. - **parameters**:: project_name: The name of the project, or an existing path to a directory. template: The name of the template used. workspace_dir: If set, the folder for the work-space. Defaults to the value returned by the config.contract_workspace() function. remove_existing: If set, overwrite any existing project. visual_studio_code: If set, open the ``VSCode``, if available. verbosity: The logging configuration. ''' project_name = utils.wslMapWindowsLinux(project_name.strip()) template = template.strip() template_dir = utils.wslMapWindowsLinux(template) if not os.path.isdir(template_dir): template_dir = os.path.join(config.eosf_dir(), TEMPLATE_CONTRACTS_DIR, template) if not os.path.isdir(template_dir): raise errors.Error(''' TemplateCreate '{}' does not exist. '''.format(template_dir)) if not workspace_dir \ or not os.path.isabs(workspace_dir) \ or not os.path.exists(workspace_dir): workspace_dir = config.contract_workspace() workspace_dir = workspace_dir.strip() if c_cpp_prop_path: c_cpp_prop_path = utils.wslMapWindowsLinux(c_cpp_prop_path) if os.path.exists(c_cpp_prop_path): try: with open(c_cpp_prop_path, "r") as input: c_cpp_properties = json.loads(input.read()) except Exception: c_cpp_properties = vscode.c_cpp_properties else: c_cpp_properties = vscode.c_cpp_properties c_cpp_properties = replace_templates(vscode.c_cpp_properties) split = os.path.split(project_name) if os.path.isdir(split[0]): project_dir = project_name project_name = split[1] else: project_dir = os.path.join(workspace_dir, project_name) if os.path.isdir(project_dir): if os.listdir(project_dir): if remove_existing: try: shutil.rmtree(project_dir) except Exception as e: raise errors.Error(str(e)) else: msg = ''' NOTE: Contract workspace '{}' already exists. Cannot overwrite it. '''.format(project_dir) if throw_exists: raise errors.Error(msg) else: logger.ERROR(msg) return try: # make contract directory and its build directory: os.makedirs(os.path.join(project_dir, "build")) except Exception as e: raise errors.Error(str(e)) def copy_dir_contents(project_dir, template_dir, directory, project_name): contents = os.listdir(os.path.join(template_dir, directory)) for item in contents: path = os.path.join(directory, item) template_path = os.path.join(template_dir, path) contract_path = os.path.join( project_dir, path.replace(TEMPLATE_NAME, project_name)) if os.path.isdir(template_path): os.mkdir(contract_path) copy_dir_contents(project_dir, template_dir, path, project_name) elif os.path.isfile(template_path): copy(template_path, contract_path, project_name) def copy(template_path, contract_path, project_name): with open(template_path, "r") as input: template = input.read() if TEMPLATE_HOME in template or TEMPLATE_ROOT in template: home = os.environ["HOME"] root = "" if is_windows_ubuntu(): replace_templates(template) template = template.replace("${" + TEMPLATE_NAME + "}", project_name) template = template.replace(C_CPP_PROP, c_cpp_properties) template = template.replace(TASK_JSON, vscode.TASKS) with open(contract_path, "w") as output: output.write(template) copy_dir_contents(project_dir, template_dir, "", project_name) logger.TRACE( ''' * Contract project '{}' created from template '{}' '''.format(project_name, template_dir), verbosity) if open_vscode: if is_windows_ubuntu(): command_line = "cmd.exe /C code {}".format( utils.wslMapLinuxWindows(project_dir)) elif uname() == "Darwin": command_line = "open -n -b com.microsoft.VSCode --args {}".format( project_dir) else: command_line = "code {}".format(project_dir) os.system(command_line) logger.INFO( ''' ######### Created contract project ``{}``, originated from template ``{}``. '''.format(project_name, template_dir), verbosity) return project_dir
def template_create(project_name, template_dir=None, workspace_dir=None, remove_existing=False, open_vscode=False, throw_exists=False): '''Given the project name and template name, create a smart contract project. ''' project_name = project_name.strip() template_dir = template_dir.strip() template_dir = utils.wslMapWindowsLinux(template_dir) if not template_dir: template_dir = config.DEFAULT_TEMPLATE if not os.path.isdir(template_dir): template_dir = os.path.join(config.eosf_dir(), TEMPLATE_CONTRACTS_DIR, template_dir) if not os.path.isdir(template_dir): raise errors.Error(''' TemplateCreate '{}' does not exist. '''.format(template_dir)) if not workspace_dir \ or not os.path.isabs(workspace_dir) \ or not os.path.exists(workspace_dir): workspace_dir = config.contract_workspace() workspace_dir = workspace_dir.strip() project_name = utils.wslMapWindowsLinux(project_name.strip()) split = os.path.split(project_name) if os.path.isdir(split[0]): project_dir = project_name project_name = split[1] else: project_dir = os.path.join(workspace_dir, project_name) if os.path.isdir(project_dir): if os.listdir(project_dir): if remove_existing: try: shutil.rmtree(project_dir) except Exception as e: raise errors.Error(str(e)) else: msg = ''' NOTE: Contract workspace '{}' already exists. Cannot overwrite it. '''.format(project_dir) if throw_exists: raise errors.Error(msg) else: logger.ERROR(msg) return try: # make contract directory and its build directory: os.makedirs(os.path.join(project_dir, "build")) except Exception as e: raise errors.Error(str(e)) def copy_dir_contents(project_dir, template_dir, directory, project_name): contents = os.listdir(os.path.join(template_dir, directory)) for item in contents: path = os.path.join(directory, item) template_path = os.path.join(template_dir, path) contract_path = os.path.join( project_dir, path.replace(TEMPLATE_NAME, project_name)) if os.path.isdir(template_path): os.mkdir(contract_path) copy_dir_contents(project_dir, template_dir, path, project_name) elif os.path.isfile(template_path): copy(template_path, contract_path, project_name) def copy(template_path, contract_path, project_name): with open(template_path, "r") as input: template = input.read() if TEMPLATE_HOME in template or TEMPLATE_ROOT in template: home = os.environ["HOME"] root = "" eosio_dir = config.eosio_repository_dir() if is_windows_ubuntu(): home = config.wsl_root() + home root = config.wsl_root() eosio_dir = config.wsl_root() + eosio_dir template = template.replace(TEMPLATE_HOME, home) template = template.replace(TEMPLATE_ROOT, root) template = template.replace(TEMPLATE_EOSIO_DIR, eosio_dir) template = template.replace("@" + TEMPLATE_NAME + "@", project_name) with open(contract_path, "w") as output: output.write(template) copy_dir_contents(project_dir, template_dir, "", project_name) logger.TRACE(''' * Contract project '{}' created from template '{}' '''.format(project_name, project_dir)) if open_vscode: if is_windows_ubuntu(): command_line = "cmd.exe /C code {}".format( utils.wslMapLinuxWindows(project_dir)) elif uname() == "Darwin": command_line = "open -n -b com.microsoft.VSCode --args {}".format( project_dir) else: command_line = "code {}".format(project_dir) os.system(command_line) return project_dir