|
1 | | -import platform |
| 1 | +import os |
2 | 2 | import subprocess |
3 | 3 | import time |
4 | 4 | from datetime import timezone, timedelta, datetime |
| 5 | +from pathlib import Path |
5 | 6 |
|
6 | 7 | import requests |
7 | 8 | import urllib3 |
@@ -414,54 +415,19 @@ def get_contests(fetch_new=True) -> List: |
414 | 415 |
|
415 | 416 | ######################################################################################## |
416 | 417 |
|
| 418 | +ROOT = Path(__file__).resolve().parents[1] |
417 | 419 |
|
418 | | -def format_rust_files_linux(): |
419 | | - # The find command to locate and format all .rs files in Linux |
420 | | - find_command = 'find . -name "*.rs" -exec rustfmt {} \\;' |
421 | 420 |
|
422 | | - # Execute the command |
423 | | - process = subprocess.Popen( |
424 | | - find_command, |
425 | | - shell=True, |
426 | | - stdout=subprocess.PIPE, |
427 | | - stderr=subprocess.PIPE, |
428 | | - text=True, |
429 | | - ) |
430 | | - |
431 | | - # Get the output and errors |
432 | | - stdout, stderr = process.communicate() |
433 | | - |
434 | | - if process.returncode == 0: |
435 | | - print("Rust files formatted successfully on Linux!") |
436 | | - print(stdout) |
437 | | - else: |
438 | | - print("Error formatting Rust files on Linux:") |
439 | | - print(stderr) |
440 | | - |
441 | | - |
442 | | -def format_rust_files_windows(): |
443 | | - # PowerShell command to format all .rs files recursively in Windows |
444 | | - ps_command = ( |
445 | | - "Get-ChildItem -Recurse -Filter *.rs | ForEach-Object { rustfmt $_.FullName }" |
446 | | - ) |
447 | | - |
448 | | - # Execute the PowerShell command |
449 | | - process = subprocess.Popen( |
450 | | - ["powershell", "-Command", ps_command], |
451 | | - stdout=subprocess.PIPE, |
452 | | - stderr=subprocess.PIPE, |
453 | | - text=True, |
454 | | - ) |
455 | | - |
456 | | - # Get the output and errors |
457 | | - stdout, stderr = process.communicate() |
458 | | - |
459 | | - if process.returncode == 0: |
460 | | - print("Rust files formatted successfully on Windows!") |
461 | | - print(stdout) |
462 | | - else: |
463 | | - print("Error formatting Rust files on Windows:") |
464 | | - print(stderr) |
| 421 | +def format_rust_files() -> None: |
| 422 | + skip = {"node_modules", "__pycache__", ".git"} |
| 423 | + rs_files = [] |
| 424 | + for dirpath, dirnames, filenames in os.walk(ROOT): |
| 425 | + dirnames[:] = [name for name in dirnames if name not in skip] |
| 426 | + for name in filenames: |
| 427 | + if name.endswith(".rs"): |
| 428 | + rs_files.append(os.path.join(dirpath, name)) |
| 429 | + for i in range(0, len(rs_files), 64): |
| 430 | + subprocess.check_call(["rustfmt", *rs_files[i : i + 64]]) |
465 | 431 |
|
466 | 432 |
|
467 | 433 | def run(): |
@@ -540,15 +506,11 @@ def run(): |
540 | 506 | generate_category_readme(ls, "JavaScript") |
541 | 507 |
|
542 | 508 | refresh(ls) |
543 | | - # 格式化 |
544 | | - os.system('cd .. && npx prettier --write "**/*.{md,js,ts,php,sql}"') |
545 | | - |
546 | | - # 格式化 rust 代码 |
547 | | - # 判断当前是 windows 还是 linux |
548 | | - if platform.system() == "Linux": |
549 | | - format_rust_files_linux() |
550 | | - elif platform.system() == "Windows": |
551 | | - format_rust_files_windows() |
| 509 | + subprocess.check_call( |
| 510 | + ["npx", "prettier", "--write", "**/*.{md,js,ts,php,sql}"], |
| 511 | + cwd=ROOT, |
| 512 | + ) |
| 513 | + format_rust_files() |
552 | 514 |
|
553 | 515 |
|
554 | 516 | if __name__ == "__main__": |
|
0 commit comments