from celery import task
from time import sleep
import subprocess
import os.path
from .models import Job, Result
from .parser import parse_sea_output

SEA_PATH = os.path.expanduser("~/SEA")

@task()
def run_simple_calculation(job_id):
    job = Job.objects.get(pk=job_id)
    with open(job.structure.name, 'r') as f:
        lines = f.readlines()
        num_lines = len(lines)
    sleep(30.) # seconds
    status = 'Done'
    job.status = status
    job.save()
    return num_lines

@task()
def run_sea_calculation(job_id):
    job = Job.objects.get(pk=job_id)
    solvate_cmd = os.path.join(SEA_PATH, "bin", "solvate")
    input_flag = "-s"
    input_path = os.path.join(SEA_PATH, "examples", "methane")
    arg_list = [solvate_cmd, input_flag, input_path]
    try:
        output_str = subprocess.check_output(arg_list, stderr=subprocess.STDOUT)
        output = parse_sea_output(output_str)
        create_result_obj(job, output)
        status = 'Done'
    except subprocess.CalledProcessError, e:
        output_str = "There was an error:\n%s" % e.output
        output_str += "return code: %s\n" % e.returncode
        status = 'Error'

    sleep(10.) # seconds
    job.status = status
    job.save()
    return "%s\n%s" % (status, output_str)

def create_result_obj(job, output):
    r = Result.objects.create(job=job, **output)
    return

@@ -1,12 +1,45 @@
+import subprocess%0Aimport os.path%0A
 from celery 
@@ -77,40 +77,88 @@
 eep%0A
-import subprocess%0Aimport os.path
+from tempfile import mkdtemp%0Afrom shutil import copyfile, rmtree%0Aimport logging%0A
 %0Afro
@@ -631,24 +631,279 @@
 (pk=job_id)%0A
+    temp_dir = mkdtemp()%0A    temp_path = os.path.join(temp_dir, 'struct')%0A    temp_struct_path = temp_path + '.gro'%0A    temp_topo_path = temp_path + '.top'%0A    copyfile(job.structure.path, temp_struct_path)%0A    copyfile(job.topology.path, temp_topo_path)%0A
     solvate_
@@ -970,24 +970,26 @@
 g = %22-s%22%0A   
+ #
  input_path 
@@ -1032,24 +1032,91 @@
  %22methane%22)%0A
+    input_path = temp_path%0A    # logging.info( %22%25s%22 %25 input_path )%0A
     arg_list
@@ -1597,32 +1597,53 @@
 %0A    job.save()%0A
+    rmtree(temp_dir)%0A
     return %22%25s%5Cn

Use user-submitted files as SEA input.