当没有项目的修订属性时获取默认修订版值

时间:2013-08-13 22:56:55

标签: python

INPUT: -

<?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 =”标签我想在输入中获得“default revsion”标签。如果不更改现有功能,如何执行此操作的任何输入?

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

2 个答案:

答案 0 :(得分:0)

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

UPD:

正如您所说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')

答案 1 :(得分:0)

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

revision = project.get('revision') if project is not None else some_default