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