Skip to content

Commit

Permalink
[Tool] format generate samples (#25233)
Browse files Browse the repository at this point in the history
* format generate sample

* test

* test

* test

* test

* test

* test

* test

* test

* change

* test

* test

* test

* reset

* update format samples

* update

* update

* update

* update

* update

* update

* update

* delete test code

* delete test code

* format

* debug

* rollback

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

Co-authored-by: Yuchao Yan <[email protected]>

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

Co-authored-by: Yuchao Yan <[email protected]>

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

Co-authored-by: Yuchao Yan <[email protected]>

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

Co-authored-by: Yuchao Yan <[email protected]>
  • Loading branch information
BigCat20196 and msyyc authored Jul 22, 2022
1 parent 106ab45 commit e6027e9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tools/azure-sdk-tools/packaging_tools/auto_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from .swaggertosdk.SwaggerToSdkCore import CONFIG_FILE, CONFIG_FILE_DPG
from .generate_sdk import generate
from .generate_utils import get_package_names, init_new_service, update_servicemetadata, judge_tag_preview
from .generate_utils import (get_package_names, init_new_service, update_servicemetadata, judge_tag_preview,
format_samples)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -47,6 +48,7 @@ def main(generate_input, generate_output):

# Generate some necessary file for new service
init_new_service(package_name, folder_name)
format_samples(sdk_code_path)

# Update metadata
try:
Expand Down
29 changes: 29 additions & 0 deletions tools/azure-sdk-tools/packaging_tools/generate_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import suppress
import json
import logging
import os
Expand Down Expand Up @@ -126,3 +127,31 @@ def judge_tag_preview(path: str) -> bool:

_LOGGER.info(f'find single api version:{api_version}')
return 'preview' in api_version


def format_samples(sdk_code_path) -> None:
generate_sample_path = Path(sdk_code_path) / 'generated_samples'
if not generate_sample_path.exists():
_LOGGER.info(f'not find generated_samples')
return

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_sample_path.glob('**/*.py')
for path in files:
with open(path, 'r') as fr:
file_content = fr.read()

with suppress(black.NothingChanged):
file_content = black.format_file_contents(file_content, fast=True, mode=_BLACK_MODE)

with open(path, 'w') as fw:
fw.write(file_content)

_LOGGER.info(f'format generated_samples successfully')
1 change: 1 addition & 0 deletions tools/azure-sdk-tools/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"wheel",
"Jinja2",
"MarkupSafe==2.0.1",
# black,
"pytoml",
"json-delta>=2.0",
# Tests
Expand Down

0 comments on commit e6027e9

Please sign in to comment.