<commit_before>from __future__ import with_statement

import os
import sys
import time
import daemon
import shutil
import logging
import datetime
import tempfile
import lockfile
import subprocess

from django.conf import settings
from django.core.management.base import NoArgsCommand, make_option

from live_studio.build.models import Build
from live_studio.templatetags.text import command_line_options

def call(logfile, args):
    logfile.write('$ %s\n' % command_line_options(args))
    logfile.flush()
    subprocess.check_call(args, stdout=logfile, stderr=logfile)

class Command(NoArgsCommand):
    option_list = NoArgsCommand.option_list + (
        make_option('--pidfile', dest='pidfile', help="Pidfile", default=None),
    )

    def handle_noargs(self, **options):
        if not options['pidfile']:
            self.run(options)
            return

        pidfile = lockfile.FileLock(options['pidfile'])

        with daemon.DaemonContext(pidfile=pidfile):
            self.run(options)

    def run(self, options):
        logging.basicConfig(level=logging.INFO)
        self.log = logging.getLogger('live-studio-runner')

        if int(options['verbosity']) > 1:
            self.log.setLevel(logging.DEBUG)

        while True:
            try:
                build = Build.objects.pop()

                def update(**kwargs):
                    self.log.debug('Updating #%d with %r', build.pk, kwargs)
                    Build.objects.filter(pk=build.pk).update(**kwargs)

                update(started=datetime.datetime.utcnow())

                tempdir = tempfile.mkdtemp(prefix='live-studio_')
                target_dir = os.path.join(settings.BUILDS_ROOT, build.ident)

                os.makedirs(target_dir)
                logfile = open(os.path.join(target_dir, 'log.txt'), 'a')

                self.log.info("Building #%d in %s", build.pk, tempdir)

                try:
                    os.chdir(tempdir)

                    call(logfile, ('lh', 'config') + build.config.options())
                    open('binary.iso', 'w').write('iso here') #call(logfile, ('lh', 'build'))

                    # Find file that was created
                    filename = None
                    for extension in ('iso', 'img'):
                        if not os.path.exists('binary.%s' % extension):
                            continue

                        filename = '%s.%s' % (build.ident, extension)
                        os.rename(
                            'binary.%s' % extension,
                            os.path.join(target_dir, filename),
                        )

                        break

                    assert filename, "Did not create any image"

                    update(
                        finished=datetime.datetime.utcnow(),
                        filename=filename,
                    )

                    self.log.info("#%d built successfully", build.pk)
                except:<commit_msg>Actually try and build an image..<commit_after>from __future__ import with_statement

import os
import sys
import time
import daemon
import shutil
import logging
import datetime
import tempfile
import lockfile
import subprocess

from django.conf import settings
from django.core.management.base import NoArgsCommand, make_option

from live_studio.build.models import Build
from live_studio.templatetags.text import command_line_options

def call(logfile, args):
    logfile.write('$ %s\n' % command_line_options(args))
    logfile.flush()
    subprocess.check_call(args, stdout=logfile, stderr=logfile)

class Command(NoArgsCommand):
    option_list = NoArgsCommand.option_list + (
        make_option('--pidfile', dest='pidfile', help="Pidfile", default=None),
    )

    def handle_noargs(self, **options):
        if not options['pidfile']:
            self.run(options)
            return

        pidfile = lockfile.FileLock(options['pidfile'])

        with daemon.DaemonContext(pidfile=pidfile):
            self.run(options)

    def run(self, options):
        logging.basicConfig(level=logging.INFO)
        self.log = logging.getLogger('live-studio-runner')

        if int(options['verbosity']) > 1:
            self.log.setLevel(logging.DEBUG)

        while True:
            try:
                build = Build.objects.pop()

                def update(**kwargs):
                    self.log.debug('Updating #%d with %r', build.pk, kwargs)
                    Build.objects.filter(pk=build.pk).update(**kwargs)

                update(started=datetime.datetime.utcnow())

                tempdir = tempfile.mkdtemp(prefix='live-studio_')
                target_dir = os.path.join(settings.BUILDS_ROOT, build.ident)

                os.makedirs(target_dir)
                logfile = open(os.path.join(target_dir, 'log.txt'), 'a')

                self.log.info("Building #%d in %s", build.pk, tempdir)

                try:
                    os.chdir(tempdir)

                    call(logfile, ('lh', 'config') + build.config.options())

                    if settings.DEBUG:
                        open('binary.iso', 'w').write('iso here')
                    else:
                        call(logfile, ('lh', 'build'))

                    # Find file that was created
                    filename = None
                    for extension in ('iso', 'img'):
                        if not os.path.exists('binary.%s' % extension):
                            continue

                        filename = '%s.%s' % (build.ident, extension)
                        os.rename(
                            'binary.%s' % extension,
                            os.path.join(target_dir, filename),
                        )

                        break

                    assert filename, "Did not create any image"

                    update(
                        finished=datetime.datetime.utcnow(),
                        filename=filename,
                    )

                    self.log.info("#%d built successfully", build.pk)
                except:
{'fraction_non_alphanumeric': 0.10738476788369404, 'fraction_numerical': 0.00033041467041136624, 'mean_word_length': 1.6870838881491346, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '526107', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1564, 'n_words': 341}