587
|
1 |
#============================================================================
|
|
2 |
#Name : ant.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
|
|
20 |
""" This module defines helper functions to be used in python Ant tasks. """
|
|
21 |
|
|
22 |
|
|
23 |
import logging
|
|
24 |
import re
|
|
25 |
import os.path
|
|
26 |
|
|
27 |
|
|
28 |
def get_property(property_name):
|
|
29 |
""" This function return None if a property has not been replaced by Ant. """
|
|
30 |
if len(property_name) > 0 and (property_name.startswith('${') or property_name.startswith('@{')):
|
|
31 |
return None
|
|
32 |
return property_name
|
|
33 |
|
|
34 |
|
|
35 |
def get_previous_build_number(build_number):
|
|
36 |
""" Determines the previous build number if possible. """
|
|
37 |
match = re.match(r'(?P<bn_txt>\w[a-zA-Z0-9_.]*\.)?(?P<bn_num>\d+)', build_number)
|
|
38 |
if match != None:
|
|
39 |
bn_txt = match.group('bn_txt')
|
|
40 |
bn_num = match.group('bn_num')
|
|
41 |
try:
|
|
42 |
bn_num_int = int(bn_num)
|
|
43 |
if bn_num_int > 1:
|
|
44 |
previous_bn_num_int = bn_num_int - 1
|
|
45 |
previous_bn_num = str(previous_bn_num_int)
|
|
46 |
if bn_num.startswith('0'):
|
|
47 |
previous_bn_num = previous_bn_num.rjust(len(bn_num), '0')
|
|
48 |
previous_bn = previous_bn_num
|
|
49 |
if bn_txt != None:
|
|
50 |
previous_bn = '%s%s' % (bn_txt, previous_bn_num)
|
|
51 |
return previous_bn
|
|
52 |
except ValueError:
|
|
53 |
logging.warning('Parsing of Ant build number failed.')
|
|
54 |
return ''
|
|
55 |
|
|
56 |
|
|
57 |
def get_next_build_number(build_number):
|
|
58 |
""" Determines the next build number if possible. """
|
|
59 |
match = re.match(r'(?P<bn_txt>\w[a-zA-Z0-9_.]*\.)?(?P<bn_num>\d+)', build_number)
|
|
60 |
if match != None:
|
|
61 |
bn_txt = match.group('bn_txt')
|
|
62 |
bn_num = match.group('bn_num')
|
|
63 |
try:
|
|
64 |
bn_num_int = int(bn_num)
|
|
65 |
previous_bn_num_int = bn_num_int + 1
|
|
66 |
previous_bn_num = str(previous_bn_num_int).rjust(len(bn_num), '0')
|
|
67 |
previous_bn = previous_bn_num
|
|
68 |
if bn_txt != None:
|
|
69 |
previous_bn = '%s%s' % (bn_txt, previous_bn_num)
|
|
70 |
return previous_bn
|
|
71 |
except ValueError:
|
|
72 |
logging.warning('Parsing of Ant build number failed.')
|
|
73 |
return ''
|
|
74 |
|
|
75 |
def get_filesets_content(project, task, elements):
|
|
76 |
""" Extract all files selected by the filesets in a script's elements. """
|
|
77 |
for eid in range(elements.get("fileset").size()):
|
|
78 |
dirscanner = elements.get("fileset").get(int(eid)).getDirectoryScanner(project)
|
|
79 |
dirscanner.scan()
|
|
80 |
for jfilename in dirscanner.getIncludedFiles():
|
|
81 |
filename = str(jfilename)
|
|
82 |
task.log("Parsing %s" % filename)
|
|
83 |
filename = os.path.join(str(dirscanner.getBasedir()), filename)
|
|
84 |
|
|
85 |
|
|
86 |
class AntHandler(logging.Handler):
|
|
87 |
""" Implement a logger hanlder that prints error message using an Ant object.
|
|
88 |
See Python documentation on how to use it.
|
|
89 |
e.g:
|
|
90 |
logging.getLogger("").addHandler(AntHandler(anttask))
|
|
91 |
This line will redirect messages to Ant logging system.
|
|
92 |
"""
|
|
93 |
def __init__(self, task, level=logging.NOTSET):
|
|
94 |
logging.Handler.__init__(self, level)
|
|
95 |
self._task = task
|
|
96 |
|
|
97 |
def emit(self, record):
|
|
98 |
""" Handle the record using Ant. """
|
|
99 |
self._task.log(str(self.format(record)))
|