# Path: sio/workers/ft.py
# def get_url_hash(filetracker_url):
# def get_cache_dir(filetracker_url):
# def init_instance(filetracker_url):
# def instance():
# def set_instance(client):
# def _use_filetracker(name, environ):
# def download(environ, key, dest=None, skip_if_exists=False, **kwargs):
# def upload(environ, key, source, dest=None, **kwargs):
# def _do_launch():
# def launch_filetracker_server():
# 
# Path: sio/workers/util.py
# def decode_fields(fields):
#     def _decode_decorator(func):
#         def _wrapper(*args, **kwargs):
#             result_dict = func(*args, **kwargs)
#             for field in fields:
#                 if not isinstance(result_dict[field], six.string_types):
#                     result_dict[field] = result_dict[field].decode()
#             return result_dict
# 
#         return _wrapper
# 
#     return _decode_decorator
# 
# def replace_invalid_UTF(a_string):
#     """Replaces invalid characters in a string.
# 
#     In python 2 strings are also bytestrings.
#     In python 3 it returns a string.
#     """
#     if six.PY2:
#         return a_string.decode('utf-8', 'replace').encode('utf-8')
#     else:
#         if not isinstance(a_string, six.string_types):
#             return a_string.decode('utf-8', 'replace')
#         else:
#             return a_string.encode('utf-8', 'replace').decode()
# 
# def tempcwd(path=None):
#     # Someone might call tempcwd twice, i.e. tempcwd(tempcwd('something'))
#     # Do nothing in this case.
#     if path is not None and os.path.isabs(path):
#         return path
#     d = threadlocal_dir.tmpdir
#     if path:
#         return os.path.join(d, path)
#     else:
#         return d
# 
# Path: sio/workers/file_runners.py
# def get_file_runner(executor, environ):
#     """Finds appropriate wrapper to run ``environ['exe_file']`` in
#     given ``executor``.
#     """
#     environ.setdefault('exec_info', {'mode': 'executable'})
#     return LanguageModeWrapper.execution_mode_wrapper(executor, environ)
# 
# Path: sio/executors/checker.py
# DEFAULT_CHECKER_TIME_LIMIT = 30000  # in ms
# DEFAULT_CHECKER_MEM_LIMIT = 256 * 2 ** 10  # in KiB
# RESULT_STRING_LENGTH_LIMIT = 1024  # in bytes
# class CheckerError(Exception):
# def _run_in_executor(env, command, executor, **kwargs):
# def _run_diff(env):
# def _run_checker(env, use_sandboxes=False):
#     def execute_checker(with_stderr=False, stderr=None):
# def _run_compare(env):
# def _limit_length(s):
# def run(environ, use_sandboxes=True):


import os
import six
from shutil import rmtree
from zipfile import ZipFile, is_zipfile
from sio.workers import ft
from sio.workers.util import decode_fields, replace_invalid_UTF, tempcwd
from sio.workers.file_runners import get_file_runner
from sio.executors import checker
    if environ.get('exec_info', {}).get('mode') == 'output-only':
        renv = _fake_run_as_exe_is_output_file(environ)
    else:
        renv = _run(environ, executor, use_sandboxes)

    _populate_environ(renv, environ)

    if environ['result_code'] == 'OK' and environ.get('check_output'):
        environ = checker.run(environ, use_sandboxes=use_sandboxes)

    for key in ('result_code', 'result_string'):
        environ[key] = replace_invalid_UTF(environ[key])

    if 'out_file' in environ:
        ft.upload(
            environ,
            'out_file',
            tempcwd('out'),
            to_remote_store=environ.get('upload_out', False),
        )

    return environ


def _run(environ, executor, use_sandboxes):
    input_name = tempcwd('in')

    file_executor = get_file_runner(executor, environ)
    exe_filename = file_executor.preferred_filename()


# Path: sio/workers/ft.py
# def get_url_hash(filetracker_url):
# def get_cache_dir(filetracker_url):
# def init_instance(filetracker_url):
# def instance():
# def set_instance(client):
# def _use_filetracker(name, environ):
# def download(environ, key, dest=None, skip_if_exists=False, **kwargs):
# def upload(environ, key, source, dest=None, **kwargs):
# def _do_launch():
# def launch_filetracker_server():
# 
# Path: sio/workers/util.py
# def decode_fields(fields):
#     def _decode_decorator(func):
#         def _wrapper(*args, **kwargs):
#             result_dict = func(*args, **kwargs)
#             for field in fields:
#                 if not isinstance(result_dict[field], six.string_types):
#                     result_dict[field] = result_dict[field].decode()
#             return result_dict
# 
#         return _wrapper
# 
#     return _decode_decorator
# 
# def replace_invalid_UTF(a_string):
#     """Replaces invalid characters in a string.
# 
#     In python 2 strings are also bytestrings.
#     In python 3 it returns a string.
#     """
#     if six.PY2:
#         return a_string.decode('utf-8', 'replace').encode('utf-8')
#     else:
#         if not isinstance(a_string, six.string_types):
#             return a_string.decode('utf-8', 'replace')
#         else:
#             return a_string.encode('utf-8', 'replace').decode()
# 
# def tempcwd(path=None):
#     # Someone might call tempcwd twice, i.e. tempcwd(tempcwd('something'))
#     # Do nothing in this case.
#     if path is not None and os.path.isabs(path):
#         return path
#     d = threadlocal_dir.tmpdir
#     if path:
#         return os.path.join(d, path)
#     else:
#         return d
# 
# Path: sio/workers/file_runners.py
# def get_file_runner(executor, environ):
#     """Finds appropriate wrapper to run ``environ['exe_file']`` in
#     given ``executor``.
#     """
#     environ.setdefault('exec_info', {'mode': 'executable'})
#     return LanguageModeWrapper.execution_mode_wrapper(executor, environ)
# 
# Path: sio/executors/checker.py
# DEFAULT_CHECKER_TIME_LIMIT = 30000  # in ms
# DEFAULT_CHECKER_MEM_LIMIT = 256 * 2 ** 10  # in KiB
# RESULT_STRING_LENGTH_LIMIT = 1024  # in bytes
# class CheckerError(Exception):
# def _run_in_executor(env, command, executor, **kwargs):
# def _run_diff(env):
# def _run_checker(env, use_sandboxes=False):
#     def execute_checker(with_stderr=False, stderr=None):
# def _run_compare(env):
# def _limit_length(s):
# def run(environ, use_sandboxes=True):


# Path: sio/executors/common.py
import os
import six
from shutil import rmtree
from zipfile import ZipFile, is_zipfile
from sio.workers import ft
from sio.workers.util import decode_fields, replace_invalid_UTF, tempcwd
from sio.workers.file_runners import get_file_runner
from sio.executors import checker

    if environ.get('exec_info', {}).get('mode') == 'output-only':
        renv = _fake_run_as_exe_is_output_file(environ)
    else:
        renv = _run(environ, executor, use_sandboxes)

    _populate_environ(renv, environ)

    if environ['result_code'] == 'OK' and environ.get('check_output'):
        environ = checker.run(environ, use_sandboxes=use_sandboxes)

    for key in ('result_code', 'result_string'):
        environ[key] = replace_invalid_UTF(environ[key])

    if 'out_file' in environ:
        ft.upload(
            environ,
            'out_file',
            tempcwd('out'),
            to_remote_store=environ.get('upload_out', False),
        )

    return environ


def _run(environ, executor, use_sandboxes):
    input_name = tempcwd('in')

    file_executor = get_file_runner(executor, environ)
    exe_filename = file_executor.preferred_filename()


    ft.download(environ, 'exe_file', exe_filename, add_to_cache=True)