Esempio n. 1
0
def validate_against_schema(uploaded_file):
    """
    Check if XML files that are part of a single upload validate
    against the TrendMiner XML schema.

    This function uses the `xmllint` tool to check if a given XML file
    conforms to the TrendMiner XML schema. The schema is defined in
    `<project-dir>/trendminer.xsd`. For any file that validates
    against the schema, the xmllint tool returns 0.
    """
    # pylint: disable-msg=E1101
    file_type = get_file_ext(uploaded_file.name)
    if file_type == '.zip' and uploaded_file.folder:
        for file_name in listdir(get_tmp_path(uploaded_file.folder)):
            if file_name.endswith('.xml') and not file_name == 'om.xml':
                command = shlex.split(
                    'xmllint --noout --schema "{0}" "{1}"'.format(
                        SCHEMA_PATH,
                        get_tmp_path(uploaded_file.folder, file_name)))
                subproc = subprocess.Popen(command)
                returncode = subproc.wait()
                if not returncode == 0:
                    raise ValidationError(
                        UploadFormErrors.FILES_SCHEMA_CONFORMITY)
    elif file_type == '.xml':
        command = shlex.split(
            'xmllint --noout --schema "{0}" "{1}"'.format(
                SCHEMA_PATH, get_tmp_path(uploaded_file.name)))
        subproc = subprocess.Popen(command)
        if not subproc.wait() == 0:
            raise ValidationError(UploadFormErrors.XML_SCHEMA_CONFORMITY)
Esempio n. 2
0
def validate_xml_well_formedness(uploaded_file):
    """
    Check if XML files that are part of a single upload are
    well-formed.

    This function uses the `xmlwf` tool to determine if a given XML
    file is well-formed. The tool does not use standard return codes
    for representing the outcome of the check. Instead, if a file is
    well-formed, it simply outputs nothing. If it's not, xmlwf writes
    a description of the problem to standard output.
    """
    # pylint: disable-msg=E1101
    file_type = get_file_ext(uploaded_file.name)
    if file_type == '.zip' and uploaded_file.folder:
        for file_name in listdir(get_tmp_path(uploaded_file.folder)):
            if file_name.endswith('.xml') and not file_name == 'om.xml':
                command = shlex.split('xmlwf "{}"'.format(
                        get_tmp_path(uploaded_file.folder, file_name)))
                subproc = subprocess.Popen(command, stdout=subprocess.PIPE)
                error_msg = subproc.stdout.read()
                if error_msg:
                    raise ValidationError(
                        UploadFormErrors.FILES_WELLFORMEDNESS)
    elif file_type == '.xml':
        command = shlex.split('xmlwf "{}"'.format(
                get_tmp_path(uploaded_file.name)))
        subproc = subprocess.Popen(command, stdout=subprocess.PIPE)
        error_msg = subproc.stdout.read()
        if error_msg:
            raise ValidationError(UploadFormErrors.XML_WELLFORMEDNESS)
Esempio n. 3
0
def _analyse(data):
    """
    Run TrendMiner on user uploaded data.
    """
    file_type = get_file_ext(data.name)
    if file_type == '.zip':
        command = 'perl -I {0} {1}'.format(
            PERL_PATH, path.join(PERL_PATH, 'om-xml.pl'))
        subprocess.call(
            command, cwd=get_tmp_path(data.folder), shell=True)
        entities = parse_results(data.folder)
    elif file_type == '.xml':
        entities = []
    return entities
Esempio n. 4
0
def validate_mime_type(uploaded_file):
    """
    Check MIME type of uploaded file and make sure it corresponds to
    the file's extension.

    This function uses the UNIX `file` command with the `--mime-type`
    option to obtain the MIME type of the uploaded file. It then
    checks to see if the MIME type corresponds to one of the types
    appropriate for the file's extension.
    """
    # pylint: disable-msg=E1101
    subproc = subprocess.Popen(
        'file --mime-type {}'.format(get_tmp_path(uploaded_file.name)),
        shell=True, stdout=subprocess.PIPE)
    mime_type = subproc.stdout.read().strip().split(': ')[-1]
    file_extension = get_file_ext(uploaded_file.name)
    if file_extension == '.zip' and not mime_type in ZIP_MIME_TYPES:
        raise ValidationError(
            UploadFormErrors.MIME_TYPE.format('.zip', mime_type))
    elif file_extension == '.xml' and not mime_type in XML_MIME_TYPES:
        raise ValidationError(UploadFormErrors.MIME_TYPE.format(
                '.xml', mime_type))
Esempio n. 5
0
def validate_extension(uploaded_file):
    """
    Check if extension of uploaded file is listed in `ACCEPTED_FILE_TYPES`.
    """
    if not get_file_ext(uploaded_file.name.lower()) in ACCEPTED_FILE_TYPES:
        raise ValidationError(UploadFormErrors.EXTENSION)