-
Notifications
You must be signed in to change notification settings - Fork 4
Streamline repo scaffolding & re-write & re-enable repo scaffolding tests #639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
27f6b77
82479ad
a4c47f3
e0efc06
c7947c5
8e4035f
ba91caf
5ca335f
7212ace
26da704
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,13 +11,17 @@ | |
| import os | ||
| import sys | ||
| import difflib | ||
| import subprocess | ||
| from shutil import rmtree | ||
| from pathlib import Path | ||
| from dataclasses import dataclass | ||
|
|
||
| import toml | ||
| import requests | ||
| import rich_click as click | ||
| from rich import print # pylint: disable=W0622 | ||
| from toml import TomlPreserveInlineDictEncoder | ||
| from jinja2 import Environment, FileSystemLoader | ||
| from rich.prompt import Prompt | ||
| from rich.progress import Progress, track | ||
| from aea.cli.utils.config import get_default_author_from_cli_config | ||
|
|
@@ -26,9 +30,11 @@ | |
| from auto_dev.enums import UserInput | ||
| from auto_dev.utils import change_dir | ||
| from auto_dev.constants import ( | ||
| THIS_REPO_ROOT, | ||
| DEFAULT_TIMEOUT, | ||
| TEMPLATE_FOLDER, | ||
| DEFAULT_ENCODING, | ||
| JINJA_TEMPLATE_FOLDER, | ||
| SAMPLE_PYTHON_CLI_FILE, | ||
| SAMPLE_PYTHON_MAIN_FILE, | ||
| CheckResult, | ||
|
|
@@ -60,6 +66,34 @@ def execute_commands(*commands: str, verbose: bool, logger, shell: bool = False) | |
| sys.exit(1) | ||
|
|
||
|
|
||
| def _render_autonomy_pyproject_template(project_name: str, authors: str) -> str: | ||
| env = Environment(loader=FileSystemLoader(JINJA_TEMPLATE_FOLDER), autoescape=False) # noqa | ||
| template = env.get_template("repo/autonomy/pyproject.jinja") | ||
|
|
||
| pyproject_path = THIS_REPO_ROOT / "pyproject.toml" | ||
| pyproject = toml.load(pyproject_path) | ||
|
|
||
| build_system = pyproject["build-system"] | ||
| classifiers = pyproject["tool"]["poetry"]["classifiers"] | ||
| python = pyproject["tool"]["poetry"]["dependencies"]["python"] | ||
| current_version = pyproject["tool"]["poetry"]["version"] | ||
| min_minor_version = ".".join(current_version.split(".")[:2]) | ||
| version = f">={min_minor_version}.0,<={current_version}" | ||
| dev_dependencies = pyproject["tool"]["poetry"]["group"]["dev"]["dependencies"] | ||
| black_config = pyproject["tool"]["black"] | ||
|
|
||
| return template.render( | ||
| build_system=toml.dumps(build_system), | ||
| project_name=project_name, | ||
| authors=authors, | ||
| classifiers=" " + ",\n ".join(f'"{c}"' for c in classifiers), | ||
| python=python, | ||
| version=version, | ||
| dev_dependencies=toml.dumps(dev_dependencies, TomlPreserveInlineDictEncoder()), | ||
| black_config=toml.dumps(black_config), | ||
| ) | ||
|
Comment on lines
+69
to
+94
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, updating dependencies in
While these failures correctly caught drift between the scaffolded repo and This PR removes the need for all that: the autonomy |
||
|
|
||
|
|
||
| cli = build_cli() | ||
|
|
||
| render_args = { | ||
|
|
@@ -102,6 +136,23 @@ def scaffold( | |
| """Scaffold files for a new repo.""" | ||
| new_repo_dir = Path.cwd() | ||
| template_folder = TEMPLATES[self.type_of_repo] | ||
|
|
||
| if self.type_of_repo == "autonomy": | ||
| project_name = self.scaffold_kwargs["project_name"] | ||
| authors = self.scaffold_kwargs["author"] | ||
| pyproject_content = _render_autonomy_pyproject_template(project_name, authors) | ||
| (new_repo_dir / "pyproject.toml").write_text(pyproject_content) | ||
| result = subprocess.run( | ||
| ["poetry", "lock"], | ||
| capture_output=True, | ||
| text=True, | ||
| check=False, | ||
| cwd=new_repo_dir, | ||
| ) | ||
| if result.returncode != 0: | ||
| msg = f"Failed to lock packages:\n{result.stderr}" | ||
| raise ValueError(msg) | ||
|
Comment on lines
+140
to
+154
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| for file in track( | ||
| self.template_files, | ||
| description=f"Scaffolding {self.type_of_repo} repo", | ||
|
|
@@ -286,7 +337,7 @@ def scaffold_new_repo(logger, name, type_of_repo, force, auto_approve, install, | |
| src_dir = Path(name) | ||
| src_dir.mkdir(exist_ok=False) | ||
| logger.debug(f"Scaffolding `{src_dir!s}`") | ||
| (src_dir / "__init__.py").touch() | ||
| (src_dir / "__init__.py").write_text(f'"""{name.capitalize()} module."""') | ||
| (src_dir / "main.py").write_text(SAMPLE_PYTHON_MAIN_FILE) | ||
| (src_dir / "cli.py").write_text(SAMPLE_PYTHON_CLI_FILE.format(project_name=name)) | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
| from aea.configurations.data_types import PublicId | ||
|
|
||
|
|
||
| THIS_REPO_ROOT = Path(__file__).parent.parent | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is useful because when working with isolated filesystems, functions like |
||
|
|
||
| DEFAULT_ENCODING = "utf-8" | ||
| DEFAULT_TZ = "UTC" | ||
| DEFAULT_TIMEOUT = 10 | ||
|
|
||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Tests module""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| [build-system] | ||
| {{ build_system }} | ||
|
|
||
| [tool.poetry] | ||
| name = "{{ project_name }}" | ||
| version = "0.1.0" | ||
| description = "" | ||
| authors = ["{{ authors }}"] | ||
| readme = "README.md" | ||
| license = "Apache-2.0" | ||
| classifiers = [ | ||
| {{ classifiers }} | ||
| ] | ||
| package-mode = false | ||
|
|
||
| [tool.poetry.dependencies] | ||
| python = "{{ python }}" | ||
| autonomy-dev = {extras = ["all"], version = "{{ version }}"} | ||
|
|
||
| [tool.poetry.group.dev.dependencies] | ||
| {{ dev_dependencies }} | ||
|
|
||
| [tool.black] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should ruff formatting be preferred? |
||
| {{ black_config }} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,10 +75,9 @@ def test_filesystem(monkeypatch): | |
|
|
||
|
|
||
| @pytest.fixture | ||
| def test_clean_filesystem(monkeypatch): | ||
| def test_clean_filesystem(): | ||
| """Fixture for invoking command-line interfaces.""" | ||
| with isolated_filesystem() as directory: | ||
| monkeypatch.setenv("PYTHONPATH", directory) | ||
| yield directory | ||
|
Comment on lines
77
to
81
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider a more minimal pyproject than adev's for scaffoled repos. (e.g. dev deps in adev pyproject include mkdocs, and may not be appropriate for scaffolded repos)