|
1 | | -name: Generate Individual File Checksums |
| 1 | +name: Generate File Checksums |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
|
7 | 7 | paths: |
8 | 8 | - 'files/**' |
9 | 9 | - '!.github/**' |
10 | | - - '!files/**/*.sha256' |
11 | | - |
| 10 | + - '!**/*.sha256' # از اجرای مجدد workflow به خاطر فایلهای checksum جلوگیری میکند |
| 11 | + |
12 | 12 | workflow_dispatch: |
13 | 13 |
|
14 | 14 | jobs: |
15 | 15 | generate-checksums: |
| 16 | + # به این جاب اجازه نوشتن در ریپازیتوری را میدهد |
16 | 17 | permissions: |
17 | 18 | contents: write |
18 | | - |
| 19 | + |
19 | 20 | runs-on: ubuntu-latest |
20 | 21 |
|
21 | 22 | steps: |
22 | 23 | - name: Checkout repository |
23 | 24 | uses: actions/checkout@v4 |
24 | 25 |
|
25 | | - - name: Generate and Remove Stale Checksums |
| 26 | + - name: Generate and Save Individual Checksums |
| 27 | + id: generate_checksums |
26 | 28 | run: | |
27 | 29 | TARGET_DIR="files" |
28 | 30 | |
29 | | - cat << 'EOF' > manage_checksums.py |
30 | | -import os |
31 | | -import hashlib |
32 | | -import sys |
33 | | - |
34 | | -def manage_checksums(root_dir): |
35 | | - source_files = set() |
36 | | - for dirpath, _, filenames in os.walk(root_dir): |
37 | | - if ".git" in dirpath: |
38 | | - continue |
39 | | - for filename in filenames: |
40 | | - if not filename.endswith('.sha256'): |
41 | | - source_files.add(os.path.join(dirpath, filename)) |
42 | | - |
43 | | - existing_checksum_files = set() |
44 | | - for dirpath, _, filenames in os.walk(root_dir): |
45 | | - if ".git" in dirpath: |
46 | | - continue |
47 | | - for filename in filenames: |
48 | | - if filename.endswith('.sha256'): |
49 | | - existing_checksum_files.add(os.path.join(dirpath, filename)) |
| 31 | + # اسکریپت پایتون برای ساخت فایلهای checksum جداگانه |
| 32 | + cat << 'EOF' > generate_checksums.py |
| 33 | + import os |
| 34 | + import hashlib |
| 35 | + import sys |
50 | 36 |
|
51 | | - for file_path in source_files: |
52 | | - sha256_hash = hashlib.sha256() |
53 | | - with open(file_path, "rb") as f: |
54 | | - for byte_block in iter(lambda: f.read(4096), b""): |
55 | | - sha256_hash.update(byte_block) |
56 | | - |
57 | | - checksum_file_path = f"{file_path}.sha256" |
58 | | - base_filename = os.path.basename(file_path) |
59 | | - |
60 | | - with open(checksum_file_path, "w", encoding="utf-8") as f: |
61 | | - f.write(f"{sha256_hash.hexdigest()} *{base_filename}\n") |
62 | | - print(f"Generated/Updated checksum for {file_path}") |
63 | | - |
64 | | - required_checksum_files = {f"{f}.sha256" for f in source_files} |
65 | | - stale_files = existing_checksum_files - required_checksum_files |
66 | | - |
67 | | - for stale_file in stale_files: |
68 | | - if os.path.exists(stale_file): |
69 | | - os.remove(stale_file) |
70 | | - print(f"Removed stale checksum file: {stale_file}") |
| 37 | + def create_individual_checksums(root_dir): |
| 38 | + """ |
| 39 | + برای هر فایل در دایرکتوری مشخص شده، یک فایل .sha256 جداگانه ایجاد میکند. |
| 40 | + """ |
| 41 | + for dirpath, _, filenames in os.walk(root_dir): |
| 42 | + for filename in filenames: |
| 43 | + # از پردازش فایلهای checksum موجود صرف نظر میکند |
| 44 | + if filename.endswith('.sha256') or ".git" in dirpath: |
| 45 | + continue |
| 46 | + |
| 47 | + file_path = os.path.join(dirpath, filename) |
| 48 | + output_filepath = f"{file_path}.sha256" |
| 49 | + |
| 50 | + # محاسبه هش SHA256 |
| 51 | + sha256_hash = hashlib.sha256() |
| 52 | + with open(file_path, "rb") as f: |
| 53 | + # خواندن فایل به صورت تکهتکه برای مدیریت فایلهای بزرگ |
| 54 | + for byte_block in iter(lambda: f.read(4096), b""): |
| 55 | + sha256_hash.update(byte_block) |
| 56 | + |
| 57 | + digest = sha256_hash.hexdigest() |
| 58 | + |
| 59 | + # نوشتن هش در فایل .sha256 مخصوص همان فایل |
| 60 | + with open(output_filepath, "w", encoding="utf-8") as f_out: |
| 61 | + # فرمت استاندارد: هش *نام_فایل |
| 62 | + f_out.write(f"{digest} *{os.path.basename(file_path)}\n") |
| 63 | + |
| 64 | + print(f"Checksum for {filename} saved to {output_filepath}") |
71 | 65 |
|
72 | | -if __name__ == "__main__": |
73 | | - if len(sys.argv) != 2: |
74 | | - print("Usage: python manage_checksums.py <target_directory>") |
75 | | - sys.exit(1) |
76 | | - |
77 | | - target_directory = sys.argv[1] |
78 | | - if os.path.isdir(target_directory): |
79 | | - manage_checksums(target_directory) |
80 | | - else: |
81 | | - print(f"Error: Directory '{target_directory}' not found.") |
82 | | - sys.exit(1) |
83 | | -EOF |
| 66 | + if __name__ == "__main__": |
| 67 | + if len(sys.argv) > 1: |
| 68 | + target_directory = sys.argv[1] |
| 69 | + if os.path.isdir(target_directory): |
| 70 | + create_individual_checksums(target_directory) |
| 71 | + else: |
| 72 | + print(f"Error: Directory '{target_directory}' not found.") |
| 73 | + sys.exit(1) |
| 74 | + else: |
| 75 | + print("Error: Please provide the target directory.") |
| 76 | + sys.exit(1) |
| 77 | + EOF |
84 | 78 |
|
85 | | - python manage_checksums.py $TARGET_DIR |
| 79 | + # اجرای اسکریپت پایتون |
| 80 | + python generate_checksums.py $TARGET_DIR |
86 | 81 |
|
87 | 82 | - name: Commit and push if changed |
88 | 83 | uses: stefanzweifel/git-auto-commit-action@v5 |
89 | 84 | with: |
90 | | - commit_message: "ci: Update individual file checksums" |
| 85 | + commit_message: "ci: Update file checksums" |
| 86 | + # الگو برای پیدا کردن تمام فایلهای .sha256 در پوشه files و زیرپوشههای آن |
91 | 87 | file_pattern: "files/**/*.sha256" |
92 | 88 | commit_user_name: "GitHub Actions Bot" |
93 | 89 | commit_user_email: "github-actions[bot]@users.noreply.github.com" |
0 commit comments