0
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
'''
|
|
17 |
Convert project ConE plugin
|
|
18 |
'''
|
|
19 |
|
|
20 |
import re
|
|
21 |
import os
|
|
22 |
import sys
|
|
23 |
import logging
|
|
24 |
import xml.parsers.expat
|
|
25 |
import shutil
|
|
26 |
import fnmatch
|
3
|
27 |
import pkg_resources
|
|
28 |
import types
|
0
|
29 |
|
|
30 |
try:
|
|
31 |
from cElementTree import ElementTree
|
|
32 |
except ImportError:
|
|
33 |
try:
|
|
34 |
from elementtree import ElementTree
|
|
35 |
except ImportError:
|
|
36 |
try:
|
|
37 |
from xml.etree import cElementTree as ElementTree
|
|
38 |
except ImportError:
|
|
39 |
from xml.etree import ElementTree
|
|
40 |
|
|
41 |
import __init__
|
|
42 |
|
|
43 |
from cone.storage import filestorage
|
|
44 |
from cone.public import exceptions,plugin,utils,api
|
|
45 |
|
|
46 |
class ConvertProjectImpl(plugin.ImplBase):
|
|
47 |
"""
|
|
48 |
Class to implements ConE plugin that convert old configuration to
|
|
49 |
configuration project. Some extra functions supported in the top
|
|
50 |
of normal file copying functions. For example creation of layer and
|
|
51 |
configuration root files automatically.
|
|
52 |
"""
|
|
53 |
|
|
54 |
IMPL_TYPE_ID = "convertprojectml"
|
|
55 |
|
|
56 |
|
|
57 |
def __init__(self,ref,configuration):
|
|
58 |
"""
|
|
59 |
Overloading the default constructor
|
|
60 |
"""
|
|
61 |
plugin.ImplBase.__init__(self,ref,configuration)
|
|
62 |
self.desc = ""
|
|
63 |
self.logger = logging.getLogger('cone.convertprojectml(%s)' % self.ref)
|
|
64 |
self.errors = False
|
|
65 |
|
|
66 |
#Internal plugin data
|
|
67 |
self.project_data = {}
|
|
68 |
self.layers = []
|
|
69 |
|
|
70 |
def generate(self, context=None):
|
|
71 |
"""
|
|
72 |
Generate the given implementation.
|
|
73 |
"""
|
|
74 |
|
|
75 |
#Generating content
|
3
|
76 |
fullOutputPath = os.path.join(context.output, self.output)
|
0
|
77 |
if self.project_data.has_key("path"):
|
|
78 |
targetPath = utils.resourceref.norm(self.project_data["path"])
|
|
79 |
if targetPath and targetPath != "":
|
3
|
80 |
fullOutputPath = os.path.join(context.output, fullOutputPath, targetPath)
|
0
|
81 |
|
|
82 |
fs = filestorage.FileStorage(fullOutputPath, "w")
|
|
83 |
newProject = api.Project(fs)
|
|
84 |
for layer in self.layers:
|
|
85 |
layer.generate(newProject, self.configuration.get_storage().get_path())
|
|
86 |
newProject.close()
|
|
87 |
|
|
88 |
#Opening project again to validate the content and remove illegal includes.
|
|
89 |
if self.project_data.has_key("validate") and self.project_data["validate"] != "false":
|
|
90 |
fs = filestorage.FileStorage(fullOutputPath, "w")
|
|
91 |
validateProject = api.Project(fs)
|
|
92 |
for conf in validateProject.list_configurations():
|
|
93 |
validateProject.get_configuration(conf).list_all_configurations()
|
|
94 |
validateProject.close()
|
|
95 |
|
|
96 |
return
|
|
97 |
|
|
98 |
def generate_layers(self,layers):
|
|
99 |
"""
|
|
100 |
Generate the given Configuration layers.
|
|
101 |
"""
|
|
102 |
self.logger.info('Generating layers %s' % layers)
|
|
103 |
self.generate()
|
|
104 |
|
|
105 |
return
|
|
106 |
|
|
107 |
def has_ref(self,ref):
|
|
108 |
"""
|
|
109 |
@returns True if the implementation uses the given ref as input value.
|
|
110 |
Otherwise return False.
|
|
111 |
"""
|
|
112 |
|
|
113 |
return None
|
|
114 |
|
|
115 |
#=================================================================
|
|
116 |
class ConvertProjectLayer(object):
|
|
117 |
"""
|
|
118 |
Object presenting layer in convertprojectml file.
|
|
119 |
"""
|
|
120 |
|
3
|
121 |
def __init__(self, path, configuration):
|
0
|
122 |
if path != None:
|
|
123 |
self.path = path
|
|
124 |
else:
|
|
125 |
self.path = ""
|
|
126 |
self.folders = []
|
3
|
127 |
self.files = []
|
|
128 |
self.source_configuration = configuration
|
0
|
129 |
|
|
130 |
def __str__(self):
|
|
131 |
retStr = ""
|
|
132 |
retStr += "\nPath: %s\n" % self.path
|
|
133 |
retStr +="Folders:\n"
|
|
134 |
for folder in self.folders:
|
|
135 |
retStr += folder.__str__()
|
|
136 |
retStr +="Files:\n"
|
|
137 |
for file in self.files:
|
|
138 |
retStr += file.__str__()
|
|
139 |
return retStr
|
|
140 |
|
|
141 |
def generate(self, project, old_structure_root):
|
|
142 |
"""
|
|
143 |
Function to handle generation to one folder.
|
|
144 |
"""
|
|
145 |
|
|
146 |
#Create layer folder.
|
|
147 |
project.get_storage().create_folder(utils.resourceref.norm(self.path))
|
|
148 |
#print "Created Layer:", utils.resourceref.norm(self.path)
|
|
149 |
|
|
150 |
for folder in self.folders:
|
|
151 |
folder.generate(project, old_structure_root)
|
|
152 |
|
|
153 |
for f in self.files:
|
|
154 |
f.generate(project, old_structure_root)
|
|
155 |
|
|
156 |
return
|
|
157 |
|
|
158 |
def addFolder(self, folder):
|
|
159 |
self.folders.append(folder)
|
|
160 |
|
|
161 |
def addFile(self, file):
|
|
162 |
self.files.append(file)
|
|
163 |
|
|
164 |
def getProjectPath(self):
|
3
|
165 |
return self.path
|
0
|
166 |
|
3
|
167 |
def solve_ref(self, inputdata):
|
|
168 |
"""
|
|
169 |
Internal function to solve whether input is ref or just normal input string.
|
|
170 |
For refs actual ConfML value is resolved and returned. Non-refs are returned
|
|
171 |
as such.
|
|
172 |
"""
|
|
173 |
dview = self.source_configuration.get_default_view()
|
|
174 |
if inputdata and isinstance(inputdata, types.StringType):
|
|
175 |
return utils.expand_refs_by_default_view(inputdata, dview)
|
|
176 |
elif inputdata and isinstance(inputdata, types.DictType):
|
|
177 |
retDict = {}
|
|
178 |
for key in inputdata:
|
|
179 |
retDict[self.solve_ref(key)] = self.solve_ref(inputdata[key])
|
|
180 |
return retDict
|
|
181 |
else:
|
|
182 |
return inputdata
|
0
|
183 |
|
|
184 |
class ConvertProjectFolder(object):
|
|
185 |
"""
|
|
186 |
Object presenting folder in convertprojectml file.
|
|
187 |
"""
|
|
188 |
|
|
189 |
def __init__(self, path, parent=None):
|
|
190 |
if path != None:
|
|
191 |
self.path = path
|
|
192 |
else:
|
|
193 |
self.path = ""
|
|
194 |
self.filters = []
|
|
195 |
self.parent = parent
|
|
196 |
|
|
197 |
def __str__(self):
|
|
198 |
retStr = ""
|
|
199 |
retStr += "\tPath: %s\n" % self.path
|
|
200 |
retStr +="\tFilters:\n"
|
|
201 |
for filter in self.filters:
|
|
202 |
retStr += filter.__str__()
|
|
203 |
return retStr
|
|
204 |
|
|
205 |
def generate(self, project, old_structure_root):
|
|
206 |
|
|
207 |
#Adding new folder to project.
|
|
208 |
project.get_storage().create_folder(utils.resourceref.norm(self.getProjectPath()))
|
|
209 |
#print "Created folder:", utils.resourceref.norm(self.getProjectPath())
|
|
210 |
|
|
211 |
for filter in self.filters:
|
|
212 |
filter.generate(project, old_structure_root, "folder")
|
|
213 |
return
|
|
214 |
|
|
215 |
def addFilter(self, filter):
|
|
216 |
self.filters.append(filter)
|
|
217 |
|
|
218 |
def getProjectPath(self):
|
|
219 |
return os.path.join(self.parent.getProjectPath(), self.path)
|
3
|
220 |
|
|
221 |
def solve_ref(self, inputdata):
|
|
222 |
return self.parent.solve_ref(inputdata)
|
|
223 |
|
|
224 |
|
0
|
225 |
class ConvertProjectFile(object):
|
|
226 |
"""
|
|
227 |
Object presenting file in convertprojectml file.
|
|
228 |
"""
|
|
229 |
|
|
230 |
def __init__(self, path, type, parent=None):
|
|
231 |
if path != None:
|
|
232 |
self.path = path
|
|
233 |
else:
|
|
234 |
self.path = ""
|
3
|
235 |
if type != None and type != "none":
|
0
|
236 |
self.type = type
|
|
237 |
else:
|
|
238 |
self.type = ""
|
|
239 |
|
|
240 |
self.filters = []
|
|
241 |
self.parent = parent
|
|
242 |
self.meta = []
|
|
243 |
self.desc = ""
|
3
|
244 |
self.configuration_name = ""
|
0
|
245 |
|
|
246 |
def __str__(self):
|
|
247 |
retStr = ""
|
|
248 |
retStr += "\tPath: %s\n" % self.path
|
|
249 |
retStr += "\tType: %s\n" % self.type
|
|
250 |
retStr +="\tFilters:\n"
|
|
251 |
for filter in self.filters:
|
|
252 |
retStr += filter.__str__()
|
|
253 |
return retStr
|
|
254 |
|
|
255 |
def generate(self, project, old_structure_root):
|
|
256 |
for filter in self.filters:
|
|
257 |
filter.generate(project, old_structure_root, self.type)
|
|
258 |
|
3
|
259 |
if self.type:
|
|
260 |
config = project.get_configuration(utils.resourceref.norm(self.getProjectPath()))
|
0
|
261 |
if self.meta:
|
|
262 |
if not config.meta:
|
|
263 |
config.meta = []
|
|
264 |
for meta in self.meta:
|
3
|
265 |
config.meta.set_property_by_tag(self.solve_ref(meta[0]), \
|
|
266 |
self.solve_ref(meta[1]), \
|
|
267 |
self.solve_ref(meta[2]), \
|
|
268 |
self.solve_ref(meta[3]))
|
0
|
269 |
if self.desc:
|
|
270 |
config.desc = self.desc
|
|
271 |
|
3
|
272 |
if self.configuration_name:
|
|
273 |
config.set_name(self.configuration_name)
|
|
274 |
config.save()
|
|
275 |
|
0
|
276 |
return
|
|
277 |
|
|
278 |
def addFilter(self, filter):
|
|
279 |
self.filters.append(filter)
|
|
280 |
|
|
281 |
def addMeta(self, meta):
|
|
282 |
self.meta = meta
|
|
283 |
|
|
284 |
def addDescription(self, desc):
|
|
285 |
self.desc = desc
|
3
|
286 |
|
|
287 |
def addConfigurationName(self, configuration_name):
|
|
288 |
self.configuration_name = configuration_name
|
0
|
289 |
|
|
290 |
def getProjectPath(self):
|
3
|
291 |
if self.type == "configuration_root":
|
|
292 |
return self.path
|
|
293 |
else:
|
|
294 |
return os.path.join(self.parent.getProjectPath(), self.path)
|
|
295 |
|
|
296 |
def solve_ref(self, inputdata):
|
|
297 |
return self.parent.solve_ref(inputdata)
|
0
|
298 |
|
|
299 |
class ConvertProjectFilter(object):
|
|
300 |
"""
|
|
301 |
Object presenting filter in convertprojectml file.
|
|
302 |
"""
|
|
303 |
|
|
304 |
def __init__(self, action, data, parent=None, remove_includes = "false", recursive = "false"):
|
|
305 |
self.action = action
|
|
306 |
self.data = data
|
|
307 |
self.parent = parent
|
|
308 |
if remove_includes:
|
|
309 |
self.remove_includes = remove_includes
|
|
310 |
else:
|
|
311 |
self.remove_includes = "false"
|
|
312 |
if recursive:
|
|
313 |
self.recursive = recursive
|
|
314 |
else:
|
3
|
315 |
self.recursive = "false"
|
0
|
316 |
|
|
317 |
def __str__(self):
|
|
318 |
retStr = ""
|
|
319 |
retStr += "\t\tAction: %s\n" % self.action
|
|
320 |
retStr += "\t\tData: %s\n" % self.data
|
|
321 |
return retStr
|
|
322 |
|
|
323 |
def generate(self, project, old_structure_root, type="none"):
|
|
324 |
"""
|
|
325 |
@param project: New configuration project
|
|
326 |
@type project:
|
|
327 |
@param old_structure_root: Path to old projects root.
|
|
328 |
@type old_structure_root:
|
|
329 |
|
|
330 |
"""
|
|
331 |
|
|
332 |
if type == "" or type == "folder":
|
|
333 |
self.handleAddRemove(project, old_structure_root)
|
|
334 |
elif type == "layer_root":
|
|
335 |
self.handleLayerRoot(project)
|
|
336 |
elif type == "configuration_root":
|
|
337 |
self.handleConfigurationRoot(project)
|
|
338 |
else:
|
|
339 |
#raise exceptions.NotSupportedException("Type: %s not supported as file type" % repr(type))
|
|
340 |
pass
|
|
341 |
return
|
|
342 |
|
|
343 |
def handleAddRemove(self, project, old_structure_root):
|
|
344 |
"""
|
|
345 |
"""
|
|
346 |
|
|
347 |
pathPart, wildCardPart = self.separatePathAndWildcard(self.data)
|
|
348 |
filesToProcess = []
|
|
349 |
if wildCardPart == "":
|
|
350 |
#No wildcards found.
|
|
351 |
if self.recursive == "false":
|
|
352 |
source = os.path.join(old_structure_root, pathPart)
|
|
353 |
targetDir = self.resolveTargetDir(project, source)
|
|
354 |
filesToProcess.append({"source": source, "targetDir": targetDir})
|
|
355 |
else:
|
|
356 |
#recursive search for directory entries.
|
|
357 |
directoryPath = os.path.join(old_structure_root, pathPart)
|
|
358 |
if os.path.isdir(directoryPath):
|
|
359 |
for root, dirs, files in os.walk(directoryPath):
|
|
360 |
for f in files:
|
|
361 |
#Handling files.
|
|
362 |
source = os.path.join(root, f)
|
|
363 |
targetDir = self.resolveTargetDir(project, source)
|
|
364 |
filesToProcess.append({"source": source, "targetDir": targetDir})
|
|
365 |
|
|
366 |
for d in dirs:
|
|
367 |
#Handling directories to get empty folders included also.
|
|
368 |
source = os.path.join(root, d)
|
|
369 |
targetDir = self.resolveTargetDir(project, source)
|
|
370 |
filesToProcess.append({"source": source, "targetDir": targetDir})
|
|
371 |
|
|
372 |
else:
|
|
373 |
#Need to handle wildcard part
|
|
374 |
filesToProcess = self.getFilesByWildcard(os.path.join(old_structure_root, pathPart)\
|
|
375 |
,wildCardPart, project)
|
|
376 |
|
|
377 |
for f in filesToProcess:
|
|
378 |
source = f["source"]
|
|
379 |
targetDir = f["targetDir"]
|
|
380 |
|
|
381 |
if source.lower().find(".svn") != -1:
|
|
382 |
#Ignoring svn files
|
|
383 |
continue
|
|
384 |
|
|
385 |
if os.path.isfile(source):
|
|
386 |
#targetDir = self.resolveTargetDir(project, f)
|
|
387 |
if self.action == "add":
|
|
388 |
if not os.path.exists(targetDir):
|
|
389 |
os.makedirs(targetDir)
|
|
390 |
shutil.copy2(source, targetDir)
|
|
391 |
elif self.action == "remove":
|
|
392 |
targetFile = os.path.join(targetDir, os.path.split(source)[1])
|
|
393 |
os.remove(targetFile)
|
|
394 |
elif os.path.isdir(source):
|
|
395 |
folderToCreate = os.path.join(targetDir, os.path.split(source)[1])
|
|
396 |
if not os.path.isdir(folderToCreate):
|
|
397 |
os.makedirs(folderToCreate)
|
|
398 |
|
|
399 |
def resolveTargetDir(self, project, filepath):
|
|
400 |
"""
|
|
401 |
"""
|
|
402 |
if self.recursive == "false":
|
|
403 |
return os.path.join(project.get_storage().get_path(), self.getProjectPath())
|
|
404 |
else:
|
|
405 |
retPath = os.path.join(project.get_storage().get_path(), self.getProjectPath())
|
|
406 |
startFound = 0
|
|
407 |
|
3
|
408 |
parts = filter(lambda p: p != '',
|
|
409 |
os.path.normpath(filepath).replace('\\', '/').split('/'))
|
|
410 |
for item in parts:
|
0
|
411 |
if self.data.find(item) != -1:
|
|
412 |
startFound = 1
|
|
413 |
if startFound and self.data.find(item) == -1:
|
|
414 |
retPath = os.path.join(retPath, item)
|
|
415 |
return os.path.split(retPath)[0]
|
|
416 |
|
|
417 |
|
|
418 |
def handleLayerRoot(self, project):
|
|
419 |
"""
|
|
420 |
"""
|
|
421 |
|
|
422 |
pathPart, wildCardPart = self.separatePathAndWildcard(self.data)
|
|
423 |
filesToProcess = []
|
|
424 |
|
|
425 |
if wildCardPart == "":
|
|
426 |
#No wildcards found. Checking still if path has folder and file elements
|
|
427 |
|
|
428 |
folderPath, filePart = os.path.split(pathPart)
|
|
429 |
if folderPath == "":
|
|
430 |
#filename only
|
|
431 |
pathPart = ""
|
|
432 |
else:
|
|
433 |
#file and folder
|
|
434 |
pathPart = folderPath
|
|
435 |
|
|
436 |
source = os.path.join(project.get_storage().get_path(), self.getProjectPath(), pathPart, filePart)
|
|
437 |
filesToProcess.append({"source": source, "targetDir": None})
|
|
438 |
|
|
439 |
else:
|
|
440 |
#Need to handle wildcard part
|
|
441 |
fullSearchPath = os.path.join(project.get_storage().get_path(), self.getProjectPath(), pathPart)
|
|
442 |
filesToProcess = self.getFilesByWildcard(fullSearchPath, wildCardPart, project)
|
|
443 |
|
|
444 |
#Creating rootfile.
|
3
|
445 |
rootFilePath = os.path.join(self.getProjectPath(), self.parent.path)
|
|
446 |
if project.is_configuration(utils.resourceref.norm(rootFilePath)):
|
|
447 |
config = project.get_configuration(utils.resourceref.norm(rootFilePath))
|
|
448 |
else:
|
|
449 |
config = project.create_configuration(utils.resourceref.norm(rootFilePath))
|
0
|
450 |
|
|
451 |
#Adding defined includes.
|
|
452 |
for f in filesToProcess:
|
|
453 |
source = f["source"]
|
|
454 |
#Getting path in configuration project and adding it as include.
|
|
455 |
filePath = utils.resourceref.norm(os.path.join(pathPart, os.path.split(source)[1]))
|
|
456 |
config.include_configuration(filePath)
|
|
457 |
if self.remove_includes == "true":
|
|
458 |
self.removeIncludes(config.get_configuration(filePath))
|
|
459 |
config.save()
|
|
460 |
|
|
461 |
def removeIncludes(self, config):
|
|
462 |
"""
|
|
463 |
@param config: Configuration object that is processed
|
|
464 |
|
|
465 |
@return: None
|
|
466 |
"""
|
|
467 |
|
|
468 |
#Getting all configurations from included configuration.
|
|
469 |
configList = config.list_configurations()
|
|
470 |
for item in configList:
|
|
471 |
config.remove_configuration(utils.resourceref.norm(item))
|
|
472 |
|
|
473 |
config.save()
|
|
474 |
|
|
475 |
|
|
476 |
def handleConfigurationRoot(self, project):
|
|
477 |
"""
|
|
478 |
"""
|
|
479 |
#Always in the root of the project
|
|
480 |
configname = utils.resourceref.norm(self.parent.path)
|
3
|
481 |
if project.is_configuration(utils.resourceref.norm(self.parent.path)):
|
0
|
482 |
config = project.get_configuration(configname)
|
|
483 |
else:
|
|
484 |
config = project.create_configuration(utils.resourceref.norm(self.parent.path))
|
|
485 |
config.include_configuration(utils.resourceref.norm(self.data))
|
|
486 |
config.save()
|
|
487 |
|
|
488 |
|
|
489 |
def getProjectPath(self):
|
|
490 |
if isinstance(self.parent, ConvertProjectFile):
|
|
491 |
#print "FILE", self.parent.parent.getProjectPath()
|
|
492 |
return self.parent.parent.getProjectPath()
|
|
493 |
else:
|
|
494 |
#print "other"
|
|
495 |
return self.parent.getProjectPath()
|
|
496 |
|
|
497 |
|
|
498 |
def getFilesByWildcard(self, folder, wildcard, project):
|
|
499 |
"""
|
|
500 |
@param folder: folder where matching is made
|
|
501 |
@type folder: string
|
|
502 |
@param wildcard: wildcard pattern
|
|
503 |
@type wildcard: string
|
|
504 |
"""
|
|
505 |
|
|
506 |
#Array of files and folders matching with the wildcard.
|
|
507 |
retArray = []
|
|
508 |
if os.path.isdir(folder):
|
|
509 |
for root, dirs, files in os.walk(folder):
|
|
510 |
if self.recursive == "false" and os.path.normpath(root) != os.path.normpath(folder):
|
|
511 |
#No recursive search used and therefore only topmost directory is handled.
|
|
512 |
continue
|
|
513 |
else:
|
|
514 |
for f in files:
|
3
|
515 |
if wildcard and wildcard[0] != "*":
|
|
516 |
#Matches path part.
|
|
517 |
wildcard = "*%s" % wildcard
|
|
518 |
|
0
|
519 |
if fnmatch.fnmatch(os.path.join(root, f), wildcard):
|
|
520 |
source = os.path.join(root, f)
|
|
521 |
targetDir = self.resolveTargetDir(project, source)
|
|
522 |
retArray.append({"source": source, "targetDir": targetDir})
|
|
523 |
|
|
524 |
for d in dirs:
|
|
525 |
if fnmatch.fnmatch(os.path.join(root, d), wildcard):
|
|
526 |
source = os.path.join(root, d)
|
|
527 |
targetDir = self.resolveTargetDir(project, source)
|
|
528 |
retArray.append({"source": source, "targetDir": targetDir})
|
|
529 |
|
|
530 |
return retArray
|
|
531 |
|
|
532 |
def separatePathAndWildcard(self, data):
|
|
533 |
"""
|
|
534 |
@param data: data from XML that may contain path and wildcard parts
|
|
535 |
@type data: string
|
|
536 |
|
|
537 |
@return: Path and wildcard parts separately.
|
|
538 |
"""
|
|
539 |
pathPart = ""
|
|
540 |
wildCardPart = ""
|
|
541 |
|
|
542 |
if data.find("*") == -1:
|
|
543 |
#Only supported wildcard is currently *
|
|
544 |
pathPart = data
|
|
545 |
wildCardPart =""
|
|
546 |
else:
|
|
547 |
#Some wildcards found. Wildcards are supported only in the last segment.
|
|
548 |
pathPart, wildCardPart = os.path.split(data)
|
|
549 |
|
|
550 |
return pathPart, wildCardPart
|
|
551 |
|
3
|
552 |
def solve_ref(self, inputdata):
|
|
553 |
return self.parent.solve_ref(inputdata)
|
|
554 |
|
0
|
555 |
#=================================================================
|
|
556 |
|
|
557 |
class ConvertProjectReader(plugin.ReaderBase):
|
|
558 |
"""
|
|
559 |
Parses a single convertprojectml file
|
|
560 |
"""
|
|
561 |
|
|
562 |
NAMESPACE = 'http://www.s60.com/xml/convertprojectml/1'
|
3
|
563 |
NAMESPACE_ID = 'convertprojectml'
|
|
564 |
ROOT_ELEMENT_NAME = 'convertprojectml'
|
0
|
565 |
FILE_EXTENSIONS = ['convertprojectml']
|
|
566 |
|
|
567 |
def __init__(self):
|
|
568 |
self.desc = None
|
|
569 |
self.output_dir = None
|
|
570 |
self.input_dir = None
|
|
571 |
self.namespaces = [self.NAMESPACE]
|
|
572 |
self.project_data = {}
|
|
573 |
self.layers = []
|
|
574 |
|
|
575 |
@classmethod
|
|
576 |
def read_impl(cls, resource_ref, configuration, etree):
|
|
577 |
reader = ConvertProjectReader()
|
3
|
578 |
reader.from_etree(etree, configuration, configuration.get_storage().get_path())
|
0
|
579 |
|
|
580 |
impl = ConvertProjectImpl(resource_ref, configuration)
|
|
581 |
impl.project_data = reader.project_data
|
|
582 |
impl.layers = reader.layers
|
|
583 |
return impl
|
|
584 |
|
3
|
585 |
@classmethod
|
|
586 |
def get_schema_data(cls):
|
|
587 |
return pkg_resources.resource_string('projectconvertplugin', 'xsd/convertprojectml.xsd')
|
|
588 |
|
|
589 |
def from_etree(self, etree, configuration, old_structure_root = ""):
|
|
590 |
self.configuration = configuration
|
|
591 |
|
|
592 |
for element in etree:
|
|
593 |
if element.tag == "{http://www.s60.com/xml/convertprojectml/1}targetProject":
|
|
594 |
self.project_data = self.parse_attributes(etree, "targetProject")
|
|
595 |
elif element.tag == "{http://www.s60.com/xml/convertprojectml/1}layer":
|
|
596 |
self.layers.append(self.parse_layer(element))
|
|
597 |
elif element.tag == "{http://www.s60.com/xml/convertprojectml/1}foreach":
|
|
598 |
for fe in self.parse_foreach(element, old_structure_root):
|
|
599 |
self.layers.append(fe)
|
|
600 |
return
|
0
|
601 |
|
|
602 |
def parse_foreach(self, etree, old_structure_root):
|
3
|
603 |
layersTmp = []
|
|
604 |
variable = etree.get("variable")
|
|
605 |
data = self.handleMapping(etree.get("data"), {})
|
|
606 |
folders = []
|
|
607 |
for item in os.listdir(os.path.join(old_structure_root, data)):
|
|
608 |
if os.path.isdir(os.path.join(old_structure_root, data, item)) and item != '.svn':
|
|
609 |
folders.append(item)
|
|
610 |
|
|
611 |
for folder in folders:
|
|
612 |
mapping_data = {variable: folder}
|
|
613 |
for layer in etree.findall("{%s}layer" % self.namespaces[0]):
|
|
614 |
layersTmp.append(self.parse_layer(layer, mapping_data))
|
0
|
615 |
|
|
616 |
return layersTmp
|
3
|
617 |
|
0
|
618 |
def parse_layer(self, etree, mapping_data=None):
|
|
619 |
path = self.handleMapping(etree.get("path"), mapping_data)
|
|
620 |
|
3
|
621 |
layerObject = ConvertProjectLayer(path, self.configuration)
|
0
|
622 |
for folder in etree.findall("{%s}folder" % self.namespaces[0]):
|
|
623 |
layerObject.addFolder(self.parse_folder(folder, layerObject, mapping_data))
|
|
624 |
|
|
625 |
for f in etree.findall("{%s}file" % self.namespaces[0]):
|
|
626 |
layerObject.addFile(self.parse_file(f, layerObject, mapping_data))
|
|
627 |
|
|
628 |
return layerObject
|
|
629 |
|
|
630 |
def parse_folder(self, etree, parent, mapping_data=None):
|
|
631 |
path = self.handleMapping(etree.get("path"), mapping_data)
|
|
632 |
|
|
633 |
folderObject = ConvertProjectFolder(path, parent)
|
|
634 |
for filter in etree.findall("{%s}filter" % self.namespaces[0]):
|
|
635 |
#Remove includes supported only for files.
|
|
636 |
folderObject.addFilter(self.parse_filter(filter, folderObject, mapping_data))
|
|
637 |
return folderObject
|
|
638 |
|
|
639 |
def parse_file(self, etree, parent, mapping_data=None):
|
|
640 |
path = self.handleMapping(etree.get("path"), mapping_data)
|
|
641 |
type = self.handleMapping(etree.get("type"), mapping_data)
|
3
|
642 |
configuration_name = self.handleMapping(etree.get("configuration_name"), mapping_data)
|
0
|
643 |
|
|
644 |
fileObject = ConvertProjectFile(path, type, parent)
|
|
645 |
for filter in etree.findall("{%s}filter" % self.namespaces[0]):
|
|
646 |
fileObject.addFilter(self.parse_filter(filter, fileObject, mapping_data))
|
|
647 |
|
|
648 |
metaElement = etree.find("{%s}meta" % self.namespaces[0])
|
|
649 |
namespacePattern = re.compile("\{(.*)\}(.*)")
|
|
650 |
metaArray = [] #tag, value, ns, attrs
|
|
651 |
if metaElement:
|
|
652 |
for item in metaElement.getiterator():
|
|
653 |
mo = namespacePattern.search(item.tag)
|
|
654 |
if mo:
|
|
655 |
if mo.group(2) != "meta":
|
|
656 |
tmpArray = []
|
|
657 |
tmpArray.append(mo.group(2)) #Tag name
|
|
658 |
tmpArray.append(item.text) #value
|
|
659 |
tmpArray.append(mo.group(1)) #Namespace
|
|
660 |
tmpDict = {}
|
|
661 |
for attribute in item.keys():
|
|
662 |
tmpDict[attribute] = item.get(attribute)
|
|
663 |
tmpArray.append(tmpDict)
|
|
664 |
metaArray.append(tmpArray)
|
|
665 |
|
|
666 |
descElement = etree.find("{%s}desc" % self.namespaces[0])
|
|
667 |
description = ""
|
|
668 |
if descElement != None:
|
|
669 |
description = descElement.text
|
|
670 |
|
|
671 |
fileObject.addMeta(metaArray)
|
3
|
672 |
fileObject.addDescription(description)
|
|
673 |
fileObject.addConfigurationName(configuration_name)
|
0
|
674 |
return fileObject
|
|
675 |
|
|
676 |
def parse_filter(self, etree, parent, mapping_data=None):
|
|
677 |
"""
|
|
678 |
"""
|
|
679 |
data = self.handleMapping(etree.get("data"), mapping_data)
|
|
680 |
action = self.handleMapping(etree.get("action"), mapping_data)
|
|
681 |
remove_includes = self.handleMapping(etree.get("remove_includes"), mapping_data)
|
|
682 |
recursive = self.handleMapping(etree.get("recursive"), mapping_data)
|
|
683 |
|
|
684 |
return ConvertProjectFilter(action, data, parent, remove_includes, recursive)
|
|
685 |
|
|
686 |
|
|
687 |
def parse_rule(self, etree, parent):
|
|
688 |
return {"name": etree.get("name"), "type": etree.get("type"), "data": etree.get("data")}
|
|
689 |
|
|
690 |
def parse_attributes(self, etree, tagName):
|
|
691 |
tmpDict = {}
|
|
692 |
tmpElement = etree.find("{%s}%s" % (self.namespaces[0], tagName))
|
|
693 |
for attribute in tmpElement.keys():
|
|
694 |
tmpDict[attribute] = tmpElement.get(attribute)
|
|
695 |
return tmpDict
|
|
696 |
|
|
697 |
def handleMapping(self, data, mapping):
|
|
698 |
"""
|
3
|
699 |
"""
|
0
|
700 |
retStr = data
|
3
|
701 |
if not mapping: mapping = {}
|
|
702 |
if data != None:
|
|
703 |
merged = dict(mapping.items() + self._get_env_variables().items())
|
|
704 |
for key in merged.keys():
|
|
705 |
retStr = retStr.replace(key, merged[key])
|
|
706 |
|
0
|
707 |
return retStr
|
|
708 |
|
3
|
709 |
def _get_env_variables(self):
|
|
710 |
if not hasattr(self, '_env_dict'):
|
|
711 |
#Making dictionary only once because of performance.
|
|
712 |
self._env_dict = {}
|
|
713 |
for var in os.environ:
|
|
714 |
self._env_dict["%%%s%%" % var] = os.environ[var]
|
|
715 |
|
|
716 |
return self._env_dict
|
0
|
717 |
|
|
718 |
|
|
719 |
|
|
720 |
|
|
721 |
|
|
722 |
|
|
723 |
|