2323#
2424
2525from __future__ import print_function
26+
2627import argparse
27- try :
28- import configparser
29- except ImportError :
30- import ConfigParser as configparser
31- try :
32- from future_builtins import filter # Python 2
33- except ImportError :
34- pass # Python 3
3528import json
36- import yaml
3729import os
38- import sys
3930import subprocess
31+ import sys
4032
33+ import yaml
34+ try :
35+ import configparser
36+ except ImportError :
37+ import ConfigParser as configparser
4138
4239valid = {
43- # taken from nodejs/node.git: ./configure
44- 'arch' : ('armv6l' , 'armv7l' , 'arm64' , 'ia32' , 'mips' , 'mipsel' , 'ppc' ,
45- 'ppc64' , 'x32' , 'x64' , 'x86' , 's390' , 's390x' ),
46-
47- # valid roles - add as necessary
48- 'type' : ('infra' , 'release' , 'test' ),
49-
50- # providers - validated for consistency
51- 'provider' : ('azure' , 'digitalocean' , 'joyent' , 'ibm' , 'linuxonecc' ,
52- 'macstadium' , 'marist' , 'mininodes' , 'msft' , 'osuosl' ,
53- 'rackspace' , 'requireio' , 'scaleway' , 'softlayer' , 'voxer' ,
54- 'packetnet' , 'nearform' )
40+ # taken from nodejs/node.git: ./configure
41+ 'arch' : ('armv6l' , 'armv7l' , 'arm64' , 'ia32' , 'mips' , 'mipsel' , 'ppc' , 'ppc64' , 'x32' , 'x64' , 'x86' , 's390' , 's390x' ),
42+
43+ # valid roles - add as necessary
44+ 'type' : ('infra' , 'release' , 'test' ),
45+
46+ # providers - validated for consistency
47+ 'provider' : (
48+ 'azure' , 'digitalocean' , 'joyent' , 'ibm' , 'linuxonecc' ,
49+ 'macstadium' , 'marist' , 'mininodes' , 'msft' , 'osuosl' ,
50+ 'rackspace' , 'requireio' , 'scaleway' , 'softlayer' , 'voxer' ,
51+ 'packetnet' , 'nearform'
52+ )
5553}
5654DECRYPT_TOOL = "gpg"
5755INVENTORY_FILENAME = "inventory.yml"
@@ -97,15 +95,16 @@ def main():
9795# https://stackoverflow.com/a/7205107
9896def merge (a , b , path = None ):
9997 "merges b into a"
100- if path is None : path = []
98+ if path is None :
99+ path = []
101100 for key in b :
102101 if key in a :
103102 if isinstance (a [key ], dict ) and isinstance (b [key ], dict ):
104103 merge (a [key ], b [key ], path + [str (key )])
105104 elif isinstance (a [key ], list ) and isinstance (b [key ], list ):
106105 a [key ] = sorted (set (a [key ]).union (b [key ]))
107106 elif a [key ] == b [key ]:
108- pass # same leaf value
107+ pass # same leaf value
109108 else :
110109 raise Exception ('Conflict at %s' % '.' .join (path + [str (key )]))
111110 else :
@@ -167,7 +166,7 @@ def load_yaml_file(file_name):
167166 # get inventory
168167 with open (file_name , 'r' ) as stream :
169168 try :
170- hosts = yaml .load (stream )
169+ hosts = yaml .safe_load (stream )
171170
172171 except yaml .YAMLError as exc :
173172 print (exc )
@@ -186,11 +185,11 @@ def load_yaml_secrets(file_name):
186185 print ("WARNING: cannot load %s" % file_name , file = sys .stderr )
187186 return None
188187
189- return yaml .load (stdout )
188+ return yaml .safe_load (stdout )
190189
191190
192191def parse_yaml (hosts , config ):
193- """Parses host information from the output of yaml.load """
192+ """Parses host information from the output of yaml.safe_load """
194193
195194 export = {'_meta' : {'hostvars' : {}}}
196195
@@ -210,7 +209,7 @@ def parse_yaml(hosts, config):
210209
211210 # some hosts have metadata appended to provider
212211 # which requires underscore
213- delimiter = "_" if host .count ('-' ) is 3 else "-"
212+ delimiter = "_" if host .count ('-' ) == 3 else "-"
214213 hostname = '{}-{}{}{}' .format (host_type , provider_name ,
215214 delimiter , host )
216215
@@ -265,7 +264,7 @@ def parse_host(host):
265264
266265 expected = ['type' , 'provider' , 'os' , 'arch' , 'uid' ]
267266
268- if len (info ) is not 5 :
267+ if len (info ) != 5 :
269268 raise Exception ('Host format is invalid: %s,' % host )
270269
271270 for key , item in enumerate (expected ):
@@ -279,9 +278,11 @@ def parse_host(host):
279278
280279
281280def has_metadata (info ):
282- """Checks for metadata in variables. These are separated from the "key"
283- metadata by underscore. Not used anywhere at the moment for anything
284- other than descriptiveness"""
281+ """
282+ Checks for metadata in variables. These are separated from the "key"
283+ metadata by underscore. Not used anywhere at the moment for anything
284+ other than descriptiveness
285+ """
285286
286287 metadata = info .split ('_' , 1 )
287288
@@ -296,9 +297,8 @@ def has_metadata(info):
296297
297298
298299if __name__ == "__main__" :
299- parser = argparse .ArgumentParser ()
300- parser .add_argument ('--list' , action = 'store_true' )
301- parser .add_argument ('--host' , action = 'store' )
302- args = parser .parse_args ()
303-
300+ # parser = argparse.ArgumentParser()
301+ # parser.add_argument('--list', action='store_true')
302+ # parser.add_argument('--host', action='store')
303+ # args = parser.parse_args()
304304 main ()
0 commit comments