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
+
1
9
import logging
2
10
import os
3
11
import subprocess
@@ -44,27 +52,27 @@ def _run_command(
44
52
45
53
t0 = time .perf_counter ()
46
54
try :
47
- logger .debug ("Executing {0} in {1}" . format ( commands , cwd ) )
55
+ logger .debug ("Executing %s in %s" , commands , cwd )
48
56
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 )
64
72
return retcode , out
65
73
finally :
66
74
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 )
68
76
69
77
70
78
def run_command (
@@ -90,13 +98,13 @@ def download_file(from_url: str, to_path: Path, with_file_name: str) -> None:
90
98
print_blue (f"- Downloading { with_file_name } from { from_url } to { to_path } " )
91
99
92
100
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 :
95
105
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."
97
107
)
98
- with open (f"{ to_path } /{ with_file_name } " , "w" ) as f :
99
- f .write (response .read ().decode ("utf-8" ))
100
108
101
109
102
110
def regenerate_restclient (api_tag , verbose ):
@@ -115,7 +123,7 @@ def regenerate_restclient(api_tag, verbose):
115
123
"--python" ,
116
124
"--track2" ,
117
125
"--version=3.6.2" ,
118
- "--use=@autorest/python@5.12.6 " ,
126
+ "--use=@autorest/python@latest " ,
119
127
f"--python-sdks-folder={ restclient_path .absolute ()} " ,
120
128
"--package-version=0.1.0" ,
121
129
tag_arg ,
@@ -138,8 +146,11 @@ def regenerate_restclient(api_tag, verbose):
138
146
"-a" ,
139
147
"--api-tag" ,
140
148
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
+ ),
143
154
)
144
155
parser .add_argument ("-v" , "--verbose" , action = "store_true" , required = False , help = "turn on verbose output" )
145
156
0 commit comments