svn服务端使用post-commit钩子

此脚本基于Python2.6.6编写,设置环境变量以兼容提交详情中有中文的情况

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import subprocess
import urllib2
import json
import sys


def execute_command(command):
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    stdout, stderr = process.communicate()
    return stdout.strip()


def main():
    # 获取参数
    repos = sys.argv[1]
    rev = sys.argv[2]

    # 环境变量设置
    cmd_prefix = "env LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8"

    # 提取提交信息
    log_cmd = "{0} /usr/bin/svnlook log -r {1} {2}".format(cmd_prefix, rev, repos)
    log_message = execute_command(log_cmd)

    # 提取提交者信息
    author_cmd = "{0} /usr/bin/svnlook author -r {1} {2}".format(cmd_prefix, rev, repos)
    author = execute_command(author_cmd)

    # 提取提交日期
    date_cmd = "{0} /usr/bin/svnlook date -r {1} {2}".format(cmd_prefix, rev, repos)
    date = execute_command(date_cmd)

    # 提取更改路径
    changed_paths_cmd = "{0} /usr/bin/svnlook changed -r {1} {2}".format(cmd_prefix, rev, repos)
    changed_paths = execute_command(changed_paths_cmd)

    # 创建提交信息字典
    data = {'author': author, 'log_message': log_message, 'date': date, 'changed_paths': changed_paths, 'revision': rev}

    # 将字典转换为JSON字符串
    json_data = json.dumps(data)

    # 创建请求
    request = urllib2.Request('http://192.168.1.1:2001/xxx', data=json_data.encode('utf-8'))
    request.add_header('Content-Type', 'application/json; charset=utf-8')

    # 发送请求
    urllib2.urlopen(request)


if __name__ == '__main__':
    main()
updatedupdated2024-10-282024-10-28