From a635168aa17764a498834e96a64576907ad9ac85 Mon Sep 17 00:00:00 2001 From: Serhii Turivnyi Date: Nov 01 2017 15:58:28 +0000 Subject: Stick to the pep8 style standard inventory --- diff --git a/scripts/merge-standard-inventory b/scripts/merge-standard-inventory index b5a288f..ef0ce71 100755 --- a/scripts/merge-standard-inventory +++ b/scripts/merge-standard-inventory @@ -12,7 +12,7 @@ def main(argv): Run all standard inventory scripts and return their merged output. Note the standard inventory scripts clean up their spawned hosts - when they detect their parent processes go away. To accomodate + when they detect their parent processes go away. To accommodate that behavior, this script forks a child process to run the inventory scripts, send back the merged inventory, and then wait for the parent of this script (ansible) to die before silently @@ -53,7 +53,7 @@ def main(argv): # close the inherited output side of the pipe os.close(pipeout) - # send eveything from the child to stdout + # send everything from the child to stdout while True: data = os.read(pipein, 999) if not data: @@ -70,7 +70,8 @@ def merge_standard_inventories(args): "TEST_DYNAMIC_INVENTORY_DIRECTORY", "/usr/share/ansible/inventory") ignore_ext_string = os.environ.get( - "ANSIBLE_INVENTORY_IGNORE", "~, .orig, .bak, .ini, .cfg, .retry, .pyc, .pyo") + "ANSIBLE_INVENTORY_IGNORE", + "~, .orig, .bak, .ini, .cfg, .retry, .pyc, .pyo") ignore_ext_list = [] for s in ignore_ext_string.split(','): if s.strip(): @@ -92,8 +93,8 @@ def merge_standard_inventories(args): try: inv_out = subprocess.check_output(cmd, stdin=None, close_fds=True) - except subprocess.CalledProcessError as ex: - raise RuntimeError("Could not run: {0}".format(str(cmd))) + except subprocess.CalledProcessError: + raise RuntimeError("Could not run: {}".format(str(cmd))) merged.merge(inv_out.decode('utf-8')) @@ -165,43 +166,58 @@ class Inventory: if not isinstance(inventory, dict): raise ValueError( - "inventory JSON does not contain the expected top level dictionary") + "inventory JSON does not contain the expected " + "top level dictionary") - if "subjects" not in inventory or not isinstance(inventory["subjects"], dict): + if "subjects" not in inventory or not isinstance(inventory["subjects"], + dict): raise ValueError( - "inventory JSON does not contain the expected [subjects] dictionary") - if "hosts" not in inventory["subjects"] or not isinstance(inventory["subjects"]["hosts"], list): + "inventory JSON does not contain the expected [subjects] " + "dictionary") + if "hosts" not in inventory["subjects"] or not isinstance( + inventory["subjects"]["hosts"], list): raise ValueError( - "inventory JSON does not contain the expected [subjects][hosts] list") + "inventory JSON does not contain the expected " + "[subjects][hosts] list") for h in inventory["subjects"]["hosts"]: self.hosts.append(h) if "_meta" not in inventory or not isinstance(inventory["_meta"], dict): raise ValueError( - "inventory JSON does not contain the expected [_meta] dictionary") - if "hostvars" not in inventory["_meta"] or not isinstance(inventory["_meta"]["hostvars"], dict): + "inventory JSON does not contain the expected [_meta] " + "dictionary") + if "hostvars" not in inventory["_meta"] or not isinstance( + inventory["_meta"]["hostvars"], dict): raise ValueError( - "inventory JSON does not contain the expected [_meta][hostvars] dict") + "inventory JSON does not contain the expected " + "[_meta][hostvars] dict") for h in inventory["_meta"]["hostvars"]: if not isinstance(inventory["_meta"]["hostvars"][h], dict): raise ValueError( - "inventory JSON does not contain the expected [_meta][hostvars][{0}] dict".format(h)) + "inventory JSON does not contain the expected " + "[_meta][hostvars][{0}] dict".format(h)) self.variables[h] = inventory["_meta"]["hostvars"][h] - if "localhost" not in inventory or not isinstance(inventory["localhost"], dict): + if "localhost" not in inventory or not isinstance( + inventory["localhost"], dict): raise ValueError( - "inventory JSON does not contain the expected [localhost] dictionary") - if "hosts" not in inventory["localhost"] or not isinstance(inventory["localhost"]["hosts"], list): + "inventory JSON does not contain the expected " + "[localhost] dictionary") + if "hosts" not in inventory["localhost"] or not isinstance( + inventory["localhost"]["hosts"], list): raise ValueError( - "inventory JSON does not contain the expected [localhost][hosts] list") + "inventory JSON does not contain the expected " + "[localhost][hosts] list") def dumps(self): - data = {"subjects": {"hosts": self.hosts, "vars": {}}, "localhost": { - "hosts": self.hosts, "vars": {}}, "_meta": {"hostvars": self.variables}} + data = {"subjects": {"hosts": self.hosts, "vars": {}}, "localhost": + {"hosts": self.hosts, "vars": {}}, "_meta": + {"hostvars": self.variables}} return json.dumps(data, indent=4, separators=(',', ': ')) + if __name__ == '__main__': sys.exit(main(sys.argv))