0

INPUT:-

<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="vendor/widevine" name="platform/vendor/google/proprietary/bluetooth" x-ship="none" x-quic-dist="none" x-grease-customer="none"/>

</manifest>

I am trying to get the revision value from the input above if the revision tag is present,if it is not present use the revision value in the default tag,I have the following code and running into following error.. can anyone provide inputs on what is wrong here?

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


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

def main ():
    branch_name = "jb_2.5.4"
    print "branch_name"
    print branch_name
    projects = ['platform/vendor/google/proprietary/widevine','platform/vendor/google/proprietary/bluetooth']
    for project in projects :
        branch = manifest_data(project)
        print branch

if __name__ == '__main__':
    main()

Error:-

  File "branch_manifest.py", line 35, in <module>
    main()
  File "branch_manifest.py", line 32, in main
    branch = manifest_data(project)
  File "branch_manifest.py", line 18, in manifest_data
    project = root.find("./project[@name='%s']" % name)
  File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 330, in find
    return ElementPath.find(self, path)
  File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 186, in find
    return _compile(path).find(element)
  File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 176, in _compile
    p = Path(path)
  File "/usr/lib/python2.6/xml/etree/ElementPath.py", line 93, in __init__
    "expected path separator (%s)" % (op or tag)
SyntaxError: expected path separator ([)
  • When I run your code with your data, I get a `ParseError: junk after document element: line 2, column 0`, because that's an XML fragment, not a complete document. If I wrap it in an outer tag so it's a complete document, I don't get that error at all; I get one success, and then an error on the `res = pattern.match(revision)` because `revision` is `None`. Please give us code and data that can reproduce the problem you want debugged, or we can't debug it. – abarnert Aug 13 '13 at 03:14
  • From your error, my guess is that whatever you're actually passing in for `name` is giving you a nonsensical XPath query. For example, maybe it has an apostrophe in it? An easy way to help debug this is to print out `"./project[@name='%s']" % name` before the line that tries to use it. – abarnert Aug 13 '13 at 03:16
  • It works well with python2.7, but got the same exception with python2.6 – David.Zheng Aug 13 '13 at 03:19
  • @all - I updated the input to reproduce the error.. –  Aug 13 '13 at 03:21

1 Answers1

1

It works well with python2.7, but got the same exception with python2.6.

You can renference ElementTree XPath - Select Element based on attribute. It gives an answer: python2.6 do not support syntax like ./project[@name='%s']

Community
  • 1
  • 1
David.Zheng
  • 903
  • 8
  • 6
  • am running using python 2.7 –  Aug 13 '13 at 03:26
  • @user2639990, Make sure you are using python 2.7 to run the program. The exception you pasted above, throws exception from /usr/lib/python2.6/xml/etree/ElementPath.py – David.Zheng Aug 13 '13 at 03:38
  • @user2639990 Are you sure you are running 2.7? `/usr/lib/python2.6/xml/etree/ElementPath.py` looks like it refers to Python 2.6 modules – thefourtheye Aug 13 '13 at 03:38