Python 使用pysvn解析svn事务提交中的多个xml文件
由于我还不是python方面的专家,我想在提交到svn存储库的事务中对每个更改的xml文件进行分类,并做一些事情。在本例中,我检查xml文件是否存在,然后发送电子邮件。为此,我使用pysvn和lxml来解析相应的标记。这是可行的,但只适用于一个文件。如果我有两个带标签的文件,我就不工作了。此外,如果有超过1个xml文件具有“需要审阅”标签,则只需发送1封包含相应文件列表的电子邮件,这是有意义的。有人知道如何改变“for changedFile…”来实现这一点吗Python 使用pysvn解析svn事务提交中的多个xml文件,python,xml,parsing,svn,pysvn,Python,Xml,Parsing,Svn,Pysvn,由于我还不是python方面的专家,我想在提交到svn存储库的事务中对每个更改的xml文件进行分类,并做一些事情。在本例中,我检查xml文件是否存在,然后发送电子邮件。为此,我使用pysvn和lxml来解析相应的标记。这是可行的,但只适用于一个文件。如果我有两个带标签的文件,我就不工作了。此外,如果有超过1个xml文件具有“需要审阅”标签,则只需发送1封包含相应文件列表的电子邮件,这是有意义的。有人知道如何改变“for changedFile…”来实现这一点吗 #!/bin/env python
#!/bin/env python
import os
import sys
import subprocess
# Import pysvn libs to act with svn
import pysvn
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Use lxml lib to parse tree for status
from lxml import html
#tmpFile = open('D:\my.log', 'w')
repository = sys.argv[1]
transactionId = sys.argv[2]
transaction = pysvn.Transaction(repository, transactionId, is_revision=True)
for changedFile in transaction.changed():
tree = html.fromstring(transaction.cat(changedFile))
if tree.xpath('//topic/@status')[0] == "Needs Review":
#tmpFile.writelines("This topic needs review")
# me == my email address
# you == recipient's email address
me = "test@example.com"
you = "test2@example.com"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "TODO Review JiveX Doku"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
text = "Some Text and please check this File."
html = """\
<html>
<head></head>
<body>
<p>The following commited Files need a Review.
Please Check.<br>
Here is the List of Files with links: <a href=".../file1">file 1</a><br><a href=".../file2">file 2</a>.
</p>
</body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
s = smtplib.SMTP('192.168.1.20')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()
#/bin/env python
导入操作系统
导入系统
导入子流程
#导入pysvn libs以与svn一起操作
导入pysvn
#为实际发送函数导入smtplib
导入smtplib
#导入我们需要的电子邮件模块
从email.mime.text导入MIMEText
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
#使用lxml库解析树的状态
从lxml导入html
#tmpFile=open('D:\my.log','w')
repository=sys.argv[1]
transactionId=sys.argv[2]
transaction=pysvn.transaction(存储库,transactionId,is_revision=True)
对于事务中的changedFile.changed():
tree=html.fromstring(transaction.cat(changedFile))
如果tree.xpath('//topic/@status')[0]==“需要检查”:
#tmpFile.writelines(“此主题需要审查”)
#我==我的电子邮件地址
#您==收件人的电子邮件地址
我=”test@example.com"
你=”test2@example.com"
#创建消息容器-正确的MIME类型为multipart/alternative。
msg=MIMEMultipart('alternative')
msg['Subject']=“待办事项回顾JiveX Doku”
msg['From']=我
msg['To']=您
#创建消息正文(纯文本和HTML版本)。
text=“一些文本,请检查此文件。”
html=”“”\
以下提交的文件需要审阅。
请检查。
以下是带有链接的文件列表:
。
"""
#记录这两部分的MIME类型-text/plain和text/html。
part1=MIMEText(文本“纯”)
part2=MIMEText(html,'html')
#将部件附加到消息容器中。
#根据RFC 2046,在本例中,多部分消息的最后一部分
#HTML消息是最好的首选。
附加信息(第1部分)
附加信息(第2部分)
#通过本地SMTP服务器发送邮件。
s=smtplib.SMTP('192.168.1.20')
#sendmail函数接受3个参数:发件人地址、收件人地址
#和要发送的消息-在这里它作为一个字符串发送。
s、 sendmail(我,你,msg.as_string())
s、 退出