예제 #1
0
def naturalize_path(path):
    path = path.replace(TEMPLATE_HOME, HOME)
    if path.find("/mnt/") != 0:
        path = ROOT + path
    return utils.wslMapLinuxWindows(path, back_slash=False)
예제 #2
0
파일: teos.py 프로젝트: vectar31/eosfactory
def project_from_template(
        project_name, template=None, workspace_dir=None,
        c_cpp_prop_path=None,
        include=None,
        libs=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.
        include: If set, comma-separated list of include folders.
        libs: If set, comma-separated list of libraries.
        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 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 = input.read()
            except Exception:
                c_cpp_properties = vscode.c_cpp_properties()
    else:
        c_cpp_properties = vscode.c_cpp_properties()

    c_cpp_properties = replace_templates(c_cpp_properties)

    if include:
        c_cpp_properties_json = json.loads(c_cpp_properties)
        c_cpp_properties_json[CONFIGURATIONS][0][INCLUDE_PATH].extend(
                                                        include.split(", "))
        c_cpp_properties_json[CONFIGURATIONS][0][BROWSE]["path"].extend(
                                                        include.split(", "))
        c_cpp_properties = json.dumps(c_cpp_properties_json, indent=4)

    if libs:
        c_cpp_properties_json = json.loads(c_cpp_properties)
        c_cpp_properties_json[CONFIGURATIONS][0]["libs"].extend(
                                                        libs.split(", "))
        c_cpp_properties = json.dumps(c_cpp_properties_json, indent=4)


    split = os.path.split(project_name)
    if os.path.isdir(split[0]):
        project_dir = project_name
        project_name = split[1]
    else:
        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_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('''
Cannot remove the directory {}.
error message:
==============
{}
                    '''.format(project_dir, str(e)))
            else:
                msg = '''
                NOTE:
                Contract workspace
                '{}'
                already exists. Cannot overwrite it.
                '''.format(project_dir)
                if throw_exists:
                    raise errors.Error(msg)
                else:
                    raise errors.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
예제 #3
0
def project_from_template(
        project_name, template=None, workspace_dir=None,
        c_cpp_prop_path=None,
        includes=None,
        libs=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_dir() function.
        includes: If set, comma-separated list of include folders.
        libs: If set, comma-separated list of libraries.
        remove_existing: If set, overwrite any existing project.
        visual_studio_code: If set, open the ``VSCode``, if available.
        verbosity: The logging configuration.
    '''
    project_name = linuxize_path(project_name.strip())
    template = linuxize_path(template.strip())
    template_dir = template if os.path.isdir(template) else \
                                os.path.join(config.template_dir(), template)
    if not os.path.isdir(template_dir):
        raise errors.Error('''
The contract project template '{}' does not exist.
        '''.format(template_dir)) 

    if c_cpp_prop_path:
        c_cpp_prop_path = linuxize_path(c_cpp_prop_path)
        if os.path.exists(c_cpp_prop_path):
            try:
                with open(c_cpp_prop_path, "r") as f:
                    c_cpp_properties = f.read()
            except Exception:
                c_cpp_properties = vscode.c_cpp_properties()
    else:
        c_cpp_properties = vscode.c_cpp_properties()
    
    c_cpp_properties_json = json.loads(c_cpp_properties)

    if includes:
        temp = includes.split(", ")
        temp_ = []
        for entry in temp:
            path = naturalize_path(entry)
            if not path in c_cpp_properties_json[CONFIGURATIONS][0]\
                                                        [vscode.INCLUDE_PATH]:
                temp_.append(path)

        c_cpp_properties_json[CONFIGURATIONS][0][vscode.INCLUDE_PATH]\
                                                                .extend(temp_)
        c_cpp_properties_json[CONFIGURATIONS][0][BROWSE]["path"].extend(temp_)

    path = config.eoside_includes_dir()
    if path:
        path = naturalize_path(path)
        if not path in c_cpp_properties_json[CONFIGURATIONS][0]\
                                                        [vscode.INCLUDE_PATH]:
            c_cpp_properties_json[CONFIGURATIONS][0]\
                                            [vscode.INCLUDE_PATH].append(path)
            c_cpp_properties_json[CONFIGURATIONS][0][BROWSE]["path"]\
                                                                .append(path)
    
    if libs:
        temp = libs.split(", ")
        temp_ = []
        for entry in libs:
            path = naturalize_path(entry)
            if not path in c_cpp_properties_json[CONFIGURATIONS][0]\
                                                                [vscode.LIBS]:
                temp_.append(path)
            
        c_cpp_properties_json[CONFIGURATIONS][0][vscode.LIBS].extend(temp_)

    eoside_libs = config.eoside_libs_dir()
    if(eoside_libs):
        eoside_libs = os.listdir(config.eoside_libs_dir())
        for lib in eoside_libs:
            path = naturalize_path(lib)
            if not path in c_cpp_properties_json[CONFIGURATIONS][0]\
                                                                [vscode.LIBS]:
                c_cpp_properties_json[CONFIGURATIONS][0]\
                                                    [vscode.LIBS].append(path)

    c_cpp_properties = json.dumps(c_cpp_properties_json, indent=4)
    c_cpp_properties = resolve_home(c_cpp_properties)
    
    split = os.path.split(project_name)
    if os.path.isdir(split[0]):
        project_dir = project_name
        project_name = split[1]
    else:
        if not workspace_dir \
                                or not os.path.isabs(workspace_dir) \
                                or not os.path.exists(workspace_dir):
            workspace_dir = config.contract_workspace_dir()
        workspace_dir = workspace_dir.strip()        
        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('''
Cannot remove the directory {}.
error message:
==============
{}
                    '''.format(project_dir, str(e)))
            else:
                msg = '''
NOTE:
Contract workspace
'{}'
already exists. Cannot overwrite it.
                '''.format(project_dir)
                if throw_exists:
                    raise errors.Error(msg)
                else:
                    raise errors.Error(msg)

    try:
        os.makedirs(os.path.join(project_dir, "build"))
        os.makedirs(os.path.join(project_dir, "tests"))
        os.makedirs(os.path.join(project_dir, "include"))
    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) \
                                        and not "__pycache__" in template_path:
                if not os.path.exists(contract_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 f:
            template = f.read()

        if TEMPLATE_HOME in template:
            resolve_home(template)

        template = template.replace(C_CPP_PROP, c_cpp_properties)
        template = template.replace(TASK_JSON, vscode.TASKS)
        template = template.replace("${" + TEMPLATE_NAME + "}", project_name)

        with open(contract_path, "w") as output:
            output.write(template)

    copy_dir_contents(project_dir, PROJECT_0_DIR, "", project_name)
    if not template_dir == PROJECT_0_DIR: 
        copy_dir_contents(project_dir, template_dir, "", project_name)  

    if open_vscode:
        if utils.is_windows_ubuntu():
            command_line = "cmd.exe /C code {}".format(
                utils.wslMapLinuxWindows(project_dir))
        elif utils.os_version() == utils.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_dir, template_dir), verbosity)

    return project_dir
예제 #4
0
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