Ejemplo n.º 1
0
def process_rule(self):
	"""
	Processes the attribute ``rule``. When present, :py:meth:`waflib.TaskGen.process_source` is disabled::

		def build(bld):
			bld(rule='cp ${SRC} ${TGT}', source='wscript', target='bar.txt')

	Main attributes processed:

	* rule: command to execute, it can be a tuple of strings for multiple commands
	* chmod: permissions for the resulting files (integer value such as Utils.O755)
	* shell: set to False to execute the command directly (default is True to use a shell)
	* scan: scanner function
	* vars: list of variables to trigger rebuilds, such as CFLAGS
	* cls_str: string to display when executing the task
	* cls_keyword: label to display when executing the task
	* cache_rule: by default, try to re-use similar classes, set to False to disable
	* source: list of Node or string objects representing the source files required by this task
	* target: list of Node or string objects representing the files that this task creates
	* cwd: current working directory (Node or string)
	* stdout: standard output, set to None to prevent waf from capturing the text
	* stderr: standard error, set to None to prevent waf from capturing the text
	* timeout: timeout for command execution (Python 3)
	* always: whether to always run the command (False by default)
	* deep_inputs: whether the task must depend on the input file tasks too (False by default)
	"""
	if not getattr(self, 'rule', None):
		return

	# create the task class
	name = str(getattr(self, 'name', None) or self.target or getattr(self.rule, '__name__', self.rule))

	# or we can put the class in a cache for performance reasons
	try:
		cache = self.bld.cache_rule_attr
	except AttributeError:
		cache = self.bld.cache_rule_attr = {}

	chmod = getattr(self, 'chmod', None)
	shell = getattr(self, 'shell', True)
	color = getattr(self, 'color', 'BLUE')
	scan = getattr(self, 'scan', None)
	_vars = getattr(self, 'vars', [])
	cls_str = getattr(self, 'cls_str', None)
	cls_keyword = getattr(self, 'cls_keyword', None)
	use_cache = getattr(self, 'cache_rule', 'True')
	deep_inputs = getattr(self, 'deep_inputs', False)

	scan_val = has_deps = hasattr(self, 'deps')
	if scan:
		scan_val = id(scan)

	key = Utils.h_list((name, self.rule, chmod, shell, color, cls_str, cls_keyword, scan_val, _vars, deep_inputs))

	cls = None
	if use_cache:
		try:
			cls = cache[key]
		except KeyError:
			pass
	if not cls:
		rule = self.rule
		if chmod is not None:
			def chmod_fun(tsk):
				for x in tsk.outputs:
					os.chmod(x.abspath(), tsk.generator.chmod)
			if isinstance(rule, tuple):
				rule = list(rule)
				rule.append(chmod_fun)
				rule = tuple(rule)
			else:
				rule = (rule, chmod_fun)

		cls = Task.task_factory(name, rule, _vars, shell=shell, color=color)

		if cls_str:
			setattr(cls, '__str__', self.cls_str)

		if cls_keyword:
			setattr(cls, 'keyword', self.cls_keyword)

		if deep_inputs:
			Task.deep_inputs(cls)

		if scan:
			cls.scan = self.scan
		elif has_deps:
			def scan(self):
				nodes = []
				for x in self.generator.to_list(getattr(self.generator, 'deps', None)):
					node = self.generator.path.find_resource(x)
					if not node:
						self.generator.bld.fatal('Could not find %r (was it declared?)' % x)
					nodes.append(node)
				return [nodes, []]
			cls.scan = scan

		if use_cache:
			cache[key] = cls

	# now create one instance
	tsk = self.create_task(name)

	for x in ('after', 'before', 'ext_in', 'ext_out'):
		setattr(tsk, x, getattr(self, x, []))

	if hasattr(self, 'stdout'):
		tsk.stdout = self.stdout

	if hasattr(self, 'stderr'):
		tsk.stderr = self.stderr

	if getattr(self, 'timeout', None):
		tsk.timeout = self.timeout

	if getattr(self, 'always', None):
		tsk.always_run = True

	if getattr(self, 'target', None):
		if isinstance(self.target, str):
			self.target = self.target.split()
		if not isinstance(self.target, list):
			self.target = [self.target]
		for x in self.target:
			if isinstance(x, str):
				tsk.outputs.append(self.path.find_or_declare(x))
			else:
				x.parent.mkdir() # if a node was given, create the required folders
				tsk.outputs.append(x)
		if getattr(self, 'install_path', None):
			self.install_task = self.add_install_files(install_to=self.install_path,
				install_from=tsk.outputs, chmod=getattr(self, 'chmod', Utils.O644))

	if getattr(self, 'source', None):
		tsk.inputs = self.to_nodes(self.source)
		# bypass the execution of process_source by setting the source to an empty list
		self.source = []

	if getattr(self, 'cwd', None):
		tsk.cwd = self.cwd

	if isinstance(tsk.run, functools.partial):
		# Python documentation says: "partial objects defined in classes
		# behave like static methods and do not transform into bound
		# methods during instance attribute look-up."
		tsk.run = functools.partial(tsk.run, tsk)
Ejemplo n.º 2
0
def process_rule(self):
    """
	Processes the attribute ``rule``. When present, :py:meth:`waflib.TaskGen.process_source` is disabled::

		def build(bld):
			bld(rule='cp ${SRC} ${TGT}', source='wscript', target='bar.txt')

	Main attributes processed:

	* rule: command to execute, it can be a tuple of strings for multiple commands
	* chmod: permissions for the resulting files (integer value such as Utils.O755)
	* shell: set to False to execute the command directly (default is True to use a shell)
	* scan: scanner function
	* vars: list of variables to trigger rebuilds, such as CFLAGS
	* cls_str: string to display when executing the task
	* cls_keyword: label to display when executing the task
	* cache_rule: by default, try to re-use similar classes, set to False to disable
	* source: list of Node or string objects representing the source files required by this task
	* target: list of Node or string objects representing the files that this task creates
	* cwd: current working directory (Node or string)
	* stdout: standard output, set to None to prevent waf from capturing the text
	* stderr: standard error, set to None to prevent waf from capturing the text
	* timeout: timeout for command execution (Python 3)
	* always: whether to always run the command (False by default)
	* deep_inputs: whether the task must depend on the input file tasks too (False by default)
	"""
    if not getattr(self, 'rule', None):
        return

    # create the task class
    name = str(
        getattr(self, 'name', None) or self.target
        or getattr(self.rule, '__name__', self.rule))

    # or we can put the class in a cache for performance reasons
    try:
        cache = self.bld.cache_rule_attr
    except AttributeError:
        cache = self.bld.cache_rule_attr = {}

    chmod = getattr(self, 'chmod', None)
    shell = getattr(self, 'shell', True)
    color = getattr(self, 'color', 'BLUE')
    scan = getattr(self, 'scan', None)
    _vars = getattr(self, 'vars', [])
    cls_str = getattr(self, 'cls_str', None)
    cls_keyword = getattr(self, 'cls_keyword', None)
    use_cache = getattr(self, 'cache_rule', 'True')
    deep_inputs = getattr(self, 'deep_inputs', False)

    scan_val = has_deps = hasattr(self, 'deps')
    if scan:
        scan_val = id(scan)

    key = Utils.h_list((name, self.rule, chmod, shell, color, cls_str,
                        cls_keyword, scan_val, _vars, deep_inputs))

    cls = None
    if use_cache:
        try:
            cls = cache[key]
        except KeyError:
            pass
    if not cls:
        rule = self.rule
        if chmod is not None:

            def chmod_fun(tsk):
                for x in tsk.outputs:
                    os.chmod(x.abspath(), tsk.generator.chmod)

            if isinstance(rule, tuple):
                rule = list(rule)
                rule.append(chmod_fun)
                rule = tuple(rule)
            else:
                rule = (rule, chmod_fun)

        cls = Task.task_factory(name, rule, _vars, shell=shell, color=color)

        if cls_str:
            setattr(cls, '__str__', self.cls_str)

        if cls_keyword:
            setattr(cls, 'keyword', self.cls_keyword)

        if deep_inputs:
            Task.deep_inputs(cls)

        if scan:
            cls.scan = self.scan
        elif has_deps:

            def scan(self):
                nodes = []
                for x in self.generator.to_list(
                        getattr(self.generator, 'deps', None)):
                    node = self.generator.path.find_resource(x)
                    if not node:
                        self.generator.bld.fatal(
                            'Could not find %r (was it declared?)' % x)
                    nodes.append(node)
                return [nodes, []]

            cls.scan = scan

        if use_cache:
            cache[key] = cls

    # now create one instance
    tsk = self.create_task(name)

    for x in ('after', 'before', 'ext_in', 'ext_out'):
        setattr(tsk, x, getattr(self, x, []))

    if hasattr(self, 'stdout'):
        tsk.stdout = self.stdout

    if hasattr(self, 'stderr'):
        tsk.stderr = self.stderr

    if getattr(self, 'timeout', None):
        tsk.timeout = self.timeout

    if getattr(self, 'always', None):
        tsk.always_run = True

    if getattr(self, 'target', None):
        if isinstance(self.target, str):
            self.target = self.target.split()
        if not isinstance(self.target, list):
            self.target = [self.target]
        for x in self.target:
            if isinstance(x, str):
                tsk.outputs.append(self.path.find_or_declare(x))
            else:
                x.parent.mkdir(
                )  # if a node was given, create the required folders
                tsk.outputs.append(x)
        if getattr(self, 'install_path', None):
            self.install_task = self.add_install_files(
                install_to=self.install_path,
                install_from=tsk.outputs,
                chmod=getattr(self, 'chmod', Utils.O644))

    if getattr(self, 'source', None):
        tsk.inputs = self.to_nodes(self.source)
        # bypass the execution of process_source by setting the source to an empty list
        self.source = []

    if getattr(self, 'cwd', None):
        tsk.cwd = self.cwd

    if isinstance(tsk.run, functools.partial):
        # Python documentation says: "partial objects defined in classes
        # behave like static methods and do not transform into bound
        # methods during instance attribute look-up."
        tsk.run = functools.partial(tsk.run, tsk)
Ejemplo n.º 3
0
def process_rule(self):
	if not getattr(self,'rule',None):
		return
	name=str(getattr(self,'name',None)or self.target or getattr(self.rule,'__name__',self.rule))
	try:
		cache=self.bld.cache_rule_attr
	except AttributeError:
		cache=self.bld.cache_rule_attr={}
	chmod=getattr(self,'chmod',None)
	shell=getattr(self,'shell',True)
	color=getattr(self,'color','BLUE')
	scan=getattr(self,'scan',None)
	_vars=getattr(self,'vars',[])
	cls_str=getattr(self,'cls_str',None)
	cls_keyword=getattr(self,'cls_keyword',None)
	use_cache=getattr(self,'cache_rule','True')
	deep_inputs=getattr(self,'deep_inputs',False)
	scan_val=has_deps=hasattr(self,'deps')
	if scan:
		scan_val=id(scan)
	key=Utils.h_list((name,self.rule,chmod,shell,color,cls_str,cls_keyword,scan_val,_vars,deep_inputs))
	cls=None
	if use_cache:
		try:
			cls=cache[key]
		except KeyError:
			pass
	if not cls:
		rule=self.rule
		if chmod is not None:
			def chmod_fun(tsk):
				for x in tsk.outputs:
					os.chmod(x.abspath(),tsk.generator.chmod)
			if isinstance(rule,tuple):
				rule=list(rule)
				rule.append(chmod_fun)
				rule=tuple(rule)
			else:
				rule=(rule,chmod_fun)
		cls=Task.task_factory(name,rule,_vars,shell=shell,color=color)
		if cls_str:
			setattr(cls,'__str__',self.cls_str)
		if cls_keyword:
			setattr(cls,'keyword',self.cls_keyword)
		if deep_inputs:
			Task.deep_inputs(cls)
		if scan:
			cls.scan=self.scan
		elif has_deps:
			def scan(self):
				nodes=[]
				for x in self.generator.to_list(getattr(self.generator,'deps',None)):
					node=self.generator.path.find_resource(x)
					if not node:
						self.generator.bld.fatal('Could not find %r (was it declared?)'%x)
					nodes.append(node)
				return[nodes,[]]
			cls.scan=scan
		if use_cache:
			cache[key]=cls
	tsk=self.create_task(name)
	for x in('after','before','ext_in','ext_out'):
		setattr(tsk,x,getattr(self,x,[]))
	if hasattr(self,'stdout'):
		tsk.stdout=self.stdout
	if hasattr(self,'stderr'):
		tsk.stderr=self.stderr
	if getattr(self,'timeout',None):
		tsk.timeout=self.timeout
	if getattr(self,'always',None):
		tsk.always_run=True
	if getattr(self,'target',None):
		if isinstance(self.target,str):
			self.target=self.target.split()
		if not isinstance(self.target,list):
			self.target=[self.target]
		for x in self.target:
			if isinstance(x,str):
				tsk.outputs.append(self.path.find_or_declare(x))
			else:
				x.parent.mkdir()
				tsk.outputs.append(x)
		if getattr(self,'install_path',None):
			self.install_task=self.add_install_files(install_to=self.install_path,install_from=tsk.outputs,chmod=getattr(self,'chmod',Utils.O644))
	if getattr(self,'source',None):
		tsk.inputs=self.to_nodes(self.source)
		self.source=[]
	if getattr(self,'cwd',None):
		tsk.cwd=self.cwd
	if isinstance(tsk.run,functools.partial):
		tsk.run=functools.partial(tsk.run,tsk)