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

与えられたエラーは、それprojectNoneこの状況にあることを意味するため、最も簡単な答えは、project is not Noneメソッドを呼び出す前にそれを確認することです。この場合、三項を使用できるように見えます:

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