buildframework/helium/sf/python/pythoncore/lib/ant.py
author lorewang
Wed, 01 Dec 2010 16:05:36 +0800
changeset 715 e0739b8406dd
parent 587 85df38eb4012
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        : ant.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 module defines helper functions to be used in python Ant tasks. """
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
import logging
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import re
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import os.path
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
def get_property(property_name):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
    """ This function return None if a property has not been replaced by Ant. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
    if len(property_name) > 0 and (property_name.startswith('${') or property_name.startswith('@{')):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
        return None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
    return property_name
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
def get_previous_build_number(build_number):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
    """ Determines the previous build number if possible. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
    match = re.match(r'(?P<bn_txt>\w[a-zA-Z0-9_.]*\.)?(?P<bn_num>\d+)', build_number)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    if match != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
        bn_txt = match.group('bn_txt')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
        bn_num = match.group('bn_num')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
        try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
            bn_num_int = int(bn_num)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
            if bn_num_int > 1:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
                previous_bn_num_int = bn_num_int - 1
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
                previous_bn_num = str(previous_bn_num_int)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
                if bn_num.startswith('0'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
                    previous_bn_num = previous_bn_num.rjust(len(bn_num), '0')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
                previous_bn = previous_bn_num
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
                if bn_txt != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
                    previous_bn = '%s%s' % (bn_txt, previous_bn_num)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
                return previous_bn
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
        except ValueError:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
            logging.warning('Parsing of Ant build number failed.')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
    return ''
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
def get_next_build_number(build_number):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
    """ Determines the next build number if possible. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
    match = re.match(r'(?P<bn_txt>\w[a-zA-Z0-9_.]*\.)?(?P<bn_num>\d+)', build_number)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
    if match != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
        bn_txt = match.group('bn_txt')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
        bn_num = match.group('bn_num')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
        try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
            bn_num_int = int(bn_num)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
            previous_bn_num_int = bn_num_int + 1
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
            previous_bn_num = str(previous_bn_num_int).rjust(len(bn_num), '0')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
            previous_bn = previous_bn_num
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
            if bn_txt != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
                previous_bn = '%s%s' % (bn_txt, previous_bn_num)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
            return previous_bn
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
        except ValueError:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
            logging.warning('Parsing of Ant build number failed.')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
    return ''
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
def get_filesets_content(project, task, elements):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
    """ Extract all files selected by the filesets in a script's elements. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
    for eid in range(elements.get("fileset").size()):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
        dirscanner = elements.get("fileset").get(int(eid)).getDirectoryScanner(project)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
        dirscanner.scan()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
        for jfilename in dirscanner.getIncludedFiles():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
            filename = str(jfilename)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
            task.log("Parsing %s" % filename)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
            filename = os.path.join(str(dirscanner.getBasedir()), filename)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
class AntHandler(logging.Handler):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
    """ Implement a logger hanlder that prints error message using an Ant object.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
        See Python documentation on how to use it.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
        e.g:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
        logging.getLogger("").addHandler(AntHandler(anttask))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
        This line will redirect messages to Ant logging system.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
    """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
    def __init__(self, task, level=logging.NOTSET):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
        logging.Handler.__init__(self, level)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
        self._task = task
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
    def emit(self, record):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
        """ Handle the record using Ant. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
        self._task.log(str(self.format(record)))