Cleanup Get-AzureConstants.py to be a bit more pythonic
Output has not changed at all
@@ -7,21 +7,11 @@ import json
time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
def getAllSizes():
- proc = subprocess.Popen(['az account list-locations'], stdout=subprocess.PIPE, shell=True)
- (locations, err) = proc.communicate()
- if err:
- raise Exception(err.message)
-
+ locations = json.loads(subprocess.check_output(['az', 'account', 'list-locations']))
sizeMap = {}
- locations = json.loads(locations)
- for location in locations:
- command = "az vm list-sizes -l %s" % (location['name'])
- proc = subprocess.Popen(command , shell=True, stdout=subprocess.PIPE)
- (sizes, err) = proc.communicate()
- if err:
- raise Exception(err.message)
- sizes = json.loads(sizes)
+ for location in locations:
+ sizes = json.loads(subprocess.check_output(['az', 'vm', 'list-sizes', '-l', location['name']]))
for size in sizes:
if not size['name'] in sizeMap and not size['name'].split('_')[0] == 'Basic':
sizeMap[size['name']] = size
@@ -53,15 +43,9 @@ def getMasterAgentMap(sizeMap):
return agentMap
def getLocations():
- proc = subprocess.Popen(['az account list-locations'], stdout=subprocess.PIPE, shell=True)
- (locations, err) = proc.communicate()
- if err:
- raise Exception(err.message)
+ locations = json.loads(subprocess.check_output(['az', 'account', 'list-locations']))
- locations = json.loads(locations)
- locationList = []
- for location in locations:
- locationList.append(location['name'])
+ locationList = [l['name'] for l in locations]
#hard code Azure China Cloud location
locationList.append('chinanorth')
@@ -221,11 +205,8 @@ func GetClassicSizeMap() string {
}"""
return text
-def getSizeDef(sizeMap):
- keys = sizeMap.keys()
- print sizeMap[keys[0]]
-
+def main():
outfile = 'pkg/acsengine/azureconst.go'
allSizes = getAllSizes()
dcosMasterMap = getDcosMasterMap(allSizes)
@@ -233,8 +214,11 @@ masterAgentMap = getMasterAgentMap(allSizes)
kubernetesAgentMap = allSizes
locations = getLocations()
text = getFileContents(dcosMasterMap, masterAgentMap, kubernetesAgentMap, allSizes, locations)
-file = open(outfile, 'w')
-file.write(text)
-file.close()
-command = "gofmt -w pkg/acsengine/azureconst.go"
-proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
+
+ with open(outfile, 'w') as f:
+ f.write(text)
+
+ subprocess.check_call(['gofmt', '-w', outfile])
+
+if __name__ == '__main__':
+ main()
