def generateOutput(output_task_id): task = OutputTask.objects.get(id=output_task_id) search_data = cPickle.loads(task.search_form.encode('utf-8')) experiments = find(search_data) return_data = search_data['out']['data'] if return_data == 'I': pass elif return_data == 'P': pass elif return_data == 'S': pass elif return_data == 'A': pass else: task.is_finished = True task.save() return # TODO -- write the results of the search # create the temporary directory to store the script results tmp_dir = tempfile.mkdtemp() out_dir = os.path.join(os.path.dirname(__file__), 'site_media', 'tasks') # TODO -- run the script script_name = 'test.sh' script = os.path.join(settings.TASK_SCRIPT_DIR, script_name) subprocess.call(script, shell=True, cwd=tmp_dir) # collect all files and zip them result_files = os.listdir(tmp_dir) tmp_tar = tempfile.NamedTemporaryFile(delete=False) t = tarfile.open(fileobj=tmp_tar, mode='w:bz2') for f in result_files: filename = os.path.join(tmp_dir, f) t.add(filename, arcname=f) t.close() tmp_tar.close() # move the results to a django-accessible directory shutil.move(tmp_tar.name, task.output_abs_path()) # delete all files (and temp_dir) shutil.rmtree(tmp_dir) # mark the task as finished task.is_finished = True task.save()
def index(request): log_str = "" forms = { 'params': (guyton.queryforms.ParamCondForm, True), 'vars': (guyton.queryforms.VarCondForm, True), 'tags': (guyton.queryforms.TagCondForm, True), 'exp': (guyton.queryforms.ExpCondForm, False), 'out': (guyton.queryforms.OutputChoiceForm, False), } action = request.POST.get('form-submit') (forms, data, valid, modified) = validate_forms(request, action, forms) if request.method == 'POST' and valid and not modified: if action == 'Count Matches': matches = find(data) total = Experiment.objects.count() matched = str(matches.count()) return render_to_response('search.html', { 'matched': matched, 'total': total, 'param_formset': forms['params'], 'var_formset': forms['vars'], 'tag_formset': forms['tags'], 'exp_form': forms['exp'], 'out_form': forms['out'], 'log_str': log_str, }, context_instance=RequestContext(request)) elif action == 'Submit Query': matches = find(data) total = matches.count() fmt = lambda n, v, t: "%s %s %s" % (t, n, v) count = 1 # Save the search criteria as the first file out_files.append(describe(data, comment_str="")) for experiment in matches: # Initial parameter values p_vals = experiment.paramvalue_set.filter(at_time__exact=0) # Changes to parameter values p_dels = experiment.paramvalue_set.filter(at_time__gt=0) exp_lines = [] # Save the initial parameter values exp_lines.extend([fmt(p.parameter.name, p.value, p.at_time) for p in p_vals]) # Save the parameter perturbations exp_lines.extend([fmt(p.parameter.name, p.value, p.at_time) for p in p_dels]) # Append this experiment to the list of files out_files.append(exp_lines) count += 1 # Define the name of each file; the first one contains the search # critera, the others contain the parameter values for each # matching experiment. def fname(num): if num == 1: return 'query.txt' else: return 'experiment%d.txt' % (num - 1,) resp = compress_files(out_files, fname, 'results') return resp # task = OutputTask.create_new(data) # generateOutput(task.id) # return HttpResponseRedirect("tasks/" + task.sha256_id) else: err_msg = 'Invalid request: ' + action raise Http404(err_msg) return render_to_response('search.html', { 'param_formset': forms['params'], 'var_formset': forms['vars'], 'tag_formset': forms['tags'], 'exp_form': forms['exp'], 'out_form': forms['out'], 'log_str': log_str, }, context_instance=RequestContext(request))