私はまだ Python の専門家ではないので、svn リポジトリへのコミット内のトランザクション内で変更された各 xml ファイルを cat して何かをしたいと考えています。この場合、xml ファイルが存在するかどうかを確認してから、電子メールを送信します。このために、pysvn と lxml を使用して、対応するタグを解析します。これは機能しますが、1 つのファイルに対してのみです。タグが付いたファイルが 2 つある場合、動作しません。さらに、「レビューが必要」というタグが付いた 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()