0

输入:-

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<default revision="jb_2.5.4" remote="quic"/>
<project name="platform/vendor/google/proprietary/widevine"
         path="vendor/widevine"
         revision="refs/heads/jb_2.6"
         x-grease-customer="none"
         x-quic-dist="none"
         x-ship="none" />
<project path="external/dbus" name="platform/external/bus" revision="refs/heads/jb_2.5" x-ship="oss" x-quic-dist="la" x-grease-customer="none"/>

<project path="external/connectivity" name="platform/test/code" x-ship="oss" x-quic-dist="la" x-grease-customer="none"/>

</manifest>

你好,

如果上面显示的输入存在,我有以下代码获取修订值,如果没有“revision =”字段,它会退出并显示下面显示的错误..当没有“revision =”标签时我想要在输入中获取“默认版本”标签..有关如何在不更改现有功能的情况下执行此操作的任何输入?

CODE:-

import shlex
import os
import sys
import json
import fileinput
import pwd
import itertools
import subprocess
import shutil
from subprocess import Popen, PIPE, STDOUT
import xml.etree.ElementTree as ET
import re

def manifest_data (name):
    print name
    pattern = re.compile('refs/heads/(.*)')
    tree = ET.parse('.repo/manifests/test.xml')
    root = tree.getroot()
    project = root.find("./project[@name='%s']" % name)
    print project
    if project != None:
        revision = project.get('revision')
        res = pattern.match(revision)
        return res.group(1)
    else:
        default = root.find("./default")
        return default.attrib.get('revision')

def main ():
    branch_name = "jb_mr2"
    print "branch_name"
    print branch_name
    projects = ['platform/vendor/google/proprietary/widevine','platform/external/bus','platform/test/code']
    #if os.path.isdir('.repo') :
        #print "Deleting .repo"
        #shutil.rmtree('.repo')
    RepoInitCmd =  'repo init -u git://git.quicinc.com/platform/manifest.git -b ' + branch_name
    proc = subprocess.Popen(shlex.split(RepoInitCmd), stderr=subprocess.PIPE)
    out, error = proc.communicate()
    for project in projects :
        branch = manifest_data(project)
        print branch

if __name__ == '__main__':
    main()

错误:-

Traceback (most recent call last):
  File "branch_manifest.py", line 45, in <module>
    main()
  File "branch_manifest.py", line 41, in main
    branch = manifest_data(project)
  File "branch_manifest.py", line 23, in manifest_data
    res = pattern.match(revision)
TypeError: expected string or buffer
4

2 回答 2

0

替换revision = project.get('revision')project.attrib.get('revision', 'No revision')应该有帮助。

升级版:

正如您所说,project标签仅包含platform/test/code属性。在行中:

project = root.find("./project[@name='%s']" % name)

您正在尝试project按名称查找标签。它失败了 -project变量变为None并且错误只是正确地说'NoneType' object has no attribute 'attrib'project在尝试获取修订之前检查:

project = root.find("./project[@name='%s']" % name)
if project:
    revision = project.get('revision')
    res = pattern.match(revision)
    return res.group(1)
else:
    default = root.find("./default")
    return default.attrib.get('revision')
于 2013-08-13T23:11:44.403 回答
0

给出的错误意味着projectNone这种情况下,因此最直接的答案是project is not None在您开始尝试对其调用方法之前验证这一点。看起来在这种情况下你可以只使用三元:

revision = project.get('revision') if project is not None else some_default
于 2013-08-14T05:19:24.247 回答