Skip to content

fix some code

fix some code #69

name: Update README Table
on:
push:
branches:
- main
paths:
- 'daily/**' # 仅监控 daily 目录下的变更
- '!daily/notify.py' # 排除 notify.py
- '!daily/**/*.md' # 排除 md 文件
jobs:
update-readme:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: 检出代码
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: 配置 Python 环境
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: 运行更新脚本
env:
TZ: Asia/Shanghai
run: |
cat << 'EOF' > update_readme.py
import os
import re
import datetime
import subprocess
# 1. 获取本次 Push 更改的文件列表
try:
diff_cmd = ['git', 'diff', '--name-only', 'HEAD~1', 'HEAD']
result = subprocess.run(diff_cmd, capture_output=True, text=True, check=True)
changed_files = result.stdout.splitlines()
except Exception:
try:
diff_cmd = ['git', 'diff', '--name-only', '--cached']
result = subprocess.run(diff_cmd, capture_output=True, text=True, check=True)
changed_files = result.stdout.splitlines()
except Exception:
changed_files = []
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# 2. 提取变更文件中的 Env 名称及更新时间
updates = {}
for f in changed_files:
if f.startswith('daily/') and os.path.exists(f):
filename = os.path.basename(f)
if filename != 'notify.py' and not f.endswith('.md'):
with open(f, 'r', encoding='utf-8') as file:
match = re.search(r'Env\s*\(\s*["\']([^"\']+)["\']\s*\)', file.read())
if match:
updates[match.group(1)] = now
# 3. 遍历 daily 目录获取所有的 Env(用于找出表格中缺失的)
all_envs = set()
if os.path.exists('daily'):
for root, _, files in os.walk('daily'):
for file in files:
if file != 'notify.py' and not file.endswith('.md'):
fpath = os.path.join(root, file).replace('\\', '/')
with open(fpath, 'r', encoding='utf-8') as f:
match = re.search(r'Env\s*\(\s*["\']([^"\']+)["\']\s*\)', f.read())
if match:
all_envs.add(match.group(1))
# 4. 修改 README.md
readme_path = 'README.md'
if not os.path.exists(readme_path):
print("README.md 不存在!")
exit(1)
with open(readme_path, 'r', encoding='utf-8') as f:
full_text = f.read()
start_tag = "<!-- README_TABLE_START -->"
end_tag = "<!-- README_TABLE_END -->"
if start_tag not in full_text or end_tag not in full_text:
print("错误:未找到锚点标记!")
exit(1)
pre_part, temp = full_text.split(start_tag)
table_section, post_part = temp.split(end_tag)
lines = table_section.strip().split('\n')
header, separator, body_lines = [], [], []
mode = 'header'
for line in lines:
l_s = line.strip()
if not l_s.startswith('|'): continue
if '脚本名称' in l_s:
header.append(line)
mode = 'separator'
elif '---' in l_s and mode == 'separator':
separator.append(line)
mode = 'body'
elif mode == 'body':
body_lines.append(line)
existing_names = set()
new_body_lines = []
# A. 处理表格中已存在的脚本
for line in body_lines:
parts = [p.strip() for p in line.split('|')]
if len(parts) >= 4:
name = parts[1]
existing_names.add(name)
# 如果该脚本在本次 push 中被修改,更新时间
if name in updates:
line = f"| {name} | {updates[name]} | ✅ |"
new_body_lines.append(line)
# B. 添加本次修改引发的新增脚本
for name, tm in updates.items():
if name not in existing_names:
new_body_lines.append(f"| {name} | {tm} | ✅ |")
existing_names.add(name)
# C. 添加存在于 daily 目录但 README 表格中缺失的脚本
for name in all_envs:
if name not in existing_names:
new_body_lines.append(f"| {name} | {now} | ✅ |")
existing_names.add(name)
final_table = "\n".join(header + separator + new_body_lines)
final_content = f"{pre_part}{start_tag}\n{final_table}\n{end_tag}{post_part}"
with open(readme_path, 'w', encoding='utf-8') as f:
f.write(final_content)
print("README.md 处理完成。")
EOF
python update_readme.py
- name: 提交并推送变更
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add README.md
if ! git diff --staged --quiet; then
git commit -m "docs: 自动更新 daily 脚本状态 [skip ci]"
git push
else
echo "无变更需推送。"
fi