Skip to content

Commit e6027e9

Browse files
BigCat20196msyyc
andauthored
[Tool] format generate samples (Azure#25233)
* 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 <yuchaoyan@microsoft.com> * Update tools/azure-sdk-tools/packaging_tools/generate_utils.py Co-authored-by: Yuchao Yan <yuchaoyan@microsoft.com> * Update tools/azure-sdk-tools/packaging_tools/generate_utils.py Co-authored-by: Yuchao Yan <yuchaoyan@microsoft.com> * Update tools/azure-sdk-tools/packaging_tools/generate_utils.py Co-authored-by: Yuchao Yan <yuchaoyan@microsoft.com>
1 parent 106ab45 commit e6027e9

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from .swaggertosdk.SwaggerToSdkCore import CONFIG_FILE, CONFIG_FILE_DPG
88
from .generate_sdk import generate
9-
from .generate_utils import get_package_names, init_new_service, update_servicemetadata, judge_tag_preview
9+
from .generate_utils import (get_package_names, init_new_service, update_servicemetadata, judge_tag_preview,
10+
format_samples)
1011

1112
_LOGGER = logging.getLogger(__name__)
1213

@@ -47,6 +48,7 @@ def main(generate_input, generate_output):
4748

4849
# Generate some necessary file for new service
4950
init_new_service(package_name, folder_name)
51+
format_samples(sdk_code_path)
5052

5153
# Update metadata
5254
try:

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import suppress
12
import json
23
import logging
34
import os
@@ -126,3 +127,31 @@ def judge_tag_preview(path: str) -> bool:
126127

127128
_LOGGER.info(f'find single api version:{api_version}')
128129
return 'preview' in api_version
130+
131+
132+
def format_samples(sdk_code_path) -> None:
133+
generate_sample_path = Path(sdk_code_path) / 'generated_samples'
134+
if not generate_sample_path.exists():
135+
_LOGGER.info(f'not find generated_samples')
136+
return
137+
138+
try:
139+
import black
140+
except Exception as e:
141+
check_call('pip install black', shell=True)
142+
import black
143+
144+
_BLACK_MODE = black.Mode()
145+
_BLACK_MODE.line_length = 120
146+
files = generate_sample_path.glob('**/*.py')
147+
for path in files:
148+
with open(path, 'r') as fr:
149+
file_content = fr.read()
150+
151+
with suppress(black.NothingChanged):
152+
file_content = black.format_file_contents(file_content, fast=True, mode=_BLACK_MODE)
153+
154+
with open(path, 'w') as fw:
155+
fw.write(file_content)
156+
157+
_LOGGER.info(f'format generated_samples successfully')

tools/azure-sdk-tools/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"wheel",
1111
"Jinja2",
1212
"MarkupSafe==2.0.1",
13+
# black,
1314
"pytoml",
1415
"json-delta>=2.0",
1516
# Tests

0 commit comments

Comments
 (0)