Skip to content

Commit a2c86cb

Browse files
authored
ci: tolerate missing contest pages and fail formatter commands (#5407)
Write contest stubs if CONTEST_README is absent so deploy does not abort. Replace os.system in run_format.py with checked subprocess.
1 parent 7012817 commit a2c86cb

2 files changed

Lines changed: 25 additions & 66 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,18 @@ jobs:
9797
fi
9898
rsync -a mkdocs/build_site.py ./
9999
rm -rf mkdocs
100-
mv solution/CONTEST_README.md docs/contest.md
101-
mv solution/CONTEST_README_EN.md docs-en/contest.md
100+
if [ -f solution/CONTEST_README.md ]; then
101+
mv solution/CONTEST_README.md docs/contest.md
102+
else
103+
echo "warning: solution/CONTEST_README.md missing; writing a stub."
104+
printf '%s\n' '---' 'comments: true' '---' '' '# 力扣竞赛' '' '竞赛列表暂未包含在本次构建中。' > docs/contest.md
105+
fi
106+
if [ -f solution/CONTEST_README_EN.md ]; then
107+
mv solution/CONTEST_README_EN.md docs-en/contest.md
108+
else
109+
echo "warning: solution/CONTEST_README_EN.md missing; writing a stub."
110+
printf '%s\n' '---' 'comments: true' '---' '' '# LeetCode Contest' '' 'The contest list is not included in this build.' > docs-en/contest.md
111+
fi
102112
103113
- name: Set MKDOCS_API_KEYS
104114
run: echo "MKDOCS_API_KEYS=${{ secrets.MKDOCS_API_KEYS }}" >> $GITHUB_ENV

run_format.py

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import List
22

33
import os.path
4-
import platform
54
import subprocess
65
import sys
76
import re
@@ -301,11 +300,13 @@ def format_inline_code(path: str):
301300
f.write(block)
302301
if suf == "go":
303302
added = add_header(file)
304-
os.system(f'gofmt -w "{file}"')
303+
subprocess.check_call(["gofmt", "-w", file])
305304
if added:
306305
remove_header(file)
307306
else:
308-
os.system(f'npx clang-format -i --style=file "{file}"')
307+
subprocess.check_call(
308+
["npx", "clang-format", "-i", "--style=file", file]
309+
)
309310
with open(file, "r", encoding="utf-8") as f:
310311
new_block = f.read()
311312
if not new_block.endswith("\n"):
@@ -329,7 +330,7 @@ def format_inline_code(path: str):
329330
file = f"{root}/tmp.rs"
330331
with open(file, "w", encoding="utf-8") as f:
331332
f.write(block)
332-
os.system(f'rustfmt "{file}"')
333+
subprocess.check_call(["rustfmt", file])
333334
with open(file, "r", encoding="utf-8") as f:
334335
new_block = f.read()
335336
if not new_block.endswith("\n"):
@@ -341,53 +342,10 @@ def format_inline_code(path: str):
341342
f.write(content)
342343

343344

344-
def format_rust_files_linux():
345-
# The find command to locate and format all .rs files in Linux
346-
find_command = 'find . -name "*.rs" -exec rustfmt {} \\;'
347-
348-
# Execute the command
349-
process = subprocess.Popen(
350-
find_command,
351-
shell=True,
352-
stdout=subprocess.PIPE,
353-
stderr=subprocess.PIPE,
354-
text=True,
355-
)
356-
357-
# Get the output and errors
358-
stdout, stderr = process.communicate()
359-
360-
if process.returncode == 0:
361-
print("Rust files formatted successfully on Linux!")
362-
print(stdout)
363-
else:
364-
print("Error formatting Rust files on Linux:")
365-
print(stderr)
366-
367-
368-
def format_rust_files_windows():
369-
# PowerShell command to format all .rs files recursively in Windows
370-
ps_command = (
371-
"Get-ChildItem -Recurse -Filter *.rs | ForEach-Object { rustfmt $_.FullName }"
372-
)
373-
374-
# Execute the PowerShell command
375-
process = subprocess.Popen(
376-
["powershell", "-Command", ps_command],
377-
stdout=subprocess.PIPE,
378-
stderr=subprocess.PIPE,
379-
text=True,
380-
)
381-
382-
# Get the output and errors
383-
stdout, stderr = process.communicate()
384-
385-
if process.returncode == 0:
386-
print("Rust files formatted successfully on Windows!")
387-
print(stdout)
388-
else:
389-
print("Error formatting Rust files on Windows:")
390-
print(stderr)
345+
def format_rust_files(paths: List[str]) -> None:
346+
rs_files = [p for p in paths if p.endswith(".rs")]
347+
for i in range(0, len(rs_files), 64):
348+
subprocess.check_call(["rustfmt", *rs_files[i : i + 64]])
391349

392350

393351
def run():
@@ -399,20 +357,11 @@ def run():
399357
if add_header(path):
400358
headered.append(path)
401359
if any(path.endswith(suf) for suf in ["c", "cpp", "java"]):
402-
# format with clang-format
403-
os.system(f'npx clang-format -i --style=file "{path}"')
404-
405-
# format with prettier
406-
os.system('npx prettier --write "**/*.{js,ts,php,sql,md}"')
360+
subprocess.check_call(["npx", "clang-format", "-i", "--style=file", path])
407361

408-
# format with gofmt
409-
os.system("gofmt -w .")
410-
411-
# format with rustfmt
412-
if platform.system() == "Linux":
413-
format_rust_files_linux()
414-
else:
415-
format_rust_files_windows()
362+
subprocess.check_call(["npx", "prettier", "--write", "**/*.{js,ts,php,sql,md}"])
363+
subprocess.check_call(["gofmt", "-w", "."])
364+
format_rust_files(paths)
416365

417366
for path in headered:
418367
remove_header(path)

0 commit comments

Comments
 (0)