buildframework/helium/sf/python/pythoncore/lib/pythoncoretests/test_amara.py
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
#============================================================================ 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
#Name        : test_ant.py 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
#Part of     : Helium 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
#All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
#This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
#under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
#which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
#Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
#Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
#Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
#Description:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
#===============================================================================
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
""" amara.py module tests. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import amara
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
from xmlhelper import recursive_node_scan
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    25
import urllib
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    26
import tempfile
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    27
import os
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    28
import sys
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
def test_amara():
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    31
    """test amara"""
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    32
    xxx = amara.parse(r'<commentLog><branchInfo category="" error="kkk" file="tests/data/comments_test.txt" originator="sanummel" since="07-03-22">Add rofsfiles for usage in paged images</branchInfo></commentLog>')
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    33
    assert str(xxx.commentLog.branchInfo) == 'Add rofsfiles for usage in paged images'
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
    
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    35
    xxx = amara.parse(r'<commentLog><branchInfo>1</branchInfo><branchInfo>2</branchInfo></commentLog>')
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    36
    for yyy in xxx.commentLog.branchInfo:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    37
        assert str(yyy) == '1'
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
        break
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
          
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
    myxml = """<DpComponent DpType="File" name="dp.cfg.xml" fileType="Binary" fileSubType="1" fileIndex="1" owner="SwUpdate" extract="true" signed="true" optional="true" crc="true" useCases="Refurbish,BackupRestore" variantPackage="true" include="true" EnableCRCVerification="true" parameters="test"/>"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
    xcf = amara.parse(myxml)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
    assert xcf.DpComponent['name'] == 'dp.cfg.xml'
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
    myxml2 = """<bomDelta><buildFrom>ido_raptor_mcl_abs_MCL.52.57</buildFrom><buildTo>mock</buildTo><content/></bomDelta>"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
    xcf2 = amara.parse(myxml2)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
    assert xcf2.bomDelta[0].buildFrom[0] == "ido_raptor_mcl_abs_MCL.52.57"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
    print xcf.DpComponent.xml_attributes
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
    doc = amara.create_document(u'bom')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
    s60_input_node = doc.xml_create_element(u'input')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
    s60_input_node.xml_append(doc.xml_create_element(u'name', content=(unicode("s60"))))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
    print s60_input_node.xml()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
    s60_input_source = s60_input_node.xml_create_element(u'source')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
    s60_input_source.xml_append(doc.xml_create_element(u'type', content=(unicode("grace"))))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
    print s60_input_source.xml()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
          
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
    doc = amara.create_document(u'bom')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
    doc.bom.xml_append(doc.xml_create_element(u'build', content=unicode("a")))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
    doc = amara.create_document(u'bomDelta')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
    content_node = doc.xml_create_element(u'content')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
    doc.bomDelta.xml_append(content_node)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
    content_node.xml_append(content_node.xml_create_element(u'b', content=unicode('a')))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
    assert doc.bomDelta.xml(indent=False) == '<bomDelta><content><b>a</b></content></bomDelta>'
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
    recursive_node_scan(doc, 'a')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
    amara.create_document()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
    xcf3 = amara.parse(r'<VariantPackingList><Variant/></VariantPackingList>')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
    assert hasattr(xcf3.VariantPackingList.Variant,"FileList") == False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
    if not hasattr(xcf3.VariantPackingList.Variant,"FileList"):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
        xcf3.VariantPackingList.Variant.xml_append(xcf3.xml_create_element(u"FileList"))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
    xcf3.VariantPackingList.Variant.FileList.xml_append_fragment(doc.xml(omitXmlDeclaration=u"yes"))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
    xcf4 = amara.parse(r"<a><p name='1'/><p name='1'/></a>")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
    found = False
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    80
    for p_path in xcf4.xml_xpath("//p"):
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    81
        assert str(p_path.name) == '1'
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
        found = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
    assert found
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
    #xcf5 = amara.parse(open(r'C:\USERS\helium\helium-dev-forbuilds\helium\tests\data\bom\build_model_bom.xml'))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
    #u'%s' % xcf5.bom.content.project.folder.task.synopsis
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    87
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    88
    doc = amara.create_document(u"commentLog")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    89
    if not doc:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    90
        pass
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    91
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    92
    xml1 = """<commentLog><branchInfo category="" error="kkk" file="comments_test.txt" originator="sanummel" since="07-03-22">Add rofsfiles for usage in paged images</branchInfo></commentLog>"""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    93
    doc2 = amara.parse(xml1)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    94
    assert doc2.commentLog.branchInfo.category == ""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    95
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    96
    doc2.commentLog.xml_remove_child(doc2.commentLog.branchInfo)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    97
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    98
    myxml3 = """<a><b value="1"/></a>"""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    99
    xml3 = amara.parse(myxml3)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   100
    for p_temp in xml3.xml_xpath("//b"):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   101
        p_temp.value = u'2'
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   102
    assert '2' in xml3.xml()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   103
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   104
    xml4 = """<a>\n\t<b name="b">a</b>\n</a>"""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   105
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   106
    print amara.parse(xml4).xml(indent="yes").replace('\n', 'n').replace('\t', 't')
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   107
    assert xml4 in amara.parse(xml4).xml(indent="yes")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   108
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   109
    ppxml = """<SettingsData>
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   110
  <ProductProfile Version="1.1">
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   111
     <Feature>
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   112
        <Index>35</Index> 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   113
        <Value>1</Value> 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   114
     </Feature>
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   115
  </ProductProfile>
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   116
</SettingsData>"""
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   117
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   118
    newppxml = amara.parse(ppxml)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   119
    oldppxml = amara.parse(ppxml)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   120
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   121
    oldppdata = {}
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   122
    for oldfeature in oldppxml.SettingsData.ProductProfile.Feature:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   123
        oldppdata[str(oldfeature.Index)] = oldfeature.Value
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   124
    for newfeature in newppxml.SettingsData.ProductProfile.Feature:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   125
        if not oldppdata.has_key(str(newfeature.Index)):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   126
            raise Exception(newfeature.Value)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   127
        elif oldppdata[str(newfeature.Index)] != str(newfeature.Value):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   128
            raise Exception(str(oldppdata[str(newfeature.Index)]) + ' ' + str(newfeature.Value))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   129
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   130
def test_amara_xinclude():
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   131
    (f_desc1, filename1) = tempfile.mkstemp()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   132
    f_file1 = os.fdopen(f_desc1, 'w')
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   133
    f_file1.write(r'<b>qwerty</b>')
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   134
    f_file1.close()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   135
    (f_desc, filename) = tempfile.mkstemp()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   136
    f_file = os.fdopen(f_desc, 'w')
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   137
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   138
    #try:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   139
    #    from Ft.Lib import Uri
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   140
    #    fileurl = Uri.OsPathToUri(filename1)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   141
    #except ImportError:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   142
    fileurl = filename1
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   143
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   144
    f_file.write(r'<a xmlns:xi="http://www.w3.org/2001/XInclude"><xi:include href="' + os.path.basename(fileurl) + r'"/></a>')
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   145
    f_file.close()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   146
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   147
    #if 'java' in sys.platform:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   148
    doc3 = amara.parse(urllib.pathname2url(filename))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   149
    #else:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   150
    #    doc3 = amara.parse(Uri.OsPathToUri(filename))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   151
    assert 'qwerty' in doc3.xml()