Python get project root

18 Python code examples are found related to " get project root". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: cli.py    From surround with BSD 3-Clause "New" or "Revised" License 8 votes vote down vote up
def get_project_root(current_directory):
    """
    Returns the project root directory for the project at the provided directory.

    :returns: path to the root of the project
    :rtype: string
    """

    home = str(Path.home())

    while True:
        list_ = os.listdir(current_directory)
        parent_directory = os.path.dirname(current_directory)
        if current_directory in (home, parent_directory):
            break
        if ".surround" in list_:
            return current_directory
        current_directory = parent_directory 
Example 2
Source File: padawan.py    From padawan.vim with MIT License 7 votes vote down vote up
def GetProjectRoot(self, filepath):
        curPath = path.dirname(filepath)
        while curPath != '/' and not path.exists(
                path.join(curPath, 'composer.json')
                ):
            curPath = path.dirname(curPath)

        if curPath == '/':
            curPath = path.dirname(filepath)

        return curPath 
Example 3
Source File: analyze.py    From RenderChan with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_project_root(path):
    if os.path.exists(os.path.join(path, "project.conf")) or os.path.exists(os.path.join(path, "remake.conf")):
        return path
    else:
        if os.path.dirname(path) == path:
            return ""
        else:
            return get_project_root(os.path.dirname(path))


# blender 2.5x stuff 
Example 4
Source File: base.py    From sublime-flowtype with MIT License 5 votes vote down vote up
def get_project_root(self):
        """Return the project root.

        Search for a .flowconfig file and consider its location as the
        project's root.
        """
        file_path = self.active_window.extract_variables()["file_path"]
        return find_in_parent_folders(".flowconfig", file_path) 
Example 5
Source File: fileoperations.py    From aws-elastic-beanstalk-cli with Apache License 2.0 5 votes vote down vote up
def get_project_root():
    cwd = os.getcwd()
    try:
        ProjectRoot.traverse()
        return os.getcwd()
    finally:
        os.chdir(cwd) 
Example 6
Source File: settings.py    From airgun with GNU General Public License v3.0 5 votes vote down vote up
def get_project_root():
    """Return the path to the project root directory.

    :return: A directory path.
    :rtype: str
    """
    return os.path.realpath(os.path.join(
        os.path.dirname(__file__),
        os.pardir,
    )) 
Example 7
Source File: cli.py    From surround with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_project_root_from_current_dir():
    """
    Returns the project root directory for the current project we're in.

    :return: path to the root of the project
    :rtype: string
    """

    return get_project_root(os.getcwd()) 
Example 8
Source File: __init__.py    From django-template with MIT License 5 votes vote down vote up
def get_project_root_path():
    """
    Return the absolute path to the root of the project.
    """
    return os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) 
Example 9
Source File: padawan_helper.py    From deoplete-padawan with MIT License 5 votes vote down vote up
def get_project_root(self, file_path):
        current_path = path.dirname(file_path)
        while current_path != '/' and not path.exists(
            path.join(current_path, 'composer.json')
        ):
            current_path = path.dirname(current_path)

        if current_path == '/':
            current_path = path.dirname(file_path)

        return current_path 
Example 10
Source File: utils.py    From dtf with Apache License 2.0 5 votes vote down vote up
def get_project_root():

    """Search for and return the dtf project root."""

    return __upsearch(CONFIG_FILE_NAME, os.getcwd()) 
Example 11
Source File: util.py    From gauge-python with MIT License 5 votes vote down vote up
def get_project_root():
    try:
        return os.path.abspath(os.environ[PROJECT_ROOT_ENV])
    except KeyError:
        return "" 
Example 12
Source File: tools.py    From vy with MIT License 5 votes vote down vote up
def get_project_root(path):
    """
    Return the project root or the file path.
    """

    # In case it receives '/file'
    # and there is '/__init__.py' file.
    if path == dirname(path):
        return path

    while True:
        tmp = dirname(path)
        if not exists(join(tmp, '__init__.py')):
            return path
        path = tmp 
Example 13
Source File: cli.py    From psync with MIT License 5 votes vote down vote up
def get_project_root():
    cwd = os.getcwd()
    root = psync.project_root(start_from=cwd)

    if root is None:
        return False, None
    else:
        return True, root 
Example 14
Source File: io.py    From nsf with MIT License 5 votes vote down vote up
def get_project_root():
    if on_cluster():
        path = '/home/s1638128/deployment/decomposition-flows'
    else:
        path = '/home/conor/Dropbox/phd/projects/decomposition-flows'
    return path 
Example 15
Source File: utils.py    From autohooks with GNU General Public License v3.0 5 votes vote down vote up
def get_project_root_path(path: Path = None) -> Path:
    if path is None:
        path = Path(os.environ['PWD'])

    path.resolve()

    if is_project_root(path):
        return path

    for parent in path.parents:
        if is_project_root(parent):
            return parent

    return path 
Example 16
Source File: utils.py    From hwrt with MIT License 4 votes vote down vote up
def get_project_root():
    """Get the project root folder as a string."""
    cfg = get_project_configuration()
    # At this point it can be sure that the configuration file exists
    # Now make sure the project structure exists
    for dirname in [
        "raw-datasets",
        "preprocessed",
        "feature-files",
        "models",
        "reports",
    ]:
        directory = os.path.join(cfg["root"], dirname)
        if not os.path.exists(directory):
            os.makedirs(directory)

    raw_yml_path = pkg_resources.resource_filename(__name__, "misc/")

    # TODO: How to check for updates if it already exists?
    raw_data_dst = os.path.join(cfg["root"], "raw-datasets/info.yml")
    if not os.path.isfile(raw_data_dst):
        raw_yml_pkg_src = os.path.join(raw_yml_path, "info.yml")
        shutil.copy(raw_yml_pkg_src, raw_data_dst)

    # Make sure small-baseline folders exists
    for dirname in [
        "models/small-baseline",
        "feature-files/small-baseline",
        "preprocessed/small-baseline",
    ]:
        directory = os.path.join(cfg["root"], dirname)
        if not os.path.exists(directory):
            os.makedirs(directory)

    # Make sure small-baseline yml files exist
    paths = [
        ("preprocessed/small-baseline/", "preprocessing-small-info.yml"),
        ("feature-files/small-baseline/", "feature-small-info.yml"),
        ("models/small-baseline/", "model-small-info.yml"),
    ]
    for dest, src in paths:
        raw_data_dst = os.path.join(cfg["root"], "%s/info.yml" % dest)
        if not os.path.isfile(raw_data_dst):
            raw_yml_pkg_src = os.path.join(raw_yml_path, src)
            shutil.copy(raw_yml_pkg_src, raw_data_dst)

    return cfg["root"] 
Example 17
Source File: __init__.py    From ReuBERT with MIT License 4 votes vote down vote up
def get_project_root():
    src = os.path.realpath(__file__)
    src_list = src.split(os.sep)
    if ":" in src_list[0]:
        src_list[0] = src_list[0] + os.sep
    idx = src_list.index("src")
    assert idx != 1, "Something is wrong."
    src_list[0] = os.sep if src_list[0] == '' else src_list[0]
    project_root = os.path.join(*src_list[:idx])
    return project_root