Skip to content

Commit

Permalink
Introduced comparator module - fix black
Browse files Browse the repository at this point in the history
  • Loading branch information
Veinar committed Nov 26, 2024
1 parent 54c68b3 commit 8f47864
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 18 deletions.
41 changes: 34 additions & 7 deletions envcloak/commands/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,47 @@
from envcloak.utils import debug_log
from envcloak.decorators.common_decorators import debug_option, no_sha_validation_option


@click.command()
@debug_option
@no_sha_validation_option
@click.option("--file1", "-f1", required=True, help="Path to the first encrypted file or directory.")
@click.option("--file2", "-f2", required=True, help="Path to the second encrypted file or directory.")
@click.option("--key1", "-k1", required=True, help="Path to the decryption key file for file1.")
@click.option("--key2", "-k2", required=False, help="Path to the decryption key file for file2. If omitted, key1 is used.")
@click.option("--output", "-o", required=False, help="Path to save the comparison result as a file.")
@click.option(
"--file1",
"-f1",
required=True,
help="Path to the first encrypted file or directory.",
)
@click.option(
"--file2",
"-f2",
required=True,
help="Path to the second encrypted file or directory.",
)
@click.option(
"--key1", "-k1", required=True, help="Path to the decryption key file for file1."
)
@click.option(
"--key2",
"-k2",
required=False,
help="Path to the decryption key file for file2. If omitted, key1 is used.",
)
@click.option(
"--output",
"-o",
required=False,
help="Path to save the comparison result as a file.",
)
def compare(file1, file2, key1, key2, output, skip_sha_validation, debug):
"""
Compare two encrypted environment files or directories.
"""
from envcloak.comparator import compare_files_or_directories

try:
diff = compare_files_or_directories(file1, file2, key1, key2, skip_sha_validation, debug, debug_log)
diff = compare_files_or_directories(
file1, file2, key1, key2, skip_sha_validation, debug, debug_log
)

diff_text = "\n".join(diff)
if output:
Expand All @@ -28,7 +53,9 @@ def compare(file1, file2, key1, key2, output, skip_sha_validation, debug):
click.echo(f"Comparison result saved to {output}")
else:
if diff:
click.echo(style("⚠️ Warning: Files or directories differ.", fg="yellow"))
click.echo(
style("⚠️ Warning: Files or directories differ.", fg="yellow")
)
click.echo(diff_text)
else:
click.echo("The files/directories are identical.")
Expand Down
53 changes: 42 additions & 11 deletions envcloak/comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ def compare_files_or_directories(
debug_log("Debug: Both inputs are files. Decrypting files.", debug)
try:
decrypt_file(
file1, file1_decrypted, key1_bytes, validate_integrity=not skip_sha_validation
file1,
file1_decrypted,
key1_bytes,
validate_integrity=not skip_sha_validation,
)
decrypt_file(
file2, file2_decrypted, key2_bytes, validate_integrity=not skip_sha_validation
file2,
file2_decrypted,
key2_bytes,
validate_integrity=not skip_sha_validation,
)
except FileDecryptionException as e:
raise ValueError(f"Decryption failed: {e}")
Expand All @@ -84,7 +90,10 @@ def compare_files_or_directories(
)
# Compare two directories
elif Path(file1).is_dir() and Path(file2).is_dir():
debug_log("Debug: Both inputs are directories. Decrypting directory contents.", debug)
debug_log(
"Debug: Both inputs are directories. Decrypting directory contents.",
debug,
)
os.makedirs(file1_decrypted, exist_ok=True)
os.makedirs(file2_decrypted, exist_ok=True)

Expand All @@ -103,13 +112,21 @@ def compare_files_or_directories(
for filename, file1_path in file1_files.items():
file1_dec = os.path.join(file1_decrypted, filename.replace(".enc", ""))
if filename in file2_files:
file2_dec = os.path.join(file2_decrypted, filename.replace(".enc", ""))
file2_dec = os.path.join(
file2_decrypted, filename.replace(".enc", "")
)
try:
decrypt_file(
str(file1_path), file1_dec, key1_bytes, validate_integrity=not skip_sha_validation
str(file1_path),
file1_dec,
key1_bytes,
validate_integrity=not skip_sha_validation,
)
decrypt_file(
str(file2_files[filename]), file2_dec, key2_bytes, validate_integrity=not skip_sha_validation
str(file2_files[filename]),
file2_dec,
key2_bytes,
validate_integrity=not skip_sha_validation,
)
except FileDecryptionException as e:
raise ValueError(f"Decryption failed for {filename}: {e}")
Expand All @@ -123,17 +140,31 @@ def compare_files_or_directories(

diff.extend(
difflib.unified_diff(
content1, content2, lineterm="", fromfile=f"File1/{filename}", tofile=f"File2/{filename}"
content1,
content2,
lineterm="",
fromfile=f"File1/{filename}",
tofile=f"File2/{filename}",
)
)
else:
debug_log(f"Debug: File {filename} exists in File1 but not in File2.", debug)
diff.append(f"File present in File1 but missing in File2: {filename}")
debug_log(
f"Debug: File {filename} exists in File1 but not in File2.",
debug,
)
diff.append(
f"File present in File1 but missing in File2: {filename}"
)

for filename in file2_files:
if filename not in file1_files:
debug_log(f"Debug: File {filename} exists in File2 but not in File1.", debug)
diff.append(f"File present in File2 but missing in File1: {filename}")
debug_log(
f"Debug: File {filename} exists in File2 but not in File1.",
debug,
)
diff.append(
f"File present in File2 but missing in File1: {filename}"
)
else:
raise ValueError("Both inputs must either be files or directories.")

Expand Down

0 comments on commit 8f47864

Please sign in to comment.