Exemplo n.º 1
0
    def validate(self, filepath, expected=None):
        logger.debug('Validating syntax of {xml}'.format(xml=filepath))

        etree.clear_error_log()
        started = timezone.now()

        try:
            etree.parse(filepath)
        except etree.XMLSyntaxError as e:
            msg = 'Syntax validation of {xml} failed'.format(xml=filepath)
            logger.exception(msg)
            done = timezone.now()
            validation_objs = []
            for error in e.error_log:
                message = '{line}: {msg}'.format(line=error.line,
                                                 msg=error.message)
                validation_objs.append(
                    Validation(
                        passed=False,
                        validator=self.__class__.__name__,
                        filename=filepath,
                        message=message,
                        time_started=started,
                        time_done=done,
                        information_package_id=self.ip,
                        task=self.task,
                    ))

            Validation.objects.bulk_create(validation_objs, 100)
            raise ValidationError(msg,
                                  errors=[o.message for o in validation_objs])
        except Exception as e:
            logger.exception(
                'Unknown error during syntax validation of {xml}'.format(
                    xml=filepath))
            done = timezone.now()
            Validation.objects.create(
                passed=False,
                validator=self.__class__.__name__,
                filename=filepath,
                message=str(e),
                time_started=started,
                time_done=done,
                information_package_id=self.ip,
                task=self.task,
            )
            raise

        Validation.objects.create(
            passed=True,
            validator=self.__class__.__name__,
            filename=filepath,
            time_started=started,
            time_done=timezone.now(),
            information_package_id=self.ip,
            task=self.task,
        )
        logger.info(
            "Successful syntax validation of {xml}".format(xml=filepath))
Exemplo n.º 2
0
 def _create_obj(self, filename, passed, msg):
     return Validation(filename=filename,
                       time_started=timezone.now(),
                       time_done=timezone.now(),
                       validator=self.__class__.__name__,
                       required=self.required,
                       task_id=self.task,
                       information_package_id=self.ip,
                       responsible=self.responsible,
                       message=msg,
                       passed=passed,
                       specification={
                           'context': self.context,
                           'options': self.options,
                       })
Exemplo n.º 3
0
    def validate(self, filepath, expected=None):
        logger.debug('Validating filename of %s' % filepath)

        val_obj = Validation(filename=filepath,
                             time_started=timezone.now(),
                             validator=self.__class__.__name__,
                             required=self.required,
                             task=self.task,
                             information_package=self.ip,
                             responsible=self.responsible,
                             specification={
                                 'context': self.context,
                                 'options': self.options,
                             })

        passed = False
        try:
            if expected is None:
                if os.path.isfile(filepath):
                    expected = DEFAULT_EXPECTED_FILE
                else:
                    expected = DEFAULT_EXPECTED_DIR

            if not re.search(expected, os.path.basename(filepath)):
                message = "Filename validation of {} failed, it does not match {}".format(
                    filepath, expected)
                logger.warning(message)
                raise ValidationError(message)

            passed = True

        except Exception:
            val_obj.message = traceback.format_exc()
            raise
        else:
            val_obj.message = 'Successfully validated filename of {}'.format(
                filepath)
            logger.info(val_obj.message)
        finally:
            val_obj.time_done = timezone.now()
            val_obj.passed = passed
            val_obj.save()
Exemplo n.º 4
0
    def validate(self, filepath):
        logger.debug('Validating extension of %s' % filepath)

        val_obj = Validation(
            filename=filepath,
            time_started=timezone.now(),
            validator=self.__class__.__name__,
            required=self.required,
            task=self.task,
            information_package=self.ip,
            responsible=self.responsible,
            specification={
                'context': self.context,
                'options': self.options,
            }
        )

        passed = False
        try:
            if re.search(REPEATED_PATTERN, filepath):
                message = "Extension validation of {} failed, repeated extensions found".format(filepath)
                logger.warning(message)
                raise ValidationError(message)

            passed = True

        except Exception:
            val_obj.message = traceback.format_exc()
            raise
        else:
            val_obj.message = 'Successfully validated extension of {}'.format(filepath)
            logger.info(val_obj.message)
        finally:
            val_obj.time_done = timezone.now()
            val_obj.passed = passed
            val_obj.save()
Exemplo n.º 5
0
    def validate(self, filepath, expected=None):
        logger.debug('Validating {xml} against {schema}'.format(
            xml=filepath, schema=self.context))
        rootdir = self.options.get('rootdir')
        etree.clear_error_log()
        started = timezone.now()
        relpath = os.path.relpath(filepath, rootdir)
        try:
            self._validate_isoschematron(filepath)
        except etree.DocumentInvalid as e:
            logger.exception(
                'ISO-Schematron validation of {xml} against {schema} failed'.
                format(xml=filepath, schema=self.context))
            done = timezone.now()
            validation_objs = []
            for error in e.error_log:
                message = '{line}: {msg}'.format(line=error.line,
                                                 msg=error.message)
                validation_objs.append(
                    Validation(
                        passed=False,
                        validator=self.__class__.__name__,
                        filename=relpath,
                        message=message,
                        time_started=started,
                        time_done=done,
                        information_package_id=self.ip,
                        task=self.task,
                    ))

            Validation.objects.bulk_create(validation_objs, 100)
            raise
        except Exception as e:
            logger.exception(
                'Unknown error during iso-schematron validation of {xml} against {schema}'
                .format(xml=filepath, schema=self.context))
            done = timezone.now()
            Validation.objects.create(
                passed=False,
                validator=self.__class__.__name__,
                filename=relpath,
                message=str(e),
                time_started=started,
                time_done=done,
                information_package_id=self.ip,
                task=self.task,
            )
            raise

        Validation.objects.create(
            passed=True,
            validator=self.__class__.__name__,
            filename=relpath,
            time_started=started,
            time_done=timezone.now(),
            information_package_id=self.ip,
            task=self.task,
        )
        logger.info(
            "Successful iso-schematron validation of {xml} against {schema}".
            format(xml=filepath, schema=self.context))