<commit_before>#!/usr/bin/env python

import datetime
import json
import os
import re
import sys

from keystoneclient.v2_0 import client
from keystoneclient.openstack.common.apiclient import exceptions

AUTH_DETAILS = {'OS_USERNAME': None,
                'OS_PASSWORD': None,
                'OS_TENANT_NAME': None,
                'OS_AUTH_URL': None}

OPENRC = '/root/openrc'
TOKEN_FILE = '/root/.auth_ref.json'


def get_auth_ref():
    auth_details = set_auth_details()
    auth_ref = get_token_from_file(auth_details)
    expires = datetime.datetime.strptime(auth_ref['token']['expires'],
                                         '%Y-%m-%dT%H:%M:%SZ')
    if datetime.datetime.now() >= expires:
        auth_ref = keystone_auth(auth_details)

    return auth_ref


def get_token_from_file(auth_details):
    if os.path.exists(TOKEN_FILE):
        with open(TOKEN_FILE) as token_file:
            auth_ref = json.load(token_file)

        return auth_ref
    else:
        return keystone_auth(auth_details)


def keystone_auth(auth_details):
    try:
        keystone = client.Client(username=auth_details['OS_USERNAME'],
                                 password=auth_details['OS_PASSWORD'],
                                 tenant_name=auth_details['OS_TENANT_NAME'],
                                 auth_url=auth_details['OS_AUTH_URL'])
    except (exceptions.Unauthorized, exceptions.AuthorizationFailure) as e:
        print "status err %s" % e
        sys.exit(1)

    with open(TOKEN_FILE, 'w') as token_file:
        json.dump(keystone.auth_ref, token_file)

    return keystone.auth_ref


def set_auth_details():
    auth_details = AUTH_DETAILS
    pattern = re.compile('^(export\s)?(?P<key>\w+)(\s+)?=(\s+)?(?P<value>.*)$')

    openrc = open(OPENRC)
    for line in openrc:
        match = pattern.match(line)<commit_msg>Refactor maas common some more<commit_after>#!/usr/bin/env python

import datetime
import json
import os
import re
import sys

from keystoneclient.v2_0 import client
from keystoneclient.openstack.common.apiclient import exceptions

AUTH_DETAILS = {'OS_USERNAME': None,
                'OS_PASSWORD': <PASSWORD>,
                'OS_TENANT_NAME': None,
                'OS_AUTH_URL': None}

OPENRC = '/root/openrc'
TOKEN_FILE = '/root/.auth_ref.json'


def is_token_expired(token):
    expires = datetime.datetime.strptime(token['expires'],
                                         '%Y-%m-%dT%H:%M:%SZ')
    return datetime.datetime.now() >= expires


def get_auth_ref():
    auth_details = get_auth_details()
    auth_ref = get_auth_from_file(auth_details)
    if auth_ref is None:
        auth_ref = keystone_auth(auth_details)

    if is_token_expired(auth_ref['token']):
        auth_ref = keystone_auth(auth_details)

    return auth_ref


def get_auth_from_file(auth_details):
    if os.path.exists(TOKEN_FILE):
        with open(TOKEN_FILE) as token_file:
            auth_ref = json.load(token_file)

        return auth_ref
    return None


def keystone_auth(auth_details):
    try:
        keystone = client.Client(username=auth_details['OS_USERNAME'],
                                 password=<PASSWORD>_details['<PASSWORD>'],
                                 tenant_name=auth_details['OS_TENANT_NAME'],
                                 auth_url=auth_details['OS_AUTH_URL'])
    except (exceptions.Unauthorized, exceptions.AuthorizationFailure) as e:
        print "status err %s" % e
        sys.exit(1)

    with open(TOKEN_FILE, 'w') as token_file:
        json.dump(keystone.auth_ref, token_file)

    return keystone.auth_ref


def get_auth_details():
    auth_details = AUTH_DETAILS
    pattern = re.compile('^(export\s)?(?P<key>\w+)(\s+)?=(\s+)?(?P<value>.*)$')

    openrc = open(OPENRC)
    for line in openrc:
        match = pattern.match(line)
{'fraction_non_alphanumeric': 0.11350499211770888, 'fraction_numerical': 0.0015764582238570678, 'mean_word_length': 2.6747104247104247, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3045711', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1206, 'n_words': 224}