0

我是 Python 的新手。我想创建一个 python 脚本,它检查(也许解析)每个提交的 xml 文件以获取特殊标签的可用性(例如 status="Needs Review")。如果 xml 文件包含此标记,则发送一封电子邮件,其中包含指向该主题的链接。如果不继续提交而不发送任何电子邮件。

是否有人有代码示例如何将其实现为 python 脚本。xml 文件样本:

<topic template="Default" status="Needs Review" lasteditedby="user1">
  <title translate="true">Sample Title</title>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Statistische Messungen</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Font Style" style="font-family:&apos;Optima LT&apos;; font-size:10pt; font-weight:normal; font-style:normal; text-decoration:none; text-transform:none; vertical-align:baseline; color:#000000; background-color:transparent; letter-spacing:normal; letter-scaling:100%;" translate="true">This is a sample Text</text></para>
  </body>
</topic>

我想有不同的方法可以做到这一点,我知道有一些用于 post-commits 的 python 编码,但我找不到这个问题的编码。

4

1 回答 1

0

Andres 的这段代码片段是一个很好的基础。我只需要 grep 提交的 xml 文件的内容,例如“svn look changed”(对于 xml-files)和每个 xml-file “svn look cat.

from lxml import html
from mailer import Mailer
from mailer import Message
import sys, os

xml = """<topic template="Default" status="Needs Review" lasteditedby="user1">
  <title translate="true">Sample Title</title>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Statistische Messungen</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Font Style" style="font-family:&apos;Optima LT&apos;; font-size:10pt; font-weight:normal; font-style:normal; text-decoration:none; text-transform:none; vertical-align:baseline; color:#000000; background-color:transparent; letter-spacing:normal; letter-scaling:100%;" translate="true">This is a sample Text</text></para>
  </body>
</topic>"""


tree = html.fromstring(xml)
if tree.xpath('//topic/@status')[0] == "Needs Review":
    print "This topic needs review"
    print "Sending e-mail"  
    # Call email function here...
else:
    print "This topic doesn't need review"
    print "Continuing commit without sending e-mail"
    # Continue with commit
于 2015-11-12T08:57:44.707 回答