<commit_before>import mist.io.methods


class MistInventory(object):
    def __init__(self, user, machines=None):
        self.user = user
        self.hosts = {}
        self.keys = {}
        self._cache = {}
        self.load(machines)

    def load(self, machines=None):
        self.hosts = {}
        self.keys = {}
        if not machines:
            machines = [(bid, m['id'])
                        for bid in self.user.backends
                        for m in self._list_machines(bid)]

        for bid, mid in machines:
            try:
                name, ip_addr = self.find_machine_details(bid, mid)
                key_id, ssh_user, port = self.find_ssh_settings(bid, mid)
            except Exception as exc:
                print exc
                continue
            if key_id not in self.keys:
                self.keys[key_id] = self.user.keypairs[key_id].private

            if name in self.hosts:
                num = 2
                while ('%s-%d' % (name, num)) in self.hosts:
                    num += 1
                name = '%s-%d' % (name, num)

            self.hosts[name] = {
                'ansible_ssh_host': ip_addr,
                'ansible_ssh_port': port,
                'ansible_ssh_user': ssh_user,
                'ansible_ssh_private_key_file': 'id_rsa/%s' % key_id,
            }

    def export(self, include_localhost=True):
        ans_inv = ''
        if include_localhost:
            ans_inv += 'localhost\tansible_connection=local\n\n'
        for name, host in self.hosts.items():
            vars_part = ' '.join(["%s=%s" % item for item in host.items()])
            ans_inv += '%s\t%s\n' % (name, vars_part)
        ans_inv += ('\n[all:vars]\n'
                    'ansible_python_interpreter="/usr/bin/env python2"\n')
        ans_cfg = '[defaults]\nhostfile=./inventory\nhost_key_checking=False\n'
        files = {'ansible.cfg': ans_cfg, 'inventory': ans_inv}
        files.update({'id_rsa/%s' % key_id: private_key
                      for key_id, private_key in self.keys.items()})
        return files

    def _list_machines(self, backend_id):
        if backend_id not in self._cache:
            print 'Actually doing list_machines for %s' % backend_id
            machines = mist.io.methods.list_machines(self.user, backend_id)
            self._cache[backend_id] = machines
        return self._cache[backend_id]

    def find_machine_details(self, backend_id, machine_id):
        machines = self._list_machines(backend_id)
        for machine in machines:
            if machine['id'] == machine_id:
                name = machine['name']
                ips = [ip for ip in machine['public_ips'] if ':' not in ip]
                if not name:
                    name = machine_id<commit_msg>Replace spaces with underscores in ansible inventories<commit_after>import mist.io.methods


class MistInventory(object):
    def __init__(self, user, machines=None):
        self.user = user
        self.hosts = {}
        self.keys = {}
        self._cache = {}
        self.load(machines)

    def load(self, machines=None):
        self.hosts = {}
        self.keys = {}
        if not machines:
            machines = [(bid, m['id'])
                        for bid in self.user.backends
                        for m in self._list_machines(bid)]

        for bid, mid in machines:
            try:
                name, ip_addr = self.find_machine_details(bid, mid)
                key_id, ssh_user, port = self.find_ssh_settings(bid, mid)
            except Exception as exc:
                print exc
                continue
            if key_id not in self.keys:
                self.keys[key_id] = self.user.keypairs[key_id].private

            if name in self.hosts:
                num = 2
                while ('%s-%d' % (name, num)) in self.hosts:
                    num += 1
                name = '%s-%d' % (name, num)

            self.hosts[name] = {
                'ansible_ssh_host': ip_addr,
                'ansible_ssh_port': port,
                'ansible_ssh_user': ssh_user,
                'ansible_ssh_private_key_file': 'id_rsa/%s' % key_id,
            }

    def export(self, include_localhost=True):
        ans_inv = ''
        if include_localhost:
            ans_inv += 'localhost\tansible_connection=local\n\n'
        for name, host in self.hosts.items():
            vars_part = ' '.join(["%s=%s" % item for item in host.items()])
            ans_inv += '%s\t%s\n' % (name, vars_part)
        ans_inv += ('\n[all:vars]\n'
                    'ansible_python_interpreter="/usr/bin/env python2"\n')
        ans_cfg = '[defaults]\nhostfile=./inventory\nhost_key_checking=False\n'
        files = {'ansible.cfg': ans_cfg, 'inventory': ans_inv}
        files.update({'id_rsa/%s' % key_id: private_key
                      for key_id, private_key in self.keys.items()})
        return files

    def _list_machines(self, backend_id):
        if backend_id not in self._cache:
            print 'Actually doing list_machines for %s' % backend_id
            machines = mist.io.methods.list_machines(self.user, backend_id)
            self._cache[backend_id] = machines
        return self._cache[backend_id]

    def find_machine_details(self, backend_id, machine_id):
        machines = self._list_machines(backend_id)
        for machine in machines:
            if machine['id'] == machine_id:
                name = machine['name'].replace(' ', '_')
                ips = [ip for ip in machine['public_ips'] if ':' not in ip]
                if not name:
                    name = machine_id
{'fraction_non_alphanumeric': 0.11299233648191054, 'fraction_numerical': 0.0010693281055070398, 'mean_word_length': 1.7269193391642372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2726492', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1642, 'n_words': 379}