# Path: doodad/mount.py
# class Mount(object):
# class MountLocal(Mount):
# class MountGit(Mount):
# class MountS3(Mount):
# class MountGCP(Mount):
#     def __init__(self, mount_point=None, pythonpath=False, output=False):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
#     def writeable(self):
#     def name(self):
#     def __str__(self):
#     def __init__(self, local_dir, mount_point=None, cleanup=True,
#                 filter_ext=('.pyc', '.log', '.git', '.mp4'),
#                 filter_dir=('data', '.git'),
#                 **kwargs):
#     def ignore_patterns(self, dirname, contents):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
#     def __str__(self):
#     def docker_mount_dir(self):
#     def __init__(self, git_url, branch=None,
#                  ssh_identity=None, **kwargs):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
#     def __init__(self,
#                 s3_path,
#                 sync_interval=15,
#                 output=True,
#                 dry=False,
#                 include_types=('*.txt', '*.csv', '*.json', '*.gz', '*.tar', '*.log', '*.pkl'),
#                 **kwargs):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
#     def __init__(self,
#                 gcp_path=None,
#                 sync_interval=15,
#                 output=True,
#                 dry=False,
#                 exclude_regex='*.tmp',
#                 **kwargs):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
# 
# Path: doodad/launch/launch_api.py
# def run_command(
#         command,
#         cli_args=None,
#         mode=launch_mode.LocalMode(),
#         mounts=tuple(),
#         return_output=False,
#         verbose=False,
#         docker_image='ubuntu:18.04',
#         singularity_image=None,
#         container_type='docker',
#         extra_container_flags='',
#     ):
# def run_python(
#         target,
#         target_mount_dir='target',
#         mounts=tuple(),
#         docker_image='python:3',
#         **kwargs
#     ):
# def make_python_command(
#         target,
#         python_cmd='python',
#     ):
# 
# Path: doodad/darchive/archive_builder_docker.py
# THIS_FILE_DIR = os.path.dirname(__file__)
# MAKESELF_PATH = os.path.join(THIS_FILE_DIR, 'makeself.sh')
# MAKESELF_HEADER_PATH = os.path.join(THIS_FILE_DIR, 'makeself-header.sh')
# BEGIN_HEADER = '--- BEGIN DAR OUTPUT ---'
# DAR_PAYLOAD_MOUNT = 'dar_payload'
# FINAL_SCRIPT = './final_script.sh'
# def build_archive(archive_filename='runfile.dar',
#                   docker_image='ubuntu:18.04',
#                   singularity_image=None,
#                   container_type='docker',
#                   extra_container_flags='',
#                   payload_script='',
#                   mounts=(),
#                   use_gpu_image=False,
#                   verbose=False):
# def write_metadata(arch_dir):
# def write_docker_hook(
#         arch_dir, image_name, mounts, script_name,
#         extra_flags='',
#         verbose=False, use_nvidia_docker=False):
# def write_singularity_hook(arch_dir, image_name, mounts,
#                            script_name,
#                            extra_flags='',
#                            verbose=False, use_nvidia_docker=False):
#     def create_bind_flag(mnt):
# def write_run_script(arch_dir, mounts, payload_script, verbose=False):
# def compile_archive(archive_dir, output_file, script_name, verbose=False):
# def run_archive(filename, cli_args='', encoding='utf-8', shell_interpreter='sh', timeout=None, get_output=True):
# def _strip_stdout(output):
# def temp_archive_file():


import math
import os
import itertools
import multiprocessing
import random
import hashlib
import doodad
from datetime import datetime
from doodad import mount
from doodad.launch import launch_api
from doodad.darchive import archive_builder_docker as archive_builder
            yield kwargs


def chunker(sweeper, num_chunks=10, confirm=True):
    chunks = [ [] for _ in range(num_chunks) ]
    print('computing chunks')
    configs = [config for config in sweeper]
    random.shuffle(configs, random.random)
    for i, config in enumerate(configs):
        chunks[i % num_chunks].append(config)
    print('num chunks:  ', num_chunks)
    print('chunk sizes: ', [len(chunk) for chunk in chunks])
    print('total jobs:  ', sum([len(chunk) for chunk in chunks]))

    resp = 'y'
    if confirm:
        print('continue?(y/n)')
        resp = str(input())

    if resp == 'y':
        return chunks
    else:
        return []


def run_sweep_doodad(target, params, run_mode, mounts, test_one=False, docker_image='python:3', return_output=False, verbose=False):

    # build archive
    target_dir = os.path.dirname(target)
    target_mount_dir = os.path.join('target', os.path.basename(target_dir))

# Path: doodad/mount.py
# class Mount(object):
# class MountLocal(Mount):
# class MountGit(Mount):
# class MountS3(Mount):
# class MountGCP(Mount):
#     def __init__(self, mount_point=None, pythonpath=False, output=False):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
#     def writeable(self):
#     def name(self):
#     def __str__(self):
#     def __init__(self, local_dir, mount_point=None, cleanup=True,
#                 filter_ext=('.pyc', '.log', '.git', '.mp4'),
#                 filter_dir=('data', '.git'),
#                 **kwargs):
#     def ignore_patterns(self, dirname, contents):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
#     def __str__(self):
#     def docker_mount_dir(self):
#     def __init__(self, git_url, branch=None,
#                  ssh_identity=None, **kwargs):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
#     def __init__(self,
#                 s3_path,
#                 sync_interval=15,
#                 output=True,
#                 dry=False,
#                 include_types=('*.txt', '*.csv', '*.json', '*.gz', '*.tar', '*.log', '*.pkl'),
#                 **kwargs):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
#     def __init__(self,
#                 gcp_path=None,
#                 sync_interval=15,
#                 output=True,
#                 dry=False,
#                 exclude_regex='*.tmp',
#                 **kwargs):
#     def dar_build_archive(self, deps_dir):
#     def dar_extract_command(self):
# 
# Path: doodad/launch/launch_api.py
# def run_command(
#         command,
#         cli_args=None,
#         mode=launch_mode.LocalMode(),
#         mounts=tuple(),
#         return_output=False,
#         verbose=False,
#         docker_image='ubuntu:18.04',
#         singularity_image=None,
#         container_type='docker',
#         extra_container_flags='',
#     ):
# def run_python(
#         target,
#         target_mount_dir='target',
#         mounts=tuple(),
#         docker_image='python:3',
#         **kwargs
#     ):
# def make_python_command(
#         target,
#         python_cmd='python',
#     ):
# 
# Path: doodad/darchive/archive_builder_docker.py
# THIS_FILE_DIR = os.path.dirname(__file__)
# MAKESELF_PATH = os.path.join(THIS_FILE_DIR, 'makeself.sh')
# MAKESELF_HEADER_PATH = os.path.join(THIS_FILE_DIR, 'makeself-header.sh')
# BEGIN_HEADER = '--- BEGIN DAR OUTPUT ---'
# DAR_PAYLOAD_MOUNT = 'dar_payload'
# FINAL_SCRIPT = './final_script.sh'
# def build_archive(archive_filename='runfile.dar',
#                   docker_image='ubuntu:18.04',
#                   singularity_image=None,
#                   container_type='docker',
#                   extra_container_flags='',
#                   payload_script='',
#                   mounts=(),
#                   use_gpu_image=False,
#                   verbose=False):
# def write_metadata(arch_dir):
# def write_docker_hook(
#         arch_dir, image_name, mounts, script_name,
#         extra_flags='',
#         verbose=False, use_nvidia_docker=False):
# def write_singularity_hook(arch_dir, image_name, mounts,
#                            script_name,
#                            extra_flags='',
#                            verbose=False, use_nvidia_docker=False):
#     def create_bind_flag(mnt):
# def write_run_script(arch_dir, mounts, payload_script, verbose=False):
# def compile_archive(archive_dir, output_file, script_name, verbose=False):
# def run_archive(filename, cli_args='', encoding='utf-8', shell_interpreter='sh', timeout=None, get_output=True):
# def _strip_stdout(output):
# def temp_archive_file():


# Path: doodad/wrappers/sweeper/hyper_sweep.py
import math
import os
import itertools
import multiprocessing
import random
import hashlib
import doodad
from datetime import datetime
from doodad import mount
from doodad.launch import launch_api
from doodad.darchive import archive_builder_docker as archive_builder

            yield kwargs


def chunker(sweeper, num_chunks=10, confirm=True):
    chunks = [ [] for _ in range(num_chunks) ]
    print('computing chunks')
    configs = [config for config in sweeper]
    random.shuffle(configs, random.random)
    for i, config in enumerate(configs):
        chunks[i % num_chunks].append(config)
    print('num chunks:  ', num_chunks)
    print('chunk sizes: ', [len(chunk) for chunk in chunks])
    print('total jobs:  ', sum([len(chunk) for chunk in chunks]))

    resp = 'y'
    if confirm:
        print('continue?(y/n)')
        resp = str(input())

    if resp == 'y':
        return chunks
    else:
        return []


def run_sweep_doodad(target, params, run_mode, mounts, test_one=False, docker_image='python:3', return_output=False, verbose=False):

    # build archive
    target_dir = os.path.dirname(target)
    target_mount_dir = os.path.join('target', os.path.basename(target_dir))

    target_mount = mount.MountLocal(local_dir=target_dir, mount_point=target_mount_dir)