Module virtualbuildarea |
[hide private]
[frames] | no frames]

# Source Code for Module virtualbuildarea

      1  #============================================================================
      2  #Name        : virtualbuildarea.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
     20  """
     21  This modules helps to recreate a fake synergy project for the
     22  ccm toolkit. The fake project can be composed of any real synergy
     23  objects.
     24  """
     25
     26
     27  # pylint: disable-msg=E1103
     28
     29
     30  import ccm
     31  import xml.dom.minidom
     32  import traceback
     33  import sys
     34  DEBUG_VIRTUALDIR = True
     35
     36 -class VirtualDir(ccm.Dir):
     37      """ Fake directory object.
     38          The four parts name will be: <dirname>-1:dir:fakedb#1
     39      """
     40 -    def __init__(self, session, name, project):
     41          fpn = "%s-1:dir:fakedb#1" % name
     42          ccm.Dir.__init__(self, session, fpn)
     43          self.__data = []
     44          self._project = project
     45
     46 -    def addChild(self, o, project):
     47          """ Add an object to the directory. """
     48          if (o.type == 'dir'):
     49              if DEBUG_VIRTUALDIR:
     50                  print "Dir adding %s to %s" % (o, self)
     51              vdir = self.getDirNamed(o.name)
     52              if vdir == None:
     53                  # Adding a directory, convert it into a virtual one
     54                  if DEBUG_VIRTUALDIR:
     55                      print "Dir not found creating a virtual one"
     56                  vdir = VirtualDir(self._session, o.name, self._project)
     57                  self.__data.append(vdir)
     58              elif vdir.type == 'project':
     59                  # this is a project, covert it into a directory
     60                  self.__data.remove(vdir)
     61                  self.addChild(vdir.root_dir(), vdir)
     62                  vdir = self.getDirNamed(o.name)
     63
     64              # Adding content of the directory into the virtual one
     65              for child in o.children(project):
     66                  vdir.addChild(child, project)
     67          elif (o.type == 'project'):
     68              # Adding a project
     69              if DEBUG_VIRTUALDIR:
     70                  print "Project adding %s to %s" % (o, self)
     71              # check for directory with the same name first
     72              vdir = self.getDirNamed(o.name)
     73              if vdir == None:
     74                  # if it is new, just add it
     75                  if DEBUG_VIRTUALDIR:
     76                      print "Adding project directly"
     77                  self.__data.append(o)
     78              else:
     79                  if vdir.type == 'project':
     80                      if DEBUG_VIRTUALDIR:
     81                          print "Replacing project by dir %s" % vdir
     82                      self.__data.remove(vdir)
     83                      self.addChild(vdir.root_dir(), vdir)
     84                      vdir = self.getDirNamed(o.name)
     85                  # if a directory already exist grab the content
     86                  if DEBUG_VIRTUALDIR:
     87                      print "Adding childs under %s" % vdir
     88                  for child in o.root_dir().children(o):
     89                      vdir.addChild(child, o)
     90          else:
     91              self.__data.append(o)
     92
     93 -    def getDirNamed(self, name):
     94          """ Look for a subdirectory named in its children.
     95          The test is done in a case insensitive way.
     96          """
     97          for o in self.__data:
     98              if (o.name.lower() == name.lower()):
     99                  return o
    100          return None
    101
    102 -    def children(self, project):
    103          """ Returns a copy of the children list. """
    104          return self.__data[:]
    105
    106 -    def virtualproject(self):
    107          """ Return the associated virtual project. """
    108          return self._project
    109
    110
    111 -class VirtualProject(ccm.Project):
    112      """ Create a fake project containing on fake directory.
    113      The four part name will be <projectname>-1:project:fakedb#1.
    114      """
    115 -    def __init__(self, session, name):
    116          fpn = "%s-1:project:fakedb#1" % name
    117          ccm.Project.__init__(self, session, fpn)
    118          self._root_dir = VirtualDir(session, name, self)
    119
    120 -    def root_dir(self):
    121          """ Returns the associated virtual directory object. """
    122          return self._root_dir
    123
    124
    125 -def __removeEmptyStrings(a):
    126      o = []
    127      for e in a:
    128          if (len(e)>0):
    129              o.append(e)
    130      return o
    131
    132 -def __createVirtualPath(vpath, root):
    133      """ Creating a directory structure using the vpath list given as input.
    134      """
    135      vpath = __removeEmptyStrings(vpath)
    136      if (root.type == 'project'):
    137          root = root.root_dir()
    138      if (len(vpath) == 0):
    139          return root
    140      else:
    141          name = vpath.pop(0)
    142          root.addChild(VirtualDir(root.session, name, root.virtualproject()), root.virtualproject())
    143          return __createVirtualPath(vpath, root.getDirNamed(name))
    144
    145 -def __getObjects(project, spath, dir=None):
    146      name = spath.pop(0)
    147      if (len(spath) == 0 and dir == None):
    148          result = []
    149          for o in project.root_dir().children(project):
    150              result.append({'project':project, 'object':o})
    151          return result
    152
    153      if (dir == None):
    154          root_dir = project.root_dir()
    155          if (root_dir.name.lower() == name.lower()):
    156              return __getObjects(project, spath, root_dir)
    157          else:
    158              Exception("getObjects: root_dir.name(%s)!=name(%s)" % (root_dir.name, name))
    159      else:
    160          for d in dir.children(project):
    161              if d.type == 'dir' and d.name.lower() == name.lower():
    162                  return __getObjects(project, spath, d)
    163          raise Exception("Could not find object %s" % name)
    164
    165
    166
    167 -def create(session, file, name = "vba"):
    168      """ Creates a virtual toplevel project using the XML configuration. """
    169      try:
    170          dom = xml.dom.minidom.parse(file)
    171          #dom = xml.dom.minidom.parseString(input)
    172
    173          vba = VirtualProject(session, name)
    174          virtualBA = dom.documentElement
    175          for child in virtualBA.childNodes:
    176              if (child.nodeType == xml.dom.Node.ELEMENT_NODE) and (child.nodeName == 'add'):
    177                  print "add node :%s (%d)" % (child.getAttribute('project'), len(child.childNodes))
    178                  pathobject = __createVirtualPath(child.getAttribute('to').split('/'), vba)
    179                  if len(child.childNodes) == 0:
    180                      p = session.create(child.getAttribute('project'))
    181                      pathobject.addChild(p, p)
    182                  else:
    183                      project = session.create(child.getAttribute('project'))
    184                      for subChild in child.childNodes:
    185                          if (subChild.nodeType == xml.dom.Node.ELEMENT_NODE) and (subChild.nodeName == 'objects'):
    186                              spath = __removeEmptyStrings(subChild.getAttribute('from').split('/'))
    187                              for t in __getObjects(project, spath):
    188                                  pathobject.addChild(t['object'], t['project'])
    189          virtualBA.unlink()
    190          return vba
    191      except Exception, e:
    192          traceback.print_exc(file = sys.stdout)
    193          raise Exception("XML cannot be parsed properly %s" % e)
    194