# Path: tar.py
# def bz2compress(data):
#     compressor = BZ2Compressor(9)
#     return compressor.compress(data) + compressor.flush()


import os
import sys
import api
import bugs
import json
import pypi
import time
import urls
import shlex
import utils
import views
import shutil
import github
import socket
import metacpan
import analytics
import bitbucket
import xmlrpclib
import subprocess
import mavencentral
import distutils.dir_util
import distutils.file_util
from models import *
from time import sleep
from decorators import *
from tar import bz2compress
from tempfile import mkdtemp
from celery.task import task
from traceback import format_tb
from django.conf import settings
from django.core.cache import cache
from django.core import cache as cache_module
from django.db import reset_queries
from django.shortcuts import render
from django.utils.html import escape
from django.db.models import Count, Q
from django.core.mail import send_mail
from htmlmin.minify import html_minify
from BeautifulSoup import BeautifulSoup
from datetime import datetime, timedelta
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.core.files.base import ContentFile
from templated_email import send_templated_mail
from django.core.handlers.wsgi import WSGIRequest
from django.contrib.auth.models import AnonymousUser
from django.core.files.storage import default_storage
from django.core.paginator import Paginator, InvalidPage, EmptyPage
            if not adv.old.download_url:
                pv_find_download_url(adv.old.pk, PACKAGE_CALLBACKS[adv.old.package.type]['get_download_url'])

            if not adv.new.released_on:
                pv_find_date(adv.new.pk, PACKAGE_CALLBACKS[adv.new.package.type]['find_date'])

            if (not adv.new.scmid) or (adv.new.scmid == utils.TAG_NOT_FOUND):
                pv_find_tags(adv.new.pk, False) # no recursion, only this version

            if not adv.new.download_url:
                pv_find_download_url(adv.new.pk, PACKAGE_CALLBACKS[adv.new.package.type]['get_download_url'])

            # found what we could, now generate analytics

            if (adv.old.status == STATUS_VERIFIED) and (adv.new.status == STATUS_VERIFIED):
                generate_advisory_files.delay(adv.pk)
            elif (adv.last_updated <= last_time) and  (STATUS_NEW not in [adv.old.status, adv.new.status]):
                generate_advisory_files.delay(adv.pk, override=True)

        except:
            logger.error("Exception: %s" % sys.exc_info()[1])
            logger.error(format_tb(sys.exc_info()[2]))
            continue

    reset_queries()


def _compress_data_if_needed(data, path, field_name):
    if len(data) > utils.MYSQL_MAX_PACKET_SIZE:
        filename = '%s%s.txt.bz2' % (path, field_name)

# Path: tar.py
# def bz2compress(data):
#     compressor = BZ2Compressor(9)
#     return compressor.compress(data) + compressor.flush()


# Path: tasks.py
import os
import sys
import api
import bugs
import json
import pypi
import time
import urls
import shlex
import utils
import views
import shutil
import github
import socket
import metacpan
import analytics
import bitbucket
import xmlrpclib
import subprocess
import mavencentral
import distutils.dir_util
import distutils.file_util
from models import *
from time import sleep
from decorators import *
from tar import bz2compress
from tempfile import mkdtemp
from celery.task import task
from traceback import format_tb
from django.conf import settings
from django.core.cache import cache
from django.core import cache as cache_module
from django.db import reset_queries
from django.shortcuts import render
from django.utils.html import escape
from django.db.models import Count, Q
from django.core.mail import send_mail
from htmlmin.minify import html_minify
from BeautifulSoup import BeautifulSoup
from datetime import datetime, timedelta
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.core.files.base import ContentFile
from templated_email import send_templated_mail
from django.core.handlers.wsgi import WSGIRequest
from django.contrib.auth.models import AnonymousUser
from django.core.files.storage import default_storage
from django.core.paginator import Paginator, InvalidPage, EmptyPage

            if not adv.old.download_url:
                pv_find_download_url(adv.old.pk, PACKAGE_CALLBACKS[adv.old.package.type]['get_download_url'])

            if not adv.new.released_on:
                pv_find_date(adv.new.pk, PACKAGE_CALLBACKS[adv.new.package.type]['find_date'])

            if (not adv.new.scmid) or (adv.new.scmid == utils.TAG_NOT_FOUND):
                pv_find_tags(adv.new.pk, False) # no recursion, only this version

            if not adv.new.download_url:
                pv_find_download_url(adv.new.pk, PACKAGE_CALLBACKS[adv.new.package.type]['get_download_url'])

            # found what we could, now generate analytics

            if (adv.old.status == STATUS_VERIFIED) and (adv.new.status == STATUS_VERIFIED):
                generate_advisory_files.delay(adv.pk)
            elif (adv.last_updated <= last_time) and  (STATUS_NEW not in [adv.old.status, adv.new.status]):
                generate_advisory_files.delay(adv.pk, override=True)

        except:
            logger.error("Exception: %s" % sys.exc_info()[1])
            logger.error(format_tb(sys.exc_info()[2]))
            continue

    reset_queries()


def _compress_data_if_needed(data, path, field_name):
    if len(data) > utils.MYSQL_MAX_PACKET_SIZE:
        filename = '%s%s.txt.bz2' % (path, field_name)

        _create_file(filename, bz2compress(data))