|
1 #============================================================================ |
|
2 #Name : io.py |
|
3 #Part of : Helium |
|
4 |
|
5 #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
6 #All rights reserved. |
|
7 #This component and the accompanying materials are made available |
|
8 #under the terms of the License "Eclipse Public License v1.0" |
|
9 #which accompanies this distribution, and is available |
|
10 #at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
11 # |
|
12 #Initial Contributors: |
|
13 #Nokia Corporation - initial contribution. |
|
14 # |
|
15 #Contributors: |
|
16 # |
|
17 #Description: |
|
18 #=============================================================================== |
|
19 import xml.dom.minidom |
|
20 import logging |
|
21 from Blocks.Packaging.BuildData import BdFile, PlainBuildData |
|
22 logger = logging.getLogger('io') |
|
23 |
|
24 class BdFileSerializer: |
|
25 """ Class used to serialize or deserialize the DBFile """ |
|
26 def __init__(self, bdfile=None): |
|
27 self.bdfile = bdfile |
|
28 |
|
29 def toXml(self): |
|
30 logger.debug("Serializing DBFile.") |
|
31 document = xml.dom.minidom.Document() |
|
32 component = document.createElement('bdfile') |
|
33 component.setAttribute('path', self.bdfile.getPath()) |
|
34 if self.bdfile.variantType is not None: |
|
35 component.setAttribute('variantType', self.bdfile.variantType) |
|
36 if self.bdfile.variantPlatform is not None: |
|
37 component.setAttribute('variantPlatform', self.bdfile.variantPlatform) |
|
38 # Owner reqs |
|
39 ownerReqs = document.createElement('ownerRequirements') |
|
40 for path in self.bdfile.ownerRequirements: |
|
41 req = document.createElement("ownerRequirement") |
|
42 req.setAttribute('path', path) |
|
43 ownerReqs.appendChild(req) |
|
44 component.appendChild(ownerReqs) |
|
45 # source Requirements |
|
46 srcReqs = document.createElement('sourceRequirements') |
|
47 for path in self.bdfile.sourceRequirements: |
|
48 req = document.createElement("sourceRequirement") |
|
49 req.setAttribute('path', path) |
|
50 srcReqs.appendChild(req) |
|
51 component.appendChild(srcReqs) |
|
52 return component.toxml() |
|
53 |
|
54 def fromXml(self, data): |
|
55 logger.debug("Deserializing DBFile.") |
|
56 node = xml.dom.minidom.parseString(data).childNodes[0] |
|
57 if self.bdfile == None: |
|
58 self.bdfile = BdFile(node.getAttribute('path')) |
|
59 |
|
60 self.bdfile.path = node.getAttribute('path') |
|
61 self.bdfile.variantPlatform = node.getAttribute('variantPlatform') |
|
62 self.bdfile.variantType = node.getAttribute('variantType') |
|
63 for src in node.getElementsByTagName('ownerRequirements')[0].getElementsByTagName('ownerRequirement'): |
|
64 self.bdfile.ownerRequirements.append(src.getAttribute('path')) |
|
65 for src in node.getElementsByTagName('sourceRequirements')[0].getElementsByTagName('sourceRequirement'): |
|
66 self.bdfile.sourceRequirements.append(src.getAttribute('path')) |
|
67 return self.bdfile |
|
68 |
|
69 class BuildDataSerializer: |
|
70 """ Class used to serialize or deserialize the plain build data """ |
|
71 def __init__(self, builddata=None): |
|
72 self.builddata = builddata |
|
73 if self.builddata is None: |
|
74 self.builddata = PlainBuildData() |
|
75 |
|
76 def toXml(self): |
|
77 logger.debug("Serializing PlainBuildData.") |
|
78 document = xml.dom.minidom.Document() |
|
79 component = document.createElement('component') |
|
80 component.setAttribute('name', self.builddata.getComponentName()) |
|
81 component.setAttribute('version', self.builddata.getComponentVersion()) |
|
82 # sources |
|
83 sources = document.createElement('sources') |
|
84 sources.setAttribute('root', self.builddata.getSourceRoot()) |
|
85 for path in self.builddata.getSourceFiles(): |
|
86 source = document.createElement("source") |
|
87 source.setAttribute('path', path) |
|
88 sources.appendChild(source) |
|
89 component.appendChild(sources) |
|
90 # targets |
|
91 targets = document.createElement('targets') |
|
92 targets.setAttribute('root', self.builddata.getTargetRoot()) |
|
93 for path in self.builddata.targetFiles.keys(): |
|
94 target = document.createElement("target") |
|
95 target.setAttribute('path', path) |
|
96 if self.builddata.targetFiles[path] is not None: |
|
97 target.appendChild(document.importNode(xml.dom.minidom.parseString(BdFileSerializer(self.builddata.targetFiles[path]).toXml()).childNodes[0], deep=1)) |
|
98 targets.appendChild(target) |
|
99 component.appendChild(targets) |
|
100 return component.toxml() |
|
101 |
|
102 def fromXml(self, data): |
|
103 logger.debug("Deserializing PlainBuildData.") |
|
104 node = xml.dom.minidom.parseString(data).childNodes[0] |
|
105 self.builddata.setComponentName(node.getAttribute('name')) |
|
106 self.builddata.setComponentVersion(node.getAttribute('version')) |
|
107 self.builddata.setSourceRoot(node.getElementsByTagName('sources')[0].getAttribute('root')) |
|
108 self.builddata.setTargetRoot(node.getElementsByTagName('targets')[0].getAttribute('root')) |
|
109 files = [] |
|
110 for src in node.getElementsByTagName('sources')[0].getElementsByTagName('source'): |
|
111 files.append(src.getAttribute('path')) |
|
112 self.builddata.addSourceFiles(files) |
|
113 |
|
114 files = [] |
|
115 for target in node.getElementsByTagName('targets')[0].getElementsByTagName('target'): |
|
116 files.append(target.getAttribute('path')) |
|
117 self.builddata.addTargetFiles(files) |
|
118 for target in node.getElementsByTagName('targets')[0].getElementsByTagName('target'): |
|
119 for bdfile in target.getElementsByTagName('bdfile'): |
|
120 self.builddata.addDeliverable(BdFileSerializer().fromXml(bdfile.toxml())) |
|
121 return self.builddata |
|
122 |
|
123 |
|
124 class BuildDataMerger: |
|
125 """ Class used to merge contents of build data """ |
|
126 def __init__(self, output): |
|
127 self.output = output |
|
128 |
|
129 def merge(self, bd): |
|
130 """ Merge the content of bd into output. """ |
|
131 if bd.getComponentName() != self.output.getComponentName(): |
|
132 raise Exception("Trying to merger two different components (different name)") |
|
133 if bd.getComponentVersion() != self.output.getComponentVersion(): |
|
134 raise Exception("Trying to merger two different components (different version)") |
|
135 if bd.getSourceRoot() != self.output.getSourceRoot(): |
|
136 raise Exception("Trying to merger two different components (different source root)") |
|
137 if bd.getTargetRoot() != self.output.getTargetRoot(): |
|
138 raise Exception("Trying to merger two different components (different target root)") |
|
139 self.output.addSourceFiles(bd.getSourceFiles()) |
|
140 self.output.addTargetFiles(bd.getTargetFiles()) |
|
141 for dep in bd.getDependencies(): |
|
142 self.output.addDeliverable(dep) |
|
143 return self.output |