587
|
1 |
#============================================================================
|
|
2 |
#Name : builders.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
|
|
20 |
"""
|
|
21 |
This modules contains the archive builders.
|
|
22 |
An archive builder is a class that is able to use data from a configuration
|
|
23 |
and generate a set of shell commands.
|
|
24 |
"""
|
|
25 |
import archive.tools
|
|
26 |
import archive.selectors
|
|
27 |
import archive.mappers
|
|
28 |
import archive.scanners
|
|
29 |
import logging
|
|
30 |
import pathaddition.relative
|
|
31 |
import buildtools
|
|
32 |
import os
|
|
33 |
import codecs
|
|
34 |
import fileutils
|
|
35 |
|
|
36 |
_logger = logging.getLogger('archive')
|
|
37 |
_logger.setLevel(logging.INFO)
|
|
38 |
|
|
39 |
class ArchivePreBuilder(buildtools.PreBuilder):
|
|
40 |
""" Processes an archive build specification. """
|
|
41 |
def __init__(self, config_set, config_name, writerType='ant', index = None):
|
|
42 |
buildtools.PreBuilder.__init__(self, config_set)
|
|
43 |
self.configs = config_set.getConfigurations()
|
|
44 |
self.spec_name = config_name
|
|
45 |
self.index = index
|
|
46 |
self.writerType = writerType
|
|
47 |
self.listToFindPrefix = []
|
|
48 |
|
|
49 |
def build_manifest(self, config):
|
|
50 |
""" Generate a manifest file from the a configuration. """
|
|
51 |
_logger.info('Processing archive config: ' + config['name'])
|
|
52 |
_scanners = archive.scanners.get_scanners(config.get_list('scanners', ['default']), config)
|
|
53 |
|
|
54 |
content_list = {}
|
|
55 |
|
|
56 |
if not os.path.exists(config['temp.build.dir']):
|
|
57 |
os.makedirs(config['temp.build.dir'])
|
|
58 |
manifest_file_path = os.path.abspath(os.path.join(config['temp.build.dir'], config['name'] + '_includefile.txt'))
|
|
59 |
out = codecs.open(manifest_file_path, 'w+', 'utf-8')
|
|
60 |
|
|
61 |
# zip.root.dir can be set to root.dir so that when zipping from another dir,
|
|
62 |
# the manifest is relative to that dir
|
|
63 |
(_, root_dir) = os.path.splitdrive(os.path.normpath(config.get('zip.root.dir', config['root.dir'])))
|
|
64 |
_logger.info(" * Scanning")
|
|
65 |
for scanner in _scanners:
|
|
66 |
_logger.debug("Scanner %s" % scanner)
|
|
67 |
for subpath in scanner.scan():
|
|
68 |
(_, subpath) = os.path.splitdrive(subpath)
|
|
69 |
if pathaddition.relative.abs2rel(subpath, root_dir):
|
|
70 |
_logger.debug(subpath)
|
|
71 |
subpath = subpath[len(root_dir):]
|
|
72 |
if subpath.startswith(os.sep):
|
|
73 |
subpath = subpath[1:]
|
|
74 |
# normpath is to remove any occurances of "..\.." before checking for duplicates
|
|
75 |
subpath = os.path.normpath(subpath)
|
|
76 |
if subpath not in content_list:
|
|
77 |
out.write(u"".join([subpath, u'\n']))
|
|
78 |
content_list[subpath] = True
|
|
79 |
|
|
80 |
out.close()
|
|
81 |
return manifest_file_path
|
|
82 |
|
|
83 |
def manifest_to_commands(self, config, manifest):
|
|
84 |
""" Generate return a command list. Commands are stored in a two dimension array."""
|
|
85 |
_logger.info(" * Generating commands")
|
|
86 |
tool = archive.tools.get_tool(config['archive.tool'])
|
|
87 |
mapper_name = 'default'
|
|
88 |
if config.has_key('mapper'):
|
|
89 |
mapper_name = config['mapper']
|
|
90 |
mapper = archive.mappers.get_mapper(mapper_name, config, tool)
|
|
91 |
return mapper.create_commands(manifest)
|
|
92 |
|
|
93 |
def create_command_list(self, commands):
|
|
94 |
""" Convert a two dimensions array of command to a CommandList object. """
|
|
95 |
stages = buildtools.CommandList()
|
|
96 |
newstage = False
|
|
97 |
for cmds_stage in commands:
|
|
98 |
_logger.debug("Stage: %s" % cmds_stage)
|
|
99 |
for cmd in cmds_stage:
|
|
100 |
stages.addCommand(cmd, newstage)
|
|
101 |
newstage = False
|
|
102 |
newstage = True
|
|
103 |
return stages
|
|
104 |
|
|
105 |
def writeTopLevel(self, build_file_path, output_path, xml_file):
|
|
106 |
"""Creates a build tool config makefile that executes archieve build."""
|
|
107 |
config_name_list = []
|
|
108 |
for config in self.configs:
|
|
109 |
config_name_list.append(config['name'])
|
|
110 |
if not os.path.exists(config['archives.dir']):
|
|
111 |
os.makedirs(config['archives.dir'])
|
|
112 |
|
|
113 |
writer = buildtools.get_writer(self.writerType, build_file_path)
|
|
114 |
writer.writeTopLevel(config_name_list, self.spec_name, output_path, xml_file)
|
|
115 |
writer.close()
|
|
116 |
|
|
117 |
def getCommonUncRoots(self, uncPaths):
|
618
|
118 |
""" Get common UNC roots.
|
|
119 |
The following [r'\\server1\foo\bar', r'\\server2\foo', r'\\server1\foo'] input
|
|
120 |
will return [r'\\server1\foo\', r'\\server2\foo'].
|
|
121 |
"""
|
587
|
122 |
commonRoots = {}
|
618
|
123 |
uncPaths = [os.path.normpath(p_path) for p_path in uncPaths]
|
|
124 |
# sorting the input by alphabetical order, so smaller roots
|
|
125 |
# are coming first.
|
|
126 |
uncPaths.sort()
|
587
|
127 |
for p_path in uncPaths:
|
618
|
128 |
common_path = "\\\\"
|
|
129 |
for p_comp in p_path[2:].split(os.sep):
|
|
130 |
common_path = common_path + p_comp + os.sep
|
|
131 |
if common_path in commonRoots:
|
|
132 |
break
|
|
133 |
else:
|
|
134 |
commonRoots[common_path] = 1
|
587
|
135 |
return commonRoots.keys()
|
|
136 |
|
|
137 |
def getPrefix(self, dir, commonUncRoots):
|
|
138 |
"""get prefix"""
|
|
139 |
for root in commonUncRoots:
|
|
140 |
if dir.startswith(root):
|
|
141 |
return root
|
|
142 |
raise Exception("Could not find root for %s." % dir)
|
|
143 |
|
|
144 |
def checkRootDirValue(self, builder, parse_xml_file, build_drive, config_type):
|
|
145 |
"""Checks UNC path in root.dir and adds the substituted drive into EMAKEROOT."""
|
|
146 |
substDrives = []
|
|
147 |
if build_drive:
|
|
148 |
substDrives.append(build_drive + os.sep)
|
|
149 |
|
|
150 |
# Read all the config's root.dir to get UNC Path if any
|
|
151 |
# Of course this is only on windows platform
|
|
152 |
if os.sep == '\\':
|
|
153 |
for config in self.configs:
|
618
|
154 |
(drive, root_dir) = os.path.splitdrive(os.path.abspath(os.path.normpath(config['root.dir'])))
|
587
|
155 |
_logger.debug("drive=%s, root_dir=%s" % (drive, root_dir))
|
|
156 |
if drive == "":
|
|
157 |
self.listToFindPrefix.append(root_dir)
|
|
158 |
|
|
159 |
commonUncRoots = self.getCommonUncRoots(self.listToFindPrefix)
|
|
160 |
_logger.debug("Common roots %s" % (commonUncRoots))
|
|
161 |
driveMapping = {}
|
|
162 |
for root in commonUncRoots:
|
|
163 |
_logger.info("Substing %s" % (root))
|
|
164 |
driveMapping[root] = self.substUncPath(root)
|
|
165 |
_logger.debug("%s subst as %s" % (root, driveMapping[root]))
|
|
166 |
substDrives.append(driveMapping[root] + os.sep)
|
|
167 |
|
|
168 |
for config in self.configs:
|
618
|
169 |
(drive, root_dir) = os.path.splitdrive(os.path.abspath(os.path.normpath(config['root.dir'])) + os.sep)
|
587
|
170 |
if drive == "":
|
|
171 |
for root in driveMapping:
|
|
172 |
if root_dir.startswith(root):
|
|
173 |
config['root.dir'] = os.path.normpath(driveMapping[root] + os.sep + root_dir[len(root):len(root_dir)])
|
|
174 |
_logger.info("Updated %s in %s" % (root_dir, config['root.dir']))
|
|
175 |
config['unsubst.dir'] = driveMapping[root]
|
618
|
176 |
break
|
|
177 |
else:
|
|
178 |
_logger.error("Could not find root for %s." % root_dir)
|
587
|
179 |
elif drive != build_drive:
|
|
180 |
if config['root.dir'] not in substDrives:
|
|
181 |
substDrives.append(config['root.dir'])
|
|
182 |
else:
|
|
183 |
for config in self.configs:
|
|
184 |
if config['root.dir'].startswith('\\\\'):
|
|
185 |
_logger.error("UNC path are not supported under this platform: %s" % (config['root.dir']))
|
|
186 |
builder.writeToXML(parse_xml_file, self.configs, config_type)
|
|
187 |
return os.path.pathsep.join(substDrives)
|
|
188 |
|
|
189 |
|
|
190 |
def substUncPath(self, path):
|
|
191 |
"""substitute UNC path"""
|
|
192 |
freedrive = fileutils.get_next_free_drive()
|
|
193 |
fileutils.subst(freedrive, path)
|
|
194 |
return freedrive
|
|
195 |
|
|
196 |
def cleanupSubstDrives(self):
|
|
197 |
""" Read all the config's root.dir to get UNC Path if any"""
|
|
198 |
drives = {}
|
|
199 |
for config in self.configs:
|
|
200 |
_logger.debug("Checking configuration...")
|
|
201 |
_logger.debug("unsubst.dir: %s" % 'unsubst.dir' in config)
|
|
202 |
_logger.debug("drives: %s" % drives)
|
|
203 |
if 'unsubst.dir' in config and not config['unsubst.dir'] in drives:
|
|
204 |
_logger.debug("Found drive to unsubst %s" % (config['unsubst.dir']))
|
|
205 |
self.unSubStituteDrives(config['unsubst.dir'])
|
|
206 |
drives[config['unsubst.dir']] = config['unsubst.dir']
|
|
207 |
|
|
208 |
def unSubStituteDrives(self, drive):
|
|
209 |
"""un-substitute Drives"""
|
|
210 |
_logger.info("Unsubsting %s" % (drive))
|
|
211 |
fileutils.unsubst(drive)
|
|
212 |
|
|
213 |
def write(self, outputname):
|
|
214 |
"""Creates a build tool configuration file that executes archive build operations.
|
|
215 |
|
|
216 |
The input to each archive build operation is an includefile that lists
|
|
217 |
all the files to be included in the archive. These text files are
|
|
218 |
generated before the build file by scanning the filesystem.
|
|
219 |
"""
|
|
220 |
stages = buildtools.CommandList()
|
|
221 |
|
|
222 |
commands = []
|
|
223 |
if self.index > len(self.configs):
|
|
224 |
raise Exception("index not found in configuration")
|
|
225 |
config = self.configs[self.index]
|
|
226 |
stages = self.manifest_to_commands(config, self.build_manifest(config))
|
|
227 |
|
|
228 |
# merging the commands
|
|
229 |
while len(commands) < len(stages):
|
|
230 |
commands.append([])
|
|
231 |
for i in range(len(stages)):
|
|
232 |
commands[i].extend(stages[i])
|
|
233 |
|
|
234 |
writer = buildtools.get_writer(self.writerType, outputname)
|
|
235 |
writer.write(self.create_command_list(commands))
|
|
236 |
writer.close()
|