Example #1
0
def get(req, db, user, path):
    import configuration

    extension_name, resource_path = path.split("/", 1)

    cursor = db.cursor()
    cursor.execute("""SELECT users.name, extensionversions.sha1
                        FROM extensions
                        JOIN extensioninstalls ON (extensioninstalls.extension=extensions.id)
             LEFT OUTER JOIN extensionversions ON (extensionversions.id=extensioninstalls.version)
             LEFT OUTER JOIN users ON (users.id=extensions.author)
                       WHERE extensions.name=%s
                         AND (extensioninstalls.uid=%s OR extensioninstalls.uid IS NULL)
                    ORDER BY extensioninstalls.uid ASC NULLS LAST
                       LIMIT 1""",
                   (extension_name, user.id))

    row = cursor.fetchone()

    if not row:
        return None, None

    author_name, version_sha1 = row

    if version_sha1 is None:
        extension_path = getExtensionPath(author_name, extension_name)
    else:
        extension_path = getExtensionInstallPath(version_sha1)

    resource_path = os.path.join(extension_path, "resources", resource_path)

    def guessContentType(name):
        extension_parts = name.split(".")[1:]
        while extension_parts:
            extension = ".".join(extension_parts)
            mimetype = configuration.mimetypes.MIMETYPES.get(extension)
            if mimetype:
                return mimetype
            extension_parts = extension_parts[1:]
        else:
            return "application/octet-stream"

    try:
        with open(resource_path) as resource_file:
            resource = resource_file.read()
    except IOError as error:
        if error.errno in (errno.ENOENT, errno.EACCES):
            return None, None
        raise
    else:
        return guessContentType(os.path.basename(resource_path)), resource
Example #2
0
def get(req, db, user, path):
    cursor = db.cursor()

    extension_name, resource_path = path.split("/", 1)

    cursor.execute(
        """SELECT extensions.author, extensionversions.sha1
                        FROM extensions
                        JOIN extensionversions ON (extensionversions.extension=extensions.id)
                        JOIN extensionroles ON (extensionroles.version=extensionversions.id)
                       WHERE extensions.name=%s
                         AND extensionroles.uid=%s
                       LIMIT 1""",
        (extension_name, user.id),
    )

    row = cursor.fetchone()
    if not row:
        return None, None

    author_id, sha1 = row

    if sha1 is None:
        author = dbutils.User.fromId(db, author_id)
        extension_path = getExtensionPath(author.name, extension_name)
    else:
        extension_path = getExtensionInstallPath(sha1)

    resource_path = os.path.join(extension_path, "resources", resource_path)

    def guessContentType(name):
        try:
            name, ext = name.split(".", 1)
            return configuration.mimetypes.MIMETYPES[ext]
        except:
            return "application/octet-stream"

    if os.path.isfile(resource_path) and os.access(resource_path, os.R_OK):
        return guessContentType(resource_path), open(resource_path).read()
    else:
        return None, None
Example #3
0
def execute(db, user_id, batch_id, output):
    cursor = db.cursor()
    cursor.execute("SELECT review, uid FROM batches WHERE id=%s", (batch_id,))

    review_id, batch_user_id = cursor.fetchone()

    cursor.execute("""SELECT extensions.id, extensions.author, extensions.name, extensionversions.sha1, roles.id, roles.script, roles.function
                        FROM extensions
                        JOIN extensionversions ON (extensionversions.extension=extensions.id)
                        JOIN extensionroles_processchanges AS roles ON (roles.version=extensionversions.id)
                       WHERE uid=%s AND roles.skip < %s""", (user_id, batch_id))

    rows = cursor.fetchall()

    if rows:
        for extension_id, author_id, extension_name, sha1, role_id, script, function in rows:
            cursor.execute("INSERT INTO extensionprocessedbatches (batch, role) VALUES (%s, %s)", (batch_id, role_id))

            # Don't do further processing of own batches.
            if batch_user_id == user_id: continue

            author = dbutils.User.fromId(db, author_id)

            if sha1 is None:
                extension_path = getExtensionPath(author.name, extension_name)
            else:
                extension_path = getExtensionInstallPath(sha1)

            class Error(Exception):
                pass

            def print_header():
                header = "%s::%s()" % (script, function)
                print >>output, "\n[%s] %s\n[%s] %s" % (extension_name, header, extension_name, "=" * len(header))

            try:
                try:
                    manifest = Manifest.load(extension_path)
                except ManifestError, error:
                    raise Error("Invalid MANIFEST:\n%s" % error.message)

                for role in manifest.roles:
                    if isinstance(role, ProcessChangesRole) and role.script == script and role.function == function:
                        break
                else:
                    continue

                argv = "[(new critic.Review(%d)).getBatch(%d)]" % (review_id, batch_id)

                try:
                    stdout_data = executeProcess(manifest, role, extension_id, user_id, argv, configuration.extensions.SHORT_TIMEOUT)
                except ProcessTimeout:
                    raise Error("Timeout after %d seconds." % configuration.extensions.SHORT_TIMEOUT)
                except ProcessError, error:
                    if error.returncode < 0:
                        if -error.returncode == signal.SIGXCPU:
                            raise Error("CPU time limit (5 seconds) exceeded.")
                        else:
                            raise Error("Process terminated by signal %d." % -error.returncode)
                    else:
                        raise Error("Process returned %d.\n%s" % (error.returncode, error.stderr))

                if stdout_data.strip():
                    print_header()
                    for line in stdout_data.splitlines():
                        print >>output, "[%s] %s" % (extension_name, line)
            except Error, error:
                print_header()
                print >>output, "[%s] Extension error: %s" % (extension_name, error.message)
Example #4
0
def execute(db, user, review, all_commits, old_head, new_head, output):
    cursor = db.cursor()
    cursor.execute("""SELECT extensions.id, extensions.author, extensions.name, extensionversions.sha1, roles.script, roles.function
                        FROM extensions
                        JOIN extensionversions ON (extensionversions.extension=extensions.id)
                        JOIN extensionroles_processcommits AS roles ON (roles.version=extensionversions.id)
                       WHERE uid=%s
                         AND filter IS NULL""", (user.id,))

    rows = cursor.fetchall()

    if rows:
        commitset = log.commitset.CommitSet(all_commits)

        assert old_head is None or old_head in commitset.getTails()
        assert new_head in commitset.getHeads()
        assert len(commitset.getHeads()) == 1

        tails = commitset.getFilteredTails(review.repository)
        if len(tails) == 1:
            tail = gitutils.Commit.fromSHA1(db, review.repository, tails.pop())
            changeset_id = changeset.utils.createChangeset(db, user, review.repository, from_commit=tail, to_commit=new_head)[0].id
            changeset_arg = "repository.getChangeset(%d)" % changeset_id
            db.commit()
        else:
            changeset_arg = "null"

        commits = "[%s]" % ",".join([("repository.getCommit(%d)" % commit.getId(db)) for commit in all_commits])

        data = { "review_id": review.id,
                 "changeset": changeset_arg,
                 "commits": commits }

        for extension_id, author_id, extension_name, sha1, script, function in rows:
            author = dbutils.User.fromId(db, author_id)

            if sha1 is None: extension_path = getExtensionPath(author.name, extension_name)
            else: extension_path = getExtensionInstallPath(sha1)

            class Error(Exception):
                pass

            def print_header():
                header = "%s::%s()" % (script, function)
                print >>output, "\n[%s] %s\n[%s] %s" % (extension_name, header, extension_name, "=" * len(header))

            try:
                try:
                    manifest = Manifest.load(extension_path)
                except ManifestError, error:
                    raise Error("Invalid MANIFEST:\n%s" % error.message)

                for role in manifest.roles:
                    if isinstance(role, ProcessCommitsRole) and role.script == script and role.function == function:
                        break
                else:
                    continue

                argv = """

(function ()
 {
   var review = new critic.Review(%(review_id)d);
   var repository = review.repository;
   var changeset = %(changeset)s;
   var commitset = new critic.CommitSet(%(commits)s);

   return [review, changeset, commitset];
 })()

""" % data
                argv = re.sub("[ \n]+", " ", argv.strip())

                try:
                    stdout_data = executeProcess(manifest, role, extension_id, user.id, argv, configuration.extensions.SHORT_TIMEOUT)
                except ProcessTimeout:
                    raise Error("Timeout after %d seconds." % configuration.extensions.SHORT_TIMEOUT)
                except ProcessError, error:
                    if error.returncode < 0:
                        if -error.returncode == signal.SIGXCPU:
                            raise Error("CPU time limit (5 seconds) exceeded.")
                        else:
                            raise Error("Process terminated by signal %d." % -error.returncode)
                    else:
                        raise Error("Process returned %d.\n%s" % (error.returncode, error.stderr))

                if stdout_data.strip():
                    print_header()
                    for line in stdout_data.splitlines():
                        print >>output, "[%s] %s" % (extension_name, line)
            except Error, error:
                print_header()
                print >>output, "[%s] Extension error: %s" % (extension_name, error.message)