tasks.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import os
  2. import requests
  3. import shutil
  4. import subprocess
  5. import shlex
  6. from celery import Celery
  7. from nova import celery, utils, db, models
  8. URL = 'http://127.0.0.1:5000/api/datasets'
  9. def create_dataset(token, parent_id, name):
  10. # TODO: refactor out with code from nova client
  11. data = dict(name=name, parent=parent_id)
  12. return requests.post(URL, params=dict(token=token), data=data).json()
  13. def get_dataset_info(token, dataset_id):
  14. return requests.get('{}/{}'.format(URL, dataset_id), params=dict(token=token)).json()
  15. @celery.task
  16. def copy(token, name, parent_id):
  17. # fetch path info about parent and new dataset
  18. src = get_dataset_info(token, parent_id)
  19. # TODO: check if parent is not closed yet and error
  20. result = create_dataset(token, parent_id, name)
  21. # check path info of new dataset
  22. dst = get_dataset_info(token, result['id'])
  23. # NOTE: we are doing a fast path here and I am not sure if this is really
  24. # the way to go ...
  25. utils.copy(src['path'], dst['path'])
  26. @celery.task
  27. def rmtree(path):
  28. shutil.rmtree(path)
  29. @celery.task
  30. def reconstruct(token, result_id, parent_id, flats, darks, projections, outname):
  31. src = get_dataset_info(token, parent_id)
  32. dst = get_dataset_info(token, result_id)
  33. cmd = ('tofu tomo'
  34. ' --projections "{input}/{projections}/" '
  35. ' --darks "{input}/{darks}/"'
  36. ' --flats "{input}/{flats}/"'
  37. ' --output "{output}/{outname}"')
  38. cmd = cmd.format(input=src['path'], output=dst['path'], projections=projections,
  39. darks=darks, flats=flats, outname=outname)
  40. args = shlex.split(cmd)
  41. proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  42. stdout, stderr = proc.communicate()
  43. print stdout, stderr
  44. proc.wait()
  45. cmd = ('ufo-launch'
  46. ' read path="{output}/" !'
  47. ' rescale width=128 height=128 !'
  48. ' map-slice number=256 !'
  49. ' write filename="{output}/.slicemaps/sm-128-128-2048-2048.jpg"')
  50. cmd = cmd.format(output=dst['path'])
  51. args = shlex.split(cmd)
  52. proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  53. stdout, stderr = proc.communicate()
  54. print stdout, stderr
  55. proc.wait()
  56. dataset = db.session.query(models.Dataset).\
  57. filter(models.Dataset.id == result_id).first()
  58. message = '{cname} / {dname} is ready.'.format(cname=dataset.collection.name, dname=dataset.name)
  59. notification = models.Notification(message=message, user=dataset.collection.user)
  60. db.session.add(notification)
  61. db.session.commit()