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