# 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
    results = []
    njobs = 0
    with archive_builder.temp_archive_file() as archive_file:
        archive = archive_builder.build_archive(archive_filename=archive_file,
                                                payload_script=command,
                                                verbose=verbose,
                                                docker_image=docker_image,
                                                use_gpu_image=run_mode.use_gpu,
                                                mounts=mounts)

        sweeper = Sweeper(params)
        for config in sweeper:
            njobs += 1
            cli_args= ' '.join(['--%s %s' % (key, config[key]) for key in config])
            cmd = archive + ' -- ' + cli_args
            result = run_mode.run_script(cmd, return_output=return_output, verbose=False)
            if return_output:
                result = archive_builder._strip_stdout(result)
                results.append(result)
            if test_one:
                break
    print('Launching completed for %d jobs' % njobs)
    run_mode.print_launch_message()
    return tuple(results)


def run_sweep_doodad_chunked(target, params, run_mode, mounts, num_chunks=10, docker_image='python:3', return_output=False, test_one=False, confirm=True, 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

    results = []
    njobs = 0
    with archive_builder.temp_archive_file() as archive_file:
        archive = archive_builder.build_archive(archive_filename=archive_file,
                                                payload_script=command,
                                                verbose=verbose,
                                                docker_image=docker_image,
                                                use_gpu_image=run_mode.use_gpu,
                                                mounts=mounts)

        sweeper = Sweeper(params)
        for config in sweeper:
            njobs += 1
            cli_args= ' '.join(['--%s %s' % (key, config[key]) for key in config])
            cmd = archive + ' -- ' + cli_args
            result = run_mode.run_script(cmd, return_output=return_output, verbose=False)
            if return_output:
                result = archive_builder._strip_stdout(result)
                results.append(result)
            if test_one:
                break
    print('Launching completed for %d jobs' % njobs)
    run_mode.print_launch_message()
    return tuple(results)


def run_sweep_doodad_chunked(target, params, run_mode, mounts, num_chunks=10, docker_image='python:3', return_output=False, test_one=False, confirm=True, 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)