Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions pre_commit_hooks/cfn_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
This module contains the logic for the cfn-guard pre-commit hook
"""

from __future__ import annotations

import argparse
import os
import platform
import shutil
import subprocess
import sys
import tarfile
import tempfile
import argparse
from collections.abc import Sequence
from pathlib import Path
from typing import Sequence, Union
from urllib.request import Request, urlopen

BIN_NAME = "cfn-guard"
Expand Down Expand Up @@ -84,9 +86,8 @@ def install_cfn_guard():
if current_os in supported_oses:
url = release_urls_dict[current_os].replace("TAG", GUARD_BINARY_VERSION)
# Download tarball of release from Github
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
with urlopen(url) as response:
shutil.copyfileobj(response, temp_file)
with tempfile.NamedTemporaryFile(delete=False) as temp_file, urlopen(url) as response:
shutil.copyfileobj(response, temp_file)

# Create the install_dir if it doesn't exist
os.makedirs(install_dir, exist_ok=True)
Expand All @@ -100,12 +101,10 @@ def install_cfn_guard():
filename = os.path.basename(member.name)
# Join the install_dir path and the filename to get the full target path
file_path = os.path.join(install_dir, filename)
# Open the archived file
with tar.extractfile(member) as source:
# Create a new file using the file_path with write binary mode
with open(file_path, "wb") as target:
# Copy the contents of the archived file(s) to the target file
shutil.copyfileobj(source, target)
# Open the archived file, and a new file at file_path in write binary mode
with tar.extractfile(member) as source, open(file_path, "wb") as target:
# Copy the contents of the archived file(s) to the target file
shutil.copyfileobj(source, target)

binary_path = os.path.join(install_dir, binary_name)
os.chmod(binary_path, 0o755)
Expand Down Expand Up @@ -135,7 +134,7 @@ def run_cfn_guard(args: str) -> int:
return run_cfn_guard(args)


def main(argv: Union[Sequence[str], None] = None) -> int:
def main(argv: Sequence[str] | None = None) -> int:
"""Entry point for the pre-commit hook"""
if argv is None:
argv = sys.argv[1:]
Expand Down
4 changes: 2 additions & 2 deletions pre_commit_hooks_tests/test_cfn_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from __future__ import annotations

from pre_commit_hooks.cfn_guard import main

import os.path

from pre_commit_hooks.cfn_guard import main


def get_guard_resource_path(relative_path):
return os.path.join(os.path.abspath(__file__ + "/../../")) + "/guard/resources" + relative_path
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest
mypy
black
ruff
ruff>=0.16.4,<0.17
Loading