Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defend against broken paths from non-editable installs #356

Open
jayqi opened this issue Apr 15, 2024 · 0 comments
Open

Defend against broken paths from non-editable installs #356

jayqi opened this issue Apr 15, 2024 · 0 comments

Comments

@jayqi
Copy link
Member

jayqi commented Apr 15, 2024

The refreshed Python module boilerplate (#354) adds a {module_name}.config module that contains pathlib.Path instances to various project directories. These paths depend on the project module being installed as editable in order to resolve correctly.

Someone following the golden path while using a CCDS-generated project should end up with an editable installation. However, it's possible for someone to not do so. Some possible mitigations:

  • Detect if not an editable install, warn users
  • Set paths a different way intelligently if not an editable install

Some prototype code for achieving this that was ultimately removed from #354

# Check if the package is installed as editable
def _is_editable():
    # https://peps.python.org/pep-0660/#frontend-requirements
    try:
        dist = importlib.metadata.distribution("{{ cookiecutter.module_name }}")
        direct_url_data = dist.read_text("direct_url.json")
        if direct_url_data is None:
            return False
        return json.loads(direct_url_data).get("dir_info", {}).get("editable", False)
    except importlib.metadata.PackageNotFoundError:
        return False



IS_EDITABLE = _is_editable()

# Determine PROJ_ROOT path
if os.getenv("PROJ_ROOT"):
    logger.debug("Reading PROJ_ROOT from environment variable.")
    PROJ_ROOT = Path(os.getenv("PROJ_ROOT"))
elif IS_EDITABLE:
    logger.debug("Setting PROJ_ROOT relative to editable package.")
    PROJ_ROOT = Path(__file__).resolve().parents[1]
else:
    logger.debug("Using current working directory as PROJ_ROOT.")
    PROJ_ROOT = Path.cwd()

Some more discussion here: #354 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant