0

I'm trying to write a post commit hook script for svn to export repository to multiple directories (so when you commit a code, that latest version of the code gets copied to other directories also). For example I have a repo which has 4 paths (or directories) A,B,C,D. I want that if I commit in A then the revision should also go to B,C,D. Similarly if I commit in B then revision should go to C,D and likewise when committed on C it should reflect on D.

I came to know about post-commit but don't know how to use it. I have written this bunch of code which is working partially. i.e when I am committing in A it goes to B but not to C or D, similarly when I commit on B it goes to C but not D.

I have not done scripting before. I have made a test-repo Test for testing my work.

#!/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)
  • What debugging have you done? If your question is "please debug and fix my script for me", I fear it is a rather broad question for Stack Overflow. – halfer May 06 '17 at 09:00
  • This sounds like a task more appropriate for a continuous integration/deployment system. That way your users wouldn't have to wait for the hook script to finish executing on each commit. – alroc May 06 '17 at 19:43

1 Answers1

0

ok got it.. working with

#!/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)

Now it does the required job as expected !!