Skip to content
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

Updates, fixes, parametrize versions #42

Merged
merged 19 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
106 changes: 94 additions & 12 deletions src/saltext/cli/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import datetime
import os
import pathlib
Expand All @@ -13,13 +14,14 @@
from jinja2 import Template
from saltext.cli import __version__
from saltext.cli import PACKAGE_ROOT
from saltext.cli.templates import LOADER_MODULE_FUNCTIONAL_TEST_TEMPLATE
from saltext.cli.templates import LOADER_MODULE_INTEGRATION_TEST_TEMPLATE
from saltext.cli.templates import LOADER_MODULE_UNIT_TEST_TEMPLATE
from saltext.cli.templates import LOADER_SDB_UNIT_TEST_TEMPLATE
from saltext.cli.templates import LOADER_STATE_FUNCTIONAL_TEST_TEMPLATE
from saltext.cli.templates import LOADER_STATE_UNIT_TEST_TEMPLATE
from saltext.cli.templates import LOADER_TEMPLATE
from saltext.cli.templates import LOADER_UNIT_TEST_TEMPLATE
from saltext.cli.templates import LOADERS_TEMPLATE
from saltext.cli.templates import MODULE_LOADER_TEMPLATE
from saltext.cli.templates import PACKAGE_INIT
from saltext.cli.templates import SDB_LOADER_TEMPLATE
Expand Down Expand Up @@ -82,6 +84,15 @@
"wrapper",
)

CURRENT_LATEST_SALT = 3006
SALT_PYTHON_SUPPORT = {
3003: {"min": (3, 7), "max": (3, 9)},
3004: {"min": (3, 7), "max": (3, 10)},
3005: {"min": (3, 7), "max": (3, 10)},
3006: {"min": (3, 7), "max": (3, 10)},
3007: {"min": (3, 8), "max": (3, 11)},
}


@click.command()
@click.version_option(version=__version__)
Expand Down Expand Up @@ -110,7 +121,16 @@
expose_value=True,
)
@click.option(
"-V", "--salt-version", help="The minimum Salt version to target", default="3003", type=str
"-V", "--salt-version", help="The minimum Salt version to target", default="3005", type=str
)
@click.option(
"--python-requires", help="The minimum Python version to support", default="", type=str
)
@click.option(
"--max-salt-version",
help="The maximum Salt major version to support",
default=CURRENT_LATEST_SALT,
type=int,
)
@click.option(
"-l",
Expand Down Expand Up @@ -151,9 +171,38 @@ def main(
salt_version: str,
force_overwrite: bool,
no_saltext_namespace: bool,
python_requires: str,
max_salt_version: int,
):
destdir: pathlib.Path = pathlib.Path(dest)

try:
salt_version = float(salt_version)
except ValueError as err:
raise ValueError(
f"Cannot parse Salt version: '{salt_version}'. "
"Please specify it like `3006` or `3006.3`."
) from err
if int(salt_version) == salt_version:
salt_version = int(salt_version)
if python_requires:
python_requires = tuple(int(x) for x in python_requires.split("."))
if not (3,) <= python_requires < (4,):
raise ValueError(
f"Invalid Python version specified: '{python_requires}'. Example: '3.8'"
)
salt_python_requires = SALT_PYTHON_SUPPORT[int(salt_version)]["min"]
if not python_requires:
python_requires = salt_python_requires
elif salt_python_requires > python_requires:
click.secho(
f"The minimum Salt version ({salt_version}) requires a higher minimum Python "
f"version '{salt_python_requires}'. Adjusting accordingly.",
fg="bright_red",
)
python_requires = salt_python_requires
max_python_minor = SALT_PYTHON_SUPPORT[max_salt_version]["max"][1]

templating_context: Dict[str, Any] = {
"project_name": project_name,
"author": author,
Expand All @@ -162,6 +211,10 @@ def main(
"url": url,
"salt_version": salt_version,
"copyright_year": datetime.datetime.today().year,
"python_requires": python_requires,
"max_python_minor": max_python_minor,
"max_salt_version": max_salt_version,
"salt_python_support": copy.deepcopy(SALT_PYTHON_SUPPORT),
}
if no_saltext_namespace:
package_namespace = package_namespace_path = package_namespace_pkg = ""
Expand Down Expand Up @@ -225,6 +278,8 @@ def main(
if "__pycache__" in src.parts:
# We're not interested in python pyc cache files
continue
if license == "other" and "LICENSE.j2" in src.parts:
continue
dst_parts = []
templating_context["loader"] = loader
for part in src.relative_to(project_template_path).parts:
Expand Down Expand Up @@ -256,18 +311,19 @@ def main(
fg="bright_red",
)
raise
dst.write_text(contents.rstrip() + "\n")
if contents:
dst.write_text(contents.rstrip() + "\n")
else:
dst.touch()

loaders_package_path = destdir / "src" / package_namespace / package_name
loaders_package_path.mkdir(0o755, parents=True)
loaders_package_path.joinpath("__init__.py").write_text(
Template(PACKAGE_INIT).render(**templating_context).rstrip() + "\n"
)
loaders_package_path.joinpath("loader.py").write_text(
Template(LOADERS_TEMPLATE).render(**templating_context).rstrip() + "\n"
)
loaders_unit_tests_path = destdir / "tests" / "unit"
loaders_integration_tests_path = destdir / "tests" / "integration"
loaders_functional_tests_path = destdir / "tests" / "functional"
for loader_name in loader:
templating_context["loader"] = loader_name
loader_dir = None
Expand Down Expand Up @@ -316,6 +372,31 @@ def main(
loader_unit_test_module = loader_unit_test_module.with_suffix(".new")
loader_unit_test_module.write_text(loader_unit_test_contents.rstrip() + "\n")

if loader_name in SINGULAR_MODULE_DIRS:
loader_functional_tests_dir = loaders_functional_tests_path / loader_name.rstrip("s")
else:
loader_functional_tests_dir = loaders_functional_tests_path / (
loader_name.rstrip("s") + "s"
)
loader_functional_tests_dir.mkdir(0o755, exist_ok=True)
loader_functional_tests_dir_init = loader_functional_tests_dir / "__init__.py"
if not loader_functional_tests_dir_init.exists():
loader_functional_tests_dir_init.write_text("")
if loader_name in ("module", "states"):
if loader_name == "states":
loader_test_template = LOADER_STATE_FUNCTIONAL_TEST_TEMPLATE
else:
loader_test_template = LOADER_MODULE_FUNCTIONAL_TEST_TEMPLATE
loader_functional_test_contents = Template(loader_test_template).render(
**templating_context
)
loader_functional_test_module = loader_functional_tests_dir / f"test_{package_name}.py"
if loader_functional_test_module.exists() and not force_overwrite:
loader_functional_test_module = loader_functional_test_module.with_suffix(".new")
loader_functional_test_module.write_text(
loader_functional_test_contents.rstrip() + "\n"
)

loader_integration_tests_dir = None
if loader_name in SINGULAR_MODULE_DIRS:
loader_integration_tests_dir = loaders_integration_tests_path / loader_name.rstrip("s")
Expand All @@ -338,13 +419,14 @@ def main(
loader_integration_test_module = loader_integration_test_module.with_suffix(".new")
loader_integration_test_module.write_text(loader_integration_test_contents.rstrip() + "\n")

requirements_dir = destdir / "requirements"
for python_version in range(python_requires[1], max_python_minor + 1):
reqdir = requirements_dir / f"py3.{python_version}"
reqdir.mkdir(0o755, exist_ok=True)
for extra in ("docs", "lint", "tests"):
(reqdir / f"{extra}.txt").touch()

click.secho("Bare bones project is created.", fg="bright_green", bold=True)
click.secho(
f"If the {project_name} extension should only target salt 3003 or "
"newer, edit 'setup.cfg', read the comment around 'options.entry-points', "
"and then run the following command:"
)
click.secho(f" rm src/{package_namespace_path}{package_name}/loader.py")
click.secho("You should now run the following commands:")
click.secho(f" python3 -m venv .env --prompt {project_name!r}")
click.secho(" source .env/bin/activate")
Expand Down
94 changes: 57 additions & 37 deletions src/saltext/cli/project/.github/workflows/test.yml.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{%- raw %}
---
name: Testing

on: [push, pull_request]
Expand All @@ -7,22 +7,23 @@ jobs:
Pre-Commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: 3.7
python-version: {{ python_requires[:2] | join(".") }}
{%- raw %}
- name: Set Cache Key
run: echo "PY=$(python --version --version | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
- name: Install System Deps
run: |
sudo apt-get update
sudo apt-get install -y libxml2 libxml2-dev libxslt-dev
- uses: actions/cache@v1
- uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- uses: pre-commit/action@v1.0.1
- uses: pre-commit/action@v3.0.0

Docs:
runs-on: ubuntu-latest
Expand All @@ -31,13 +32,13 @@ jobs:
timeout-minutes: 10

steps:
- uses: actions/checkout@v2

- name: Set up Python 3.7 For Nox
uses: actions/setup-python@v1
- uses: actions/checkout@v4
{% endraw %}
- name: Set up Python {{ python_requires[:2] | join(".") }} For Nox
uses: actions/setup-python@v4
with:
python-version: 3.7

python-version: {{ python_requires[:2] | join(".") }}
{% raw %}
- name: Install Nox
run: |
python -m pip install --upgrade pip
Expand All @@ -63,20 +64,23 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
python-version:
- 3.6
- 3.7
- 3.8
salt-version:
include:
{%- endraw %}
- {{ salt_version }}
{%- for sversion in range(salt_version | int, max_salt_version + 1) %}
{%- for python_version in range(
(python_requires[1], salt_python_support[sversion]["min"][1]) | max,
(max_python_minor, salt_python_support[sversion]["max"][1]) | min + 1
) %}
- {salt-version: "{{ sversion }}", python-version: "3.{{ python_version }}"}
{%- endfor %}
{%- endfor %}
{%- raw %}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand Down Expand Up @@ -170,7 +174,7 @@ jobs:

- name: Upload Logs
if: always()
uses: actions/upload-artifact@main
uses: actions/upload-artifact@v3
with:
name: runtests-${{ runner.os }}-py${{ matrix.python-version }}-Salt${{ matrix.salt-version }}.log
path: artifacts/runtests-*.log
Expand All @@ -185,19 +189,27 @@ jobs:
fail-fast: false
max-parallel: 3
matrix:
python-version:
- 3.6
- 3.7
salt-version:
include:
{%- endraw %}
- {{ salt_version }}
{%- for sversion in range(salt_version | int, max_salt_version + 1) %}
{%- if sversion != max_salt_version %}
- {salt-version: "{{ sversion }}", python-version: "{{ (salt_python_support[sversion]["min"], python_requires) | max | join(".") }}"}
{%- else %}
{%- for python_version in range(
(python_requires[1], salt_python_support[sversion]["min"][1]) | max,
(max_python_minor, salt_python_support[sversion]["max"][1]) | min + 1
) %}
- {salt-version: "{{ sversion }}", python-version: "3.{{ python_version }}"}
{%- endfor %}
{%- endif %}
{%- endfor %}
{%- raw %}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand Down Expand Up @@ -296,7 +308,7 @@ jobs:

- name: Upload Logs
if: always()
uses: actions/upload-artifact@main
uses: actions/upload-artifact@v3
with:
name: runtests-${{ runner.os }}-py${{ matrix.python-version }}-Salt${{ matrix.salt-version }}.log
path: artifacts/runtests-*.log
Expand All @@ -311,19 +323,27 @@ jobs:
fail-fast: false
max-parallel: 3
matrix:
python-version:
- 3.6
- 3.7
salt-version:
include:
{%- endraw %}
- {{ salt_version }}
{%- for sversion in range(salt_version | int, max_salt_version + 1) %}
{%- if sversion != max_salt_version | int %}
- {salt-version: "{{ sversion }}", python-version: "{{ salt_python_support[sversion]["max"] | join(".") }}"}
{%- else %}
{%- for python_version in range(
(python_requires[1], salt_python_support[sversion]["min"][1]) | max,
(max_python_minor, salt_python_support[sversion]["max"][1]) | min + 1
) %}
- {salt-version: "{{ sversion }}", python-version: "3.{{ python_version }}"}
{%- endfor %}
{%- endif %}
{%- endfor %}
{%- raw %}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand Down Expand Up @@ -417,7 +437,7 @@ jobs:

- name: Upload Logs
if: always()
uses: actions/upload-artifact@main
uses: actions/upload-artifact@v3
with:
name: runtests-${{ runner.os }}-py${{ matrix.python-version }}-Salt${{ matrix.salt-version }}.log
path: artifacts/runtests-*.log
Expand Down