Skip to content

Commit 2d672cb

Browse files
authored
optimize format logic (Azure#40224)
1 parent fe880b7 commit 2d672cb

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

tools/azure-sdk-tools/packaging_tools/generate_utils.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

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

380380

381381
def format_samples_and_tests(sdk_code_path) -> None:
382+
try:
383+
import black
384+
except Exception as e:
385+
try:
386+
call("pip install black", shell=True)
387+
except:
388+
pass
382389
for item in ["generated_samples", "generated_tests"]:
383390
generate_path = Path(sdk_code_path) / item
384-
if not generate_path.exists():
385-
_LOGGER.info(f"not find {generate_path}")
386-
continue
387-
388-
try:
389-
import black
390-
except Exception as e:
391-
check_call("pip install black", shell=True)
392-
import black
393-
394-
_BLACK_MODE = black.Mode()
395-
_BLACK_MODE.line_length = 120
396-
files = generate_path.glob("**/*.py")
397-
for path in files:
391+
if generate_path.exists():
398392
try:
399-
with open(path, "r") as fr:
400-
file_content = fr.read()
401-
402-
file_content = black.format_file_contents(file_content, fast=True, mode=_BLACK_MODE)
403-
404-
with open(path, "w") as fw:
405-
fw.write(file_content)
393+
call(f"black {generate_path} -l 120", shell=True)
394+
_LOGGER.info(f"format {generate_path} successfully")
406395
except Exception as e:
407-
_LOGGER.warning(f"Failed to format {path}: {e}")
408-
409-
_LOGGER.info(f"format {generate_path} successfully")
396+
_LOGGER.info(f"failed to format {generate_path}: {e}")
410397

411398

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

0 commit comments

Comments
 (0)