<commit_before>import os
from django.utils.safestring import mark_safe
from django.contrib.staticfiles import finders
from django.utils import six
if six.PY2:
    from urlparse import urljoin
elif six.PY3:
    from urllib.parse import urljoin
from .services import WebpackService
from .exceptions import ConfigNotFound
from .settings import BUNDLE_ROOT, BUNDLE_URL, CACHE

webpack_service = WebpackService()
cache = {}


def bundle(path_to_config):
    if os.path.isabs(path_to_config):
        if not os.path.exists(path_to_config):
            raise ConfigNotFound(path_to_config)
    else:
        absolute_path_to_config = finders.find(path_to_config)
        if absolute_path_to_config:
            path_to_config = absolute_path_to_config
        else:
            raise ConfigNotFound(path_to_config)

    if not os.path.exists(path_to_config):
        raise ConfigNotFound(path_to_config)

    if CACHE and cache.get(path_to_config, None):
        return cache[path_to_config]

    stats = webpack_service.bundle(path_to_config)

    asset_names = [asset['name'] for asset in stats['assets']]

    assets = []

    for asset_name in asset_names:
        assets.append({
            'name': asset_name,
            'path': os.path.join(BUNDLE_ROOT, asset_name)
        })

    if CACHE:
        cache[path_to_config] = assets

    return assets


class WebpackBundle(object):
    path_to_config = None
    assets = None

    def __init__(self, path_to_config):
        self.path_to_config = path_to_config

    def render(self):
        """
        Returns HTML script elements pointing to the assets generated by webpack
        """
        scripts = []
        for url in self.get_urls():
            scripts.append(
                '<script src="{url}"></script>'.format(url=url)
            )
        return mark_safe(''.join(scripts))

    def get_urls(self):
        """
        Returns a url to the bundle
        """
        urls = []
        for asset in self.get_assets():
            rel_path = asset['path'].split(BUNDLE_ROOT)[-1]
            if rel_path.startswith('/'):
                rel_path = rel_path[1:]
            url = urljoin(BUNDLE_URL, rel_path)
            urls.append(url)
        return urls

    def get_assets(self):
        if self.assets is None:
            self.assets = bundle(self.path_to_config)
        return self.assets<commit_msg>Ensuring that django will not silently fail when rendering a bundle.<commit_after>import sys
import os
from django.utils.safestring import mark_safe
from django.contrib.staticfiles import finders
from django.utils import six
if six.PY2:
    from urlparse import urljoin
elif six.PY3:
    from urllib.parse import urljoin
from .services import WebpackService
from .exceptions import ConfigNotFound, BundlingError
from .settings import BUNDLE_ROOT, BUNDLE_URL, CACHE

webpack_service = WebpackService()
cache = {}


def bundle(path_to_config):
    if os.path.isabs(path_to_config):
        if not os.path.exists(path_to_config):
            raise ConfigNotFound(path_to_config)
    else:
        absolute_path_to_config = finders.find(path_to_config)
        if absolute_path_to_config:
            path_to_config = absolute_path_to_config
        else:
            raise ConfigNotFound(path_to_config)

    if not os.path.exists(path_to_config):
        raise ConfigNotFound(path_to_config)

    if CACHE and cache.get(path_to_config, None):
        return cache[path_to_config]

    stats = webpack_service.bundle(path_to_config)

    asset_names = [asset['name'] for asset in stats['assets']]

    assets = []

    for asset_name in asset_names:
        assets.append({
            'name': asset_name,
            'path': os.path.join(BUNDLE_ROOT, asset_name)
        })

    if CACHE:
        cache[path_to_config] = assets

    return assets


class WebpackBundle(object):
    path_to_config = None
    assets = None

    def __init__(self, path_to_config):
        self.path_to_config = path_to_config

    def render(self):
        """
        Returns HTML script elements pointing to the assets generated by webpack
        """
        scripts = []
        # Ensure that all exceptions raise flags, even the ones that django will silently suppress
        try:
            urls = self.get_urls()
        except (TypeError, AttributeError) as e:
            six.reraise(BundlingError, BundlingError(*e.args), sys.exc_info()[2])
        for url in urls:
            scripts.append(
                '<script src="{url}"></script>'.format(url=url)
            )
        return mark_safe(''.join(scripts))

    def get_urls(self):
        """
        Returns a url to the bundle
        """
        urls = []
        for asset in self.get_assets():
            rel_path = asset['path'].split(BUNDLE_ROOT)[-1]
            if rel_path.startswith('/'):
                rel_path = rel_path[1:]
            url = urljoin(BUNDLE_URL, rel_path)
            urls.append(url)
        return urls

    def get_assets(self):
        if self.assets is None:
            self.assets = bundle(self.path_to_config)
        return self.assets
{'fraction_non_alphanumeric': 0.08773991382687035, 'fraction_numerical': 0.0017626321974148062, 'mean_word_length': 2.4670739986422268, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4477820', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1519, 'n_words': 373}