Example #1
0
def slave():
    import StringIO
    import traceback

    import index

    def reject(message):
        sys_stdout.write(json_encode({ "status": "reject", "message": message }))
        sys.exit(0)

    def error(message):
        sys_stdout.write(json_encode({ "status": "error", "error": message }))
        sys.exit(0)

    try:
        data = sys.stdin.read()
        request = json_decode(data)

        create_branches = []
        delete_branches = []
        update_branches = []

        create_tags = []
        delete_tags = []
        update_tags = []

        user_name = request["user_name"]
        repository_name = request["repository_name"]

        if request["flags"] and user_name == configuration.base.SYSTEM_USER_NAME:
            flags = dict(flag.split("=", 1) for flag in request["flags"].split(","))
        else:
            flags = {}

        sys.stdout = StringIO.StringIO()

        index.init()

        commits_to_process = set()

        for ref in request["refs"]:
            name = ref["name"]
            old_sha1 = ref["old_sha1"]
            new_sha1 = ref["new_sha1"]

            if "//" in name:
                reject("invalid ref name: '%s'" % name)
            if not name.startswith("refs/"):
                reject("unexpected ref name: '%s'" % name)

            if new_sha1 != '0000000000000000000000000000000000000000':
                commits_to_process.add(new_sha1)

            name = name[len("refs/"):]

            if name.startswith("heads/"):
                name = name[len("heads/"):]
                if new_sha1 == '0000000000000000000000000000000000000000':
                    delete_branches.append((name, old_sha1))
                elif old_sha1 == '0000000000000000000000000000000000000000':
                    create_branches.append((name, new_sha1))
                else:
                    update_branches.append((name, old_sha1, new_sha1))
            elif name.startswith("tags/"):
                name = name[len("tags/"):]
                if old_sha1 == '0000000000000000000000000000000000000000':
                    create_tags.append((name, new_sha1))
                elif new_sha1 == '0000000000000000000000000000000000000000':
                    delete_tags.append(name)
                else:
                    update_tags.append((name, old_sha1, new_sha1))
            elif name.startswith("temporary/") or name.startswith("keepalive/"):
                # len("temporary/") == len("keepalive/")
                name = name[len("temporary/"):]
                if name != new_sha1:
                    reject("invalid update of '%s'; value is not %s" % (ref["name"], name))
            else:
                reject("unexpected ref name: '%s'" % ref["name"])

        multiple = (len(delete_branches) + len(update_branches) + len(create_branches) + len(delete_tags) + len(update_tags) + len(create_tags)) > 1
        info = []

        for sha1 in commits_to_process:
            index.processCommits(repository_name, sha1)

        for name, old in delete_branches:
            index.deleteBranch(user_name, repository_name, name, old)
            info.append("branch deleted: %s" % name)

        for name, old, new in update_branches:
            index.updateBranch(user_name, repository_name, name, old, new, multiple, flags)
            info.append("branch updated: %s (%s..%s)" % (name, old[:8], new[:8]))

        index.createBranches(user_name, repository_name, create_branches, flags)

        for name, new in create_branches:
            info.append("branch created: %s (%s)" % (name, new[:8]))

        for name in delete_tags:
            index.deleteTag(repository_name, name)
            info.append("tag deleted: %s" % name)

        for name, old, new in update_tags:
            index.updateTag(repository_name, name, old, new)
            info.append("tag updated: %s (%s..%s)" % (name, old[:8], new[:8]))

        for name, new in create_tags:
            index.createTag(repository_name, name, new)
            info.append("tag created: %s (%s)" % (name, new[:8]))

        sys_stdout.write(json_encode({ "status": "ok", "accept": True, "output": sys.stdout.getvalue(), "info": info }))

        index.finish()
    except index.IndexException as exception:
        sys_stdout.write(json_encode({ "status": "ok", "accept": False, "output": exception.message, "info": info }))
    except SystemExit:
        raise
    except:
        exception = traceback.format_exc()
        message = """\
%s

Request:
%s

%s""" % (exception.splitlines()[-1], json_encode(request, indent=2), traceback.format_exc())

        sys_stdout.write(json_encode({ "status": "error", "error": message }))
    finally:
        index.abort()
Example #2
0
            multiple = (len(delete_branches) + len(update_branches) +
                        len(create_branches) + len(delete_tags) +
                        len(update_tags) + len(create_tags)) > 1
            info = []

            for name in delete_branches:
                index.deleteBranch(repository_name, name)
                info.append("branch deleted: %s" % name)

            for name, old, new in update_branches:
                index.updateBranch(user_name, repository_name, name, old, new,
                                   multiple)
                info.append("branch updated: %s (%s..%s)" %
                            (name, old[:8], new[:8]))

            index.createBranches(user_name, repository_name, create_branches)

            for name, new in create_branches:
                info.append("branch created: %s (%s)" % (name, new[:8]))

            for name in delete_tags:
                index.deleteTag(repository_name, name)
                info.append("tag deleted: %s" % name)

            for name, old, new in update_tags:
                index.updateTag(repository_name, name, old, new)
                info.append("tag updated: %s (%s..%s)" %
                            (name, old[:8], new[:8]))

            for name, new in create_tags:
                index.createTag(repository_name, name, new)
Example #3
0
def slave():
    import StringIO
    import traceback

    import index

    def reject(message):
        sys_stdout.write(json_encode({"status": "reject", "message": message}))
        sys.exit(0)

    def error(message):
        sys_stdout.write(json_encode({"status": "error", "error": message}))
        sys.exit(0)

    try:
        data = sys.stdin.read()
        request = json_decode(data)

        create_branches = []
        delete_branches = []
        update_branches = []

        create_tags = []
        delete_tags = []
        update_tags = []

        user_name = request["user_name"]
        repository_name = request["repository_name"]

        if request[
                "flags"] and user_name == configuration.base.SYSTEM_USER_NAME:
            flags = dict(
                flag.split("=", 1) for flag in request["flags"].split(","))
        else:
            flags = {}

        sys.stdout = StringIO.StringIO()

        index.init()

        commits_to_process = set()

        for ref in request["refs"]:
            name = ref["name"]
            old_sha1 = ref["old_sha1"]
            new_sha1 = ref["new_sha1"]

            if "//" in name:
                reject("invalid ref name: '%s'" % name)
            if not name.startswith("refs/"):
                reject("unexpected ref name: '%s'" % name)

            if new_sha1 != '0000000000000000000000000000000000000000':
                commits_to_process.add(new_sha1)

            name = name[len("refs/"):]

            if name.startswith("heads/"):
                name = name[len("heads/"):]
                if new_sha1 == '0000000000000000000000000000000000000000':
                    delete_branches.append((name, old_sha1))
                elif old_sha1 == '0000000000000000000000000000000000000000':
                    create_branches.append((name, new_sha1))
                else:
                    update_branches.append((name, old_sha1, new_sha1))
            elif name.startswith("tags/"):
                name = name[len("tags/"):]
                if old_sha1 == '0000000000000000000000000000000000000000':
                    create_tags.append((name, new_sha1))
                elif new_sha1 == '0000000000000000000000000000000000000000':
                    delete_tags.append(name)
                else:
                    update_tags.append((name, old_sha1, new_sha1))
            elif name.startswith("temporary/") or name.startswith(
                    "keepalive/"):
                # len("temporary/") == len("keepalive/")
                name = name[len("temporary/"):]
                if name != new_sha1:
                    reject("invalid update of '%s'; value is not %s" %
                           (ref["name"], name))
            else:
                reject("unexpected ref name: '%s'" % ref["name"])

        multiple = (len(delete_branches) + len(update_branches) +
                    len(create_branches) + len(delete_tags) +
                    len(update_tags) + len(create_tags)) > 1
        info = []

        for sha1 in commits_to_process:
            index.processCommits(repository_name, sha1)

        for name, old in delete_branches:
            index.deleteBranch(user_name, repository_name, name, old)
            info.append("branch deleted: %s" % name)

        for name, old, new in update_branches:
            index.updateBranch(user_name, repository_name, name, old, new,
                               multiple, flags)
            info.append("branch updated: %s (%s..%s)" %
                        (name, old[:8], new[:8]))

        index.createBranches(user_name, repository_name, create_branches,
                             flags)

        for name, new in create_branches:
            info.append("branch created: %s (%s)" % (name, new[:8]))

        for name in delete_tags:
            index.deleteTag(repository_name, name)
            info.append("tag deleted: %s" % name)

        for name, old, new in update_tags:
            index.updateTag(repository_name, name, old, new)
            info.append("tag updated: %s (%s..%s)" % (name, old[:8], new[:8]))

        for name, new in create_tags:
            index.createTag(repository_name, name, new)
            info.append("tag created: %s (%s)" % (name, new[:8]))

        sys_stdout.write(
            json_encode({
                "status": "ok",
                "accept": True,
                "output": sys.stdout.getvalue(),
                "info": info
            }))

        index.finish()
    except index.IndexException as exception:
        sys_stdout.write(
            json_encode({
                "status": "ok",
                "accept": False,
                "output": exception.message,
                "info": info
            }))
    except SystemExit:
        raise
    except:
        exception = traceback.format_exc()
        message = """\
%s

Request:
%s

%s""" % (exception.splitlines()[-1], json_encode(
            request, indent=2), traceback.format_exc())

        sys_stdout.write(json_encode({"status": "error", "error": message}))
    finally:
        index.abort()
Example #4
0
def slave():
    import StringIO
    import traceback

    import dbutils
    import gitutils
    import index

    def reject(message):
        sys_stdout.write(json_encode({ "status": "reject", "message": message }))
        sys.exit(0)

    def error(message):
        sys_stdout.write(json_encode({ "status": "error", "error": message }))
        sys.exit(0)

    db = dbutils.Database.forUser()

    try:
        data = sys.stdin.read()
        request = json_decode(data)

        create_branches = []
        delete_branches = []
        update_branches = []

        create_tags = []
        delete_tags = []
        update_tags = []

        user = getUser(db, request["user_name"])
        authentication_labels = auth.DATABASE.getAuthenticationLabels(user)

        db.setUser(user, authentication_labels)

        try:
            repository = gitutils.Repository.fromName(
                db, request["repository_name"], for_modify=True)
        except auth.AccessDenied as error:
            reject(error.message)

        if request["flags"] and user.isSystem():
            flags = dict(flag.split("=", 1) for flag in request["flags"].split(","))
        else:
            flags = {}

        sys.stdout = StringIO.StringIO()

        commits_to_process = set()

        for ref in request["refs"]:
            name = ref["name"]
            old_sha1 = ref["old_sha1"]
            new_sha1 = ref["new_sha1"]

            if "//" in name:
                reject("invalid ref name: '%s'" % name)
            if not name.startswith("refs/"):
                reject("unexpected ref name: '%s'" % name)

            if new_sha1 != '0000000000000000000000000000000000000000':
                commits_to_process.add(new_sha1)

            name = name[len("refs/"):]

            if name.startswith("heads/"):
                name = name[len("heads/"):]
                if new_sha1 == '0000000000000000000000000000000000000000':
                    delete_branches.append((name, old_sha1))
                elif old_sha1 == '0000000000000000000000000000000000000000':
                    create_branches.append((name, new_sha1))
                else:
                    update_branches.append((name, old_sha1, new_sha1))
            elif name.startswith("tags/"):
                name = name[len("tags/"):]
                if old_sha1 == '0000000000000000000000000000000000000000':
                    create_tags.append((name, new_sha1))
                elif new_sha1 == '0000000000000000000000000000000000000000':
                    delete_tags.append(name)
                else:
                    update_tags.append((name, old_sha1, new_sha1))
            elif name.startswith("temporary/") or name.startswith("keepalive/"):
                # len("temporary/") == len("keepalive/")
                name = name[len("temporary/"):]
                if name != new_sha1:
                    reject("invalid update of '%s'; value is not %s" % (ref["name"], name))
            else:
                reject("unexpected ref name: '%s'" % ref["name"])

        multiple = (len(delete_branches) + len(update_branches) + len(create_branches) + len(delete_tags) + len(update_tags) + len(create_tags)) > 1
        info = []

        for sha1 in commits_to_process:
            index.processCommits(db, repository, sha1)

        for name, old in delete_branches:
            index.deleteBranch(db, user, repository, name, old)
            info.append("branch deleted: %s" % name)

        for name, old, new in update_branches:
            index.updateBranch(db, user, repository, name, old, new, multiple, flags)
            info.append("branch updated: %s (%s..%s)" % (name, old[:8], new[:8]))

        index.createBranches(db, user, repository, create_branches, flags)

        for name, new in create_branches:
            info.append("branch created: %s (%s)" % (name, new[:8]))

        for name in delete_tags:
            index.deleteTag(db, user, repository, name)
            info.append("tag deleted: %s" % name)

        for name, old, new in update_tags:
            index.updateTag(db, user, repository, name, old, new)
            info.append("tag updated: %s (%s..%s)" % (name, old[:8], new[:8]))

        for name, new in create_tags:
            index.createTag(db, user, repository, name, new)
            info.append("tag created: %s (%s)" % (name, new[:8]))

        sys_stdout.write(json_encode({ "status": "ok", "accept": True, "output": sys.stdout.getvalue(), "info": info }))

        db.commit()
    except index.IndexException as exception:
        sys_stdout.write(json_encode({ "status": "ok", "accept": False, "output": exception.message, "info": info }))
    except SystemExit:
        raise
    except:
        exception = traceback.format_exc()
        message = """\
%s

Request:
%s

%s""" % (exception.splitlines()[-1], json_encode(request, indent=2), traceback.format_exc())

        sys_stdout.write(json_encode({ "status": "error", "error": message }))
    finally:
        db.close()
Example #5
0
                        index.processCommits(repository_name, new_sha1)
                else:
                    reject("unexpected ref name: '%s'" % ref["name"])

            multiple = (len(delete_branches) + len(update_branches) + len(create_branches) + len(delete_tags) + len(update_tags) + len(create_tags)) > 1
            info = []

            for name in delete_branches:
                index.deleteBranch(repository_name, name)
                info.append("branch deleted: %s" % name)

            for name, old, new in update_branches:
                index.updateBranch(user_name, repository_name, name, old, new, multiple)
                info.append("branch updated: %s (%s..%s)" % (name, old[:8], new[:8]))

            index.createBranches(user_name, repository_name, create_branches)

            for name, new in create_branches:
                info.append("branch created: %s (%s)" % (name, new[:8]))

            for name in delete_tags:
                index.deleteTag(repository_name, name)
                info.append("tag deleted: %s" % name)

            for name, old, new in update_tags:
                index.updateTag(repository_name, name, old, new)
                info.append("tag updated: %s (%s..%s)" % (name, old[:8], new[:8]))

            for name, new in create_tags:
                index.createTag(repository_name, name, new)
                info.append("tag created: %s (%s)" % (name, new[:8]))