# Path: msibi/utils/general.py
# def backup_file(filename):
#     """Backup a file based on the number of backups in the file's directory.
# 
#     Parameters
#     ----------
#     filename : str
#         Full path to file to make backup of.
# 
#     """
#     n_backups = _count_backups(filename)
#     new_backup = _backup_name(filename, n_backups)
#     shutil.copy(filename, new_backup)
# 
# Path: msibi/utils/exceptions.py
# class UnsupportedEngine(Exception):
#     def __init__(self, engine):
#         message = 'Unsupported engine: "{0}". Supported engines are: {1}'.format(
#             engine, ', '.join(SUPPORTED_ENGINES))
#         super(UnsupportedEngine, self).__init__(message)


from distutils.spawn import find_executable
from math import ceil
from multiprocessing import cpu_count
from multiprocessing.dummy import Pool
from subprocess import Popen
from msibi.utils.general import backup_file
from msibi.utils.exceptions import UnsupportedEngine
import itertools
import logging
import os
    """Worker for managing a single HOOMD-blue simulation. """

    state, idx, gpus = args
    log_file = os.path.join(state.state_dir, 'log.txt')
    err_file = os.path.join(state.state_dir, 'err.txt')

    if state.HOOMD_VERSION == 1:
        executable = 'hoomd'
    elif state.HOOMD_VERSION == 2:
        executable = 'python'
    with open(log_file, 'w') as log, open(err_file, 'w') as err:
        if gpus:
            card = gpus[idx % len(gpus)]
            cmds = [executable, 'run.py', '--gpu={card}'.format(**locals())]
        else:
            logging.info('    Running state {state.name} on CPU'.format(**locals()))
            cmds = [executable, 'run.py']

        proc = Popen(cmds, cwd=state.state_dir, stdout=log, stderr=err,
                     universal_newlines=True)
        logging.info("    Launched HOOMD in {state.state_dir}".format(**locals()))
        proc.communicate()
        logging.info("    Finished in {state.state_dir}.".format(**locals()))
    _post_query(state)

def _post_query(state):
    """Reload the query trajectory and make backups. """

    state.reload_query_trajectory()
    backup_file(os.path.join(state.state_dir, 'log.txt'))

# Path: msibi/utils/general.py
# def backup_file(filename):
#     """Backup a file based on the number of backups in the file's directory.
# 
#     Parameters
#     ----------
#     filename : str
#         Full path to file to make backup of.
# 
#     """
#     n_backups = _count_backups(filename)
#     new_backup = _backup_name(filename, n_backups)
#     shutil.copy(filename, new_backup)
# 
# Path: msibi/utils/exceptions.py
# class UnsupportedEngine(Exception):
#     def __init__(self, engine):
#         message = 'Unsupported engine: "{0}". Supported engines are: {1}'.format(
#             engine, ', '.join(SUPPORTED_ENGINES))
#         super(UnsupportedEngine, self).__init__(message)


# Path: msibi/workers.py
from distutils.spawn import find_executable
from math import ceil
from multiprocessing import cpu_count
from multiprocessing.dummy import Pool
from subprocess import Popen
from msibi.utils.general import backup_file
from msibi.utils.exceptions import UnsupportedEngine
import itertools
import logging
import os

    """Worker for managing a single HOOMD-blue simulation. """

    state, idx, gpus = args
    log_file = os.path.join(state.state_dir, 'log.txt')
    err_file = os.path.join(state.state_dir, 'err.txt')

    if state.HOOMD_VERSION == 1:
        executable = 'hoomd'
    elif state.HOOMD_VERSION == 2:
        executable = 'python'
    with open(log_file, 'w') as log, open(err_file, 'w') as err:
        if gpus:
            card = gpus[idx % len(gpus)]
            cmds = [executable, 'run.py', '--gpu={card}'.format(**locals())]
        else:
            logging.info('    Running state {state.name} on CPU'.format(**locals()))
            cmds = [executable, 'run.py']

        proc = Popen(cmds, cwd=state.state_dir, stdout=log, stderr=err,
                     universal_newlines=True)
        logging.info("    Launched HOOMD in {state.state_dir}".format(**locals()))
        proc.communicate()
        logging.info("    Finished in {state.state_dir}.".format(**locals()))
    _post_query(state)

def _post_query(state):
    """Reload the query trajectory and make backups. """

    state.reload_query_trajectory()
    backup_file(os.path.join(state.state_dir, 'log.txt'))

    backup_file(os.path.join(state.state_dir, 'err.txt'))