<commit_before># Copyright (c) 2005-2007 rPath, Inc
# All rights reserved

import cherrypy
import turbogears

import os
import re
import time

from mint import loadmirror
from mint import config

from raa.modules.raawebplugin import rAAWebPlugin
from raa.modules.raawebplugin import immedTask
from raa.localhostonly import localhostOnly
from raa.db.database import DatabaseTable, writeOp, readOp

class LoadMirrorTable(DatabaseTable):
    name = 'plugin_rpath_LoadMirrorTable'
    createSQL = """CREATE TABLE %s
                   (
                        schedId INT UNIQUE NOT NULL,
                        command VARCHAR(255),
                        error VARCHAR(255),
                        done INT
                   )""" % (name)
    fields = ['schedId', 'command', 'error', 'done']
    tableVersion = 1

    @writeOp
    def setCommand(self, cu, schedId, command, done = False, error = ''):
        self.db.transaction()
        cu.execute("DELETE FROM %s" % self.name)
        cu.execute("INSERT INTO %s (schedId, command, error, done) VALUES (?, ?, ?, ?)" % \
            self.name, schedId, command, error, str(int(done)))
        return True

    @readOp
    def getCommand(self, cu, schedId):
        cu.execute("""SELECT command, done FROM %s WHERE schedId=?""" % (self.name), schedId)
        command = cu.fetchone()
        if not command:
            return '', False
        else:
            return command[0], command[1]

    @readOp
    def getError(self, cu, schedId):
        cu.execute("""SELECT error FROM %s WHERE schedId=?""" % (self.name), schedId)
        command = cu.fetchone()
        if not command:
            return ''
        else:
            return command[0]


class LoadMirror(rAAWebPlugin):
    displayName = _("Pre-load Mirrored Repositories")

    tableClass = LoadMirrorTable
    preloadLogPath = '/srv/rbuilder/logs/load-mirror.log'

    @turbogears.expose(html="rPath.loadmirror.index")
    @turbogears.identity.require(turbogears.identity.not_anonymous())
    def index(self):
        schedId = self._setCommand("mount")

        return dict(schedId = schedId['schedId'])

    @immedTask
    def _setCommand(self, command, done = False, error = ''):
        def callback(schedId):
            self.table.setCommand(schedId, command, done, error)

        return dict(callback = callback)

    @turbogears.expose(allow_json=True)
    @turbogears.identity.require(turbogears.identity.not_anonymous())
    def callGetPreloads(self, schedId):
        _, done = self.table.getCommand(schedId)
        errors = self.table.getError(schedId)

        if done:
            projects, preloadErrors = self._getProjects()
        else:
            projects = []
            preloadErrors = {}
            done = False<commit_msg>check for a running task on the backend before calling _setCommand<commit_after># Copyright (c) 2005-2007 rPath, Inc
# All rights reserved

import cherrypy
import turbogears

import os
import re
import time

from mint import loadmirror
from mint import config

from raa.db import schedule
from raa.modules.raawebplugin import rAAWebPlugin
from raa.modules.raawebplugin import immedTask
from raa.localhostonly import localhostOnly
from raa.db.database import DatabaseTable, writeOp, readOp

class LoadMirrorTable(DatabaseTable):
    name = 'plugin_rpath_LoadMirrorTable'
    createSQL = """CREATE TABLE %s
                   (
                        schedId INT UNIQUE NOT NULL,
                        command VARCHAR(255),
                        error VARCHAR(255),
                        done INT
                   )""" % (name)
    fields = ['schedId', 'command', 'error', 'done']
    tableVersion = 1

    @writeOp
    def setCommand(self, cu, schedId, command, done = False, error = ''):
        self.db.transaction()
        cu.execute("DELETE FROM %s" % self.name)
        cu.execute("INSERT INTO %s (schedId, command, error, done) VALUES (?, ?, ?, ?)" % \
            self.name, schedId, command, error, str(int(done)))
        return True

    @readOp
    def getCommand(self, cu, schedId):
        cu.execute("""SELECT command, done FROM %s WHERE schedId=?""" % (self.name), schedId)
        command = cu.fetchone()
        if not command:
            return '', False
        else:
            return command[0], command[1]

    @readOp
    def getError(self, cu, schedId):
        cu.execute("""SELECT error FROM %s WHERE schedId=?""" % (self.name), schedId)
        command = cu.fetchone()
        if not command:
            return ''
        else:
            return command[0]


class LoadMirror(rAAWebPlugin):
    displayName = _("Pre-load Mirrored Repositories")

    tableClass = LoadMirrorTable
    preloadLogPath = '/srv/rbuilder/logs/load-mirror.log'

    @turbogears.expose(html="rPath.loadmirror.index")
    @turbogears.identity.require(turbogears.identity.not_anonymous())
    def index(self):
        sids = cherrypy.root.execution.getUnfinishedSchedules(types=schedule.typesValid, taskId=self.taskId)
        if not sids:
            schedId = self._setCommand("mount")
            schedId = schedId['schedId']
            inProgress = False
        else:
            schedId = sids[0]
            inProgress = True
        return dict(schedId = schedId, inProgress=inProgress)


    @immedTask
    def _setCommand(self, command, done = False, error = ''):
        def callback(schedId):
            self.table.setCommand(schedId, command, done, error)

        return dict(callback = callback)

    @turbogears.expose(allow_json=True)
    @turbogears.identity.require(turbogears.identity.not_anonymous())
    def callGetPreloads(self, schedId):
        _, done = self.table.getCommand(schedId)
        errors = self.table.getError(schedId)
        if done:
            projects, preloadErrors = self._getProjects()
        else:
            projects = []
            preloadErrors = {}
            done = False
{'fraction_non_alphanumeric': 0.10723951285520975, 'fraction_numerical': 0.006258457374830852, 'mean_word_length': 2.419895893580104, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6430376', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1696, 'n_words': 433}