1+ # coding: utf-8
2+
3+ # -------------------------------------------------------------------------
4+ # Copyright (c) Microsoft Corporation. All rights reserved.
5+ # Licensed under the MIT License. See License.txt in the project root for
6+ # license information.
7+ # --------------------------------------------------------------------------
8+
19import logging
210import os
311import subprocess
@@ -44,27 +52,27 @@ def _run_command(
4452
4553 t0 = time .perf_counter ()
4654 try :
47- logger .debug ("Executing {0} in {1}" . format ( commands , cwd ) )
55+ logger .debug ("Executing %s in %s" , commands , cwd )
4856 out = ""
49- p = subprocess .Popen (commands , stdout = subprocess .PIPE , stderr = stderr , cwd = cwd , shell = shell , env = env )
50- for line in p .stdout :
51- line = line .decode ("utf-8" ).rstrip ()
52- if line and line .strip ():
53- logger .debug (line )
54- if stream_stdout :
55- sys .stdout .write (line )
56- sys .stdout .write ("\n " )
57- out += line
58- out += "\n "
59- p .communicate ()
60- retcode = p .poll ()
61- if throw_on_retcode :
62- if retcode :
63- raise subprocess .CalledProcessError (retcode , p .args , output = out , stderr = p .stderr )
57+ with subprocess .Popen (commands , stdout = subprocess .PIPE , stderr = stderr , cwd = cwd , shell = shell , env = env ) as p :
58+ for line in p .stdout :
59+ line = line .decode ("utf-8" ).rstrip ()
60+ if line and line .strip ():
61+ logger .debug (line )
62+ if stream_stdout :
63+ sys .stdout .write (line )
64+ sys .stdout .write ("\n " )
65+ out += line
66+ out += "\n "
67+ p .communicate ()
68+ retcode = p .poll ()
69+ if throw_on_retcode :
70+ if retcode :
71+ raise subprocess .CalledProcessError (retcode , p .args , output = out , stderr = p .stderr )
6472 return retcode , out
6573 finally :
6674 t1 = time .perf_counter ()
67- logger .debug ("Execution took {0}s for {1} in {2}" . format ( t1 - t0 , commands , cwd ) )
75+ logger .debug ("Execution took %ss for %s in %s" , t1 - t0 , commands , cwd )
6876
6977
7078def run_command (
@@ -90,13 +98,13 @@ def download_file(from_url: str, to_path: Path, with_file_name: str) -> None:
9098 print_blue (f"- Downloading { with_file_name } from { from_url } to { to_path } " )
9199
92100 try :
93- response = urlopen (from_url )
94- except Exception :
101+ with urlopen (from_url ) as response :
102+ with open (f"{ to_path } /{ with_file_name } " , "w" , encoding = "utf-8" ) as f :
103+ f .write (response .read ().decode ("utf-8" ))
104+ except (OSError , URLError , HTTPError ) as e :
95105 sys .exit (
96- f"Connection error while trying to download file from { from_url } . Please try running the script again."
106+ f"Connection error while trying to download file from { from_url } : { e } . Please try running the script again."
97107 )
98- with open (f"{ to_path } /{ with_file_name } " , "w" ) as f :
99- f .write (response .read ().decode ("utf-8" ))
100108
101109
102110def regenerate_restclient (api_tag , verbose ):
@@ -115,7 +123,7 @@ def regenerate_restclient(api_tag, verbose):
115123 "--python" ,
116124 "--track2" ,
117125 "--version=3.6.2" ,
118- "--use=@autorest/python@5.12.6 " ,
126+ "--use=@autorest/python@latest " ,
119127 f"--python-sdks-folder={ restclient_path .absolute ()} " ,
120128 "--package-version=0.1.0" ,
121129 tag_arg ,
@@ -138,8 +146,11 @@ def regenerate_restclient(api_tag, verbose):
138146 "-a" ,
139147 "--api-tag" ,
140148 required = False ,
141- help = """Specifies which API to generate using autorest. If not supplied, all APIs are targeted.
142- Must match the name of a tag in the sdk/ml/azure-ai-ml/swagger/machinelearningservices/resource-manager/readme.md file.""" ,
149+ help = (
150+ "Specifies which API to generate using autorest. If not supplied, all APIs are targeted.\n "
151+ "Must match the name of a tag in the sdk/ml/azure-ai-ml/swagger/machinelearningservices/"
152+ "resource-manager/readme.md file."
153+ ),
143154 )
144155 parser .add_argument ("-v" , "--verbose" , action = "store_true" , required = False , help = "turn on verbose output" )
145156
0 commit comments