Skip to content

Commit

Permalink
chore: remove pylint references (#141)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Mannocci <[email protected]>
  • Loading branch information
amannocci authored Dec 30, 2024
1 parent ce2f75b commit 3712866
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 62 deletions.
4 changes: 3 additions & 1 deletion scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def run() -> None:
pyinstaller("terranova.spec", _out=sys.stdout, _err=sys.stderr)
arch = platform.machine()
arch = "amd64" if arch == "x86_64" else arch
Path("./dist/terranova").replace(Path(f"./dist/terranova-{version}-{system}-{arch}"))
Path("./dist/terranova").replace(
Path(f"./dist/terranova-{version}-{system}-{arch}")
)
elif system == "linux":
# Use cross-build to build both amd64 and arm64 versions.
cmd, env = container_backend()
Expand Down
2 changes: 1 addition & 1 deletion scripts/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from sh import ErrorReturnCode

from scripts.utils import detect_git, detect_uv, detect_ruff, fatal
from scripts.utils import detect_git, detect_ruff, detect_uv, fatal


def git_branch_delete(branch_name: str) -> None:
Expand Down
49 changes: 39 additions & 10 deletions scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,56 @@ def __set_version(version: str) -> None:
try:
data = Constants.TERRANOVA_INIT_PATH.read_text()
except Exception as err:
print(f"The `{Constants.TERRANOVA_INIT_PATH.as_posix()}` can't be read", file=sys.stderr)
print(
f"The `{Constants.TERRANOVA_INIT_PATH.as_posix()}` can't be read",
file=sys.stderr,
)
raise err

data = re.sub(r"__version__ = \"(.*)\"", f'__version__ = "{version}"', data, count=1)
data = re.sub(
r"__version__ = \"(.*)\"", f'__version__ = "{version}"', data, count=1
)
try:
Constants.TERRANOVA_INIT_PATH.write_text(data)
except Exception as err:
print(f"The `{Constants.TERRANOVA_INIT_PATH.as_posix()}` file can't be written", file=sys.stderr)
print(
f"The `{Constants.TERRANOVA_INIT_PATH.as_posix()}` file can't be written",
file=sys.stderr,
)
raise err

# Update project version
try:
data = Constants.PYPROJECT_PATH.read_text()
except Exception as err:
print(f"The `{Constants.PYPROJECT_PATH.as_posix()}` can't be read", file=sys.stderr)
print(
f"The `{Constants.PYPROJECT_PATH.as_posix()}` can't be read",
file=sys.stderr,
)
raise err

data = re.sub(r"version = \"(.+)\"", f'version = "{version}"', data, count=1)
try:
Constants.PYPROJECT_PATH.write_text(data)
except Exception as err:
print(f"The `{Constants.PYPROJECT_PATH.as_posix()}` file can't be written", file=sys.stderr)
print(
f"The `{Constants.PYPROJECT_PATH.as_posix()}` file can't be written",
file=sys.stderr,
)
raise err


def pre() -> None:
# Ensure we have inputs
release_version = os.getenv("RELEASE_VERSION")
if not release_version:
return print("You must define `RELEASE_VERSION` environment variable.", file=sys.stderr)
return print(
"You must define `RELEASE_VERSION` environment variable.", file=sys.stderr
)
if not re.match(r"(\d){1,2}\.(\d){1,2}\.(\d){1,2}", release_version):
return print("The `RELEASE_VERSION` should match semver format.", file=sys.stderr)
return print(
"The `RELEASE_VERSION` should match semver format.", file=sys.stderr
)

# Create a new branch
branch_name = f"release/v{release_version}"
Expand All @@ -74,7 +92,14 @@ def pre() -> None:

# Push release branch
git("add", "--all", _out=sys.stdout, _err=sys.stderr)
git("commit", "-m", f"release: terranova v{release_version}", "--no-verify", _out=sys.stdout, _err=sys.stderr)
git(
"commit",
"-m",
f"release: terranova v{release_version}",
"--no-verify",
_out=sys.stdout,
_err=sys.stderr,
)
git("push", "origin", branch_name, _out=sys.stdout, _err=sys.stderr)

# Create a PR
Expand All @@ -100,7 +125,9 @@ def run() -> None:
git("tag", release_version)
git("push", "origin", release_version)
except ErrorReturnCode:
return print(f"The release `v{release_version}` already exists.", file=sys.stderr)
return print(
f"The release `v{release_version}` already exists.", file=sys.stderr
)

# Create the release
args = [
Expand All @@ -121,7 +148,9 @@ def post() -> None:
# Ensure we have inputs
next_version = os.getenv("NEXT_VERSION")
if not next_version:
return print("You must define `NEXT_VERSION` environment variable", file=sys.stderr)
return print(
"You must define `NEXT_VERSION` environment variable", file=sys.stderr
)
if not re.match(r"(\d){1,2}\.(\d){1,2}\.(\d){1,2}-dev", next_version):
return print("The `NEXT_VERSION` should match semver format.", file=sys.stderr)

Expand Down
4 changes: 0 additions & 4 deletions terranova/binds.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from .utils import Constants, Log, SharedContext


# pylint: disable=R0903
class Bind:
"""Represents an abstract external bind."""

Expand All @@ -48,7 +47,6 @@ def binary_path(self) -> Path:
Returns:
path where binary is installed.
"""
# pylint: disable=protected-access
return Path(self._cmd._path)

@contextmanager
Expand Down Expand Up @@ -153,7 +151,6 @@ def is_allowed_env_var(env_var: str) -> bool:
kwargs["_cwd"] = self.__work_dir
return super()._exec_ctx(*args, **kwargs)

# pylint: disable=too-many-arguments,too-many-positional-arguments
def init(
self,
backend_config: dict[str, str] | None = None,
Expand Down Expand Up @@ -196,7 +193,6 @@ def fmt(self) -> None:
"""Reformat your configuration in the standard style."""
self._exec("fmt", _inherit=True)

# pylint: disable=redefined-builtin,too-many-positional-arguments
def plan(
self,
compact_warnings: bool,
Expand Down
110 changes: 84 additions & 26 deletions terranova/commands/binds.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,40 @@
from terranova.utils import Constants, Log, SharedContext


# pylint: disable=R0914
@click.command("init")
@click.argument("path", type=str, required=False)
@click.option("--migrate-state", help="Reconfigure a backend, and attempt to migrate any existing state.", is_flag=True)
@click.option(
"--no-backend", help="Disable backend for this configuration and use what was previously instead.", is_flag=True
"--migrate-state",
help="Reconfigure a backend, and attempt to migrate any existing state.",
is_flag=True,
)
@click.option(
"--no-backend",
help="Disable backend for this configuration and use what was previously instead.",
is_flag=True,
)
@click.option(
"--reconfigure",
help="Reconfigure a backend, ignoring any saved configuration.",
is_flag=True,
)
@click.option(
"--upgrade", help="Install the latest module and provider versions.", is_flag=True
)
@click.option("--reconfigure", help="Reconfigure a backend, ignoring any saved configuration.", is_flag=True)
@click.option("--upgrade", help="Install the latest module and provider versions.", is_flag=True)
@click.option(
"--fail-at-end",
help="If specified, only fail afterwards; allow all non-impacted projects to continue.",
default=False,
is_flag=True,
)
# pylint: disable-next=R0913,too-many-positional-arguments
def init(
path: str | None, migrate_state: bool, no_backend: bool, reconfigure: bool, upgrade: bool, fail_at_end: bool
path: str | None,
migrate_state: bool,
no_backend: bool,
reconfigure: bool,
upgrade: bool,
fail_at_end: bool,
) -> None:
# pylint: disable=R0912
"""Init resources manifest."""
# Find all resources manifests
paths = resource_dirs(path)
Expand Down Expand Up @@ -100,7 +114,9 @@ def init(
target_dirname = os.path.dirname(dependency.target)
os.symlink(
os.path.relpath(
SharedContext.shared_dir().joinpath(dependency.source).as_posix(),
SharedContext.shared_dir()
.joinpath(dependency.source)
.as_posix(),
full_path.joinpath(target_dirname).as_posix(),
),
dependency.target,
Expand All @@ -126,7 +142,9 @@ def init(
# Mount terraform context
terraform = mount_context(full_path, manifest)
terraform.init(
backend_config={"key": os.path.relpath(full_path, SharedContext.resources_dir())},
backend_config={
"key": os.path.relpath(full_path, SharedContext.resources_dir())
},
migrate_state=migrate_state,
no_backend=no_backend,
reconfigure=reconfigure,
Expand All @@ -144,7 +162,9 @@ def init(

@click.command("get")
@click.argument("path", type=str, required=False)
@click.option("--selector", "selectors", type=SelectorType(), required=False, multiple=True)
@click.option(
"--selector", "selectors", type=SelectorType(), required=False, multiple=True
)
def get(path: str | None, selectors: list[Selector] | None) -> None:
"""Display one or many resources."""
# Find all resources manifests
Expand Down Expand Up @@ -221,7 +241,9 @@ def validate(path: str | None, fail_at_end: bool) -> None:

# Report any errors if fail_at_end has been enabled
if errors:
Log.fatal("The syntax is probably incorrect in one of the projects. See above for errors.")
Log.fatal(
"The syntax is probably incorrect in one of the projects. See above for errors."
)


@click.command("docs")
Expand All @@ -240,7 +262,14 @@ def docs(docs_dir: Path) -> None:
for file in files:
if os.path.basename(file) == Constants.MANIFEST_FILE_NAME:
jobs.append(
(Path(path), docs_dir.joinpath(os.path.relpath(path, SharedContext.resources_dir().as_posix())))
(
Path(path),
docs_dir.joinpath(
os.path.relpath(
path, SharedContext.resources_dir().as_posix()
)
),
)
)

# Clean docs dir
Expand All @@ -260,20 +289,32 @@ def docs(docs_dir: Path) -> None:
# Write documentation file
rendering = tmpl.render({"manifest": manifest, "resources": resources})
formatted = mdformat.text(rendering)
target_path.with_suffix(".md").write_text(data=formatted, encoding=Constants.ENCODING_UTF_8)
target_path.with_suffix(".md").write_text(
data=formatted, encoding=Constants.ENCODING_UTF_8
)


# pylint: disable=redefined-builtin
@click.command("plan")
@click.argument("path", type=str, required=False)
@click.option(
"--compact-warnings",
help="If Terraform produces any warnings that are not by errors, show them in a more compact that includes only the summary messages.",
is_flag=True,
)
@click.option("--input/--no-input", help="Ask for input for variables if not directly set.", default=True)
@click.option("--no-color", help="If specified, output won't contain any color.", is_flag=True)
@click.option("--parallelism", help="Limit the number of parallel resource operations.", type=int, default=10)
@click.option(
"--input/--no-input",
help="Ask for input for variables if not directly set.",
default=True,
)
@click.option(
"--no-color", help="If specified, output won't contain any color.", is_flag=True
)
@click.option(
"--parallelism",
help="Limit the number of parallel resource operations.",
type=int,
default=10,
)
@click.option(
"--fail-at-end",
help="If specified, only fail afterwards; allow all non-impacted projects to continue.",
Expand All @@ -298,7 +339,6 @@ def docs(docs_dir: Path) -> None:
type=click.Path(path_type=Path, dir_okay=False, writable=True),
required=False,
)
# pylint: disable-next=R0913,too-many-positional-arguments
def plan(
path: str | None,
compact_warnings: bool,
Expand Down Expand Up @@ -343,10 +383,14 @@ def plan(
args["out"] = path
try:
terraform.plan(**args)
execution_plan[rel_path] = b64encode(path.read_bytes()).decode(Constants.ENCODING_UTF_8)
execution_plan[rel_path] = b64encode(path.read_bytes()).decode(
Constants.ENCODING_UTF_8
)
except ErrorReturnCode as plan_err:
if plan_err.exit_code == 2:
execution_plan[rel_path] = b64encode(path.read_bytes()).decode(Constants.ENCODING_UTF_8)
execution_plan[rel_path] = b64encode(
path.read_bytes()
).decode(Constants.ENCODING_UTF_8)
raise plan_err
else:
terraform.plan(**args)
Expand Down Expand Up @@ -383,19 +427,27 @@ def write_plan():

@click.command("apply")
@click.argument("path_or_plan", type=str, required=False)
@click.option("--auto-approve", help="Skip interactive approval of plan before applying.", is_flag=True)
@click.option(
"--auto-approve",
help="Skip interactive approval of plan before applying.",
is_flag=True,
)
@click.option("--target", help="Apply changes for specific target.", type=str)
@click.option(
"--fail-at-end",
help="If specified, only fail afterwards; allow all non-impacted projects to continue.",
default=False,
is_flag=True,
)
def apply(path_or_plan: str | None, auto_approve: bool, target: str, fail_at_end: bool) -> None:
def apply(
path_or_plan: str | None, auto_approve: bool, target: str, fail_at_end: bool
) -> None:
"""Create or update resources."""
# Check if there is a plan to apply
if path_or_plan.endswith("tnplan"):
execution_plan = json.loads(Path(path_or_plan).read_text(Constants.ENCODING_UTF_8))
execution_plan = json.loads(
Path(path_or_plan).read_text(Constants.ENCODING_UTF_8)
)
paths = []
for rel_path in execution_plan.keys():
paths.append((SharedContext.resources_dir().joinpath(rel_path), rel_path))
Expand All @@ -421,7 +473,11 @@ def apply(path_or_plan: str | None, auto_approve: bool, target: str, fail_at_end
with NamedTemporaryFile(prefix="terranova-") as file_descriptor:
path = Path(file_descriptor.name)
path.write_bytes(b64decode(execution_plan[rel_path]))
terraform.apply(plan=file_descriptor.name, auto_approve=auto_approve, target=target)
terraform.apply(
plan=file_descriptor.name,
auto_approve=auto_approve,
target=target,
)
else:
terraform.apply(auto_approve=auto_approve, target=target)
except ErrorReturnCode:
Expand Down Expand Up @@ -547,7 +603,9 @@ def runbook(path: str, name: str) -> None:
manifest = read_manifest(full_path)

# Extract runbook
matching_runbooks = [rb for rb in manifest.runbooks if rb.name == name] if manifest.runbooks else []
matching_runbooks = (
[rb for rb in manifest.runbooks if rb.name == name] if manifest.runbooks else []
)
if not matching_runbooks:
Log.fatal("execute runbook", MissingRunbookError(name))
if len(matching_runbooks) > 1:
Expand Down
Loading

0 comments on commit 3712866

Please sign in to comment.