Last active
September 8, 2023 18:55
-
-
Save ahgraber/c5eb8916774fc413f5be3f3b72fb30fc to your computer and use it in GitHub Desktop.
Get path of repository folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def isdrive(path): | |
"""Return `True` if path is the drive root | |
Parameters | |
---------- | |
path : path-like | |
""" | |
path = os.path.abspath(path) | |
drive = os.path.splitdrive(path) | |
# *nix drive root | |
if drive == ('', '/'): | |
return True | |
# windows drive root | |
elif drive[0].__contains__(':'): | |
return True | |
def get_repo_path(filepath): | |
"""Detects root of repo given `.git` dir | |
Parameters | |
---------- | |
filepath : pathlike | |
`__file__` from the calling script | |
Examples | |
-------- | |
>>> if (__name__ == '__main__') and (__package__ is None): | |
>>> REPO_ROOT = get_repo_root(__file__) | |
>>> sys.path.append(REPO_ROOT + '/src') | |
>>> os.chdir(os.path.join(REPO_ROOT, 'src')) | |
""" | |
if os.path.isdir(filepath): | |
path_ = os.path.abspath(filepath) | |
else: | |
path_ = os.path.abspath(os.path.dirname(filepath)) | |
while not isdrive(path_): | |
dirs_at_path = [os.path.basename(f.path) for f in os.scandir(path_) if f.is_dir()] | |
if ".git" in dirs_at_path: | |
# parent contains a .git folder | |
return path_ | |
else: | |
# get parent dir | |
path_ = os.path.abspath(os.path.dirname(path_)) | |
else: | |
logger.warning( | |
f"File running from unrecognized/unmanaged path: {os.path.dirname(os.path.abspath(path_))}" | |
) | |
def get_project_path(filepath): | |
"""Detects root of project directory from calling filepath assuming repo contains at least directories ['data', 'notebooks', 'src'] | |
Parameters | |
---------- | |
filepath : pathlike | |
`__file__` from the calling script | |
Examples | |
-------- | |
>>> if (__name__ == '__main__') and (__package__ is None): | |
>>> PROJECT_ROOT = get_project_root(__file__) | |
>>> sys.path.append(PROJECT_ROOT + '/src') | |
""" | |
# repo root can be identified by having this minimal set of subdirs | |
repo_subdirs = { | |
# 'artifacts', | |
'data', | |
# 'docs', | |
'notebooks', | |
# 'queries', | |
'src', | |
# 'tests', | |
# 'writeup', | |
} | |
if os.path.isdir(filepath): | |
path_ = os.path.abspath(filepath) | |
else: | |
path_ = os.path.abspath(os.path.dirname(filepath)) | |
while not isdrive(path_): | |
parent = os.path.dirname(path_) | |
if repo_subdirs.issubset(set(os.listdir(parent))): | |
# parent contains the minimum folders we expect in our repos | |
return parent | |
else: | |
path_ = parent | |
else: | |
logger.warning( | |
f"File running from unrecognized/unmanaged path: {os.path.dirname(os.path.abspath(path_))}" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
def src_path(): | |
"""Return path to 'src/' directory.""" | |
return next(p for p in Path(__file__).parents if str(p).endswith("src")) | |
SRC_DIR = src_path() | |
PERSIST_DIR = SRC_DIR.parent | |
sys.path.insert(1, str(SRC_DIR)) | |
# __package__ = "<PACKAGE_NAME>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment