|
6 | 6 |
|
7 | 7 | from ci_tools.git_tools import get_add_diff_file_list |
8 | 8 | 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 |
10 | 10 | from typing import Dict, Any |
11 | 11 | from glob import glob |
12 | 12 | import yaml |
@@ -379,34 +379,21 @@ def gen_dpg(rest_readme_path: str, autorest_config: str, spec_folder: str) -> Di |
379 | 379 |
|
380 | 380 |
|
381 | 381 | 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 |
382 | 389 | for item in ["generated_samples", "generated_tests"]: |
383 | 390 | 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(): |
398 | 392 | 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") |
406 | 395 | 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}") |
410 | 397 |
|
411 | 398 |
|
412 | 399 | def generate_ci(template_path: Path, folder_path: Path, package_name: str) -> None: |
|
0 commit comments