<commit_before>from celery.utils.log import get_task_logger
from unidecode import unidecode
from celery.task import task
import zipfile
from couchexport.files import Temp
from couchexport.models import Format, ExportSchema
import tempfile
import os
from soil.util import expose_download
from couchexport.export import SchemaMismatchException, ExportConfiguration

logging = get_task_logger(__name__)

@task
def export_async(custom_export, download_id, format=None, filename=None, **kwargs):
    try:
        export_files = custom_export.get_export_files(format=format, process=export_async, **kwargs)
    except SchemaMismatchException, e:
        # fire off a delayed force update to prevent this from happening again
        rebuild_schemas.delay(custom_export.index)
        expiry = 10*60*60
        expose_download(
            "Sorry, the export failed for %s, please try again later" % custom_export._id,
            expiry,
            content_disposition="",
            mimetype="text/html",
            download_id=download_id
        ).save(expiry)
    else:
        if export_files.format is not None:
            format = export_files.format
        if not filename:
            filename = custom_export.name
        return cache_file_to_be_served(export_files.file, export_files.checkpoint, download_id, format, filename)

@task
def rebuild_schemas(index):
    """<commit_msg>don't crash on empty exports<commit_after>from celery.utils.log import get_task_logger
from unidecode import unidecode
from celery.task import task
import zipfile
from couchexport.files import Temp
from couchexport.models import Format, ExportSchema
import tempfile
import os
from soil.util import expose_download
from couchexport.export import SchemaMismatchException, ExportConfiguration

logging = get_task_logger(__name__)

@task
def export_async(custom_export, download_id, format=None, filename=None, **kwargs):
    try:
        export_files = custom_export.get_export_files(format=format, process=export_async, **kwargs)
    except SchemaMismatchException, e:
        # fire off a delayed force update to prevent this from happening again
        rebuild_schemas.delay(custom_export.index)
        expiry = 10*60*60
        expose_download(
            "Sorry, the export failed for %s, please try again later" % custom_export._id,
            expiry,
            content_disposition="",
            mimetype="text/html",
            download_id=download_id
        ).save(expiry)
    else:
        if export_files:
            if export_files.format is not None:
                format = export_files.format
            if not filename:
                filename = custom_export.name
            return cache_file_to_be_served(export_files.file, export_files.checkpoint, download_id, format, filename)
        else:
            return cache_file_to_be_served(None, None, download_id, format, filename)

@task
def rebuild_schemas(index):
    """
{'fraction_non_alphanumeric': 0.06764106050305914, 'fraction_numerical': 0.004078857919782461, 'mean_word_length': 3.353550295857988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4078512', 'n_tokens_mistral': 836, 'n_tokens_neox': 806, 'n_words': 227}