Bläddra i källkod

Add ingest code

Matthias Vogelgesang 9 år sedan
förälder
incheckning
ed6433c224
2 ändrade filer med 52 tillägg och 23 borttagningar
  1. 1 1
      dm/models.py
  2. 51 22
      reco.py

+ 1 - 1
dm/models.py

@@ -82,5 +82,5 @@ class User(EntityContainer):
 
     def get_credentials(self):
         url = self.client.url('information/properties')
-        response = self.client.get_response(url, userId=self.ldap_id)
+        response = self.client.get(url, userId=self.ldap_id)
         return Credentials(response)

+ 51 - 22
reco.py

@@ -55,15 +55,15 @@ def configure_logging():
         logging.basicConfig(level=logging.INFO)
 
 
-def reconstruct_scan(path):
+def reconstruct_scan(input_path, output_path):
     filenames = os.listdir(path)
     log = logging.getLogger(APP_NAME)
 
     if 'radios' in filenames:
         params = tofu.config.TomoParams().get_defaults()
-        params.input = os.path.join(path, 'radios')
+        params.input = os.path.join(input_path, 'radios')
         params.from_projections = True
-        params.output = os.path.join(os.getcwd(), 'slices', 'slice-%05i.tif')
+        params.output = os.path.join(output_path, 'data', 'slices', 'slice-%05i.tif')
         params.number = 1698
         params.axis = 500
         params.angle_step = math.radians(0.105945)
@@ -75,36 +75,65 @@ def reconstruct_scan(path):
         log.info("Finished reconstruction")
 
 
-def reconstruct(path):
-    log = logging.getLogger(APP_NAME)
-    log.info("Reconstructing from {}".format(path))
-    prefix, path = path.split('file:', 1)
-
-    try:
-        reconstruct_scan(os.path.join(path, 'data'))
-    except OSError as e:
-        log.error("Could not reconstruct: {}".format(str(e)))
-
-
 class Application(object):
     def __init__(self):
-        self.client = dm.Client()
+        self.client = dm.Client(token="TjMgcEtjGyL2qXZ4", token_secret="xBJS0Oq9j5Hjc8Kq")
         self.log = logging.getLogger(APP_NAME)
 
     def run(self):
         last_update = get_last_timestamp()
+        self.objects = {o.uuid: o for o in self.client.get_objects(limit=100)}
+        self.accesspoint = self.client.get_accesspoints('file://')[0]
 
-        for oid in self.client.get_ingest_ids():
-            ingest = self.client.get_ingest(oid)
-
-            if ingest.staging_url:
-                reconstruct(ingest.staging_url)
+        for ingest in self.client.get_ingests():
+            self.log.info("Found ingest {}".format(ingest.id))
+            if ingest.staging_url and ingest.id == 1:
+                self.reconstruct(ingest)
 
         now = datetime.datetime.now()
         update_timestamp(now)
 
-    def reconstruct(self, oid):
-        obj = self.client.get_object(oid)
+    def reconstruct(self, ingest):
+        prefix, path = ingest.staging_url.split('file:', 1)
+
+        if os.access(path, os.R_OK):
+            self.log.info("Using ingest={} [path={} status={}]".format(ingest.id, path, ingest.status))
+
+            if ingest.object_uuid in self.objects:
+                obj = self.objects[ingest.object_uuid]
+
+                output_path = self.create_working_dir(obj)
+
+                if output_path:
+                    self.log.info(" Created {} [uploader={}]".format(output_path, obj.uploader))
+                    reconstruct_scan(os.path.join(path, 'data'), output_path)
+            else:
+                self.log.warn("Object {} not found for {}".format(ingest.object_uuid, ingest.staging_url))
+        else:
+            self.log.error("Cannot access {}".format(path))
+
+    def create_working_dir(self, obj):
+        try:
+            user = self.client.get_user(obj.uploader)
+            credentials = user.get_credentials()
+
+            user_client = dm.Client(token=credentials.key, token_secret=credentials.secret)
+
+            new_obj = user_client.create_object(obj.investigation_id, user.id, label='foo2')
+            self.log.info("  Created new object={}".format(new_obj.id))
+
+            new_ingest = user_client.create_ingest(new_obj.id, self.accesspoint.uuid)
+            self.log.info("  Created new ingest={} [url={}]".format(new_ingest.id, new_ingest.staging_url))
+
+            # get the ingest again to acccess the staging url ...
+            new_ingest = user_client.get_ingest(new_ingest.id)
+            prefix, path = new_ingest.staging_url.split('file:', 1)
+            return path
+        except dm.exceptions.NotFoundError:
+            self.log.warn(" No credentials found for user={}".format(obj.uploader))
+        except dm.exceptions.ArbitraryError as e:
+            self.log.error(" Could not create object [{}]".format(e))
+
 
 
 if __name__ == '__main__':