没有项目的修订属性时出错

时间:2013-08-15 06:26:36

标签: 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

1 个答案:

答案 0 :(得分:0)

您需要更改manifest_data,如下所示:

def manifest_data (name):
    ....
    if project != None:
        revision = project.get('revision')
        if revision:
            res = pattern.match(revision)
            return res.group(1)
    default = root.find("./default")
    return default.attrib.get('revision')