Skip to content

Commit 34efb39

Browse files
committed
wip
1 parent 1df8a01 commit 34efb39

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

.generator/cli.py

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def _get_library_id(request_data: Dict) -> str:
357357

358358
@track_time
359359
def _run_post_processor(output: str, library_id: str, is_mono_repo: bool):
360-
"""Runs the synthtool post-processor on the output directory.
360+
"""Runs the synthtool post-processor (templates) and Ruff formatter (lint/format).
361361
362362
Args:
363363
output(str): Path to the directory in the container where code
@@ -367,25 +367,58 @@ def _run_post_processor(output: str, library_id: str, is_mono_repo: bool):
367367
"""
368368
os.chdir(output)
369369
path_to_library = f"packages/{library_id}" if is_mono_repo else "."
370-
logger.info("Running Python post-processor...")
370+
371+
# 1. Run Synthtool (Templates & Fixers only)
372+
# Note: This relies on 'nox' being disabled in your environment (via run_fast.sh shim)
373+
# to avoid the slow formatting step inside owlbot.
374+
logger.info("Running Python post-processor (Templates & Fixers)...")
371375
if SYNTHTOOL_INSTALLED:
372-
if is_mono_repo:
373-
python_mono_repo.owlbot_main(path_to_library)
374-
else:
375-
# Some repositories have customizations in `librarian.py`.
376-
# If this file exists, run those customizations instead of `owlbot_main`
377-
if Path(f"{output}/librarian.py").exists():
378-
subprocess.run(["python3.14", f"{output}/librarian.py"])
376+
try:
377+
if is_mono_repo:
378+
python_mono_repo.owlbot_main(path_to_library)
379379
else:
380-
python.owlbot_main()
381-
else:
382-
raise SYNTHTOOL_IMPORT_ERROR # pragma: NO COVER
383-
384-
# If there is no noxfile, run `isort`` and `black` on the output.
385-
# This is required for proto-only libraries which are not GAPIC.
386-
if not Path(f"{output}/{path_to_library}/noxfile.py").exists():
387-
subprocess.run(["isort", output])
388-
subprocess.run(["black", output])
380+
# Handle custom librarian scripts if present
381+
if Path(f"{output}/librarian.py").exists():
382+
subprocess.run(["python3.14", f"{output}/librarian.py"])
383+
else:
384+
python.owlbot_main()
385+
except Exception as e:
386+
logger.warning(f"Synthtool warning (non-fatal): {e}")
387+
388+
# 2. Run RUFF (Fast Formatter & Import Sorter)
389+
# This replaces both 'isort' and 'black' and runs in < 1 second.
390+
# We hardcode flags here to match Black defaults so you don't need config files.
391+
# logger.info("🚀 Running Ruff (Fast Formatter)...")
392+
# try:
393+
# # STEP A: Fix Imports (like isort)
394+
# subprocess.run(
395+
# [
396+
# "ruff", "check",
397+
# "--select", "I", # Only run Import sorting rules
398+
# "--fix", # Auto-fix them
399+
# "--line-length=88", # Match Black default
400+
# "--known-first-party=google", # Prevent 'google' moving to 3rd party block
401+
# output
402+
# ],
403+
# check=False,
404+
# stdout=subprocess.DEVNULL,
405+
# stderr=subprocess.DEVNULL
406+
# )
407+
408+
# # STEP B: Format Code (like black)
409+
# subprocess.run(
410+
# [
411+
# "ruff", "format",
412+
# "--line-length=88", # Match Black default
413+
# output
414+
# ],
415+
# check=False,
416+
# stdout=subprocess.DEVNULL,
417+
# stderr=subprocess.DEVNULL
418+
# )
419+
# except FileNotFoundError:
420+
# logger.warning("⚠️ Ruff binary not found. Code will be unformatted.")
421+
# logger.warning(" Please run: pip install ruff")
389422

390423
logger.info("Python post-processor ran successfully.")
391424

.generator/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ starlark-pyo3>=2025.1
55
build
66
black==23.7.0
77
isort==5.11.0
8+
ruff

0 commit comments

Comments
 (0)