Skip to content

Commit ee35a38

Browse files
LLC, update automation, enable update swagger/README*.md (Azure#26154)
* add requirement.txt for python * use common mvn build * update readme if available
1 parent 5ec8d96 commit ee35a38

File tree

2 files changed

+109
-47
lines changed

2 files changed

+109
-47
lines changed

eng/mgmt/automation/generate_data.py

Lines changed: 107 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
import argparse
77
import re
88
import glob
9+
import subprocess
10+
import yaml
911
from typing import List
1012

1113
from parameters import *
1214
from utils import set_or_increase_version
1315
from utils import update_service_ci_and_pom
1416
from utils import update_root_pom
1517
from utils import update_version
18+
from utils import ListIndentDumper
1619

1720

21+
GROUP_ID = 'com.azure'
1822
LLC_ARGUMENTS = '--java --low-level-client --sdk-integration --generate-samples'
1923

2024

@@ -48,8 +52,7 @@ def sdk_automation(config: dict) -> List[dict]:
4852
generated_folder = 'sdk/{0}/{1}'.format(service, module)
4953

5054
if succeeded:
51-
install_build_tools(sdk_root)
52-
compile_package(os.path.join(sdk_root, generated_folder))
55+
compile_package(sdk_root, GROUP_ID, module)
5356

5457
artifacts = [
5558
'{0}/pom.xml'.format(generated_folder)
@@ -94,54 +97,116 @@ def generate(
9497
shutil.rmtree(os.path.join(output_dir, 'src/samples/java', namespace.replace('.', '/'), 'generated'),
9598
ignore_errors=True)
9699

97-
credential_arguments = '--java.credential-types={0}'.format(credential_types)
98-
if credential_scopes:
99-
credential_arguments += ' --java.credential-scopes={0}'.format(credential_scopes)
100+
readme_relative_path = update_readme(output_dir, input_file, credential_types, credential_scopes, title)
101+
if readme_relative_path:
102+
logging.info('[GENERATE] Autorest from README {}'.format(readme_relative_path))
100103

101-
input_arguments = '--input-file={0}'.format(input_file)
102-
103-
artifact_arguments = '--artifact-id={0}'.format(module)
104-
if title:
105-
artifact_arguments += ' --title={0}'.format(title)
106-
107-
command = 'autorest --version={0} --use={1} --java.azure-libraries-for-java-folder={2} --java.output-folder={3} --java.namespace={4} {5}'.format(
108-
autorest,
109-
use,
110-
os.path.abspath(sdk_root),
111-
os.path.abspath(output_dir),
112-
namespace,
113-
' '.join((LLC_ARGUMENTS, input_arguments, credential_arguments, artifact_arguments, autorest_options)),
114-
)
115-
logging.info(command)
116-
if os.system(command) != 0:
117-
logging.error('[GENERATE] Autorest fail')
118-
return False
119-
120-
group = "com.azure"
121-
set_or_increase_version(sdk_root, group, module)
122-
update_service_ci_and_pom(sdk_root, service, group, module)
123-
update_root_pom(sdk_root, service)
124-
update_version(sdk_root, output_dir)
104+
command = 'autorest --version={0} --use={1} {2}'.format(
105+
autorest,
106+
use,
107+
readme_relative_path
108+
)
109+
logging.info(command)
110+
try:
111+
subprocess.run(command, shell=True, cwd=output_dir, check=True)
112+
except subprocess.CalledProcessError:
113+
logging.error('[GENERATE] Autorest fail')
114+
return False
115+
else:
116+
logging.info('[GENERATE] Autorest from JSON {}'.format(input_file))
117+
118+
credential_arguments = '--java.credential-types={0}'.format(credential_types)
119+
if credential_scopes:
120+
credential_arguments += ' --java.credential-scopes={0}'.format(credential_scopes)
121+
122+
input_arguments = '--input-file={0}'.format(input_file)
123+
124+
artifact_arguments = '--artifact-id={0}'.format(module)
125+
if title:
126+
artifact_arguments += ' --title={0}'.format(title)
127+
128+
command = 'autorest --version={0} --use={1} ' \
129+
'--java.azure-libraries-for-java-folder={2} --java.output-folder={3} ' \
130+
'--java.namespace={4} {5}'\
131+
.format(
132+
autorest,
133+
use,
134+
os.path.abspath(sdk_root),
135+
os.path.abspath(output_dir),
136+
namespace,
137+
' '.join((LLC_ARGUMENTS, input_arguments, credential_arguments, artifact_arguments, autorest_options))
138+
)
139+
logging.info(command)
140+
if os.system(command) != 0:
141+
logging.error('[GENERATE] Autorest fail')
142+
return False
143+
144+
set_or_increase_version(sdk_root, GROUP_ID, module)
145+
update_service_ci_and_pom(sdk_root, service, GROUP_ID, module)
146+
update_root_pom(sdk_root, service)
147+
update_version(sdk_root, output_dir)
125148

126149
return True
127150

128151

129-
def install_build_tools(sdk_root: str):
130-
command = 'mvn --no-transfer-progress clean install -f {0} -pl com.azure:sdk-build-tools'.format(os.path.join(sdk_root, 'pom.xml'))
152+
def compile_package(sdk_root: str, group_id: str, module: str):
153+
command = 'mvn --no-transfer-progress clean verify package -f {0}/pom.xml -pl {1}:{2} -am'.format(
154+
sdk_root, group_id, module)
131155
logging.info(command)
132156
if os.system(command) != 0:
133-
logging.error('[COMPILE] Maven build fail for sdk-build-tools')
157+
logging.error('[COMPILE] Maven build fail')
134158
return False
135159
return True
136160

137161

138-
def compile_package(output_dir: str):
139-
command = 'mvn --no-transfer-progress clean verify package -f {0}'.format(os.path.join(output_dir, 'pom.xml'))
140-
logging.info(command)
141-
if os.system(command) != 0:
142-
logging.error('[COMPILE] Maven build fail')
143-
return False
144-
return True
162+
def update_readme(output_dir: str, input_file: str, credential_types: str, credential_scopes: str, title: str) -> str:
163+
readme_relative_path = ''
164+
165+
swagger_dir = os.path.join(output_dir, 'swagger')
166+
if os.path.isdir(swagger_dir):
167+
for filename in os.listdir(swagger_dir):
168+
if filename.lower().startswith('readme') and filename.lower().endswith('.md'):
169+
readme_yaml_found = False
170+
readme_path = os.path.join(swagger_dir, filename)
171+
with open(readme_path, 'r', encoding='utf-8') as f_in:
172+
content = f_in.read()
173+
if content:
174+
yaml_blocks = re.findall(r'```\s?(?:yaml|YAML)\n(.*?)```', content, re.DOTALL)
175+
for yaml_str in yaml_blocks:
176+
yaml_json = yaml.safe_load(yaml_str)
177+
if 'low-level-client' in yaml_json and yaml_json['low-level-client']:
178+
# yaml block found, update
179+
yaml_json['input-file'] = [input_file]
180+
if title:
181+
yaml_json['title'] = title
182+
if credential_types:
183+
yaml_json['credential-types'] = credential_types
184+
if credential_scopes:
185+
yaml_json['credential-scopes'] = credential_scopes
186+
187+
# write updated yaml
188+
updated_yaml_str = yaml.dump(yaml_json,
189+
sort_keys=False,
190+
Dumper=ListIndentDumper)
191+
192+
if not yaml_str == updated_yaml_str:
193+
# update readme
194+
updated_content = content.replace(yaml_str, updated_yaml_str, 1)
195+
with open(readme_path, 'w', encoding='utf-8') as f_out:
196+
f_out.write(updated_content)
197+
198+
logging.info('[GENERATE] YAML block in README updated from\n{0}\nto\n{1}'.format(
199+
yaml_str, updated_yaml_str
200+
))
201+
202+
readme_yaml_found = True
203+
break
204+
205+
if readme_yaml_found:
206+
readme_relative_path = 'swagger/{}'.format(filename)
207+
break
208+
209+
return readme_relative_path
145210

146211

147212
def parse_args() -> argparse.Namespace:
@@ -206,14 +271,9 @@ def main():
206271
base_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
207272
sdk_root = os.path.abspath(os.path.join(base_dir, SDK_ROOT))
208273

209-
generate(sdk_root, **args)
210-
211-
output_dir = os.path.join(
212-
sdk_root,
213-
'sdk', args['service'], args['module']
214-
)
215-
install_build_tools(sdk_root)
216-
compile_package(output_dir)
274+
succeeded = generate(sdk_root, **args)
275+
if succeeded:
276+
compile_package(sdk_root, GROUP_ID, args['module'])
217277

218278

219279
if __name__ == '__main__':
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PyYAML ~= 6.0
2+
requests ~= 2.26.0

0 commit comments

Comments
 (0)