<commit_before># -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals

from xml.etree import cElementTree as ET

from .utils import create_unique_id, create_software_id
from .utils import create_sha256_hash, create_sha384_hash, create_sha512_hash

import ntpath
import os


ROLE = 'tagCreator'
VERSION_SCHEME = 'alphanumeric'
XMLNS = 'http://standards.iso.org/iso/19770/-2/2015/schema.xsd'
XML_DECLARATION = '<?xml version="1.0" encoding="utf-8"?>'
N8060 = 'http://csrc.nist.gov/schema/swid/2015-extensions/swid-2015-extensions-1.0.xsd'


def _create_payload_tag(package_info, hash_algorithms):
    payload = ET.Element('Payload')
    last_full_pathname = ""
    last_directory_tag = ""

    for file_info in package_info.files:

        head, file_name = ntpath.split(file_info.full_pathname)
        root, folder_name = ntpath.split(head)

        full_pathname = root + folder_name

        if last_full_pathname == full_pathname:
            file_tag = ET.SubElement(last_directory_tag, 'File')
            file_tag.set('name', file_name)
        else:
            directory_tag = ET.SubElement(payload, 'Directory')
            directory_tag.set('root', root)
            directory_tag.set('name', folder_name)
            file_tag = ET.SubElement(directory_tag, 'File')
            file_tag.set('name', file_name)
            last_full_pathname = full_pathname
            last_directory_tag = directory_tag

        if file_info.mutable:
            file_tag.set('mutable', "True")

        full_path = file_info.location + '/' + file_info.name

        file_element.set('size', str(os.path.getsize(full_path)))

        if 'sha256' in hash_algorithms:
            file_element.set('SHA256:hash', create_sha256_hash(full_path))
        if 'sha384' in hash_algorithms:
            file_element.set('SHA384:hash', create_sha384_hash(full_path))
        if 'sha512' in hash_algorithms:
            file_element.set('SHA512:hash', create_sha512_hash(full_path))

    return payload


def all_matcher(ctx):
    return True


def package_name_matcher(ctx, value):
    return ctx['package_info'].package == value


def software_id_matcher(ctx, value):
    env = ctx['environment']
    os_string = env.get_os_string()
    architecture = env.get_architecture()
    unique_id = create_unique_id(ctx['package_info'], os_string, architecture)
    software_id = create_software_id(ctx['regid'], unique_id)
    return software_id == value


def create_swid_tags(environment, entity_name, regid, hash_algorithms, full=False, matcher=all_matcher):
    """
    Return SWID tags as utf8-encoded xml bytestrings for all available<commit_msg>Fixed merge error (adding of hashes)<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals

from xml.etree import cElementTree as ET

from .utils import create_unique_id, create_software_id
from .utils import create_sha256_hash, create_sha384_hash, create_sha512_hash

import ntpath
import os


ROLE = 'tagCreator'
VERSION_SCHEME = 'alphanumeric'
XMLNS = 'http://standards.iso.org/iso/19770/-2/2015/schema.xsd'
XML_DECLARATION = '<?xml version="1.0" encoding="utf-8"?>'
N8060 = 'http://csrc.nist.gov/schema/swid/2015-extensions/swid-2015-extensions-1.0.xsd'


def _create_payload_tag(package_info, hash_algorithms):
    payload = ET.Element('Payload')
    last_full_pathname = ""
    last_directory_tag = ""

    for file_info in package_info.files:

        head, file_name = ntpath.split(file_info.full_pathname)
        root, folder_name = ntpath.split(head)

        full_pathname = root + folder_name

        if last_full_pathname == full_pathname:
            file_tag = ET.SubElement(last_directory_tag, 'File')
            file_tag.set('name', file_name)
        else:
            directory_tag = ET.SubElement(payload, 'Directory')
            directory_tag.set('root', root)
            directory_tag.set('name', folder_name)
            file_tag = ET.SubElement(directory_tag, 'File')
            file_tag.set('name', file_name)
            last_full_pathname = full_pathname
            last_directory_tag = directory_tag

        if file_info.mutable:
            file_tag.set('mutable', "True")

        file_tag.set('size', str(os.path.getsize(file_info.full_pathname)))

        if 'sha256' in hash_algorithms:
            file_tag.set('SHA256:hash', create_sha256_hash(file_info.full_pathname))
        if 'sha384' in hash_algorithms:
            file_tag.set('SHA384:hash', create_sha384_hash(file_info.full_pathname))
        if 'sha512' in hash_algorithms:
            file_tag.set('SHA512:hash', create_sha512_hash(file_info.full_pathname))

    return payload


def all_matcher(ctx):
    return True


def package_name_matcher(ctx, value):
    return ctx['package_info'].package == value


def software_id_matcher(ctx, value):
    env = ctx['environment']
    os_string = env.get_os_string()
    architecture = env.get_architecture()
    unique_id = create_unique_id(ctx['package_info'], os_string, architecture)
    software_id = create_software_id(ctx['regid'], unique_id)
    return software_id == value


def create_swid_tags(environment, entity_name, regid, hash_algorithms, full=False, matcher=all_matcher):
    """
    Return SWID tags as utf8-encoded xml bytestrings for all available
{'fraction_non_alphanumeric': 0.10667661320402835, 'fraction_numerical': 0.02424468481909735, 'mean_word_length': 3.6113499570077385, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 2, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6878122', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1709, 'n_words': 327}