def _validate_iso_metadata(iso_header): if 'CD001' not in Codec.decode(iso_header.volume_id): raise KiwiIsoMetaDataError( '%s: this is not an iso9660 filesystem' % iso_header.isofile ) if 'EL TORITO SPECIFICATION' not in Codec.decode( iso_header.eltorito_id ): raise KiwiIsoMetaDataError( '%s: this iso is not bootable' % iso_header.isofile ) if iso_header.path_table_sector < 0x11: raise KiwiIsoMetaDataError( 'strange path table location: 0x%x' % iso_header.path_table_sector ) if iso_header.boot_catalog_sector < 0x12: raise KiwiIsoMetaDataError( 'strange boot catalog location: 0x%x' % iso_header.boot_catalog_sector ) if not iso_header.boot_catalog: raise KiwiIsoMetaDataError( '%s: no boot catalog found' % iso_header.isofile )
def test_decode_utf8_failure(self, mock_warn, mock_decode): def mocked_decode(literal, charset): if charset: raise KiwiDecodingError('utf-8 decoding failure') else: raise KiwiDecodingError('ascii decoding failure') mock_decode.side_effect = mocked_decode Codec.decode(self.literal) assert mock_warn.called
def test_decode_utf8_failure(self, mock_decode): def mocked_decode(literal, charset): if charset: raise KiwiDecodingError('utf-8 decoding failure') else: raise KiwiDecodingError('ascii decoding failure') mock_decode.side_effect = mocked_decode with raises(KiwiDecodingError): with self._caplog.at_level(logging.WARNING): Codec.decode(self.literal)
def get_error_output(self): """ Provide data which was sent to the stderr channel :return: stderr data :rtype: str """ return Codec.decode(self.command_error_output)
def test_decode_ascii_failure(self, mock_decode): msg = 'utf-8 compatible string' def mocked_decode(literal, charset): if charset: return msg else: raise KiwiDecodingError('ascii decoding failure') mock_decode.side_effect = mocked_decode with self._caplog.at_level(logging.WARNING): assert msg == Codec.decode(self.literal)
def test_decode_ascii_failure(self, mock_warn, mock_decode): msg = 'utf-8 compatible string' def mocked_decode(literal, charset): if charset: return msg else: raise KiwiDecodingError('ascii decoding failure') mock_decode.side_effect = mocked_decode assert msg == Codec.decode(self.literal) assert mock_warn.called
def __next__(self): line_read = None if self.command.process.poll() is not None: if self.output_eof_reached and self.errors_eof_reached: raise StopIteration() if self.command.output_available(): byte_read = self.command.output.read(1) if not byte_read: self.output_eof_reached = True elif byte_read == bytes(b'\n'): line_read = Codec.decode(self.command_output_line) self.command_output_line = bytes(b'') else: self.command_output_line += byte_read if self.command.error_available(): byte_read = self.command.error.read(1) if not byte_read: self.errors_eof_reached = True else: self.command_error_output += byte_read return line_read
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 test_decode(self, mock_decode): msg = 'utf-8 compatible string' mock_decode.return_value = msg assert msg == Codec.decode(self.literal)
def test_decode_None_literal(self): assert '' == Codec.decode(None)
def test_real_decode_non_ascii(self, mock_warn): reload(sys) # noqa:F821 sys.setdefaultencoding('ASCII') assert unichr(252) == Codec.decode(self.literal) # noqa:F821 assert mock_warn.called