# Path: n_utils/cf_utils.py
# def region(self):
#     if 'region' in self._info:
#         return self._info['region']
#     else:
#         return None


from builtins import str
from os import devnull
from subprocess import check_call, STDOUT
from botocore.exceptions import ClientError
from .cf_utils import region
import base64
import boto3
from __future__ import print_function





def ensure_repo(name):
    ecr = boto3.client("ecr", region_name=region())
    repo = None
    try:
        repo_resp = ecr.describe_repositories(repositoryNames=[name])
        if 'repositories' in repo_resp:
            repo = repo_resp['repositories'][0]
    except ClientError:
        repo_resp = ecr.create_repository(repositoryName=name)
        if 'repository' in repo_resp:
            repo = repo_resp['repository']
    if not repo:
        raise Exception("Failed to find or create repo")
    print("REPO=\"" + repo['repositoryUri'] + "\"")
    token_resp = ecr.get_authorization_token(registryIds=[repo['registryId']])
    if 'authorizationData' in token_resp:
        auth_data = token_resp['authorizationData'][0]
        full_token = base64.b64decode(auth_data['authorizationToken']).decode("utf-8").split(":")
        user = full_token[0]
        token = full_token[1]
        print("docker login -u " + user + " -p " + token + " " + auth_data['proxyEndpoint'])


def repo_uri(name):

# Path: n_utils/cf_utils.py
# def region(self):
#     if 'region' in self._info:
#         return self._info['region']
#     else:
#         return None


# Path: n_utils/ecr_utils.py
from builtins import str
from os import devnull
from subprocess import check_call, STDOUT
from botocore.exceptions import ClientError
from .cf_utils import region
import base64
import boto3

from __future__ import print_function





def ensure_repo(name):
    ecr = boto3.client("ecr", region_name=region())
    repo = None
    try:
        repo_resp = ecr.describe_repositories(repositoryNames=[name])
        if 'repositories' in repo_resp:
            repo = repo_resp['repositories'][0]
    except ClientError:
        repo_resp = ecr.create_repository(repositoryName=name)
        if 'repository' in repo_resp:
            repo = repo_resp['repository']
    if not repo:
        raise Exception("Failed to find or create repo")
    print("REPO=\"" + repo['repositoryUri'] + "\"")
    token_resp = ecr.get_authorization_token(registryIds=[repo['registryId']])
    if 'authorizationData' in token_resp:
        auth_data = token_resp['authorizationData'][0]
        full_token = base64.b64decode(auth_data['authorizationToken']).decode("utf-8").split(":")
        user = full_token[0]
        token = full_token[1]
        print("docker login -u " + user + " -p " + token + " " + auth_data['proxyEndpoint'])


def repo_uri(name):

    ecr = boto3.client("ecr", region_name=region())