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)

@@ -117,16 +117,61 @@
 t time%0A%0A
+from .configure_dns import normalize_domain%0A%0A
 REGION =
@@ -2325,32 +2325,81 @@
 in, s3_bucket):%0A
+    normalized_domain = normalize_domain(domain)%0A
     rule_set_nam
@@ -2434,24 +2434,35 @@
 %7B0%7D%22.format(
+normalized_
 domain)%0A    
@@ -2474,32 +2474,43 @@
 = generate_rule(
+normalized_
 domain, s3_bucke

Allow wildcard domains as arguments