qibuild.command - Launch processes

Calling process

qibuild.command.check_is_in_path(executable, build_env=None)

Check that the given executable is to be found in %PATH%

qibuild.command.find_program(executable, env=None)

Get the full path of an executable by looking at PATH environment variable (and PATHEXT on windows)

Returns:None if program was not found, the full path to executable otherwize
qibuild.command.call(cmd, cwd=None, env=None, ignore_ret_code=False)

Execute a command line.

If ignore_ret_code is False:
raise CommandFailedException if returncode is None.
Else:
simply returns the returncode of the process

Note: first arg of the cmd is assumed to be something inside %PATH%. (or in env[PATH] if env is not None)

Note: the shell= argument of the subprocess.Popen call will always be False.

can raise:
  • CommandFailedException if ignore_ret_code is False and returncode is non zero
  • NotInPath if first arg of cmd is not in %PATH%
  • And a normal exception if cwd is given and is not an existing directory.

If sys.stdout or sys.stderr are not a tty, only write the last 300 lines of the process to sys.stdout if the returncode is not zero, else write everything.

Note: this trick with sys.stderr, sys.stdout and subprocess does not work on windows with python < 2.7, so it is simply disabled, and you have a normal behavior instead.

Notes about call()

Finding the executable to run

When using call(cmd, ...), check_is_in_path() is always call with cmd[0] as argument, then replaced with the result of find_program().

This way, on Windows:

qibuild.command.call(["cmake", ...])

works as soon as cmake.exe is in PATH

Behavior

subprocess.Popen is always called with shell=False, for security reasons.

Unless explicitly told not to, CommandFailedException is raised when the return code of the command is not zero.

Running process in the background

qibuild.command.call_background(*args, **kwds)

To be used in a “with” statement:

with call_background(...):
   do_stuff()

do_other_stuff()

Process is run in the background, then do_stuff() is called.

By the time you are executing do_other_stuff(), you know that the process has been killed, better yet, if an exception has occurred during do_stuff, this exception is re-raised after the process has been killed.

class qibuild.command.ProcessThread(cmd, name=None, cwd=None, env=None)

A simple way to run commands.

The thread will terminate when the command terminates

The full log is available in self.out, and the subprocess.Popen object in self.process

Exceptions

class qibuild.command.CommandFailedException