import os import subprocess from pathlib import Path from doit.exceptions import TaskFailed from .task import FlowTask, ShellEnv class MissingTarget ( Exception ): pass class Genpat ( FlowTask ): @staticmethod def mkRule ( rule, targets, depends=[], flags=0 ): return Genpat( rule, targets, depends, flags ) def __init__ ( self, rule, targets, depends, flags ): super().__init__( rule, targets, depends ) self.inputFile = Path( self.file_depend(0) ) self.outputFile = Path( self.targets[0] ) self.command = [ 'genpat' ] self.command += [ self.inputFile.stem ] self.addClean( self.targets ) def __repr__ ( self ): return '<{}>'.format( ' '.join(self.command) ) def doTask ( self ): from CRL import AllianceFramework from helpers.io import ErrorMessage shellEnv = ShellEnv() shellEnv.export() state = subprocess.run( self.command ) if state.returncode: e = ErrorMessage( 1, 'Genpat.doTask(): UNIX command failed ({}).' \ .format( state.returncode )) return TaskFailed( e ) return self.checkTargets( 'Genpat.doTask' ) def create_doit_tasks ( self ): return { 'basename' : self.basename , 'actions' : [ self.doTask ] , 'doc' : 'Run {}.'.format( self ) , 'targets' : self.targets , 'file_dep' : self.file_dep }