1- name : Generate and Manage File Checksums
1+ name : Generate and Manage Individual File Checksums
22
33on :
44 push :
77 paths :
88 - ' files/**'
99 - ' !.github/**'
10- - ' !files/**/*.sha256'
10+ - ' !files/**/*.sha256' # از اجرای مجدد به خاطر فایلهای checksum جلوگیری میکند
1111
1212 workflow_dispatch :
1313
@@ -22,47 +22,67 @@ jobs:
2222 - name : Checkout repository
2323 uses : actions/checkout@v4
2424
25- - name : Generate/Update and Clean Checksums using Shell
25+ - name : Generate and Save Individual Checksums
26+ id : generate_checksums
2627 run : |
27- echo "--- Starting Checksum Generation ---"
28- # 1. Generate/Update checksums for existing files
29- find files -type f -not -name "*.sha256" | while read -r file; do
30- echo "Processing file: $file"
31- # Calculate checksum
32- checksum=$(sha256sum "$file" | awk '{print $1}')
33- # Get just the filename, not the whole path
34- base_filename=$(basename "$file")
35- # Write to the .sha256 file in the desired format
36- echo "$checksum *$base_filename" > "$file.sha256"
37- echo "--> Created/Updated $file.sha256"
38- done
28+ TARGET_DIR="files"
29+
30+ cat << 'EOF' > generate_individual_checksums.py
31+ import os
32+ import hashlib
33+ import sys
3934
40- echo ""
41- echo "--- Starting Stale Checksum Cleanup ---"
42- # 2. Remove stale checksums (where the source file was deleted)
43- find files -type f -name "*.sha256" | while read -r checksum_file; do
44- # Construct the expected source file name by removing the .sha256 extension
45- source_file="${checksum_file%.sha256}"
46- # Check if the source file no longer exists
47- if [ ! -f "$source_file" ]; then
48- echo "Stale checksum found: $checksum_file. Source file '$source_file' is missing."
49- rm "$checksum_file"
50- echo "--> Removed $checksum_file"
51- fi
52- done
53- echo "--- Process Complete ---"
35+ def manage_individual_checksums(root_dir):
36+ # پیدا کردن تمام فایلهای اصلی (غیر از خود checksum ها)
37+ for dirpath, _, filenames in os.walk(root_dir):
38+ # از پردازش پوشه گیت صرف نظر میکنیم
39+ if ".git" in dirpath:
40+ continue
41+
42+ for filename in filenames:
43+ # فقط فایلهایی را پردازش کن که checksum نیستند
44+ if filename.endswith('.sha256'):
45+ continue
5446
55- - name : Check Git status for changes
56- run : |
57- echo "--- Checking for uncommitted changes ---"
58- git status --porcelain
59- echo "----------------------------------------"
47+ file_path = os.path.join(dirpath, filename)
48+ checksum_file_path = f"{file_path}.sha256"
49+
50+ # محاسبه هش SHA256
51+ sha256_hash = hashlib.sha256()
52+ with open(file_path, "rb") as f:
53+ for byte_block in iter(lambda: f.read(4096), b""):
54+ sha256_hash.update(byte_block)
55+
56+ digest = sha256_hash.hexdigest()
57+
58+ # نوشتن هش در فایل .sha256 مخصوص همان فایل
59+ # فرمت استاندارد: هش *نام_فایل
60+ with open(checksum_file_path, "w", encoding="utf-8") as f_out:
61+ f_out.write(f"{digest} *{os.path.basename(file_path)}\n")
62+
63+ print(f"--> Generated/Updated checksum for: {filename}")
64+
65+ if __name__ == "__main__":
66+ if len(sys.argv) > 1:
67+ target_directory = sys.argv[1]
68+ if os.path.isdir(target_directory):
69+ manage_individual_checksums(target_directory)
70+ else:
71+ print(f"Error: Directory '{target_directory}' not found.")
72+ sys.exit(1)
73+ else:
74+ print("Error: Please provide the target directory.")
75+ sys.exit(1)
76+ EOF
77+
78+ # اجرای اسکریپت پایتون
79+ python generate_individual_checksums.py $TARGET_DIR
6080
6181 - name : Commit and push if changed
6282 uses : stefanzweifel/git-auto-commit-action@v5
6383 with :
6484 commit_message : " ci: Update individual file checksums"
65- # The pattern remains the same, as we are still committing .sha256 files
85+ # الگو برای پیدا کردن تمام فایلهای .sha256 جدید یا تغییر یافته
6686 file_pattern : " files/**/*.sha256"
6787 commit_user_name : " GitHub Actions Bot"
6888 commit_user_email : " github-actions[bot]@users.noreply.github.com"
0 commit comments