buildframework/helium/sf/python/pythoncore/lib/preparation.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        : preparation.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
""" This package implements the new update work area functionality.
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
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import logging
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import os
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import time
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
import ccm
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
import ccm.extra
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
import fileutils
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    31
from xml.dom.minidom import getDOMImplementation
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    32
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
# Uncomment this line to enable logging in this module, or configure logging elsewhere
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
logging.basicConfig(level=logging.INFO)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
_logger = logging.getLogger("preparation.ccmgetinput")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
DEFAULT_THREADS = 1
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
THREADS_MIN_TOTAL = 1
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
THREADS_MAX_TOTAL = 10
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
def find(function, seq):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
    """Return first item in sequence where f(item) == True."""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
    for item in seq:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
        if function(item): 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
            return item
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
    return None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
class PreparationAction(object):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
    """ Implements an abstract preparation function. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
    def __init__(self, config, builder):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
        self._config = config
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
        self._builder = builder
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
    def check(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
        """ Checks if project is available in synergy. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
        self._check_object(self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
    def _check_object(self, fpn):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
        """ Check if ccmobject exists in synergy database. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
        session = self.get_session()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
        ccm_object = session.create(fpn)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
        if ccm_object.exists():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
            _logger.info("Checking '%s'...Ok" % fpn)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
        else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
            _logger.info("Checking '%s'...Not Found!" % fpn)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
            raise Exception("Could not find  object %s in the database." % fpn)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
    def execute(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
        """ This method needs to be overridden by child class.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
        It should implement the action to achieve.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
        """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
        pass
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
    def cleanup(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
        """ This method needs to be overridden by child class.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
            It should implement the action to achieve.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
        """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
        pass
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    85
    
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    86
    def extract_release_data(self):
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    87
        """ This method is used to retrieve current project
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    88
            environment data. (e.g. project name) 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    89
        """
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    90
        return None
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
    def get_session(self):
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    93
        """ Helper that retrieve the session from the builder. Setting threads correctly. """
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
        if self._config.has_key('database'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
            return self._builder.session(self._config['database'], self.get_threads())
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
        if not self._config.has_key('host'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
            raise Exception("Database engine host configuration is not found")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
        elif not self._config.has_key('dbpath'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
            raise Exception("Database path configuration is not found")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
        else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
            return self._builder.session(None, self.get_threads(), self._config['host'], self._config['dbpath'])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   103
    def get_threads(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
        """ Returning the number of threads that should be used. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   105
        threads = self._config.get_int('threads', DEFAULT_THREADS)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
        if threads < THREADS_MIN_TOTAL:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
            threads = THREADS_MIN_TOTAL
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
        if threads > THREADS_MAX_TOTAL:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
            threads = THREADS_MAX_TOTAL
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   110
        return threads
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   111
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   112
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
class PreparationSnapshot(PreparationAction):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   114
    """ Implements a Snapshot preparation function. 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   115
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   116
    Support the parallel snapshotter.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   117
    """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   118
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   119
    def __init__(self, config, builder):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   120
        """ Initialization. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   121
        PreparationAction.__init__(self, config, builder)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   122
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   123
    def cleanup(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   124
        """ Method that implement the cleanup of create snapshots """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   125
        session = self.get_session()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
        project = session.create(self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   127
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   128
        target_dir = os.path.normpath(os.path.join(self._config['dir'], project.name))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   129
        _logger.info("Deleting snapshot under %s." % target_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   130
        if os.path.exists(target_dir):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   131
            _logger.info("Deleting '%s'." % target_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   132
            fileutils.rmtree(target_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   133
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   134
    def execute(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   135
        """ Method that implements snapshoting of the project into a folder. """        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   136
        _logger.info("=== Stage=snapshot = %s" % self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   137
        _logger.info("++ Started at %s" % time.strftime("%H:%M:%S", time.localtime()))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   138
        session = self.get_session()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   139
        project = session.create(self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   140
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   141
        target_dir = os.path.normpath(os.path.join(self._config['dir'], project.name))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   142
        _logger.info("Looking for snapshot under %s." % target_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   143
        if not self._check_version(project, target_dir):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   144
            if not os.path.exists(target_dir):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   145
                _logger.info("Creating '%s'." % target_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   146
                os.makedirs(target_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   147
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   148
                _logger.info("Project needs to be updated, so deleting '%s'." % target_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   149
                fileutils.rmtree(target_dir)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   150
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   151
            try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   152
                _logger.info("Snapshotting project.")                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   153
                if self.get_threads() == 1:                    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   154
                    _logger.info(project.snapshot(target_dir, True))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   155
                else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   156
                    _logger.info(ccm.extra.FastSnapshot(project, target_dir, self.get_threads()))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   157
                                    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   158
                # writing version file                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   159
                _logger.info("Saving project version information.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   160
                versionfile = open(os.path.join(self._config['dir'], project.name, 'project.version'), "w+")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   161
                versionfile.write(str(project))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   162
                versionfile.close()                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   163
            except Exception, exc:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   164
                if isinstance(exc, ccm.extra.CCMExtraException):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   165
                    for sexc in exc.subexceptions:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   166
                        _logger.info(sexc)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   167
                _logger.info("ERROR: snapshotting %s" % self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   168
                _logger.info(exc)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   169
                raise exc
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   170
        else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   171
            _logger.info("Project snapshot is still up to date. Nothing to do.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   172
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   173
        _logger.info("++ Finished at %s" % time.strftime("%H:%M:%S", time.localtime()))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   174
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   175
    def _check_version(self, project, targetdir):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   176
        """ Check the version file for snaphot and identify if the project has to be snapshot or not.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   177
            Returns True if the content of the file matches the project to snapshot (nothing to do).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   178
            Returns falls either if the file is missing, or the content is different.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   179
        """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   180
        versionfile = os.path.join(targetdir, 'project.version')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   181
        if (os.path.exists(versionfile)):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   182
            file_ = open(versionfile, "r")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   183
            projectname = file_.read().strip()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   184
            file_.close()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   185
            if (projectname == project.objectname):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   186
                return True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   187
        return False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   188
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   189
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   190
class PreparationCheckout(PreparationAction):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   191
    """ Handle the checkout and update of project content. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   192
    def __init__(self, config, builder):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   193
        """ Initialization. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   194
        PreparationAction.__init__(self, config, builder)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   195
        self.__role = None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   196
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   197
    def check(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   198
        """ Checks if all synergy resources are available. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   199
        PreparationAction.check(self)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   200
        if self._config.has_key('release'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   201
            self._check_object(str(self._config['release']))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   202
        else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   203
            raise Exception("'release' property is not defined for %s" % self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   204
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   205
        for task in self.__get_tasks():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   206
            self._check_object("Task %s" % task)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   207
        for folder in self.__get_folders():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   208
            self._check_object("Folder %s" % folder)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   209
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   210
        for project in self.__get_subbaselines():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   211
            self._check_object(project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   212
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   213
        try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   214
            if (not os.path.exists(self._config['dir'])):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   215
                os.makedirs(self._config['dir'])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   216
        except Exception:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   217
            _logger.info("ERROR: Not able to create the synergy workarea %s " % (self._config['dir']))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   218
            raise Exception("ERROR: Not able to create the synergy workarea %s" % self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   219
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   220
        # checking if the purpose exists
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   221
        if self._config.has_key('purpose'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   222
            session = self.get_session()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   223
            purposes = session.purposes()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   224
            if purposes.has_key(str(self._config['purpose'])):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   225
                _logger.info("Checking purpose '%s'...Ok" % (self._config['purpose']))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   226
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   227
                _logger.info("Checking purpose '%s'...Not Found!" % (self._config['purpose']))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   228
                raise Exception("Could not find purpose %s in the database." % self._config['purpose'])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   229
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   230
            role = session.role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   231
            co_role = ccm.get_role_for_purpose(session, str(self._config['purpose']))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   232
            _logger.info("Try to switch user to role: %s" % co_role)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   233
            session.role = co_role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   234
            session.role = role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   235
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   236
    def cleanup(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   237
        """ Method that implement the deletion of checked out projects. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   238
        session = self.get_session()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   239
        project = session.create(self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   240
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   241
        session.home = self._config['dir']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   242
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   243
        result = self.__find_project(project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   244
        if (result != None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   245
            _logger.info("Project found: '%s'" % result)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   246
            role = session.role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   247
            co_role = ccm.get_role_for_purpose(session, str(self._config['purpose']))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   248
            session.role = co_role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   249
            try:
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   250
                delResult = result.delete(scope='project_and_subproject_hierarchy')
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   251
            finally:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   252
                session.role = role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   253
            ccm.log_result(delResult, ccm.CHECKOUT_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   254
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   255
    def extract_release_data(self):
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   256
        """ Extracting data for a checked out project. """
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   257
        data = None
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   258
        session = self.get_session()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   259
        project = session.create(self._config.name)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   260
        
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   261
        session.home = self._config['dir']
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   262
        
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   263
        result = self.__find_project(project)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   264
        if (result != None) and (self._config.get_boolean('releasable', False)):
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   265
            if  'baseline.release' in self._config:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   266
                data = {}
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   267
                _logger.info("Releasing: '%s'" % result)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   268
                data['name'] = result.objectname
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   269
                data['database'] = session.database()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   270
                data['role'] = ccm.get_role_for_purpose(session, str(self._config['purpose']))
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   271
                data['dir'] = os.path.normpath(self._config['dir'])
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   272
                data['pst'] = result.name
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   273
                data['release'] = self._config['baseline.release']
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   274
            else:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   275
                _logger.warning("Could not release " + result.objectname + " because the 'baseline.release' property is missing.")
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   276
        return data
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   277
    
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   278
    def execute(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   279
        """ Creates a checkout of the project, or updates an existing checkout if one is found.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   280
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   281
        The work area is maintained as part of this.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   282
        """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   283
        _logger.info("=== Stage=checkout = %s" % self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   284
        _logger.info("++ Started at %s" % time.strftime("%H:%M:%S", time.localtime()))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   285
        session = self.get_session()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   286
        project = session.create(self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   287
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   288
        session.home = self._config['dir']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   289
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   290
        result = self.__find_project(project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   291
        # for testing: result = session.create("ppd_sw-fa1f5132#wbernard2:project:sa1spp#1")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   292
        if (result != None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   293
            _logger.info("Project found: '%s'" % result)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   294
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   295
            # setting up the project
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   296
            self.__setup_project(project, result)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   297
        else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   298
            _logger.info("Checking out from '%s'." % project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   299
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   300
            purpose = None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   301
            if self._config.has_key('purpose'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   302
                purpose = self._config['purpose']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   303
                _logger.info("Using purpose: '%s'" % purpose)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   304
                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   305
            version = None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   306
            if self._config.has_key('version'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   307
                version = self._config['version']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   308
                _logger.info("Using version: '%s'" % version)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   309
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   310
            try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   311
                self.__setRole(session)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   312
                result = project.checkout(session.create(self._config['release']), version=version, purpose=purpose)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   313
                ccm.log_result(result, ccm.CHECKOUT_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   314
            except ccm.CCMException, exc:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   315
                ccm.log_result(exc.result, ccm.CHECKOUT_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   316
                raise exc
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   317
            finally:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   318
                self.__restoreRole(session)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   319
            _logger.info('Checkout complete')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   320
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   321
            if result.project != None and result.project.exists():                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   322
                _logger.info("Project checked out: '%s'" % result.project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   323
                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   324
                try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   325
                    self.__setRole(session)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   326
                    _logger.info("Maintaining the workarea...")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   327
                    if self.get_threads() == 1:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   328
                        output = result.project.work_area(True, True, True, self._config['dir'], result.project.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   329
                    else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   330
                        output = ccm.extra.FastMaintainWorkArea(result.project, self._config['dir'], result.project.name, self.get_threads())
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   331
                    ccm.log_result(output, ccm.CHECKOUT_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   332
                finally:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   333
                    self.__restoreRole(session)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   334
                self.__setup_project(project, result.project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   335
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   336
                raise Exception("Error checking out '%s'" % project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   337
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   338
        _logger.info("++ Finished at %s" % time.strftime("%H:%M:%S", time.localtime()))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   339
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   340
    def __find_project(self, project):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   341
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   342
        if (os.path.exists(os.path.join(self._config['dir'], project.name, "project.version"))):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   343
            _logger.info("Snapshot to checkout deleting '%s'." % os.path.join(self._config['dir'], project.name))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   344
            fileutils.rmtree(os.path.join(self._config['dir'], project.name))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   345
            return None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   346
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   347
        path = os.path.join(self._config['dir'], project.name, project.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   348
        try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   349
            result = project.session.get_workarea_info(path)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   350
            if(result == None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   351
                fileutils.rmtree(path)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   352
                return result
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   353
            return result['project']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   354
        except ccm.CCMException:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   355
            # Delete the project dir if found
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   356
            if os.path.exists(os.path.dirname(path)):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   357
                fileutils.rmtree(os.path.dirname(path))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   358
            return None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   359
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   360
    def __setRole(self, session):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   361
        """ Updating the role of a session. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   362
        self.__role = session.role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   363
        if self._config.has_key('purpose'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   364
            co_role = ccm.get_role_for_purpose(session, self._config['purpose'])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   365
            _logger.info("Switching user to role: %s" % co_role)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   366
            session.role = co_role
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   367
            _logger.info("Switched user to role: %s" % session.role)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   368
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   369
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   370
    def __restoreRole(self, session):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   371
        """ Restoring to default user role. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   372
        if self.__role:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   373
            _logger.info("Switching user to role: %s" % self.__role)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   374
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   375
            session.role = self.__role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   376
            self.__role = None
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   377
            _logger.info("Switched user to role: %s" % session.role)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   378
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   379
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   380
    def __setup_project(self, project, coproject):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   381
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   382
        session = self.get_session()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   383
        self.__setRole(session)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   384
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   385
        newprojs = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   386
        if not self._config.get_boolean('use.reconfigure.template', False):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   387
            _logger.info("Validating release")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   388
            self.__set_release(coproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   389
            _logger.info("Setting update properties to manual")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   390
            coproject.set_update_method('manual', True)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   391
            _logger.info("Setting the baseline to '%s'" % project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   392
            coproject.set_baseline(project, True)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   393
            self.__set_subbaselines(coproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   394
            _logger.info("Cleaning up update properties")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   395
            self._clean_update_properties(coproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   396
            _logger.info("Setting update properties.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   397
            self._set_tasks_and_folders(coproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   398
            _logger.info("Applying update properties.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   399
            coproject.apply_update_properties(baseline=False)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   400
        else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   401
            _logger.info("Validating release")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   402
            self.__set_release(coproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   403
                        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   404
        replace_subprojects = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   405
        if not self._config.get_boolean('replace.subprojects', True):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   406
            _logger.info("NOT replacing subprojects")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   407
            replace_subprojects = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   408
        update_keepgoing = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   409
        if self._config.get_boolean('update.failonerror', False):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   410
            _logger.info("The build will fail with update errors")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   411
            update_keepgoing = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   412
        _logger.info("Updating...")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   413
        result = coproject.update(True, replace_subprojects, update_keepgoing, result=ccm.UpdateResultSimple(coproject.session))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   414
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   415
        if self._config.get_boolean('fix.missing.baselines', False) and replace_subprojects:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   416
            newprojs = self.__fix_baseline(coproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   417
            if len(newprojs) > 0:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   418
                result = coproject.update(True, replace_subprojects, update_keepgoing, result=ccm.UpdateResultSimple(coproject.session))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   419
                ccm.log_result(result, ccm.UPDATE_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   420
                _logger.info("Detected additional projects into baseline - Maintaining the whole toplevel project again...")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   421
                coproject.work_area(True, True)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   422
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   423
                ccm.log_result(result, ccm.UPDATE_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   424
        else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   425
            ccm.log_result(result, ccm.UPDATE_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   426
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   427
        # Running sync
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   428
        self._sync(coproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   429
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   430
        # Running check conflicts
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   431
        self._check_conflicts(coproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   432
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   433
        self.__restoreRole(session)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   434
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   435
    def _sync(self, coproject):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   436
        """ Run the sync if the 'sync' property is defined to true in the 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   437
            configuration
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   438
        """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   439
        if self._config.get_boolean('sync', False):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   440
            _logger.info("Synchronizing...")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   441
            result = coproject.sync(True, True)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   442
            ccm.log_result(result, ccm.SYNC_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   443
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   444
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   445
    def __set_release(self, project):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   446
        """ Update the release of the project hierarchy if required. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   447
        release = project.session.create(self._config['release'])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   448
        _logger.info("Current release: '%s'" % project.release)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   449
        _logger.info("Configuration release: '%s'" % release)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   450
        if project.release != release:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   451
            _logger.info("Updating release on the project hierarchy.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   452
            for subp in [project] + project.subprojects:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   453
                subp.release = release
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   454
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   455
    def __fix_baseline(self, coproject):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   456
        """ Check for project in a different status, then check them out. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   457
        newprojs = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   458
        _logger.info("Looking for new projects in the check out.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   459
        status = coproject['status']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   460
        for subproj in coproject.subprojects:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   461
            if subproj['status'] == status:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   462
                continue           
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   463
            _logger.info("New project detected in the checkout '%s'" % subproj.objectname)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   464
            purpose = None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   465
            if self._config.has_key('purpose'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   466
                purpose = self._config['purpose']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   467
                _logger.info("Using purpose: '%s'" % purpose)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   468
                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   469
            version = None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   470
            if self._config.has_key('version'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   471
                version = self._config['version']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   472
                _logger.info("Using version: '%s'" % version)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   473
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   474
            result = subproj.checkout(subproj.session.create(self._config['release']), version=version, purpose=purpose, subprojects=False)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   475
            _logger.info('Checkout complete')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   476
            if result.project != None and result.project.exists():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   477
                newcop = result.project
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   478
                newprojs.append(newcop)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   479
                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   480
                _logger.info("Setting is_relative to true")                    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   481
                if "is_relative" in newcop.keys():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   482
                    newcop["is_relative"] = "TRUE"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   483
                else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   484
                    newcop.create_attribute("is_relative", "boolean", "TRUE")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   485
                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   486
                if not self._config.get_boolean('use.reconfigure.template', False):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   487
                    newcop.set_update_method('manual', False)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   488
                    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   489
                    _logger.info("Setting the baseline to '%s'" % subproj)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   490
                    newcop.set_baseline(subproj, True)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   491
                                            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   492
                    _logger.info("Cleaning up update properties")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   493
                    self._clean_update_properties(newcop)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   494
                    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   495
                    _logger.info("Setting update properties.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   496
                    self._set_tasks_and_folders(newcop)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   497
        return newprojs        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   498
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   499
    def _check_conflicts(self, coproject):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   500
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   501
        conflictsobjects = self._config.get_boolean('show.conflicts.objects', False)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   502
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   503
        if self._config.get_boolean('show.conflicts', False) or conflictsobjects:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   504
            result = coproject.conflicts(True, not conflictsobjects)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   505
            ccm.log_result(result, ccm.CONFLICTS_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   506
#        for project in result.keys():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   507
#            for error in result[project]:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   508
#                if 'object' in error:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   509
#                    _logger.info("CONFLICTS: %s" % error['comment'])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   510
#                else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   511
#                    _logger.info("CONFLICTS: %s" % error['comment'])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   512
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   513
    @staticmethod
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   514
    def _clean_update_properties(project):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   515
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   516
        for task in project.tasks:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   517
            project.remove_task(task)        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   518
        for folder in project.folders:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   519
            project.remove_folder(folder)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   520
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   521
    @staticmethod
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   522
    def __find_subproject(subprojects, project):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   523
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   524
        for subproj in subprojects:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   525
            if subproj.is_same_family(project):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   526
                return subproj
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   527
        raise Exception("Error could not identify check out project for '%s'" % project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   528
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   529
    def __set_subbaselines(self, project):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   530
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   531
        if len(self.__get_subbaselines()) > 0:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   532
            subprojects = project.subprojects
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   533
            for subbaseline in self.__get_subbaselines():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   534
                subbaseline = project.session.create(subbaseline)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   535
                subproj = self.__find_subproject(subprojects, subbaseline)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   536
                _logger.info("Setting subproject '%s' baseline to '%s'" % (subproj, subbaseline))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   537
                subproj.set_baseline(subbaseline, True)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   538
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   539
    def __get_array(self, key):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   540
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   541
        result = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   542
        if (self._config.has_key(key)):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   543
            if isinstance(self._config[key], type([])):                
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   544
                for value in self._config[key]:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   545
                    value = value.strip()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   546
                    if len(value) > 0:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   547
                        result.append(value) 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   548
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   549
                value = self._config[key].strip()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   550
                if len(value) > 0:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   551
                    result.append(value)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   552
        return result
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   553
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   554
    def __get_subbaselines(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   555
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   556
        return self.__get_array('subbaselines')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   557
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   558
    def __get_tasks(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   559
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   560
        return self.__get_array('tasks')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   561
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   562
    def __get_folders(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   563
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   564
        return self.__get_array('folders')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   565
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   566
    def _set_tasks_and_folders(self, project):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   567
        """ Private method. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   568
        for task in self.__get_tasks():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   569
            _logger.info("Adding task %s" % task)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   570
            project.add_task(project.session.create("Task %s" % task))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   571
        for folder in self.__get_folders():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   572
            _logger.info("Adding folder %s" % folder)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   573
            project.add_folder(project.session.create("Folder %s" % folder))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   574
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   575
class PreparationUpdate(PreparationCheckout):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   576
    """ Synergy project updater. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   577
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   578
    def __init__(self, config, builder):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   579
        """ Initialization. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   580
        PreparationCheckout.__init__(self, config, builder)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   581
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   582
    def check(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   583
        """ Checks if all synergy resources are available. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   584
        PreparationAction.check(self)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   585
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   586
        session = self.get_session()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   587
        ccm_object = session.create(self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   588
        role = session.role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   589
        co_role = ccm.get_role_for_status(ccm_object['status'])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   590
        _logger.info("Try to switch user to role: %s" % co_role)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   591
        session.role = co_role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   592
        session.role = role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   593
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   594
    def execute(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   595
        """ Updating the mentioned project. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   596
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   597
        session = self.get_session()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   598
        ccmproject = session.create(self._config.name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   599
        role = session.role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   600
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   601
        status = ccmproject['status']
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   602
        co_role = ccm.get_role_for_status(status)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   603
        session.role = co_role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   604
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   605
        if not self._config.get_boolean('use.reconfigure.template', False):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   606
            _logger.info("Setting update properties to manual")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   607
            ccmproject.set_update_method('manual', True)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   608
            _logger.info("Cleaning up update properties")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   609
            self._clean_update_properties(ccmproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   610
            _logger.info("Setting update properties.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   611
            self._set_tasks_and_folders(ccmproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   612
            _logger.info("Applying update properties.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   613
            ccmproject.apply_update_properties(baseline=False)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   614
        replace_subprojects = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   615
        if not self._config.get_boolean('replace.subprojects', True):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   616
            _logger.info("NOT replacing subprojects")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   617
            replace_subprojects = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   618
        update_keepgoing = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   619
        if self._config.get_boolean('update.failonerror', False):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   620
            _logger.info("The build will fail with update errors")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   621
            update_keepgoing = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   622
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   623
        _logger.info("Updating %s..." % ccmproject.objectname)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   624
        result = ccmproject.update(True, replace_subprojects, update_keepgoing, result=ccm.UpdateResultSimple(ccmproject.session))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   625
        ccm.log_result(result, ccm.UPDATE_LOG_RULES, _logger)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   626
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   627
        self._sync(ccmproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   628
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   629
        self._check_conflicts(ccmproject)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   630
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   631
        session.role = role
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   632
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   633
    def extract_release_data(self):
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   634
        """ Extracting data for an updated project. """
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   635
        data = None
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   636
        session = self.get_session()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   637
        project = session.create(self._config.name)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   638
        
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   639
        session.home = self._config['dir']
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   640
        
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   641
        if self._config.get_boolean('releasable', False):
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   642
            if 'baseline.release' in self._config:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   643
                data = {}
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   644
                _logger.info("Releasing: '%s'" % project)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   645
                data['name'] = project.objectname
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   646
                data['database'] = session.database()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   647
                data['role'] = ccm.get_role_for_purpose(session, str(self._config['purpose']))
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   648
                data['dir'] = os.path.normpath(self._config['dir'])
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   649
                data['pst'] = project.name
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   650
                data['release'] = self._config['baseline.release']
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   651
            else:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   652
                _logger.warning("Could not release " + project + " because the 'baseline.release' property is missing.")
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   653
        return data
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   654
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   655
class PreparationBuilder(object):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   656
    """ Creates an updated work area from a configuration. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   657
    def __init__(self, configs, username = None, password = None, cache=None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   658
        """ Initialization. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   659
        self._configs = configs
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   660
        self._sessions = {}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   661
        self._actions = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   662
        self.__username = username
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   663
        self.__password = password
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   664
        self.__provider = ccm.extra.CachedSessionProvider(cache=cache)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   665
        for config in self._configs:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   666
            if config.type == "snapshot":
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   667
                self._actions.append(PreparationSnapshot(config, self))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   668
            elif config.type == "checkout":
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   669
                self._actions.append(PreparationCheckout(config, self))        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   670
            elif config.type == "update":
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   671
                self._actions.append(PreparationUpdate(config, self))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   672
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   673
    def check(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   674
        """ Check that all dependencies are there. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   675
        for action in self._actions:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   676
            action.check()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   677
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   678
    def cleanup(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   679
        """ Check that all dependencies are there. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   680
        for action in self._actions:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   681
            action.cleanup()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   682
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   683
    def get_content(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   684
        """ Run the each action. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   685
        for action in self._actions:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   686
            action.execute()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   687
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   688
    def extract_release_data(self, filename):
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   689
        """ Extract project information into an xml file. """
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   690
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   691
        impl = getDOMImplementation()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   692
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   693
        doc = impl.createDocument(None, "release", None)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   694
        top_element = doc.documentElement
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   695
        
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   696
        for action in self._actions:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   697
            data = action.extract_release_data()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   698
            if data:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   699
                project_node = doc.createElement('project')
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   700
                for attr in data:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   701
                    project_node.setAttribute(attr, data[attr])
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   702
                top_element.appendChild(project_node)
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   703
        
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   704
        f_file = open(filename, 'w+')
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   705
        f_file.write(doc.toprettyxml(indent="  "))
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   706
        f_file.close()
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   707
        
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   708
    def session(self, database, size=1, engine=None, dbpath=None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   709
        """ Handles pool rather that sessions. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   710
        assert size > 0, "The pool must contains at least one session!"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   711
        if self.__provider is None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   712
            raise Exception("The builder has been closed.") 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   713
        if not self._sessions.has_key(database):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   714
            _logger.info("Get a session for %s" % database)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   715
            session = ccm.SessionPool(self.__username, self.__password, engine, dbpath, database, size, opener=self.__provider.get)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   716
            self._sessions[database] = session
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   717
            # be developer by default
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   718
            session.role = "developer"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   719
        session = self._sessions[database]
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   720
        if session.size < size:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   721
            _logger.info("Resizing the pool for database %s to %d" % (database, size))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   722
            session.size = size
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   723
            # be developer by default
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   724
            session.role = "developer"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   725
        return session
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   726
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   727
    def close(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   728
        """ This is the preparation cleanup method.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   729
            It closes all opened sessions.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   730
        """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   731
        _logger.debug("Closing sessions...")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   732
        dbs = self._sessions.keys()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   733
        while len(dbs) > 0:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   734
            session = self._sessions.pop(dbs.pop())
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   735
            session.close()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   736
        if self.__provider is not None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   737
            self.__provider.close()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   738
            self.__provider = None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   739
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   740
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   741
    def __del__(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   742
        self.close()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   743
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   744
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   745