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

chore: add unique id to write-sdl bin script #4169

Merged
merged 2 commits into from
Mar 12, 2024
Merged
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
34 changes: 34 additions & 0 deletions toolchains/workspace-pnpm/package_prod_tsc_build_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,38 @@
production node_modules.
"""
import argparse
import hashlib
import os
import stat

def hash_dir(dir_path):
def hash_file(filepath):
"""Hash a single file using SHA-256 and return the hex digest."""
hasher = hashlib.sha256()
with open(filepath, 'rb') as f:
buf = f.read()
hasher.update(buf)
return hasher.hexdigest()

def hash_directory(directory_path):
"""Recursively hash all files in directory and return a dictionary of file paths and their hashes."""
file_hashes = {}
for root, dirs, files in os.walk(directory_path):
for filename in files:
filepath = os.path.join(root, filename)
file_hashes[filepath] = hash_file(filepath)
return file_hashes

def combine_hashes(file_hashes):
"""Combine file hashes into a single directory fingerprint."""
combined_hash_string = ''.join(hash_val for _, hash_val in sorted(file_hashes.items()))
return hashlib.sha256(combined_hash_string.encode()).hexdigest()

file_hashes = hash_directory(dir_path)

return combine_hashes(file_hashes)


if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
Expand Down Expand Up @@ -49,6 +78,11 @@
js_path = os.path.join(dist_path, args.run_file)
binary_content = [
"#!/usr/bin/env sh",
"",
f"# TODO: execute natively in buck by figuring out some way to make the 'prod_tsc_build_bin'",
f"# rule return a changed state of its deps for any consuming rules.",
f"# Deps hash: {hash_dir(dist_path)}",
"",
f"exec node \"{js_path}\" \"$@\""
]

Expand Down
Loading