Example #1
0
def test_dir_parents():
    if sys.platform == 'win32':
        pytest.skip('not sure how to test on win32 at present')
    dir = os.path.dirname(__file__)
    dirs = list(walk_directory_parents(dir))
    assert dirs[0] == dir
    assert dirs[-1] == '/'
Example #2
0
    def get_project(self, directory: str) -> Optional[Union['RemoteProject', 'Project']]:
        """
        Get the Valohai project object for a directory context.
        The directory tree is walked upwards to find an actual linked directory.

        If a project override is active, it is always returned.

        :param dir: Directory
        :return: Project object, or None.
        """
        if self.override_project:
            return self.override_project

        links = self.links
        if not links:
            return None
        for directory in walk_directory_parents(directory):  # noqa: B020
            project_obj = links.get(directory)
            if project_obj:
                from valohai_cli.models.project import Project
                return Project(data=project_obj, directory=directory)
        return None  # No project.
Example #3
0
def get_project(dir=None, require=False):
    """
    Get the Valohai project object for a directory context.

    The object is augmented with the `dir` key.

    :param dir: Directory (defaults to cwd)
    :param require: Raise an exception if no project is found
    :return: Project object, or None.
    :rtype: valohai_cli.models.project.Project|None
    """
    links = settings.get('links') or {}
    if not links:
        if require:
            raise NoProject('No projects are configured')
        return None
    orig_dir = dir or get_project_directory()
    for dir in walk_directory_parents(orig_dir):
        project_obj = links.get(dir)
        if project_obj:
            return Project(data=project_obj, directory=dir)
    if require:
        raise NoProject('No project is linked to %s' % orig_dir)
    return None