示例#1
0
def convert(media_root, uploadedFiles, convert_file_save_loc, input_format, output_format, to_email):
  # Send begin job email
  content = "Hello,\n\n You requested the following files be converted:"
  for fn in uploadedFiles:
    content += "\n- " + os.path.basename(fn)
  content += "\n\nTo the following formats:"
  for fmt in output_format:
    content += "\n- " + fmt

  sendEmail(to_email, "Job launch Notification", content+"\n\n")
  # End Email junk

  err_msg = ""
  outfn = ""
  for fn in uploadedFiles:
    outfn, err_msg = convertTo.convert_graph(fn, input_format, convert_file_save_loc, *output_format)

  dwnld_loc = get_download_path(convert_file_save_loc)
  print "Download path: {0}".format(dwnld_loc)

  if (err_msg):
    err_msg = "Your job completed with errors. The result can be found at {}.\n\n"\
        "Message: %s\n\n" % err_msg
    sendJobFailureEmail(to_email, err_msg, dwnld_loc)
  else:
    sendJobCompleteEmail(to_email,dwnld_loc) 
示例#2
0
def convert(request, webargs=None):
  ''' Form '''

  if (request.method == 'POST' and not webargs):
    form = ConvertForm(request.POST, request.FILES) # instantiating form
    if form.is_valid():

      baseDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime('formUpload%a%d%b%Y_%H.%M.%S/', localtime()))
      saveDir = os.path.join(baseDir,'upload') # Save location of original uploads
      convertFileSaveLoc = os.path.join(baseDir,'converted') # Save location of converted data

      if not (os.path.exists(convertFileSaveLoc)):
        os.makedirs(convertFileSaveLoc)

      savedFile = os.path.join(saveDir, request.FILES['fileObj'].name)

      saveFileToDisk(request.FILES['fileObj'], savedFile)

      # If zip is uploaded
      if os.path.splitext(request.FILES['fileObj'].name)[1].strip() == '.zip':
        zipper.unzip(savedFile, saveDir)
        # Delete zip so its not included in the graphs we uploaded
        os.remove(savedFile)
        uploadedFiles = glob(os.path.join(saveDir, "*")) # get the uploaded file names

      else:
        uploadedFiles = [savedFile]

      err_msg=""
      for fn in uploadedFiles:
        err_msg = convert_graph(fn, form.cleaned_data['input_format'],
                        convertFileSaveLoc, *form.cleaned_data['output_format'])

      dwnldLoc = request.META['wsgi.url_scheme'] + '://' + \
                    request.META['HTTP_HOST'] + convertFileSaveLoc.replace(' ','%20')

      if (err_msg):
        err_msg = "Your job completed with errors. The result can be found at %s\n. Here are the errors:%s" % (dwnldLoc, err_msg)
        return render_to_response(
        'convertupload.html',
        {'convertForm': form, 'err_msg': err_msg+"\n"},
        context_instance=RequestContext(request))
      #else
      return HttpResponseRedirect(dwnldLoc)

  elif(request.method == 'POST' and webargs):
    # webargs is {inFormat}/{outFormat}
    inFormat = webargs.split('/')[0] # E.g 'graphml'| 'dot' | 'leda'
    outFormat =  webargs.split('/')[1].split(',')

    outFormat = list(set(outFormat)) # Eliminate duplicates if any exist

    baseDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime('progUpload%a%d%b%Y_%H.%M.%S/', localtime()))
    saveDir = os.path.join(baseDir,'upload') # Save location of original uploads
    convertFileSaveLoc = os.path.join(baseDir,'converted') # Save location of converted data

    if not os.path.exists(saveDir): os.makedirs(saveDir)
    if not os.path.exists(convertFileSaveLoc): os.makedirs(convertFileSaveLoc)

    uploadedFiles = writeBodyToDisk(request.body, saveDir)# can only be one file # TODO: Check me

    # Check for zip
    if os.path.splitext(uploadedFiles[0])[1].strip() == '.zip':
        zipper.unzip(uploadedFiles[0], saveDir)
        # Delete zip so its not included in the graphs we uploaded
        os.remove(uploadedFiles[0])
        uploadedFiles = glob(os.path.join(saveDir, "*")) # get the uploaded file names

    err_msg = ""
    for fn in uploadedFiles:
      err_msg = convert_graph(fn, inFormat,
                        convertFileSaveLoc, *outFormat)

    dwnldLoc = request.META['wsgi.url_scheme'] + '://' + \
                    request.META['HTTP_HOST'] + convertFileSaveLoc.replace(' ','%20')

    if err_msg:
      err_msg = "Completed with errors. View Data at: %s\n. Here are the errors:%s" % (dwnldLoc, err_msg)
      return HttpResponse(err_msg)

    return HttpResponse ( "Converted files available for download at " + dwnldLoc + " . The directory " +
            "may be empty if you try to convert from, and to the same format.") # change to render of a page with a link to data result

  else:
    form = ConvertForm() # An empty, unbound form

  # Render the form
  return render_to_response(
      'convertupload.html',
      {'convertForm': form},
      context_instance=RequestContext(request))
示例#3
0
def graphLoadInv(request, webargs=None):
  ''' Form '''
  if request.method == 'POST' and not webargs:
    form = GraphUploadForm(request.POST, request.FILES) # instantiating form
    if form.is_valid():

      request.session['graphsize'] = 'small' # This accounts for use LCC or not
      request.session['email'] =  form.cleaned_data['email']

      data = form.files['fileObj'] # get data
      request.session['invariants'] = form.cleaned_data['Select_Invariants_you_want_computed']

      dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime()))
      request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants')

      makeDirIfNone([dataDir])

      # We got a zip
      if os.path.splitext(data.name)[1] == '.zip':
        writeBodyToDisk(data.read(), dataDir)
        graphs = glob(os.path.join(dataDir,'*')) # TODO: better way to make sure we are actually collecting graphs here

      else: # View only accepts .mat & zip as regulated by template
        graphs = [os.path.join(dataDir, data.name)]
        saveFileToDisk(data, graphs[0])

      request.session['uploaded_graphs'] = graphs
      request.session['graph_format'] = form.cleaned_data['graph_format']
      request.session['dataDir'] = dataDir
      request.session['email'] = form.cleaned_data['email']

      # Launch thread for graphs & email user
      sendJobBeginEmail(request.session['email'], request.session['invariants'], genGraph=False)

      #asyncInvCompute(request)
      thr = threading.Thread(target=asyncInvCompute, args=(request,))
      thr.start()

      request.session['success_msg'] = \
"""
Your job was successfully launched. You should receive an email when your  job begins and another one when it completes.
The process may take several hours per graph (dependent on graph size) if you selected to compute all invariants.
If you do not see an email in your INBOX check the SPAM folder and add [email protected] to your safe list.
"""
      return HttpResponseRedirect(get_script_prefix()+'success')

  # Programmatic RESTful API
  elif request.method == 'POST' and webargs:
    dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime()))
    makeDirIfNone([dataDir])

    uploadedZip = writeBodyToDisk(request.body, dataDir)[0] # Not necessarily a zip

    try: # Assume its a zip first
      zipper.unzip(uploadedZip, dataDir) # Unzip the zip
      os.remove(uploadedZip) # Delete the zip
    except:
      print "Non-zip file uploaded ..."
    graphs = glob(os.path.join(dataDir,'*'))

    try:
      request.session['invariants'] = webargs.split('/')[0].split(',')
      inGraphFormat = webargs.split('/')[1]
    except:
      return HttpResponse("Malformated input invariants list or graph format")

    request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants')

    for graph_fn in graphs:
      invariant_fns = runInvariants(request.session['invariants'], graph_fn,
                        request.session['graphInvariants'], inGraphFormat)
      print 'Computing Invariants for annoymous project %s complete...' % graph_fn

      err_msg=""
      if len(webargs.split('/')) > 2:
        err_msg = "" # __init__
        err_msg = convert_graph(invariant_fns["out_graph_fn"], "graphml",
                request.session['graphInvariants'], *webargs.split('/')[2].split(','))

      dwnldLoc = request.META['wsgi.url_scheme'] + '://' + \
                    request.META['HTTP_HOST'] + dataDir.replace(' ','%20')
      if err_msg:
        err_msg = "Completed with errors. View Data at: %s\n. Here are the errors:%s" % (dwnldLoc, err_msg)
        return HttpResponse(err_msg)

    return HttpResponse("View Data at: " + dwnldLoc)
  # Browser
  else:
    form = GraphUploadForm() # An empty, unbound form

  # Render the form
  return render_to_response(
      'graphupload.html',
      {'graphUploadForm': form},
      context_instance=RequestContext(request) # Some failure to input data & returns a key signaling what is requested
  )