# Path: picrust/make_cluster_jobs.py
# def make_sge_jobs(commands, job_prefix, queue, jobs_dir="jobs/",num_jobs=100,max_hours_per_job=24):
#     """prepare qsub text files.
# 
#     command: list of commands
# 
#     job_prefix: a short, descriptive name for the job.
# 
#     queue: name of the queue to submit to
# 
#     jobs_dir: path to directory where job submision scripts are written
# 
#     max_hours_per_job: the maximum expected time for each command (this will be multiplied by number of commands per job to get a 'walltime'
# 
#     ncpus: number of cpus
# 
#     nodes: number of nodes
# 
#     keep_output: keep standard error, standard out, both, or neither
#                  o=std out, e=std err, oe=both, n=neither
#     """
# 
#     filenames=[]
#     create_dir(jobs_dir)
# 
#     #calculate the number of commands to put in each job
#     num_commands_per_job=int(ceil(len(commands)/float(num_jobs)))
# 
#     #calculate the walltime (time before job will be killed by scheduler if still running)
#     total_time = max_hours_per_job*num_commands_per_job
#     walltime= "{0}:00:00".format(total_time)
# 
#     for command_group in grouper(commands,num_commands_per_job,''):
#         job_name = get_tmp_filename(tmp_dir=jobs_dir, prefix=job_prefix+"_",
#                                     suffix = ".txt")
#         out_fh = open(job_name,"w")
# 
#         stderr_fp = job_name+"_stderr"
#         stdout_fp = job_name+"_stdout"
#         out_fh.write(SGE_QSUB_TEXT % (walltime, stderr_fp, stdout_fp, "\n".join(command_group)))
#         out_fh.close()
#         filenames.append(job_name)
#     return filenames
# 
# def submit_cluster_jobs(filenames, verbose=False, delay=0):
#     """Submit jobs in filenames.
# 
#     filenames: list of prepared qsub job scripts, ready to be submitted
# 
#     verbose: a binary verbose flag
#     """
#     if(not app_path("qsub")):
#         raise ApplicationNotFoundError,"qsub not found. Can't submit jobs."
# 
#     for file in filenames:
#         command = 'qsub %s' % file
#         result = Popen(command, shell=True, universal_newlines=True,\
#                            stdout=PIPE, stderr=STDOUT).stdout.read()
#         if verbose:
#             print result
#         sleep(delay)


from optparse import OptionParser
from os.path import exists
from os import remove, rename, rmdir, makedirs
from cogent.util.misc import app_path
from cogent.app.util import get_tmp_filename
from picrust.make_cluster_jobs import make_sge_jobs, submit_cluster_jobs
from cogent.util.option_parsing import parse_command_line_parameters, make_option
script_info['version'] = __version__
script_info['disallow_positional_arguments'] = False

def main():
    option_parser, opts, args =\
       parse_command_line_parameters(**script_info)
       
    if opts.submit_jobs and not opts.make_jobs:
        option_parser.error('Must pass -m if passing -s. (Sorry about this, '+\
        'it\'s for backwards-compatibility.)') 

    min_args = 2
    if len(args) != min_args:
        option_parser.error('Program requires <commands file> and  <job prefix>')

    if (len(args[1])>10 or len(args[1])==0):
        option_parser.error('job prefix must be 1-10 characters long')
 
    commands = list(open(args[0]))
    job_prefix = args[1]

    if(not exists(opts.job_dir)):
        try:
            makedirs(opts.job_dir)
        except OSError:
            exit(" Jobs directory can not be created. "
                 +"Check for permissions or file with the same name: %s\n"
                 % opts.job_dir)

    if (opts.make_jobs):

# Path: picrust/make_cluster_jobs.py
# def make_sge_jobs(commands, job_prefix, queue, jobs_dir="jobs/",num_jobs=100,max_hours_per_job=24):
#     """prepare qsub text files.
# 
#     command: list of commands
# 
#     job_prefix: a short, descriptive name for the job.
# 
#     queue: name of the queue to submit to
# 
#     jobs_dir: path to directory where job submision scripts are written
# 
#     max_hours_per_job: the maximum expected time for each command (this will be multiplied by number of commands per job to get a 'walltime'
# 
#     ncpus: number of cpus
# 
#     nodes: number of nodes
# 
#     keep_output: keep standard error, standard out, both, or neither
#                  o=std out, e=std err, oe=both, n=neither
#     """
# 
#     filenames=[]
#     create_dir(jobs_dir)
# 
#     #calculate the number of commands to put in each job
#     num_commands_per_job=int(ceil(len(commands)/float(num_jobs)))
# 
#     #calculate the walltime (time before job will be killed by scheduler if still running)
#     total_time = max_hours_per_job*num_commands_per_job
#     walltime= "{0}:00:00".format(total_time)
# 
#     for command_group in grouper(commands,num_commands_per_job,''):
#         job_name = get_tmp_filename(tmp_dir=jobs_dir, prefix=job_prefix+"_",
#                                     suffix = ".txt")
#         out_fh = open(job_name,"w")
# 
#         stderr_fp = job_name+"_stderr"
#         stdout_fp = job_name+"_stdout"
#         out_fh.write(SGE_QSUB_TEXT % (walltime, stderr_fp, stdout_fp, "\n".join(command_group)))
#         out_fh.close()
#         filenames.append(job_name)
#     return filenames
# 
# def submit_cluster_jobs(filenames, verbose=False, delay=0):
#     """Submit jobs in filenames.
# 
#     filenames: list of prepared qsub job scripts, ready to be submitted
# 
#     verbose: a binary verbose flag
#     """
#     if(not app_path("qsub")):
#         raise ApplicationNotFoundError,"qsub not found. Can't submit jobs."
# 
#     for file in filenames:
#         command = 'qsub %s' % file
#         result = Popen(command, shell=True, universal_newlines=True,\
#                            stdout=PIPE, stderr=STDOUT).stdout.read()
#         if verbose:
#             print result
#         sleep(delay)


# Path: scripts/start_parallel_picrust_jobs_sge.py
from optparse import OptionParser
from os.path import exists
from os import remove, rename, rmdir, makedirs
from cogent.util.misc import app_path
from cogent.app.util import get_tmp_filename
from picrust.make_cluster_jobs import make_sge_jobs, submit_cluster_jobs
from cogent.util.option_parsing import parse_command_line_parameters, make_option

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

def main():
    option_parser, opts, args =\
       parse_command_line_parameters(**script_info)
       
    if opts.submit_jobs and not opts.make_jobs:
        option_parser.error('Must pass -m if passing -s. (Sorry about this, '+\
        'it\'s for backwards-compatibility.)') 

    min_args = 2
    if len(args) != min_args:
        option_parser.error('Program requires <commands file> and  <job prefix>')

    if (len(args[1])>10 or len(args[1])==0):
        option_parser.error('job prefix must be 1-10 characters long')
 
    commands = list(open(args[0]))
    job_prefix = args[1]

    if(not exists(opts.job_dir)):
        try:
            makedirs(opts.job_dir)
        except OSError:
            exit(" Jobs directory can not be created. "
                 +"Check for permissions or file with the same name: %s\n"
                 % opts.job_dir)

    if (opts.make_jobs):

        filenames = make_sge_jobs(commands, job_prefix, opts.queue, opts.job_dir,opts.num_jobs)