Skip to content

Commit eba2a52

Browse files
authored
Update 2.yml
1 parent df55ed5 commit eba2a52

1 file changed

Lines changed: 56 additions & 60 deletions

File tree

.github/workflows/2.yml

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Generate Individual File Checksums
1+
name: Generate File Checksums
22

33
on:
44
push:
@@ -7,87 +7,83 @@ on:
77
paths:
88
- 'files/**'
99
- '!.github/**'
10-
- '!files/**/*.sha256'
11-
10+
- '!**/*.sha256' # از اجرای مجدد workflow به خاطر فایل‌های checksum جلوگیری می‌کند
11+
1212
workflow_dispatch:
1313

1414
jobs:
1515
generate-checksums:
16+
# به این جاب اجازه نوشتن در ریپازیتوری را می‌دهد
1617
permissions:
1718
contents: write
18-
19+
1920
runs-on: ubuntu-latest
2021

2122
steps:
2223
- name: Checkout repository
2324
uses: actions/checkout@v4
2425

25-
- name: Generate and Remove Stale Checksums
26+
- name: Generate and Save Individual Checksums
27+
id: generate_checksums
2628
run: |
2729
TARGET_DIR="files"
2830
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
5036
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}")
7165
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
8478
85-
python manage_checksums.py $TARGET_DIR
79+
# اجرای اسکریپت پایتون
80+
python generate_checksums.py $TARGET_DIR
8681
8782
- name: Commit and push if changed
8883
uses: stefanzweifel/git-auto-commit-action@v5
8984
with:
90-
commit_message: "ci: Update individual file checksums"
85+
commit_message: "ci: Update file checksums"
86+
# الگو برای پیدا کردن تمام فایل‌های .sha256 در پوشه files و زیرپوشه‌های آن
9187
file_pattern: "files/**/*.sha256"
9288
commit_user_name: "GitHub Actions Bot"
9389
commit_user_email: "github-actions[bot]@users.noreply.github.com"

0 commit comments

Comments
 (0)