From bf3738f77760ae6f4e942e2cb1639f5c9461a937 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Thu, 8 Aug 2024 09:49:33 +0300 Subject: [PATCH] project: add NIAR_WORKING_DIRECTORY support. --- CHANGELOG.md | 4 ++++ niar/project.py | 26 ++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cccb35..aed5088 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.1.3 (unreleased) +New: + +* project: `NIAR_WORKING_DIRECTORY` can be used to override the origin. + ## 0.1.2 New: diff --git a/niar/project.py b/niar/project.py index 5b6831f..ecc9fd7 100644 --- a/niar/project.py +++ b/niar/project.py @@ -1,3 +1,4 @@ +import os import sys from pathlib import Path from typing import Optional @@ -100,16 +101,21 @@ class Project: ] def __init_subclass__(cls): - # We expect to be called from project-root/module/__init.py__ or similar; - # cls.origin is project-root. Keep going up until we find pyproject.toml. - origin = Path(sys._getframe(1).f_code.co_filename).absolute().parent - while True: - if (origin / "pyproject.toml").is_file(): - cls.origin = origin - break - if not any(origin.parents): - assert False, "could not find pyproject.toml" - origin = origin.parent + if origin := os.getenv("NIAR_WORKING_DIRECTORY"): + cls.origin = Path(origin).absolute() + else: + # We expect to be called from project-root/module/__init.py__ or similar; + # cls.origin is project-root. Keep going up until we find pyproject.toml. + origin = Path(sys._getframe(1).f_code.co_filename).absolute().parent + while True: + if (origin / "pyproject.toml").is_file(): + cls.origin = origin + break + if not any(origin.parents): + assert False, "could not find pyproject.toml" + origin = origin.parent + + os.chdir(cls.origin) extras = cls.__dict__.keys() - {"__module__", "__doc__", "origin"} for prop in cls.PROPS: