# Path: picrust/parallel.py
# def grouper(iterable, n, fillvalue=None):
#         args = [iter(iterable)] * n
#         return izip_longest(*args, fillvalue=fillvalue)


from cogent.util.option_parsing import parse_command_line_parameters, make_option
from subprocess import Popen
from os import makedirs, chmod, getenv, remove
from os.path import exists
from shutil import rmtree
from stat import S_IRWXU
from picrust.parallel import grouper
from math import ceil
script_info['version'] = __version__
script_info['disallow_positional_arguments'] = False

def write_job_files(output_dir,commands,run_id,num_jobs=4):
    jobs_dir = '%s/jobs/' % output_dir
    job_fps = []
    if not exists(jobs_dir):
        try:
            makedirs(jobs_dir)
        except OSError,e:
            raise OSError, "Error creating jobs directory in working dir: %s" % output_dir +\
            " (specified in qiime_config). Do you have write access?. "+\
            "Original error message follows:\n%s" % str(e)
        paths_to_remove = [jobs_dir]
    else:
        # point paths_to_remove at job_fps
        paths_to_remove = job_fps
    
    # This is messy right now as our clusters (bmf, bmf2) require us to
    # start and exit a shell for some reason which we haven't figured out. 
    # Running these commands as parallel shell scripts gets screwed up by 
    # this. For the time-being, I'm stripping this out here. Once the new 
    # clusters are up, I'm going to move the wrapping of commands in 
    # bash/exit to the cluster_jobs script. At that point this function
    # will be greatly simplified.
    ignored_subcommands = {}.fromkeys(['/bin/bash','exit'])

    #calculate the number of commands to put in each job
    num_commands_per_job=int(ceil(len(commands)/float(num_jobs)))


# Path: picrust/parallel.py
# def grouper(iterable, n, fillvalue=None):
#         args = [iter(iterable)] * n
#         return izip_longest(*args, fillvalue=fillvalue)


# Path: scripts/start_parallel_picrust_jobs.py
from cogent.util.option_parsing import parse_command_line_parameters, make_option
from subprocess import Popen
from os import makedirs, chmod, getenv, remove
from os.path import exists
from shutil import rmtree
from stat import S_IRWXU
from picrust.parallel import grouper
from math import ceil

script_info['version'] = __version__
script_info['disallow_positional_arguments'] = False

def write_job_files(output_dir,commands,run_id,num_jobs=4):
    jobs_dir = '%s/jobs/' % output_dir
    job_fps = []
    if not exists(jobs_dir):
        try:
            makedirs(jobs_dir)
        except OSError,e:
            raise OSError, "Error creating jobs directory in working dir: %s" % output_dir +\
            " (specified in qiime_config). Do you have write access?. "+\
            "Original error message follows:\n%s" % str(e)
        paths_to_remove = [jobs_dir]
    else:
        # point paths_to_remove at job_fps
        paths_to_remove = job_fps
    
    # This is messy right now as our clusters (bmf, bmf2) require us to
    # start and exit a shell for some reason which we haven't figured out. 
    # Running these commands as parallel shell scripts gets screwed up by 
    # this. For the time-being, I'm stripping this out here. Once the new 
    # clusters are up, I'm going to move the wrapping of commands in 
    # bash/exit to the cluster_jobs script. At that point this function
    # will be greatly simplified.
    ignored_subcommands = {}.fromkeys(['/bin/bash','exit'])

    #calculate the number of commands to put in each job
    num_commands_per_job=int(ceil(len(commands)/float(num_jobs)))


    for i,command_group in enumerate(grouper(commands,num_commands_per_job,'')):