Example #1
0
 def run(self):
     Task.run(self)
     if self.valid:
         # prepare a task product
         self.product = []
         
         # if the action preset is not None or empty initialize the transform
         if self.preset['action'][self.ontology['action']]:
             
             # ensure all resources are loaded in the asset
             self.resource.asset.touch()
             
             # initialize a transform
             self.transform = ResourceTransform(self.resource)
             self.transform.transform(self.preset, self.ontology['action'])
             self.node['transform'] = self.transform.node
             
             # if the transform yields not viable input there really is nothing much to do...
             if not any((any(pivot.stream) for pivot in self.transform.pivot.values())):
                 self.invalidate(u'Task did not yield any viable input')
                 
         # if we are still go, invoke the action
         if self.valid: self.action()
Example #2
0
class ResourceTask(Task):
    def __init__(self, job, ontology, path):
        Task.__init__(self, job, ontology)
        self.location = self.env.repository[self.env.host].decode_resource_path(path)
        self.resource = None
        self.transform = None
        self.product = None
        self.action = None
        self._preset = None
        
        if self.location is None:
            self.invalidate(u'Invalid resource path provided {}'.format(path))
            
    @property
    def preset(self):
        if self._preset is None:
            if self.ontology['preset'] in self.env.preset:
                self._preset = self.env.preset[self.ontology['preset']]
        return self._preset
        
    def load(self):
        Task.load(self)
        if self.valid:
            if self.preset is None:
                self.invalidate(u'Could not determine preset')
            else:
                # Add the resource location and transform to the node 
                self.node['origin'] = self.location.node
                
                self.resource = self.cache.find(self.location)
                if self.resource:
                    # if the action is defined in the preset, the preset supports the action
                    if self.ontology['action'] in self.preset['action']:
                        
                        # locate a method that implements the action
                        self.action = getattr(self, self.ontology['action'], None)
                        
                        if self.action is None:
                            self.invalidate(u'Unknown action {}'.format(self.ontology['action']))
                    else:
                        self.invalidate(u'Action {} is not defined in preset {}'.format(self.ontology['action'], self.ontology['preset']))
                else:
                    self.invalidate(u'Invalid resource location')
                    
    def unload(self):
        if self.valid:
            # Mark products as volatile to make sure they are indexed
            # and add their location to the task node
            if self.product:
                self.node['product'] = []
                for p in self.product:
                    p.volatile = True
                    self.node['product'].append(p.location.node)
                    
            # Commit the asset 
            self.resource.asset.commit()
            
        Task.unload(self)
        
    def run(self):
        Task.run(self)
        if self.valid:
            # prepare a task product
            self.product = []
            
            # if the action preset is not None or empty initialize the transform
            if self.preset['action'][self.ontology['action']]:
                
                # ensure all resources are loaded in the asset
                self.resource.asset.touch()
                
                # initialize a transform
                self.transform = ResourceTransform(self.resource)
                self.transform.transform(self.preset, self.ontology['action'])
                self.node['transform'] = self.transform.node
                
                # if the transform yields not viable input there really is nothing much to do...
                if not any((any(pivot.stream) for pivot in self.transform.pivot.values())):
                    self.invalidate(u'Task did not yield any viable input')
                    
            # if we are still go, invoke the action
            if self.valid: self.action()
            
    def produce(self, override=None):
        # copy the location ontology
        p = Ontology.clone(self.location)
        
        # allow the location to recalculate those concepts 
        del p['volume path']
        del p['file name']
        del p['directory']
        
        # explicitly set the volume and host from the task
        p['host'] = self.env.host
        p['volume'] = self.ontology['volume']
        
        # for copy and move we try to set a profile from the source
        if self.ontology['action'] in set(('copy', 'move', 'pack')):
            if self.resource.meta['profile']:
                p['profile'] = self.resource.meta['profile']
                
        # for transcode we try to set the profile from the transform
        elif self.ontology['action'] == 'transcode':
            for pivot in self.transform.pivot.values():
                if 'profile' in pivot.location:
                    p['profile'] = pivot.location['profile']
                    
        # whatever happened, if a profile has been explicitly provided by the task
        # it will override anything we set implicitly
        if self.ontology['profile']:
            p['profile'] = self.ontology['profile']
            
        # if an override was given set some concepts from it 
        if override:
            for i in set((
                'kind', 
                'language',
                'stream order',
                'resource path digest',
                'routing type'
            )):
                if i in override: p[i] = override[i]
                
        # try to produce a product
        product = self.resource.asset.locate_resource(p)
        if product:
            self.product.append(product)
        else:
            self.log.error(u'Could not determine destination path from:\n%s', self.env.encode_json(p))
            
        return product
        
    def info(self):
        self.resource.info(self)
        
    def copy(self):
        self.resource.copy(self)
        
    def move(self):
        self.resource.move(self)
        
    def delete(self):
        self.resource.delete(self)
        
    def explode(self):
        self.resource.explode(self)
        
    def pack(self):
        self.resource.pack(self)
        
    def tag(self):
        self.resource.tag(self)
        
    def optimize(self):
        self.resource.optimize(self)
        
    def transcode(self):
        self.resource.transcode(self)
        
    def update(self):
        self.resource.update(self)
        
    def prepare(self):
        # push a task to explode the resource
        o = self.job.ontology.project('ns.system.task')
        o['action'] = 'explode'
        explode = ResourceTask(self.job, o, self.location['path'])
        explode.constrain({'scope':'task', 'reference':self.key, 'status':'completed'})
        self.job.push(explode)
        
        # push a task to repack the resource fragments
        o = self.job.ontology.project('ns.system.task')
        o['action'] = 'pack'
        o['preset'] = 'fragment'
        pack = ResourceTask(self.job, o, self.location['path'])
        pack.constrain({'scope':'task', 'reference':self.key, 'status':'completed'})
        pack.constrain({'scope':'group', 'reference':explode.key, 'status':'completed'})
        self.job.push(pack)