Skip to content

Commit

Permalink
fix: check-project command (#18)
Browse files Browse the repository at this point in the history
* fix(check-project): install deps in temp folder, run mypy with top namespace

* docs: add check-project specific info

* bump version to 1.1.1
  • Loading branch information
DavidVujic authored Dec 27, 2022
1 parent 980f2c5 commit ae188bc
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 12 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ poetry self add poetry-multiproject-plugin

## What does it do?

the `poetry build-command` will:
the `poetry build-project` command will:

1. copy the actual project into a temporary folder.
2. collect relative includes - such as `include = "foo/bar", from = "../../shared"` - and copy them into the temprary folder.
Expand All @@ -51,6 +51,16 @@ the `poetry build-command` will:
6. remove the temporary folder.


the `poetry check-project` command will:

1. copy the actual project into a temporary folder.
2. collect relative includes - such as `include = "foo/bar", from = "../../shared"` - and copy them into the temprary folder.
3. generate a new pyproject.toml.
4. run `poetry install` in the temporary folder.
5. run `poetry run mypy` in the temporary folder.
6. remove the temporary folder.


## How is it different from the "poetry build" command?
Poetry does not allow package includes outside of the __project__ root.

Expand Down
25 changes: 19 additions & 6 deletions poetry_multiproject_plugin/commands/checkproject/check.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
from pathlib import Path
from typing import List

Expand All @@ -7,21 +8,27 @@
from poetry.factory import Factory

from poetry_multiproject_plugin.components.check import check_for_errors
from poetry_multiproject_plugin.components.deps import install_deps
from poetry_multiproject_plugin.components.project import (
cleanup,
create,
packages,
prepare,
)
from poetry_multiproject_plugin.components.toml import read

command_name = "check-project"


def run_check(destination: Path, config_file: str) -> List[str]:
rows = check_for_errors(destination, config_file)
def run_check(destination: Path, pyproj: str, config_file: str) -> List[str]:
paths = read.package_paths(destination / pyproj)

rows = [check_for_errors(destination, str(path), config_file) for path in paths]
flattened = itertools.chain.from_iterable(rows)

dest = str(destination)

return [row.replace(dest, "") for row in rows if "error:" in row]
return [row.replace(dest, "") for row in flattened]


class ProjectCheckCommand(BuildCommand):
Expand Down Expand Up @@ -51,18 +58,24 @@ def prepare_for_build(self, path: Path):
self.set_poetry(project_poetry)

def handle(self):
path = Path("pyproject.toml").absolute()
pyproj = "pyproject.toml"
path = Path(pyproj).absolute()

project_path = self.collect_project(path)
self.prepare_for_build(project_path.absolute())

self.io.set_verbosity(Verbosity.QUIET)
super(ProjectCheckCommand, self).handle()

self.io.set_verbosity(Verbosity.NORMAL)
mypy_config = self.option("config-file")
res = run_check(project_path, mypy_config)

cleanup.remove_file(project_path, "poetry.lock")

install_deps(project_path)

res = run_check(project_path, pyproj, mypy_config)

self.io.set_verbosity(Verbosity.NORMAL)
for r in res:
self.line(r)

Expand Down
10 changes: 6 additions & 4 deletions poetry_multiproject_plugin/components/check/mypy_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
]


def run(dest: str, config_file: Union[str, None]) -> List[str]:
def run(dest: str, top_ns: str, config_file: Union[str, None]) -> List[str]:
os.environ["MYPYPATH"] = dest

args = ["--config-file", config_file] if config_file else default_args
cmd = ["mypy"] + args + [f"{dest}/demo"]
cmd = ["poetry", "run", "mypy"] + args + [f"{dest}/{top_ns}"]

res = subprocess.run(cmd, capture_output=True, text=True)

return res.stdout.splitlines()


def check_for_errors(destination: Path, config_file: Union[str, None]) -> List[str]:
lines = run(str(destination), config_file)
def check_for_errors(
destination: Path, top_ns: str, config_file: Union[str, None]
) -> List[str]:
lines = run(str(destination), top_ns, config_file)

return [line for line in lines if "error:" in line]
3 changes: 3 additions & 0 deletions poetry_multiproject_plugin/components/deps/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from poetry_multiproject_plugin.components.deps.installer import install_deps

__all__ = ["install_deps"]
20 changes: 20 additions & 0 deletions poetry_multiproject_plugin/components/deps/installer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import subprocess
from pathlib import Path


def run_install_command():
subprocess.run(["poetry", "install", "--only", "main", "--quiet"])


def navigate_to(path: Path):
os.chdir(str(path))


def install_deps(destination: Path):
current_dir = Path.cwd()

navigate_to(destination)
run_install_command()

navigate_to(current_dir)
5 changes: 5 additions & 0 deletions poetry_multiproject_plugin/components/project/cleanup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import shutil
import os
from pathlib import Path


def remove_project(project_path: Path):
shutil.rmtree(project_path)


def remove_file(project_path: Path, file_name: str):
os.remove(project_path / file_name)
10 changes: 10 additions & 0 deletions poetry_multiproject_plugin/components/toml/read.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
from pathlib import Path
from typing import List

from tomlkit.toml_document import TOMLDocument
from tomlkit.toml_file import TOMLFile

from poetry_multiproject_plugin.components.toml.packages import join_package_paths


def toml(path: Path) -> TOMLDocument:
p = path.as_posix()

toml_file = TOMLFile(p)

return toml_file.read()


def package_paths(path: Path) -> List[Path]:
data: dict = toml(path)
packages = data["tool"]["poetry"]["packages"]

return [join_package_paths(p) for p in packages]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poetry-multiproject-plugin"
version = "1.1.0"
version = "1.1.1"
description = "A Poetry plugin that makes it possible to use relative package includes."
authors = ["David Vujic"]
homepage = "https://github.com/davidvujic/poetry-multiproject-plugin"
Expand Down

0 comments on commit ae188bc

Please sign in to comment.