<commit_before>#!/usr/bin/python3
# NB: This script uses optparse, not argparse, for Python 2.6 compatibility and
# to avoid the need to depend on any other packages.
from optparse import OptionParser
import json
import random
import string
import sys
import urllib
import boto3

stackname = '<%= @aws_stackname -%>'
region = '<%= @aws_region -%>'

def commasep_callback(option, opt, value, parser):
    setattr(parser.values, option.dest, value.split(','))

def ec2_nodes(stackname, nodeclass=None):
    client = boto3.client('ec2', region_name=region)

    if nodeclass:
        response = client.describe_instances(
                   Filters=[
                          {'Name': 'tag:aws_stackname', 'Values': [stackname, 'govuk']},
                          {'Name': 'tag:aws_migration', 'Values': [nodeclass]},
                          {'Name': 'instance-state-name', 'Values': ['running']}
                   ]
               )
    else:
        response = client.describe_instances(
                   Filters=[
                          {'Name': 'tag:aws_stackname', 'Values': [stackname, 'govuk']},
                          {'Name': 'instance-state-name','Values': ['running']}
                   ]
               )

    hosts = []

    reservations = response['Reservations']
    for reservation in reservations:
        instances = reservation['Instances']
        for instance in instances:
            hosts.append((instance['PrivateDnsName']))

    return(hosts)

def ec2_nodes_list_with_puppet_class(stackname):
    client = boto3.client('ec2', region_name=region)

    response = client.describe_instances(
                   Filters=[
                          {'Name': 'tag:aws_stackname', 'Values': [stackname, 'govuk']},
                          {'Name': 'instance-state-name','Values': ['running']}
                   ]
               )

    reservations = response['Reservations']

    hostname_and_puppet_class = []

    for reservation in reservations:
        for instance in list(reservation['Instances']):
            hostname_and_puppet_class.append("%s:%s" % (
                instance['PrivateDnsName'],
                next(
                    filter(
                        lambda t: t['Key'] == 'aws_hostname',
                        instance['Tags']
                    )
                )['Value']))

    return hostname_and_puppet_class

def ec2_classes(stackname):
    client = boto3.client('ec2', region_name=region)

    response = client.describe_instances(
           Filters=[
              {'Name': 'tag:aws_stackname', 'Values': [stackname, 'govuk']},
              {'Name': 'instance-state-name','Values': ['running']}
           ]
       )

    classes = []
<commit_msg>Refactor `--with-puppet-class` code to be more Pythonic<commit_after>#!/usr/bin/python3
# NB: This script uses optparse, not argparse, for Python 2.6 compatibility and
# to avoid the need to depend on any other packages.
from optparse import OptionParser
import json
import random
import string
import sys
import urllib
import boto3

stackname = '<%= @aws_stackname -%>'
region = '<%= @aws_region -%>'

def commasep_callback(option, opt, value, parser):
    setattr(parser.values, option.dest, value.split(','))

def ec2_nodes(stackname, nodeclass=None):
    client = boto3.client('ec2', region_name=region)

    if nodeclass:
        response = client.describe_instances(
                   Filters=[
                          {'Name': 'tag:aws_stackname', 'Values': [stackname, 'govuk']},
                          {'Name': 'tag:aws_migration', 'Values': [nodeclass]},
                          {'Name': 'instance-state-name', 'Values': ['running']}
                   ]
               )
    else:
        response = client.describe_instances(
                   Filters=[
                          {'Name': 'tag:aws_stackname', 'Values': [stackname, 'govuk']},
                          {'Name': 'instance-state-name','Values': ['running']}
                   ]
               )

    hosts = []

    reservations = response['Reservations']
    for reservation in reservations:
        instances = reservation['Instances']
        for instance in instances:
            hosts.append((instance['PrivateDnsName']))

    return(hosts)

def ec2_nodes_list_with_puppet_class(stackname):
    client = boto3.client('ec2', region_name=region)

    response = client.describe_instances(
                   Filters=[
                          {'Name': 'tag:aws_stackname', 'Values': [stackname, 'govuk']},
                          {'Name': 'instance-state-name','Values': ['running']}
                   ]
               )

    reservations = response['Reservations']

    hostname_and_puppet_class = []

    for reservation in reservations:
        for instance in reservation['Instances']:
            private_dns_name = instance['PrivateDnsName']
            hostname = next(tag['Value'] for tag in instance['Tags'] if tag['Key'] == 'aws_hostname')
            hostname_and_puppet_class.append("{private_dns_name}:{hostname}".format(private_dns_name=private_dns_name, hostname=hostname))

    return hostname_and_puppet_class

def ec2_classes(stackname):
    client = boto3.client('ec2', region_name=region)

    response = client.describe_instances(
           Filters=[
              {'Name': 'tag:aws_stackname', 'Values': [stackname, 'govuk']},
              {'Name': 'instance-state-name','Values': ['running']}
           ]
       )

    classes = []

{'fraction_non_alphanumeric': 0.13111760409057707, 'fraction_numerical': 0.004747991234477721, 'mean_word_length': 2.2066744730679155, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4039965', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1524, 'n_words': 325}