Skip to content

Commit

Permalink
feat(build-project): adding an option to set a custom temp path to us…
Browse files Browse the repository at this point in the history
…e during project-build (#67)

* feat(build-project): adding an option to set a custom temp path to use during project-build

* feat(check-project): adding an option to set a custom temp path to use during project-check

* add docs about the new --custom-temp-path option
  • Loading branch information
DavidVujic authored Nov 17, 2024
1 parent 236b57f commit 40bf40c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ Check the code, with a custom `MyPy` configuration to override the defaults:
poetry check-project --config-file <PATH-TO-MYPY.INI-CONFIG-FILE>
```

### Available command options

#### Custom temp path
A custom temporary path to use for reading, writing and deleting project content during the project build.
This option is useful for environments with restrictions on where scripts are allowed to store content.

NOTE: The default temp path will be created as a sibling to the project to build, and is the recommended way in most cases.

``` shell
poetry build-project --custom-temp-path /tmp
poetry check-project --custom-temp-path /tmp
```


### Usage for libraries
The `build-project` has a solution to the problem with top namespaces in libraries for __Python 3.9__ and more.
You can choose a custom namespace to be used in the build process, by using the `--with-top-namespace` flag.
Expand Down
17 changes: 13 additions & 4 deletions poetry_multiproject_plugin/commands/buildproject/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ def create_command_options() -> List[Option]:
long_name="with-top-namespace",
description="To arrange relative includes, and to modify import statements.",
flag=False,
)
),
option(
long_name="custom-temp-path",
description="Temporary path to use for reading, writing and deleting content during the project build.",
flag=False,
),
]

return parent + current
Expand All @@ -37,8 +42,10 @@ class ProjectBuildCommand(BuildCommand):

options = create_command_options()

def collect_project(self, path: Path, top_ns: Union[str, None]) -> Path:
destination = prepare.get_destination(path, "prepare")
def collect_project(
self, path: Path, top_ns: Union[str, None], temp_path: Union[str, None]
) -> Path:
destination = prepare.get_destination(path, "prepare", temp_path)

prepare.copy_project(path, destination)
packages.copy_packages(path, destination, top_ns)
Expand Down Expand Up @@ -80,7 +87,9 @@ def handle(self):
self.line(f"Using <c1>{path}</c1>")

top_ns = prepare.normalize_top_namespace(self.option("with-top-namespace"))
project_path = self.collect_project(path, top_ns)
temp_path = self.option("custom-temp-path")

project_path = self.collect_project(path, top_ns, temp_path)

if top_ns:
self.rewrite_modules(project_path, top_ns)
Expand Down
17 changes: 12 additions & 5 deletions poetry_multiproject_plugin/commands/checkproject/check.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import itertools
from pathlib import Path
from typing import List
from typing import List, Union

from cleo.helpers import option
from cleo.io.outputs.output import Verbosity
Expand Down Expand Up @@ -44,11 +44,16 @@ class ProjectCheckCommand(Command):
long_name="config-file",
description="Path to mypy config file. Use it to override the defaults.",
flag=False,
)
),
option(
long_name="custom-temp-path",
description="Temporary path to use for reading, writing and deleting content during the project check.",
flag=False,
),
]

def collect_project(self, path: Path) -> Path:
destination = prepare.get_destination(path, "check")
def collect_project(self, path: Path, temp_path: Union[str, None]) -> Path:
destination = prepare.get_destination(path, "check", temp_path)

prepare.copy_project(path, destination)
packages.copy_packages(path, destination)
Expand All @@ -64,9 +69,11 @@ def prepare_for_build(self, path: Path):

def handle(self):
is_verbose = self.option("verbose")
temp_path = self.option("custom-temp-path")

path = self.poetry.file.path.absolute()

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

if not is_verbose:
Expand Down
15 changes: 13 additions & 2 deletions poetry_multiproject_plugin/components/project/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@
from poetry_multiproject_plugin.components.toml import read


def get_destination(project_file: Path, prefix: str) -> Path:
def get_destination_folder(project_file: Path, temp_path: Union[str, None]) -> Path:
if temp_path:
return Path(temp_path)

grandparent = project_file.parent.parent

if project_file.parent == grandparent:
raise ValueError(f"Failed to navigate to the parent of {project_file.parent}")

return grandparent


def get_destination(
project_file: Path, prefix: str, temp_path: Union[str, None]
) -> Path:
folder = get_destination_folder(project_file, temp_path)

project_name = read.project_name(project_file)
sibling = f".{prefix}_{project_name}"

return Path(grandparent / sibling)
return Path(folder / sibling)


def copy_project(project_file: Path, destination: Path) -> Path:
Expand Down
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.6.0"
version = "1.7.0"
description = "A Poetry plugin that makes it possible to use relative package includes."
authors = ["David Vujic"]
license = "MIT"
Expand Down

0 comments on commit 40bf40c

Please sign in to comment.