Skip to content

Commit

Permalink
project: add NIAR_WORKING_DIRECTORY support.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Aug 8, 2024
1 parent bca15c1 commit a478292
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 0.1.3 (unreleased)

New:

* project: `NIAR_WORKING_DIRECTORY` can be used to override the origin.

## 0.1.2

New:
Expand Down
26 changes: 16 additions & 10 deletions src/niar/project.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a478292

Please sign in to comment.