由于我仍然不是 python 专家,我想在提交到 svn 存储库的事务中对每个更改的 xml 文件进行分类并做一些事情。在这种情况下,我检查是否存在 xml 文件,然后发送电子邮件。为此,我使用 pysvn 和 lxml 来解析相应的标签。这有效,但仅适用于一个文件。如果我有 2 个带有标签的文件,我将无法正常工作。此外,如果有超过 1 个带有“需要审查”标签的 xml 文件发送仅包含相应文件列表的 1 封电子邮件,这也是有意义的。有人知道如何更改“for changedFile ...”来实现这一点吗?
#!/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()