Example #1
0
def action_dir():
    '''
    Returns a JSON with the list of spaces and versions
    '''
    # get de space repo path
    spaces_path = pathlib.Path(utils.space_repository_path())

    # get directories in space repo path
    dirs = [x for x in spaces_path.iterdir() if x.is_dir()]

    # if dir contains dev/ -> is space (NAIVE APPROACH)
    # get last dir name [-1]: space name
    space_dirs = [d.parts[-1] for d in dirs if list(d.glob('dev'))]

    results = []
    for ispace in space_dirs:
        idict = {}
        idict["spacename"] = ispace
        versions = [0]

        for iversion in os.listdir(utils.space_tree_path(ispace)):
            if iversion.startswith('ver'):
                versions.append(utils.modeldir2ver(iversion))

        idict["versions"] = versions
        results.append(idict)

    # print (json.dumps(results))
    return True, json.dumps(results)
Example #2
0
def action_new(space):
    '''
    Create a new space tree, using the given name.
    This creates the development version "dev",
    copying inside default child classes
    '''

    if not space:
        return False, 'empty space label'

    # importlib does not allow using 'test' and issues a misterious error when we
    # try to use this name. This is a simple workaround to prevent creating spaces 
    # with this name 
    if space == 'test':
        #LOG.warning(f'the name "test" is disallowed, please use any other name')
        return False, 'the name "test" is disallowed, please use any other name'

    # space directory with /dev (default) level
    ndir = pathlib.Path(utils.space_tree_path(space)) / 'dev'

    # check if there is already a tree for this endpoint
    if ndir.exists():
        #LOG.warning(f'Endpoint {space} already exists')
        return False, f'Endpoint {space} already exists'

    try:
        ndir.mkdir(parents=True)
        LOG.debug(f'{ndir} created')
    except:
        return False, f'Unable to create path for {space} endpoint'

    # Copy classes skeletons to ndir
    wkd = pathlib.Path(os.path.dirname(os.path.abspath(__file__)))
    children_names = ['sapply', 'idata', 'odata', 'slearn']

    for cname in children_names:
        filename = cname + '_child.py'
        src_path = wkd / 'children' / filename
        dst_path = ndir / filename
        try:
            shutil.copy(src_path, dst_path)
        except:
            return False, f'Unable to copy {cname} file'

    LOG.debug(f'copied class skeletons from {src_path} to {dst_path}')
    
    # copy parameter yml file
    params_path = wkd / 'children/parameters.yaml'
    shutil.copy(params_path, ndir)

    # copy documentation yml file
    documentation_path = wkd / 'children/documentation.yaml'
    shutil.copy(documentation_path, ndir)
  
    LOG.info(f'New space {space} created')
    
    return True, 'New space '+space+' created'
Example #3
0
def action_dir():
    '''
    Returns a the list of spaces and versions
    '''
    # get de space repo path
    spaces_path = pathlib.Path(utils.space_repository_path())
    if spaces_path.is_dir() is False:
        return False, 'the spaces repository path does not exist. Please run "flame -c config".'

    # get directories in space repo path
    dirs = [x for x in spaces_path.iterdir() if x.is_dir()]

    # if dir contains dev/ -> is space (NAIVE APPROACH)
    # get last dir name [-1]: space name
    space_dirs = [d.parts[-1] for d in dirs if list(d.glob('dev'))]

    # results = []
    # for ispace in space_dirs:
    #     idict = {}
    #     idict ["spacename"] = ispace
    #     versions = [0]

    #     for iversion in os.listdir(utils.space_tree_path(ispace)):
    #         if iversion.startswith('ver'):
    #             versions.append(utils.modeldir2ver(iversion))

    #     idict ["versions"] = versions
    #     results.append(idict)

    results = []
    for ispace in space_dirs:
        idict = {}
        idict["spacename"] = ispace
        idict["version"] = 0
        idict["info"] = action_info(ispace, 0, output=None)[1]

        results.append(idict)

        for iversion in os.listdir(utils.space_tree_path(ispace)):
            if iversion.startswith('ver'):
                idict = {}
                idict["spacename"] = ispace
                idict["version"] = utils.modeldir2ver(iversion)
                idict["info"] = action_info(ispace,
                                            idict["version"],
                                            output=None)[1]

                results.append(idict)

    # print (json.dumps(results))
    return True, results
Example #4
0
def action_list(space):
    '''
    Lists all versions for the space provided as argument
    '''

    # if a space name is provided, list versions
    base_path = utils.space_tree_path(space)

    num_versions = 0
    for x in os.listdir(base_path):
        if x.startswith("ver"):
            num_versions += 1
            LOG.info(f'\t{space} : {x}')

    return True, f'space {space} has {num_versions} published versions'
Example #5
0
def action_kill(space):
    '''
    removes the space tree described by the argument
    '''

    if not space:
        return False, 'Empty space name'

    ndir = utils.space_tree_path(space)

    if not os.path.isdir(ndir):
        #LOG.error(f'space {space} not found')
        return False, f'space {space} not found'

    try:
        shutil.rmtree(ndir, ignore_errors=True)
    except:
        return False, f'Failed to remove space {space}'

    LOG.info(f'space {space} removed')
    #print(f'space {space} removed')
    return True, f'space {space} removed'
Example #6
0
def action_publish(space):
    '''
    clone the development "dev" version as a new space version,
     assigning a sequential version number
    '''

    if not space:
        return False, 'Empty space label'

    base_path = utils.space_tree_path(space)

    if not os.path.isdir(base_path):
        #LOG.error(f'space {space} not found')
        return False, f'space {space} not found'

    # gets version number
    v = [int(x[-6:]) for x in os.listdir(base_path) if x.startswith("ver")]

    if not v:
        max_version = 0
    else:
        max_version = max(v)

    new_path = os.path.join(base_path,f'ver{max_version+1:06}')

    if os.path.isdir(new_path):
        #LOG.error(f'Versin {v} of space {space} not found')
        return False, f'Version {max_version+1} of space {space} already exists'

    src_path = os.path.join (base_path,'dev')

    try:
        shutil.copytree(src_path, new_path)
    except:
        return False, f'Unable to copy contents of dev version for space {space}'

    LOG.info(f'New space version created from {src_path} to {new_path}')
    return True, f'New space version created from {src_path} to {new_path}'