Subversion post-commit hook用于触发将repo导出到其他目录

时间:2017-05-06 03:18:03

标签: python svn version-control subprocess post-commit

我正在尝试为svn编写一个post commit hook脚本,以将存储库导出到多个目录(因此,当您提交代码时,最新版本的代码也会被复制到其他目录中)。例如,我有一个包含4个路径(或目录)A,B,C,D的仓库。我希望如果我在A中提交,那么修订也应该转到B,C,D。同样地,如果我在B中提交,则修订应该转到C,D,同样在C上提交时,它应该反映在D上。

我开始了解提交后但不知道如何使用它。我已经编写了一堆部分代码。也就是说,当我在A中提交时,它会转到B但不会转到C或D,类似地,当我在B上提交它时,它会转到C而不是D.但

我之前没有编写过脚本。我做了一个测试回购测试来测试我的工作。

#!/usr/bin/python

import os
import re
import sys

from subprocess import Popen, PIPE

REPOS, REV, TXN_NAME = sys.argv[1:]

SVNADMIN = '/opt/softwares/csvn/bin/svnadmin'

MAPPINGS = [
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'A'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'B'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'A'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'C'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'A'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'D'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'B'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'C'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'B'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'D'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'C'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'D'}
    },
]

TMP_MAPPINGS = MAPPINGS
MAPPINGS = []

for mapping in TMP_MAPPINGS:
    if mapping not in MAPPINGS:
        MAPPINGS.append(mapping)

del TMP_MAPPINGS
MAPPINGS.sort(key=lambda mapping: len(mapping['FROM']['PATH']), reverse=True)

def map_(revision_content, to_repos):
    pattern = '\n(?:Node-path|Node-copyfrom-path): ([^\n]*)\n'
    mapped = {'VALUE': False}

    def repl(match):
        path = match.group(1)

        for mapping in MAPPINGS:
            if os.path.samefile(mapping['FROM']['REPOS'], REPOS) \
                and mapping['TO']['REPOS'] == to_repos \
                and path.startswith(mapping['FROM']['PATH']):

                path = mapping['TO']['PATH'] + path[len(mapping['FROM']['PATH']):]
                mapped['VALUE'] = True
                break

        return re.sub(': [^\n]*', ': ' + path, match.group(0), 1)

    return re.sub(pattern, repl, revision_content), mapped['VALUE']

there_were_errors = False
processed_to_repos = []
revision_content = None

for mapping in MAPPINGS:
    if not os.path.samefile(mapping['FROM']['REPOS'], REPOS):
        continue

    to_repos = mapping['TO']['REPOS']

    if to_repos in processed_to_repos:
        continue
    processed_to_repos.append(to_repos)

    if revision_content is None:
        dump_process = Popen([SVNADMIN, 'dump', REPOS, '-r', REV, '--incremental', '-q'], stdout=PIPE, stderr=PIPE)
        revision_content, errors = dump_process.communicate()

        if errors:
            print >> sys.stderr, errors
            there_were_errors = True
            break

    mapped_revision_content, mapped = map_(revision_content, to_repos)

    if not mapped:
        continue

    load_process = Popen([SVNADMIN, 'load', to_repos, '-r', REV, '--ignore-uuid', '-q'], stdin=PIPE, stderr=PIPE)
    _, errors = load_process.communicate(mapped_revision_content)

    if errors:
        print >> sys.stderr, errors
        there_were_errors = True

if there_were_errors:
    sys.exit(1)
sys.exit(0)

1 个答案:

答案 0 :(得分:0)

好吧得到了......和

一起工作
#!/usr/bin/python

import os
import re
import sys

from subprocess import Popen, PIPE

REPOS, REV, TXN_NAME = sys.argv[1:]

SVNADMIN = '/opt/softwares/csvn/bin/svnadmin'

MAPPINGS = [
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'A'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'B'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'A'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'C'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'A'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'D'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'B'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'C'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'B'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'D'}
    },
    {
        'FROM': {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'C'},
        'TO':   {'REPOS': '/opt/softwares/csvn/data/repositories/Test', 'PATH': 'D'}
    },
]

MAPPINGS.sort(key=lambda mapping: len(mapping['FROM']['PATH']), reverse=True)

def map_(revision_content, to_repos):
    global MAPPINGS
    pattern = '\n(?:Node-path|Node-copyfrom-path): ([^\n]*)\n'
    mapped = {'VALUE': False}

    def repl(match):
        global MAPPINGS
        path = match.group(1)
        for mapping in MAPPINGS:
            if os.path.samefile(mapping['FROM']['REPOS'], REPOS) \
                and mapping['TO']['REPOS'] == to_repos \
                and path.startswith(mapping['FROM']['PATH']):
                path = mapping['TO']['PATH'] + path[len(mapping['FROM']['PATH']):]
                mapped['VALUE'] = True
                break
        return re.sub(': [^\n]*', ': ' + path, match.group(0), 1)
    return re.sub(pattern, repl, revision_content), mapped['VALUE']

there_were_errors = False
revision_content = None

dump_process = Popen([SVNADMIN, 'dump', REPOS, '-r', REV, '--incremental', '-q'], stdout=PIPE, stderr=PIPE)
revision_content, errors = dump_process.communicate()
if errors:
    print >> sys.stderr, errors
    there_were_errors = True

for mapping in MAPPINGS:
    if there_were_errors:
        break
    if not os.path.samefile(mapping['FROM']['REPOS'], REPOS):
        continue
    to_repos = mapping['TO']['REPOS']
    mapped_revision_content, mapped = map_(revision_content, to_repos)
    if not mapped:
        continue
    load_process = Popen([SVNADMIN, 'load', to_repos, '-r', REV, '--ignore-uuid', '-q'], stdin=PIPE, stderr=PIPE)
    _, errors = load_process.communicate(mapped_revision_content)
    revision_content = mapped_revision_content
    if errors:
        print >> sys.stderr, errors
        there_were_errors = True

if there_were_errors:
    sys.exit(1)
sys.exit(0)

现在它按预期完成了所需的工作!!