Kli*_*Joe 6 python http review-board
我一直在努力通过他们的API将差异发布到ReviewBoard.我已设法登录到服务器并创建一个新帖子,但我未能正确发布diff文件的内容.
我是编写此类应用程序的新手,但我的目标是使用一步脚本:
可能是以后,该脚本可以是svn预提交钩子的一部分.
我的python尝试看起来像:
import urllib.request
import urllib.parse
import os.path
... login to the reviewboard server with
urllib.request.HTTPBasicAuthHandler ...
diff_path = '/path/to/file'
diff_name = 'my.diff'
diff_path = os.path.join(diff_path, diff_name)
diff_val = open(diff_path,'r')
# load the diff into the http data POST request
diff_header = \
'-- SoMe BoUnDaRy \n' \
+ 'Content-Disposition: form-data; name=path; filename=' \
+ '"' + diff_name + '"\n\n' \
+ diff_val.read() + '\n' \
+ '-- SoMe BoUnDaRy --'
data ={'path': diff_header, 'basedir': '/path/to/file/in/rep'}
print( data['path'] )
data = urllib.parse.urlencode(data)
data = data.encode('utf-8')
opener.open( \
'http://xxx.xxx.x.xxx/api/review-requests/26/diffs/', data)
Run Code Online (Sandbox Code Playgroud)
使用此代码,我得到一个BAD REQUEST(400)错误,特别是:"一个或多个字段有错误"(105).
我知道有一些库可以与ReviewBoard API对话.我也知道存在复审后的情况.我宁愿不必向其他开发人员分发另一个python库,并且在从多个位置传播文件时,后期审查似乎不太灵活.
根据下面的建议,我在这里添加了服务器响应:
CREATING PASSWD MANAGER...
CREATING PASSWD MANAGER... done
CREATING PASSWD HANDLER...
CREATING PASSWD HANDLER... done
CREATING URL OPENER...
CREATING URL OPENER... done
LOADING DIFF...
send: b'POST /api/review-requests/26/diffs/ HTTP/1.1\r\nAccept-Encoding:
identity\r\nContent-Length: 723\r\nHost: xxx.xxx.x.xxx\r\nContent-Type:
application/x-www-form-urlencoded\r\nConnection: close\r\nUser-Agent:
[empty no username+password] Python-urllib/3.2\r\n\r\
npath=--+SoMe+BoUnDaRy+++%...[the rest of my post]
reply: 'HTTP/1.1 401 UNAUTHORIZED\r\n'
header: Date header: Server header: Content-Language header: Expires header:
Vary header: Cache-Control header: WWW-Authenticate header:
Content-Length header: Last-Modified header: Connection header:
Content-Type send: b'POST /api/review-requests/26/diffs/
HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 723\r\nHost:
xxx.xxx.x.xxx\r\nUser-Agent: Python-urllib/3.2\r\nConnection:
close\r\nContent-Type: application/x-www-form-urlencoded\r\nAuthorization:
Basic [with username+password]\r\n\r\npath=
--+SoMe+BoUnDaRy+++%0AContent-Disposition%...
reply: 'HTTP/1.1 400 BAD REQUEST\r\n'
header: Date header: Server header: Content-Language header: Expires header:
Vary header: Cache-Control header: Set-Cookie header: Content-Length header:
Last-Modified header: Connection header: Content-Type HTTPError thrown
Run Code Online (Sandbox Code Playgroud)
乍一看,我的猜测是我的密码处理程序发生了一些事情.我不确定它发生了什么.以防万一,这就是我生成身份验证的方式:
manager_passwd = urllib.request.HTTPPasswordMgr()
manager_passwd.add_password(...)
handler_passwd = urllib.request.HTTPBasicAuthHandler(manager_passwd)
opener = urllib.request.build_opener(handler_passwd)
Run Code Online (Sandbox Code Playgroud)
身份验证似乎有效.我通过创建一个新的评论帖子测试了它.因此,当我发布差异时,身份验证失败.
Reviewboard 已经有一个 python 工具,用于通过其 API 发布 diff,它称为 postreview.py。您可以在以下位置找到它:
http://reviewboard.googlecode.com/svn/trunk/wxpostreview/postreview.py
抓住并使用它们ReviewBoardServer进行登录并发布差异!
(此外,在您的请求中,不仅需要身份验证,还需要 cookie 文件。这就是为什么您需要 2 个请求(一个用于登录并获取 cookie,另一个用于发送差异。))
| 归档时间: |
|
| 查看次数: |
1915 次 |
| 最近记录: |