<commit_before>from boto.ec2 import EC2Connection
from boto.ec2.securitygroup import SecurityGroup
import logging
from mako.template import Template
import urllib2

def get_self_instance_id():
    '''
    Get this instance's id.
    '''
    logging.debug('get_self_instance_id()')
    response = urllib2.urlopen('http://169.254.169.254/1.0/meta-data/instance-id')
    instance_id = response.read()
    return instance_id


def steal_elastic_ip(access_key=None, secret_key=None, ip=None):
    '''
    Assign an elastic IP to this instance.
    '''
    logging.debug('steal_elastic_ip()')
    instance_id = get_self_instance_id()
    conn = EC2Connection(aws_access_key_id=access_key,
                         aws_secret_access_key=secret_key)
    conn.associate_address(instance_id=instance_id, public_ip=ip)


def get_running_instances(access_key=None, secret_key=None, security_group=None):
    '''
    Get all running instances. Only within a security group if specified.
    '''
    logging.debug('get_running_instances()')

    instances_all_regions_list = []
    conn = EC2Connection(aws_access_key_id=access_key,
                         aws_secret_access_key=secret_key)
    ec2_region_list = conn.get_all_regions()

    if security_group:
        for index, region in enumerate(ec2_region_list):
            conn = EC2Connection(aws_access_key_id=access_key,
                                 aws_secret_access_key=secret_key,
                                 region=ec2_region_list[index])
            running_instances = []
            for s in conn.get_all_security_groups():
               if s.name == security_group:
                   running_instances = [i for i in sg.instances() if i.state == 'running']
            if running_instances:
                for instance in running_instances:
                    instances_all_regions_list.append(instance)
    else:
        for index, region in enumerate(ec2_region_list):
            conn = EC2Connection(aws_access_key_id=access_key,
                                 aws_secret_access_key=secret_key,
                                 region=ec2_region_list[index])
            reserved_instances = conn.get_all_instances()
            if reserved_instances:
                for reservation in reserved_instances:
                    for instance in reservation.instances:
                        if instance.stat == 'running':
                            instances_all_regions_list.append(instance)
    return instances_all_regions_list


def file_contents(filename=None, content=None):
    '''
    Just return the contents of a file as a string or write if content
    is specified. Returns the contents of the filename either way.
    '''
    logging.debug('file_contents()')
    if content:<commit_msg>Fix sg is not defined<commit_after>from boto.ec2 import EC2Connection
from boto.ec2.securitygroup import SecurityGroup
import logging
from mako.template import Template
import urllib2

def get_self_instance_id():
    '''
    Get this instance's id.
    '''
    logging.debug('get_self_instance_id()')
    response = urllib2.urlopen('http://169.254.169.254/1.0/meta-data/instance-id')
    instance_id = response.read()
    return instance_id


def steal_elastic_ip(access_key=None, secret_key=None, ip=None):
    '''
    Assign an elastic IP to this instance.
    '''
    logging.debug('steal_elastic_ip()')
    instance_id = get_self_instance_id()
    conn = EC2Connection(aws_access_key_id=access_key,
                         aws_secret_access_key=secret_key)
    conn.associate_address(instance_id=instance_id, public_ip=ip)


def get_running_instances(access_key=None, secret_key=None, security_group=None):
    '''
    Get all running instances. Only within a security group if specified.
    '''
    logging.debug('get_running_instances()')

    instances_all_regions_list = []
    conn = EC2Connection(aws_access_key_id=access_key,
                         aws_secret_access_key=secret_key)
    ec2_region_list = conn.get_all_regions()

    if security_group:
        for index, region in enumerate(ec2_region_list):
            conn = EC2Connection(aws_access_key_id=access_key,
                                 aws_secret_access_key=secret_key,
                                 region=ec2_region_list[index])
            running_instances = []
            for s in conn.get_all_security_groups():
               if s.name == security_group:
                   running_instances = [i for i in s.instances() if i.state == 'running']
            if running_instances:
                for instance in running_instances:
                    instances_all_regions_list.append(instance)
    else:
        for index, region in enumerate(ec2_region_list):
            conn = EC2Connection(aws_access_key_id=access_key,
                                 aws_secret_access_key=secret_key,
                                 region=ec2_region_list[index])
            reserved_instances = conn.get_all_instances()
            if reserved_instances:
                for reservation in reserved_instances:
                    for instance in reservation.instances:
                        if instance.stat == 'running':
                            instances_all_regions_list.append(instance)
    return instances_all_regions_list


def file_contents(filename=None, content=None):
    '''
    Just return the contents of a file as a string or write if content
    is specified. Returns the contents of the filename either way.
    '''
    logging.debug('file_contents()')
    if content:
{'fraction_non_alphanumeric': 0.0778518652009371, 'fraction_numerical': 0.010091908451973329, 'mean_word_length': 2.4839924670433144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4782732', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1548, 'n_words': 329}