<commit_before>import os
from pathlib import Path
import re
import secrets
import datetime
import string
import boto3
import in_place
from installer.configurators.validate_tags import prepare_tags, validate_replication_tags


# TODO revisit the use of an env var here, only the destroy script uses it, and it probably shouldn't
def get_installer_root():
    # Set the repo root path as an env var here,
    # so subsequent scripts don't need to hardcode absolute paths.
    return os.environ.get('JAZZ_INSTALLER_ROOT', str(Path('.').absolute()))


def get_terraform_folder():
    return get_installer_root() + "/installer/terraform/jazz-terraform-unix-noinstances/"


def get_tfvars_file():
    return get_terraform_folder() + "terraform.tfvars"


def passwd_generator():
    """
        Random password generator for jazz_ui admin email ID login
    """
    alphabet = string.ascii_letters + string.digits
    return ''.join(secrets.choice(alphabet) for i in range(8))


def get_account_id():
    return boto3.client('sts').get_caller_identity().get('Account')


def update_main_terraform_vars(branch, adminemail, stackprefix, region, tags):
    """
        Method parse the parameters send from run.py and these common variables
        are set in terraform.tfvars and other files needed
    """
    jazz_branch = branch
    cognito_details = [adminemail, passwd_generator()]
    jazz_account_id = get_account_id()
    prefix = stackprefix.lower()
    tag_exempt = (datetime.datetime.today() +
                  datetime.timedelta(days=1)).strftime("%m/%d/%Y")

    # ----------------------------------------------------------
    # Populate Terraform variables in terraform.tfvars and Chef cookbook
    # -----------------------------------------------------------

    # populating BRANCH name
    replace_tfvars('github_branch', jazz_branch, get_tfvars_file())

    # Populating Jazz Account ID
    replace_tfvars('jazz_accountid', jazz_account_id, get_tfvars_file())

    # Populating Cognito Details
    replace_tfvars('cognito_pool_username', cognito_details[0],
                   get_tfvars_file())
    replace_tfvars('cognito_pool_password', cognito_details[1],
                   get_tfvars_file())

    # Set the default values of scm_username/passwd to the cognito credentials
    # If using existing SCM instance, that scenario (e.g. 1) will obviously overwrite these values.

    replace_tfvars('scm_username', cognito_details[0], get_tfvars_file())
    replace_tfvars('scm_passwd', cognito_details[1], get_tfvars_file())

    # Populating Jazz Tag env
    replace_tfvars('envPrefix', prefix, get_tfvars_file())
    replace_tfvars('tagsExempt', tag_exempt, get_tfvars_file())

    # Populating user tags
    if tags:
        input_tags = prepare_tags(tags)
        try:
            aws_tags, aws_formatted_tags = validate_replication_tags(input_tags)
            replace_tfvars("aws_tags", str(aws_tags), get_tfvars_file())
            replace_tfvars("additional_tags", str(aws_tags), get_tfvars_file(), quoteVal=False)
        except ValueError as err:
            print("Invalid Tag!" + str(err))
            exit(0)

    # TODO replace this with `terraform output` and drop sed
    # subprocess.call([
    #     'sed', '-i',
    #     's|stack_name=.*.$|stack_name="%s"|g' % (prefix),
    #     "scripts/destroy.sh"
    # ])


# Uses sed to modify the values of key-value pairs within a file
# (such as a .tfvars file) that follow the form 'key = value'

def replace_tfvars(key, value, fileName, quoteVal=True):
    with in_place.InPlace(fileName) as fileContent:
        for line in fileContent:
            if quoteVal:
                fileContent.write(re.sub(r'({0} = )(.*)'.format(key), r'\1"{0}"'.format(value), line))
            else:
                fileContent.write(re.sub(r'({0} = )(.*)'.format(key), r'\1{0}'.format(value), line))


# def replace_tfvars(key, value, fileName):
#     subprocess.call([
#         'sed', "-i\'.bak\'",
#         r's|\(%s = \)\(.*\)|\1\"%s\"|g' % (key, value), fileName
#     ])

# replace it without double quotes
# def replace_tfvars_map(key, value, fileName):
#     subprocess.call([
#         'sed', "-i\'.bak\'",
#         r's|\(%s = \)\(.*\)|\1%s|g' % (key, value), fileName
#     ])


def validate_email_id(email_id):
    """
        Parse the parameters send from run.py and validate Cognito details
    """
    if re.search(r'[^@]+@[^@]+.[^@]+', email_id) is None:
        return False
    else:
        return True
<commit_msg>Export region (for the time being, again only used for destroy script)<commit_after>import os
from pathlib import Path
import re
import secrets
import datetime
import string
import boto3
import in_place
from installer.configurators.validate_tags import prepare_tags, validate_replication_tags


# TODO revisit the use of an env var here, only the destroy script uses it, and it probably shouldn't
def get_installer_root():
    # Set the repo root path as an env var here,
    # so subsequent scripts don't need to hardcode absolute paths.
    return os.environ.get('JAZZ_INSTALLER_ROOT', str(Path('.').absolute()))


def get_terraform_folder():
    return get_installer_root() + "/installer/terraform/jazz-terraform-unix-noinstances/"


def get_tfvars_file():
    return get_terraform_folder() + "terraform.tfvars"


def passwd_generator():
    """
        Random password generator for jazz_ui admin email ID login
    """
    alphabet = string.ascii_letters + string.digits
    return ''.join(secrets.choice(alphabet) for i in range(8))


def get_account_id():
    return boto3.client('sts').get_caller_identity().get('Account')


def update_main_terraform_vars(branch, adminemail, stackprefix, region, tags):
    """
        Method parse the parameters send from run.py and these common variables
        are set in terraform.tfvars and other files needed
    """
    jazz_branch = branch
    cognito_details = [adminemail, passwd_generator()]
    jazz_account_id = get_account_id()
    prefix = stackprefix.lower()
    tag_exempt = (datetime.datetime.today() +
                  datetime.timedelta(days=1)).strftime("%m/%d/%Y")

    # ----------------------------------------------------------
    # Populate Terraform variables in terraform.tfvars
    # -----------------------------------------------------------

    # populating BRANCH name
    replace_tfvars('github_branch', jazz_branch, get_tfvars_file())

    # populating region
    replace_tfvars('region', region, get_tfvars_file())
    os.environ['AWS_DEFAULT_REGION'] = region

    # Populating Jazz Account ID
    replace_tfvars('jazz_accountid', jazz_account_id, get_tfvars_file())

    # Populating Cognito Details
    replace_tfvars('cognito_pool_username', cognito_details[0],
                   get_tfvars_file())
    replace_tfvars('cognito_pool_password', cognito_details[1],
                   get_tfvars_file())

    # Set the default values of scm_username/passwd to the cognito credentials
    # If using existing SCM instance, that scenario (e.g. 1) will obviously overwrite these values.

    replace_tfvars('scm_username', cognito_details[0], get_tfvars_file())
    replace_tfvars('scm_passwd', cognito_details[1], get_tfvars_file())

    # Populating Jazz Tag env
    replace_tfvars('envPrefix', prefix, get_tfvars_file())
    replace_tfvars('tagsExempt', tag_exempt, get_tfvars_file())

    # Populating user tags
    if tags:
        input_tags = prepare_tags(tags)
        try:
            aws_tags, aws_formatted_tags = validate_replication_tags(input_tags)
            replace_tfvars("aws_tags", str(aws_tags), get_tfvars_file())
            replace_tfvars("additional_tags", str(aws_tags), get_tfvars_file(), quoteVal=False)
        except ValueError as err:
            print("Invalid Tag!" + str(err))
            exit(0)

    # TODO replace this with `terraform output` and drop sed
    # subprocess.call([
    #     'sed', '-i',
    #     's|stack_name=.*.$|stack_name="%s"|g' % (prefix),
    #     "scripts/destroy.sh"
    # ])


# Uses sed to modify the values of key-value pairs within a file
# (such as a .tfvars file) that follow the form 'key = value'

def replace_tfvars(key, value, fileName, quoteVal=True):
    with in_place.InPlace(fileName) as fileContent:
        for line in fileContent:
            if quoteVal:
                fileContent.write(re.sub(r'({0} = )(.*)'.format(key), r'\1"{0}"'.format(value), line))
            else:
                fileContent.write(re.sub(r'({0} = )(.*)'.format(key), r'\1{0}'.format(value), line))


# def replace_tfvars(key, value, fileName):
#     subprocess.call([
#         'sed', "-i\'.bak\'",
#         r's|\(%s = \)\(.*\)|\1\"%s\"|g' % (key, value), fileName
#     ])

# replace it without double quotes
# def replace_tfvars_map(key, value, fileName):
#     subprocess.call([
#         'sed', "-i\'.bak\'",
#         r's|\(%s = \)\(.*\)|\1%s|g' % (key, value), fileName
#     ])


def validate_email_id(email_id):
    """
        Parse the parameters send from run.py and validate Cognito details
    """
    if re.search(r'[^@]+@[^@]+.[^@]+', email_id) is None:
        return False
    else:
        return True

{'fraction_non_alphanumeric': 0.14756603361711418, 'fraction_numerical': 0.003929273084479371, 'mean_word_length': 2.963235294117647, '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': '7466002', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2816, 'n_words': 707}