# Path: src/funfuzz/util/s3cache.py
# def isEC2VM():  # pylint: disable=invalid-name,missing-return-doc,missing-return-type-doc
#     def __init__(self, bucket_name):
#     def connect(self):  # pylint: disable=missing-return-doc,missing-return-type-doc
#     def downloadFile(self, origin, dest):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc
#     def compressAndUploadDirTarball(self, directory, tarball_path):  # pylint: disable=invalid-name,missing-param-doc
#     def uploadFileToS3(self, filename):  # pylint: disable=invalid-name,missing-param-doc,missing-type-doc
#     def uploadStrToS3(self, destDir, filename, contents):  # pylint: disable=invalid-name,missing-param-doc
# class S3Cache:  # pylint: disable=missing-docstring
#         EC2_PROFILE = None if isEC2VM() else "laniakea"  # pylint: disable=invalid-name
# 
# Path: src/funfuzz/util/sm_compile_helpers.py
# def ensure_cache_dir(base_dir):
# def autoconf_run(working_dir):
# def envDump(shell, log):  # pylint: disable=invalid-name,missing-param-doc,missing-type-doc
# def extract_vers(objdir):  # pylint: disable=inconsistent-return-statements
# def get_lock_dir_path(cache_dir_base, repo_dir, tbox_id=""):
# def verify_full_win_pageheap(shell_path):
# 
# Path: src/funfuzz/util/subprocesses.py
# def vdump(inp):  # pylint: disable=missing-param-doc,missing-type-doc


import copy
import io
import multiprocessing
import os
import platform
import shutil
import subprocess
import sys
import tarfile
import traceback
from optparse import OptionParser  # pylint: disable=deprecated-module
from pathlib import Path
from shlex import quote
from pkg_resources import parse_version
from . import build_options
from . import inspect_shell
from ..util import file_system_helpers
from ..util import hg_helpers
from ..util import s3cache
from ..util import sm_compile_helpers
from ..util import subprocesses as sps
from ..util.lock_dir import LockDir
    else:
        print(f"{MAKE_BINARY} did not result in a js shell:")
        with io.open(str(shell.get_shell_cache_dir() / f"{shell.get_shell_name_without_ext()}.busted"), "a",
                     encoding="utf-8", errors="replace") as f:
            f.write(f"Compilation of {shell.get_repo_name()} rev {shell.get_hg_hash()} "
                    f"failed with the following output:\n")
            f.write(out)
        raise OSError(f"{MAKE_BINARY} did not result in a js shell.")

    return shell.get_shell_compiled_path()


def makeTestRev(options):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc,missing-return-type-doc
    def testRev(rev):  # pylint: disable=invalid-name,missing-return-doc,missing-return-type-doc
        shell = CompiledShell(options.build_options, rev)
        print(f"Rev {rev}:", end=" ")

        try:
            obtainShell(shell, updateToRev=rev)
        except (subprocess.CalledProcessError, OSError):
            return options.compilationFailedLabel, "compilation failed"

        print("Testing...", end=" ")
        return options.testAndLabel(shell.get_shell_cache_js_bin_path(), rev)
    return testRev


def obtainShell(shell, updateToRev=None, updateLatestTxt=False):  # pylint: disable=invalid-name,missing-param-doc
    # pylint: disable=missing-raises-doc,missing-type-doc,too-many-branches,too-complex,too-many-statements
    """Obtain a js shell. Keep the objdir for now, especially .a files, for symbols."""

# Path: src/funfuzz/util/s3cache.py
# def isEC2VM():  # pylint: disable=invalid-name,missing-return-doc,missing-return-type-doc
#     def __init__(self, bucket_name):
#     def connect(self):  # pylint: disable=missing-return-doc,missing-return-type-doc
#     def downloadFile(self, origin, dest):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc
#     def compressAndUploadDirTarball(self, directory, tarball_path):  # pylint: disable=invalid-name,missing-param-doc
#     def uploadFileToS3(self, filename):  # pylint: disable=invalid-name,missing-param-doc,missing-type-doc
#     def uploadStrToS3(self, destDir, filename, contents):  # pylint: disable=invalid-name,missing-param-doc
# class S3Cache:  # pylint: disable=missing-docstring
#         EC2_PROFILE = None if isEC2VM() else "laniakea"  # pylint: disable=invalid-name
# 
# Path: src/funfuzz/util/sm_compile_helpers.py
# def ensure_cache_dir(base_dir):
# def autoconf_run(working_dir):
# def envDump(shell, log):  # pylint: disable=invalid-name,missing-param-doc,missing-type-doc
# def extract_vers(objdir):  # pylint: disable=inconsistent-return-statements
# def get_lock_dir_path(cache_dir_base, repo_dir, tbox_id=""):
# def verify_full_win_pageheap(shell_path):
# 
# Path: src/funfuzz/util/subprocesses.py
# def vdump(inp):  # pylint: disable=missing-param-doc,missing-type-doc


# Path: src/funfuzz/js/compile_shell.py
import copy
import io
import multiprocessing
import os
import platform
import shutil
import subprocess
import sys
import tarfile
import traceback
from optparse import OptionParser  # pylint: disable=deprecated-module
from pathlib import Path
from shlex import quote
from pkg_resources import parse_version
from . import build_options
from . import inspect_shell
from ..util import file_system_helpers
from ..util import hg_helpers
from ..util import s3cache
from ..util import sm_compile_helpers
from ..util import subprocesses as sps
from ..util.lock_dir import LockDir

    else:
        print(f"{MAKE_BINARY} did not result in a js shell:")
        with io.open(str(shell.get_shell_cache_dir() / f"{shell.get_shell_name_without_ext()}.busted"), "a",
                     encoding="utf-8", errors="replace") as f:
            f.write(f"Compilation of {shell.get_repo_name()} rev {shell.get_hg_hash()} "
                    f"failed with the following output:\n")
            f.write(out)
        raise OSError(f"{MAKE_BINARY} did not result in a js shell.")

    return shell.get_shell_compiled_path()


def makeTestRev(options):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc,missing-return-type-doc
    def testRev(rev):  # pylint: disable=invalid-name,missing-return-doc,missing-return-type-doc
        shell = CompiledShell(options.build_options, rev)
        print(f"Rev {rev}:", end=" ")

        try:
            obtainShell(shell, updateToRev=rev)
        except (subprocess.CalledProcessError, OSError):
            return options.compilationFailedLabel, "compilation failed"

        print("Testing...", end=" ")
        return options.testAndLabel(shell.get_shell_cache_js_bin_path(), rev)
    return testRev


def obtainShell(shell, updateToRev=None, updateLatestTxt=False):  # pylint: disable=invalid-name,missing-param-doc
    # pylint: disable=missing-raises-doc,missing-type-doc,too-many-branches,too-complex,too-many-statements
    """Obtain a js shell. Keep the objdir for now, especially .a files, for symbols."""

    assert sm_compile_helpers.get_lock_dir_path(Path.home(), shell.build_opts.repo_dir).is_dir()