Skip to content

Commit

Permalink
renamed _builder.py to _console.py; moved support.py into its own dir…
Browse files Browse the repository at this point in the history
…ectory; Target.build no longer catches its own errors
  • Loading branch information
nickeldan committed Sep 6, 2023
1 parent 217b88a commit 46cb347
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 38 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__pycache__
.mypy_cache
.pytest_cache
.vscode
.python-version
*.egg-info
/.vscode
/.python-version
/sandworm.egg-info
/build
5 changes: 1 addition & 4 deletions sandworm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
from .core import init_logging, root_build, make_clean # noqa: F401
from .core import VERSION, init_logging, root_build, make_clean # noqa: F401
from .target import Target, FileTarget, Environment # noqa: F401
from ._builder import main, VERSION # noqa: F401

from . import support # noqa: F401
8 changes: 1 addition & 7 deletions sandworm/_builder.py → sandworm/_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from . import core
from . import target

VERSION = "0.1.0"


def get_args() -> tuple[argparse.Namespace, list[str]]:
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -87,10 +85,6 @@ def create_environment(args: argparse.Namespace, extra_args: list[str]) -> targe


def do_build(env: target.Environment, target_str: str, max_workers: int | None) -> bool:
if max_workers is not None and max_workers < 1:
print("The number of workers must be either unspecified or at least 1.", file=sys.stderr)
return False

if target_str:
if (target := env.targets.get(target_str)) is None:
print(f"No such target: {target_str}", file=sys.stderr)
Expand All @@ -107,7 +101,7 @@ def main() -> int:
args, extra_args = get_args()

if args.version:
print(VERSION)
print(core.VERSION)
return 0

wormfile = pathlib.Path.cwd() / "Wormfile.py"
Expand Down
2 changes: 1 addition & 1 deletion sandworm/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def find_cycle(self) -> list[target.Target] | None:
for dep in top.dependencies:
match self.visited[dep]:
case VisitState.IN_STACK:
return self.stack[self.stack.index(dep) :]
return self.stack[self.stack.index(dep) :] + [dep]
case VisitState.NOT_VISITED:
self.stack.append(dep)
if (cycle := self.find_cycle()) is not None:
Expand Down
5 changes: 4 additions & 1 deletion sandworm/_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def run(self, leaves: list[Job]) -> bool:
try:
future.result()
except Exception:
logger.exception(f"Job for target {job.targ.fullname()} crashed:")
logger.exception(f"Exception caught building {job.targ.fullname()}:")

self._handle_finished_job(result)

Expand Down Expand Up @@ -166,6 +166,9 @@ def populate_job_pre_map(

token_set: set[int] = set()
for dep in targ.dependencies:
if not dep.out_of_date:
continue

dep_ctx = populate_job_pre_map(job_pre_map, counter, dep)
if dep_ctx.token is None:
if isinstance(dep_ctx.deps, int):
Expand Down
16 changes: 10 additions & 6 deletions sandworm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from . import _parallel
from . import target

VERSION = "0.1.0"

_logger = logging.getLogger("sandworm.core")


Expand All @@ -23,7 +25,7 @@ def format(self, record: logging.LogRecord) -> str:


def init_logging(
*, fmt: str = "%(message)s", verbose: bool = False, stream: typing.TextIO = sys.stdout
*, fmt: str = "%(message)s", verbose: bool = False, stream: typing.TextIO = sys.stderr
) -> None:
handler = logging.StreamHandler(stream=stream)
handler.setFormatter(_ColorFormatter(fmt=fmt))
Expand All @@ -35,10 +37,8 @@ def init_logging(

def _display_cycle(cycle: list[target.Target]) -> None:
_logger.error("Dependency cycle detected:")
base = cycle[0].env.basedir
for t in cycle:
_logger.error(f"\t{t.fullname()} from {t.env.basedir.relative_to(base)}")
_logger.error(f"\t{cycle[0].fullname()} from .")
for targ in cycle:
_logger.error(f"\t{targ.fullname()}")


def root_build(main: target.Target, max_workers: int | None = 1) -> bool:
Expand Down Expand Up @@ -76,7 +76,11 @@ def _build_sequence(sequence: list[target.Target]) -> bool:
if targ.built:
continue

if not targ.build():
try:
if not targ.build():
return False
except Exception:
_logger.exception(f"Exception caught building {targ.fullname():}")
return False

return True
Expand Down
File renamed without changes.
33 changes: 18 additions & 15 deletions sandworm/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def __eq__(self, other: typing.Any) -> bool:
def __hash__(self) -> int:
return hash((type(self), self.fullname()))

def __repr__(self) -> str:
return f"{type(self).__name__}({self._name})"

@property
@typing.final
def name(self) -> str:
Expand Down Expand Up @@ -75,23 +78,20 @@ def build(self: _T) -> bool:
_logger.debug(f"Building {self.fullname()}")

if self._builder is None:
if self.exists or self.dependencies:
if self.exists or (type(self) is Target and self.dependencies):
return True
else:
_logger.error(f"No rule to build {self.fullname()}.")
return False

different = (pwd := pathlib.Path.cwd()) != self.env.basedir
if different:
_logger.debug(f"Switching directories to {self.env.basedir}")
os.chdir(self.env.basedir)
try:
ret = self._builder(self)
except Exception:
_logger.exception(f"Exception caught while building {self.fullname()}")
ret = False
finally:
if different:
os.chdir(pwd)
ret = self._builder(self)
if different:
_logger.debug(f"Switching directories to {pwd}")
os.chdir(pwd)

if ret:
_logger.debug(f"Build for {self.fullname()} succeeded")
Expand Down Expand Up @@ -135,15 +135,19 @@ def __init__(
self: _T,
name: str,
*,
path: pathlib.Path | str | None = None,
dependencies: collections.abc.Iterable[Target] = (),
builder: Builder[_T] | None = None,
) -> None:
if isinstance(name, pathlib.Path):
name = str(name)
if path is None:
path = pathlib.Path(name)
elif isinstance(path, str):
path = pathlib.Path(path)
self.path = path
super().__init__(name, dependencies=dependencies, builder=builder)

def _fullpath(self) -> pathlib.Path:
return self.env.basedir / self.name
return self.env.basedir / self.path

def fullname(self) -> str:
return str(self._fullpath())
Expand All @@ -154,10 +158,9 @@ def exists(self) -> bool:

@functools.cached_property
def last_modified(self) -> int | None:
try:
st = os.stat(self._fullpath())
except FileNotFoundError:
if not self.exists:
return None
st = os.stat(self._fullpath())
return int(st.st_mtime)


Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ max-line-length = 110

[options.entry_points]
console_scripts =
sandworm=sandworm:main
sandworm=sandworm._console:main

0 comments on commit 46cb347

Please sign in to comment.