author | Daniel Jacobs <daniel.jacobs@nokia.com> |
Mon, 17 May 2010 11:07:30 +0100 | |
changeset 554 | e7dd8533063f |
parent 549 | d633be326c9f |
permissions | -rw-r--r-- |
3 | 1 |
# |
547
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
2 |
# Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). |
3 | 3 |
# All rights reserved. |
4 |
# This component and the accompanying materials are made available |
|
5 |
# under the terms of the License "Eclipse Public License v1.0" |
|
6 |
# which accompanies this distribution, and is available |
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
# |
|
9 |
# Initial Contributors: |
|
10 |
# Nokia Corporation - initial contribution. |
|
11 |
# |
|
12 |
# Contributors: |
|
13 |
# |
|
14 |
# Description: |
|
15 |
# raptor_utilities module |
|
16 |
# Useful wrapper functions and classes used in Raptor processing |
|
17 |
# |
|
18 |
||
19 |
import generic_path |
|
20 |
import os.path |
|
21 |
import re |
|
22 |
import sys |
|
547
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
23 |
import stat |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
24 |
import shutil |
3 | 25 |
|
26 |
dosSlashRegEx = re.compile(r'\\') |
|
27 |
unixSlashRegEx = re.compile(r'/') |
|
28 |
dosDriveRegEx = re.compile("^([A-Za-z]{1}):") |
|
29 |
||
30 |
def getOSPlatform(): |
|
31 |
return sys.platform.lower() |
|
32 |
||
33 |
def getOSFileSystem(): |
|
34 |
if getOSPlatform().startswith("win"): |
|
35 |
return "cygwin" |
|
36 |
else: |
|
37 |
return "unix" |
|
38 |
||
39 |
def convertToUnixSlash(aReference): |
|
40 |
return dosSlashRegEx.sub(r'/', aReference) |
|
41 |
||
42 |
def convertToDOSSlash(aReference): |
|
43 |
return unixSlashRegEx.sub(r'\\', aReference) |
|
44 |
||
45 |
def absPathFromPath(aPathRoot, aReference): |
|
46 |
pathRoot = convertToUnixSlash(aPathRoot) |
|
47 |
reference = convertToUnixSlash(aReference) |
|
48 |
||
49 |
if os.path.isabs(reference): |
|
50 |
reference = reference.lstrip(r'/') |
|
51 |
||
52 |
joined = os.path.join(pathRoot, reference) |
|
53 |
||
54 |
return os.path.abspath(joined) |
|
55 |
||
56 |
||
57 |
def absPathFromFile(aFileRoot, aReference): |
|
58 |
pathRoot = os.path.dirname(aFileRoot) |
|
59 |
return absPathFromPath(pathRoot, aReference) |
|
60 |
||
61 |
def sanitise(aPotentialFilename): |
|
62 |
"Take a string and return a version suitable for use as a filename." |
|
63 |
return re.sub("(\\\\|\/|:|;| )", "_", aPotentialFilename) |
|
64 |
||
65 |
def resolveSymbianPath(aFileRoot, aReference, aMainType="", aSubType="", aEPOCROOT="$(EPOCROOT)"): |
|
66 |
""" Convert raw Symbian metadata path/file references into absolute makefile references, or list of references |
|
67 |
||
68 |
<drive>-prefix : maps to an emulated drive depending on the following cases: |
|
69 |
(a) If the drive is C:, it maps to the *two* locations |
|
70 |
$(EPOCROOT)/epoc32/data/<drive>/<path> and |
|
71 |
$(EPOCROOT)/epoc32/winscw/<drive>/<path> |
|
72 |
(b) If the drive is A:, B:, or D: to Z:, it maps to the *three* locations |
|
73 |
$(EPOCROOT)/epoc32/data/<drive>/<path> and |
|
74 |
$(EPOCROOT)/epoc32/release/winscw/udeb/<drive>/<path> and |
|
75 |
$(EPOCROOT)/epoc32/release/winscw/urel/<drive>/<path> |
|
76 |
Absolute : true absolute if: |
|
77 |
(a) PRJ_*EXPORTS destination or DEFFILE location and |
|
78 |
(b) not starting with an 'epoc32' |
|
79 |
otherwise relative to $(EPOCROOT) |
|
80 |
Relative : relative to $(EPOCROOT)/epoc32/include if: |
|
81 |
(a) PRJ_EXPORTS destination and |
|
82 |
(b) not a :zip statement, |
|
83 |
relative to $(EPOCROOT) if: |
|
84 |
(a) PRJ_(TEST)EXPORTS destination and |
|
85 |
(b) a :zip statement, |
|
86 |
otherwise relative to aFileRoot |
|
87 |
|-prefix : relative to aFileRoot |
|
88 |
+-prefix : relative to $(EPOCROOT)/epoc32""" |
|
89 |
||
90 |
# Both reference and fileroot can have backslashes - so convert them. |
|
91 |
reference = convertToUnixSlash(aReference) |
|
92 |
fileroot = convertToUnixSlash(aFileRoot) |
|
93 |
||
94 |
# Remove Trailing backslashes so that the expansions doesnt mess up the shell |
|
95 |
if reference.endswith('/') and len(reference) > 1: |
|
96 |
reference = reference.rstrip('/') |
|
97 |
||
98 |
emulatedDrive = dosDriveRegEx.match(reference) |
|
99 |
if emulatedDrive: |
|
100 |
# Emulated drive C:/ Z:/ and the like |
|
101 |
# C: drive |
|
102 |
if reference.lower().startswith("c"): |
|
103 |
resolvedPath = [] |
|
104 |
resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/data/'+emulatedDrive.group(1), reference)) |
|
105 |
resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/winscw/'+emulatedDrive.group(1), reference)) |
|
106 |
else: # Other letters: A, B and D to Z |
|
107 |
resolvedPath = [] |
|
108 |
resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/data/'+emulatedDrive.group(1), reference)) |
|
109 |
resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/release/winscw/udeb/'+emulatedDrive.group(1), reference)) |
|
110 |
resolvedPath.append(dosDriveRegEx.sub(aEPOCROOT+'/epoc32/release/winscw/urel/'+emulatedDrive.group(1), reference)) |
|
111 |
elif os.path.isabs(reference): |
|
112 |
# Absolute |
|
113 |
if re.search("(DEFFILE|PRJ_(TEST)?EXPORTS)", aMainType, re.I) and not re.search("^\/epoc32\/", reference, re.I): |
|
114 |
# Ensures prepending of drive if on Windows |
|
115 |
resolvedPath = os.path.abspath(reference) |
|
116 |
else: |
|
117 |
resolvedPath = aEPOCROOT + reference |
|
118 |
||
119 |
elif reference.startswith("+"): |
|
120 |
# '+' prefix |
|
121 |
reference = reference.lstrip(r'+') |
|
122 |
resolvedPath = aEPOCROOT + '/epoc32'+reference |
|
123 |
elif reference.startswith("|"): |
|
124 |
# '|' prefix |
|
125 |
reference = reference.lstrip(r'|') |
|
126 |
resolvedPath = absPathFromFile(fileroot, reference) |
|
127 |
else: |
|
128 |
# Relative |
|
129 |
if aMainType == "PRJ_EXPORTS" and aSubType != ":zip": |
|
130 |
resolvedPath = aEPOCROOT + '/epoc32/include/'+reference |
|
131 |
elif aSubType == ":zip": |
|
132 |
resolvedPath = aEPOCROOT + '/' + reference |
|
133 |
else: |
|
134 |
resolvedPath = absPathFromFile(fileroot, aReference) |
|
135 |
||
136 |
if isinstance(resolvedPath, list): |
|
137 |
# In this case, this is a list of export destinations, |
|
138 |
makefilePath = map(lambda x: str(generic_path.Path(x)), resolvedPath) |
|
139 |
else: |
|
140 |
makefilePath = str(generic_path.Path(resolvedPath)) |
|
141 |
||
142 |
return makefilePath # Note this is either a list of strings, or a single string |
|
143 |
||
144 |
||
145 |
class ExternalTool(object): |
|
146 |
""" Generic wrapper for an external tool |
|
147 |
||
148 |
Provides the basic means to wrap up a tool that is external to Raptor with a |
|
149 |
consistent interface for both invocation and the capture of output.""" |
|
150 |
||
151 |
def __init__(self, aTool): |
|
152 |
self.__Tool = aTool |
|
153 |
self.__Output = [] |
|
154 |
||
155 |
def call(self, aArgs): |
|
156 |
print "RUNNNING: %s %s" %(self.__Tool, aArgs) |
|
157 |
(input, output) = os.popen2(self.__Tool + " " + aArgs) |
|
158 |
self.__Output = output.read() |
|
159 |
return output.close() |
|
160 |
||
161 |
def getTool(self): |
|
162 |
return self.__Tool |
|
163 |
||
164 |
def getOutput(self): |
|
165 |
return self.__Output |
|
166 |
||
167 |
def getOutputLines(self): |
|
168 |
return self.__Output.split("\n") |
|
169 |
||
170 |
||
171 |
class NullLog(object): |
|
172 |
""" If your class has these methods then it can act as a log """ |
|
173 |
def Info(self, format, *extras): |
|
174 |
"Send an information message to the configured channel" |
|
175 |
return |
|
176 |
||
177 |
def ClockInfo(self): |
|
178 |
"Print a timestamp in seconds" |
|
179 |
return |
|
180 |
||
181 |
def Debug(self, format, *extras): |
|
182 |
"Send a debugging message to the configured channel" |
|
183 |
return |
|
184 |
||
185 |
def Warn(self, format, *extras): |
|
186 |
"Send a warning message to the configured channel" |
|
187 |
return |
|
188 |
||
189 |
def Error(self, format, *extras): |
|
190 |
"Send an error message to the configured channel" |
|
191 |
return |
|
192 |
||
193 |
nulllog = NullLog() |
|
547
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
194 |
|
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
195 |
def copyfile(_source, _destination): |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
196 |
"""Copy the source file to the destination file (create a directory |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
197 |
to copy into if it does not exist). Don't copy if the destination |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
198 |
file exists and has an equal or newer modification time.""" |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
199 |
source = generic_path.Path(str(_source).replace('%20',' ')) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
200 |
destination = generic_path.Path(str(_destination).replace('%20',' ')) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
201 |
dest_str = str(destination) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
202 |
source_str = str(source) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
203 |
|
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
204 |
try: |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
205 |
|
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
206 |
|
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
207 |
destDir = destination.Dir() |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
208 |
if not destDir.isDir(): |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
209 |
os.makedirs(str(destDir)) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
210 |
shutil.copyfile(source_str, dest_str) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
211 |
return |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
212 |
# Destination file exists so we have to think about updating it |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
213 |
sourceMTime = 0 |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
214 |
destMTime = 0 |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
215 |
sourceStat = 0 |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
216 |
try: |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
217 |
sourceStat = os.stat(source_str) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
218 |
sourceMTime = sourceStat[stat.ST_MTIME] |
549
d633be326c9f
fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents:
547
diff
changeset
|
219 |
except OSError, e: |
d633be326c9f
fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents:
547
diff
changeset
|
220 |
message = "Source of copyfile does not exist: " + str(source) |
d633be326c9f
fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents:
547
diff
changeset
|
221 |
raise IOError(message) |
d633be326c9f
fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents:
547
diff
changeset
|
222 |
try: |
547
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
223 |
destMTime = os.stat(dest_str)[stat.ST_MTIME] |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
224 |
except OSError, e: |
549
d633be326c9f
fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents:
547
diff
changeset
|
225 |
pass # destination doesn't have to exist |
547
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
226 |
|
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
227 |
if destMTime == 0 or destMTime < sourceMTime: |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
228 |
if os.path.exists(dest_str): |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
229 |
os.chmod(dest_str,stat.S_IREAD | stat.S_IWRITE) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
230 |
shutil.copyfile(source_str, dest_str) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
231 |
|
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
232 |
# Ensure that the destination file remains executable if the source was also: |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
233 |
os.chmod(dest_str,sourceStat[stat.ST_MODE] | stat.S_IREAD | stat.S_IWRITE | stat.S_IWGRP ) |
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
234 |
|
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
235 |
|
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
236 |
except Exception,e: |
549
d633be326c9f
fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents:
547
diff
changeset
|
237 |
message = "Could not update " + dest_str + " from " + source_str + " : " + str(e) |
d633be326c9f
fix: depcrunch was using re.match where re.findall was what was wanted. Add a test.
timothy.murphy@nokia.com
parents:
547
diff
changeset
|
238 |
raise IOError(message) |
547
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
239 |
|
9fe7d0ab0f8f
fixes for review comments. better docs. copyrights. make copy filter more modular, change <copy> tag to <filtercopy>
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
240 |
return |