Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK generation pipeline] optimize format logic #40224

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions tools/azure-sdk-tools/packaging_tools/generate_utils.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

from ci_tools.git_tools import get_add_diff_file_list
from pathlib import Path
from subprocess import check_output, CalledProcessError, check_call, STDOUT
from subprocess import check_output, CalledProcessError, check_call, STDOUT, call
from typing import Dict, Any
from glob import glob
import yaml
@@ -379,34 +379,21 @@ def gen_dpg(rest_readme_path: str, autorest_config: str, spec_folder: str) -> Di


def format_samples_and_tests(sdk_code_path) -> None:
try:
import black
except Exception as e:
try:
call("pip install black", shell=True)
except:
pass
for item in ["generated_samples", "generated_tests"]:
generate_path = Path(sdk_code_path) / item
if not generate_path.exists():
_LOGGER.info(f"not find {generate_path}")
continue

try:
import black
except Exception as e:
check_call("pip install black", shell=True)
import black

_BLACK_MODE = black.Mode()
_BLACK_MODE.line_length = 120
files = generate_path.glob("**/*.py")
for path in files:
if generate_path.exists():
try:
with open(path, "r") as fr:
file_content = fr.read()

file_content = black.format_file_contents(file_content, fast=True, mode=_BLACK_MODE)

with open(path, "w") as fw:
fw.write(file_content)
call(f"black {generate_path} -l 120", shell=True)
_LOGGER.info(f"format {generate_path} successfully")
except Exception as e:
_LOGGER.warning(f"Failed to format {path}: {e}")

_LOGGER.info(f"format {generate_path} successfully")
_LOGGER.info(f"failed to format {generate_path}: {e}")


def generate_ci(template_path: Path, folder_path: Path, package_name: str) -> None: