def test_load_schema_description_from_data_invalid_no_jing( self, mock_xslt, mock_command, mock_parse, mock_relax, mock_schematron): mock_rng_validate = mock.Mock() mock_rng_validate.validate = mock.Mock(return_value=False) mock_sch_validate = mock.Mock() mock_sch_validate.validate = mock.Mock(return_value=True) mock_relax.return_value = mock_rng_validate mock_schematron.return_value = mock_sch_validate mock_command.side_effect = KiwiCommandNotFound('No jing command') self.description_from_data.load()
def create_image_format(self): """ Create ova disk format using ovftool from https://www.vmware.com/support/developer/ovf """ # Check for required ovftool ovftool = Path.which(filename='ovftool', access_mode=os.X_OK) if not ovftool: tool_not_found_message = dedent('''\n Required tool {0} not found in PATH on the build host Building OVA images requires VMware's {0} tool which can be installed from the following location https://www.vmware.com/support/developer/ovf ''') raise KiwiCommandNotFound( tool_not_found_message.format(ovftool) ) # Create the vmdk disk image and vmx config self.vmdk.create_image_format() # Convert to ova using ovftool vmx = self.get_target_file_path_for_format('vmx') ova = self.get_target_file_path_for_format('ova') try: os.unlink(ova) except OSError: pass ovftool_options = [] if CommandCapabilities.has_option_in_help( ovftool, '--shaAlgorithm', raise_on_error=False ): ovftool_options.append('--shaAlgorithm=SHA1') Command.run( [ovftool] + ovftool_options + [vmx, ova] ) # ovftool ignores the umask and creates files with 0600 # apply file permission bits set in the vmx file to the # ova file st = os.stat(vmx) os.chmod(ova, stat.S_IMODE(st.st_mode))
def run(command, custom_env=None, raise_on_error=True, stderr_to_stdout=False): """ Execute a program and block the caller. The return value is a hash containing the stdout, stderr and return code information. Unless raise_on_error is set to false an exception is thrown if the command exits with an error code not equal to zero Example: .. code:: python result = Command.run(['ls', '-l']) :param list command: command and arguments :param list custom_env: custom os.environ :param bool raise_on_error: control error behaviour :param bool stderr_to_stdout: redirects stderr to stdout :return: Contains call results in command type .. code:: python command(output='string', error='string', returncode=int) :rtype: namedtuple """ from .path import Path log.debug('EXEC: [%s]', ' '.join(command)) environment = os.environ if custom_env: environment = custom_env if not Path.which( command[0], custom_env=environment, access_mode=os.X_OK): message = 'Command "%s" not found in the environment' % command[0] if not raise_on_error: log.debug('EXEC: %s', message) return command_type(output=None, error=None, returncode=-1) else: raise KiwiCommandNotFound(message) stderr = subprocess.STDOUT if stderr_to_stdout else subprocess.PIPE try: process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=stderr, env=environment) except Exception as e: raise KiwiCommandError('%s: %s: %s' % (command[0], type(e).__name__, format(e))) output, error = process.communicate() if process.returncode != 0 and not error: error = bytes(b'(no output on stderr)') if process.returncode != 0 and not output: output = bytes(b'(no output on stdout)') if process.returncode != 0 and raise_on_error: log.debug('EXEC: Failed with stderr: {0}, stdout: {1}'.format( Codec.decode(error), Codec.decode(output))) raise KiwiCommandError('{0}: stderr: {1}, stdout: {2}'.format( command[0], Codec.decode(error), Codec.decode(output))) return command_type(output=Codec.decode(output), error=Codec.decode(error), returncode=process.returncode)
def call(command, custom_env=None): """ Execute a program and return an io file handle pair back. stdout and stderr are both on different channels. The caller must read from the output file handles in order to actually run the command. This can be done using the CommandIterator from command_process Example: .. code:: python process = Command.call(['ls', '-l']) :param list command: command and arguments :param list custom_env: custom os.environ :return: Contains process results in command type .. code:: python command( output='string', output_available=bool, error='string', error_available=bool, process=subprocess ) :rtype: namedtuple """ from .path import Path log.debug('EXEC: [%s]', ' '.join(command)) environment = os.environ if custom_env: environment = custom_env if not Path.which( command[0], custom_env=environment, access_mode=os.X_OK): raise KiwiCommandNotFound( 'Command "%s" not found in the environment' % command[0]) try: process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=environment) except Exception as e: raise KiwiCommandError('%s: %s' % (type(e).__name__, format(e))) def output_available(): def _select(): descriptor_lists = select.select([process.stdout], [], [process.stdout], 1e-4) readable = descriptor_lists[0] exceptional = descriptor_lists[2] if readable and not exceptional: return True return _select def error_available(): def _select(): descriptor_lists = select.select([process.stderr], [], [process.stderr], 1e-4) readable = descriptor_lists[0] exceptional = descriptor_lists[2] if readable and not exceptional: return True return _select command = namedtuple('command', [ 'output', 'output_available', 'error', 'error_available', 'process' ]) return command(output=process.stdout, output_available=output_available(), error=process.stderr, error_available=error_available(), process=process)
def side_effect(arg): if len(command_results) == 0: raise KiwiCommandNotFound('ovftool not found') return command_results.pop()