<commit_before>from __future__ import print_function, absolute_import, division

import json
import logging
import subprocess
import time

REGION = "--region=eu-west-1"


def run(command):
    # On clean build systems, the "aws ...." commands fail because no default
    # region is configured. We need to explicitly name it in every command.
    command.append(REGION)

    logging.debug("Running this command: %r", command)
    subprocess.check_call(command)


def get_active_rule_set():
    """Return the name of the active rule set (if any), else None"""
    # Avoid throttling
    time.sleep(1)
    currently_active_rule_set = subprocess.check_output([
        'aws ses describe-active-receipt-rule-set ' + REGION],
        shell=True)
    if not currently_active_rule_set:
        return
    return json.loads(currently_active_rule_set)['Metadata']['Name']


def deactivate_rule_set_if_active(rule_set_name):
    if get_active_rule_set() != rule_set_name:
        return

    # Without parameter, set-active-receipt-rule-set deactivates the
    # currently active rule.
    run(['aws', 'ses', 'set-active-receipt-rule-set'])


def delete_rule_set(rule_set_name):
    """Delete the given rule set if it exists

    If the rule set is currently active (which would normally prevent
    deletion), the rule set is deactivated first.
    """
    deactivate_rule_set_if_active(rule_set_name)
    run(['aws', 'ses', 'delete-receipt-rule-set',
         '--rule-set-name', rule_set_name])


def generate_rule(domain, s3_bucket):
    rule = {
        "Name": "postmaster",
        "Enabled": True,
        "Recipients": ["postmaster@{0}".format(domain)],
        "Actions": [{
            "S3Action": {
                "BucketName": s3_bucket
            }
        }]
    }
    return json.dumps(rule)


def create_rule_set(rule_set_name, rule):
    """Create the given rule set and activate it

    This assumes that no rule set of that name currently exists
    """
    run(['aws', 'ses', 'create-receipt-rule-set',
         '--rule-set-name', rule_set_name])

    run(['aws', 'ses', 'create-receipt-rule',
         '--rule-set-name', rule_set_name, '--rule', rule])

    run(['aws', 'ses', 'set-active-receipt-rule-set',
         '--rule-set-name', rule_set_name])


def setup_ses_rule_set(domain, s3_bucket):
    rule_set_name = "standard_addresses_for_{0}".format(domain)
    rule = generate_rule(domain, s3_bucket)

    delete_rule_set(rule_set_name)
    create_rule_set(rule_set_name, rule)<commit_msg>Allow wildcard domains as arguments<commit_after>from __future__ import print_function, absolute_import, division

import json
import logging
import subprocess
import time

from .configure_dns import normalize_domain

REGION = "--region=eu-west-1"


def run(command):
    # On clean build systems, the "aws ...." commands fail because no default
    # region is configured. We need to explicitly name it in every command.
    command.append(REGION)

    logging.debug("Running this command: %r", command)
    subprocess.check_call(command)


def get_active_rule_set():
    """Return the name of the active rule set (if any), else None"""
    # Avoid throttling
    time.sleep(1)
    currently_active_rule_set = subprocess.check_output([
        'aws ses describe-active-receipt-rule-set ' + REGION],
        shell=True)
    if not currently_active_rule_set:
        return
    return json.loads(currently_active_rule_set)['Metadata']['Name']


def deactivate_rule_set_if_active(rule_set_name):
    if get_active_rule_set() != rule_set_name:
        return

    # Without parameter, set-active-receipt-rule-set deactivates the
    # currently active rule.
    run(['aws', 'ses', 'set-active-receipt-rule-set'])


def delete_rule_set(rule_set_name):
    """Delete the given rule set if it exists

    If the rule set is currently active (which would normally prevent
    deletion), the rule set is deactivated first.
    """
    deactivate_rule_set_if_active(rule_set_name)
    run(['aws', 'ses', 'delete-receipt-rule-set',
         '--rule-set-name', rule_set_name])


def generate_rule(domain, s3_bucket):
    rule = {
        "Name": "postmaster",
        "Enabled": True,
        "Recipients": ["post<EMAIL>(domain)],
        "Actions": [{
            "S3Action": {
                "BucketName": s3_bucket
            }
        }]
    }
    return json.dumps(rule)


def create_rule_set(rule_set_name, rule):
    """Create the given rule set and activate it

    This assumes that no rule set of that name currently exists
    """
    run(['aws', 'ses', 'create-receipt-rule-set',
         '--rule-set-name', rule_set_name])

    run(['aws', 'ses', 'create-receipt-rule',
         '--rule-set-name', rule_set_name, '--rule', rule])

    run(['aws', 'ses', 'set-active-receipt-rule-set',
         '--rule-set-name', rule_set_name])


def setup_ses_rule_set(domain, s3_bucket):
    normalized_domain = normalize_domain(domain)
    rule_set_name = "standard_addresses_for_{0}".format(normalized_domain)
    rule = generate_rule(normalized_domain, s3_bucket)

    delete_rule_set(rule_set_name)
    create_rule_set(rule_set_name, rule)
{'fraction_non_alphanumeric': 0.1204514496983849, 'fraction_numerical': 0.0033080365829928, 'mean_word_length': 3.3229604709840204, 'pattern_counts': {'":': 12, '<': 4, '<?xml version=': 0, '>': 4, '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': '5824785', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1600, 'n_words': 399}