Exemplo n.º 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()
Exemplo n.º 2
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()
Exemplo n.º 3
0
            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()
else:
    import configuration

    from background.utils import PeerServer, indent

    class GitHookServer(PeerServer):
        class ChildProcess(PeerServer.ChildProcess):
            def __init__(self, server, client):
                super(GitHookServer.ChildProcess,
                      self).__init__(server,
                                     [sys.executable, sys.argv[0], "--slave"])
                self.__client = client

            def handle_input(self, data):
                try:
Exemplo n.º 4
0
            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()
else:
    import configuration

    from background.utils import PeerServer, indent

    class GitHookServer(PeerServer):
        class ChildProcess(PeerServer.ChildProcess):
            def __init__(self, server, client):
                super(GitHookServer.ChildProcess, self).__init__(server, [sys.executable, sys.argv[0], "--slave"])
                self.__client = client

            def handle_input(self, data):
                try: result = json_decode(data)
                except ValueError: result = { "status": "error", "error": "invalid response:\n" + indent(data) }
                if result["status"] == "ok":