26
26
_DPG_README = "README.md"
27
27
28
28
29
+ # input example: "../azure-rest-api-specs/specification/informatica/Informatica.DataManagement"
30
+ def del_outdated_generated_files (readme : str ):
31
+ tspconfig = Path (readme ) / "tspconfig.yaml"
32
+ if not tspconfig .exists ():
33
+ _LOGGER .info (f"do not find tspconfig.yaml: { tspconfig } " )
34
+ return
35
+
36
+ with open (tspconfig , "r" ) as file_in :
37
+ content = yaml .safe_load (file_in )
38
+ # tspconfig.yaml example: https://github.com/Azure/azure-rest-api-specs/pull/29080/files
39
+ service_dir = content .get ("parameters" , {}).get ("service-dir" , {}).get ("default" , "" )
40
+ package_dir = content .get ("options" , {}).get ("@azure-tools/typespec-python" , {}).get ("package-dir" , "" )
41
+ if not service_dir or not package_dir :
42
+ _LOGGER .info (f"do not find service-dir or package-dir in tspconfig.yaml: { tspconfig } " )
43
+ return
44
+ target_dir = Path (service_dir ) / package_dir / package_dir .split ("-" )[0 ]
45
+ if target_dir .exists ():
46
+ generated_files = [
47
+ file
48
+ for file in target_dir .glob ("**/*" )
49
+ if all (
50
+ i not in str (file )
51
+ for i in (
52
+ "__pycache__" ,
53
+ "node_modules" ,
54
+ ".tox" ,
55
+ ".mypy_cache" ,
56
+ )
57
+ )
58
+ and file .suffix == ".py"
59
+ ]
60
+ for file in generated_files :
61
+ if file .stem != "_patch" :
62
+ os .remove (file )
63
+ _LOGGER .info (f"delete outdated generated files except _patch.py successfully" )
64
+
65
+
29
66
def check_api_version_in_subfolder (sdk_code_path : str ):
30
67
folders = glob (f"{ sdk_code_path } /**/_configuration.py" , recursive = True )
31
68
configs = [str (Path (f )) for f in folders if re .compile ("v\d{4}_\d{2}_\d{2}" ).search (f )]
@@ -75,6 +112,7 @@ def call_build_config(package_name: str, folder_name: str):
75
112
# shell=True,
76
113
# )
77
114
115
+
78
116
def init_new_service (package_name , folder_name ):
79
117
if "azure-mgmt-" in package_name :
80
118
call_build_config (package_name , folder_name )
@@ -88,7 +126,7 @@ def init_new_service(package_name, folder_name):
88
126
generate_ci (
89
127
template_path = Path ("scripts/quickstart_tooling_dpg/template_ci" ),
90
128
folder_path = Path (folder_name ),
91
- package_name = package_name
129
+ package_name = package_name ,
92
130
)
93
131
94
132
@@ -105,10 +143,12 @@ def update_servicemetadata(sdk_folder, data, config, folder_name, package_name,
105
143
else :
106
144
metadata = {}
107
145
108
- metadata .update ({
109
- "commit" : data ["headSha" ],
110
- "repository_url" : data ["repoHttpsUrl" ],
111
- })
146
+ metadata .update (
147
+ {
148
+ "commit" : data ["headSha" ],
149
+ "repository_url" : data ["repoHttpsUrl" ],
150
+ }
151
+ )
112
152
if for_swagger_gen :
113
153
readme_file = str (Path (spec_folder , input_readme ))
114
154
global_conf = config ["meta" ]
@@ -122,12 +162,14 @@ def update_servicemetadata(sdk_folder, data, config, folder_name, package_name,
122
162
cmd += build_autorest_options (global_conf , local_conf )
123
163
124
164
# metadata
125
- metadata .update ({
126
- "autorest" : global_conf ["autorest_options" ]["version" ],
127
- "use" : global_conf ["autorest_options" ]["use" ],
128
- "autorest_command" : " " .join (cmd ),
129
- "readme" : input_readme ,
130
- })
165
+ metadata .update (
166
+ {
167
+ "autorest" : global_conf ["autorest_options" ]["version" ],
168
+ "use" : global_conf ["autorest_options" ]["use" ],
169
+ "autorest_command" : " " .join (cmd ),
170
+ "readme" : input_readme ,
171
+ }
172
+ )
131
173
else :
132
174
metadata ["typespec_src" ] = input_readme
133
175
metadata .update (config )
@@ -362,33 +404,38 @@ def format_samples(sdk_code_path) -> None:
362
404
363
405
_LOGGER .info (f"format generated_samples successfully" )
364
406
407
+
365
408
def generate_ci (template_path : Path , folder_path : Path , package_name : str ) -> None :
366
409
ci = Path (folder_path , "ci.yml" )
367
410
service_name = folder_path .name
368
411
safe_name = package_name .replace ("-" , "" )
369
412
if not ci .exists ():
370
413
env = Environment (loader = FileSystemLoader (template_path ), keep_trailing_newline = True )
371
- template = env .get_template (' ci.yml' )
414
+ template = env .get_template (" ci.yml" )
372
415
content = template .render (package_name = package_name , service_name = service_name , safe_name = safe_name )
373
416
else :
374
417
with open (ci , "r" ) as file_in :
375
418
content = file_in .readlines ()
376
419
for line in content :
377
420
if package_name in line :
378
421
return
379
- content .append (f' - name: { package_name } \n ' )
380
- content .append (f' safeName: { safe_name } \n ' )
422
+ content .append (f" - name: { package_name } \n " )
423
+ content .append (f" safeName: { safe_name } \n " )
381
424
with open (ci , "w" ) as file_out :
382
425
file_out .writelines (content )
383
426
427
+
384
428
def gen_typespec (typespec_relative_path : str , spec_folder : str , head_sha : str , rest_repo_url : str ) -> Dict [str , Any ]:
385
429
typespec_python = "@azure-tools/typespec-python"
386
430
autorest_python = "@autorest/python"
387
431
# call scirpt to generate sdk
388
432
try :
389
433
tsp_dir = (Path (spec_folder ) / typespec_relative_path ).resolve ()
390
- repo_url = rest_repo_url .replace ('https://github.com/' , "" )
391
- check_output (f"tsp-client init --tsp-config { tsp_dir } --local-spec-repo { tsp_dir } --commit { head_sha } --repo { repo_url } --debug" , shell = True )
434
+ repo_url = rest_repo_url .replace ("https://github.com/" , "" )
435
+ check_output (
436
+ f"tsp-client init --tsp-config { tsp_dir } --local-spec-repo { tsp_dir } --commit { head_sha } --repo { repo_url } --debug" ,
437
+ shell = True ,
438
+ )
392
439
except CalledProcessError as e :
393
440
_LOGGER .error (f"Failed to generate sdk from typespec: { e .output .decode ('utf-8' )} " )
394
441
raise e
@@ -399,7 +446,7 @@ def gen_typespec(typespec_relative_path: str, spec_folder: str, head_sha: str, r
399
446
data = json .load (file_in )
400
447
npm_package_verstion = {
401
448
typespec_python : data ["dependencies" ][typespec_python ],
402
- autorest_python : autorest_python_version ,
449
+ autorest_python : autorest_python_version ,
403
450
}
404
451
405
452
return npm_package_verstion
0 commit comments