craftr.ext.rules

craftr.ext.rules.run(commands, args=(), inputs=(), outputs=None, cwd=None, pool=None, description=None, target_name=None)

This function creates a Target that runs a custom command. The function is three different modes based on the first parameter.

  1. If commands is a Target, that target must list exactly one file in its outputs and that file is assumed to be a binary and will be executed by the target created by this function. The args parameter may be a list of additional arguments for the program.
  2. If commands is a list, it is handled as a list of commands, never as a single command. Thus a string in the list represents a complete command, as does a list of strings (representing the command as its individual arguments).
  3. If commands is a string, it will be treated as a single command.

If multiple commands need to be invoked, TargetBuilder.write_multicommand_file is used to create a script to invoke multiple commands.

__Examples__

main = ld.link(
  output = 'main',
  inputs = objects,
)
run = rules.run(main, args = [path.local('testfile.dat')])
run = rules.run([
  'command1 args11 args12 args13',
  ['command2', 'args21', 'args22', 'args23'],
], cwd = path.local('test'))
Parameters:
  • commands – A Target, string or list of strings/command lists.
  • args – Additional program arguments when a Target is specified for commands.
  • inputs – A list of input files for the command. These can be referenced using the Ninja variable %in in the command(s).
  • outputs – A list of outputs generated by the command. These can be referenced using the Ninja variable %out in the command(s).
  • cwd – An optional working directory to switch to when executing the command(s). If None is passed, the build directory is used.
  • pool – Override the default pool that the command is executed in. If a Target is passed for commands, this will default to console.
  • description – Optional target description displayed when building with Ninja.
  • target_name – An optional override for the return target’s name.
Returns:

A Target.

class craftr.ext.rules.PythonTool(inputs, outputs, frameworks=(), **kwargs)

Base class for Python tools. Python tools are class objects that can be invoked from Ninja through the Craftr RTS (Runtime Server) using socket communication and the craftr-rts-invoke command.

Note

This class overrides __new__() and returns a Target rather than an instance of PythonTool.

__call__(args)

Called when the tool is invoked. args is a list of command-line arguments were passed additionally to craftr-rts-invoke. This is just an empty list in most cases as the PythonTool instance can save all data it needs in its attributes.

Important

This method could be called in a threaded context. Don’t import new modules and be thread-safe.

class craftr.ext.rules.render_template(template, output, **context)

This is a simple Python tool that can render a template file to a new file given a set of key/value pairs. Variables in the template are references by ${KEY}$. There is currently not escape mechanism implemented.