1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
#
# Defines Ice components
#
import ConfigParser, sys, os, logging, fnmatch, os.path, shutil, re, pprint
def listFileLists():
"""Information routine for getting lists of file lists from a components file"""
cfg = ConfigParser.SafeConfigParser()
cfg.read("./components/components.ini")
for f in cfg.sections():
try:
if cfg.getint(f, "active") == 1:
for item, value in cfg.items(f):
if item.startswith("filelist"):
print value
except ConfigParser.NoOptionError:
continue
class StageFileError:
"""Thrown when there is a problem with the stage configuration file."""
def __init__(self, msg = None):
self.msg = msg
def __str__(self):
return repr(self.msg)
class ComponentDefError:
"""Indicates a component definition file has an error that provides
proper interpretation"""
def __init__(self, msg = None):
self.msg = msg
def __str__(self):
return repr(self.msg)
class FileSpecError(ComponentDefError):
"""Indicates a filespec component definition file has a syntactical
error"""
def __init__(self, msg = None):
ComponentDefError.__init__(self, msg)
def recursiveListing(path):
"""Provides a recursive directory listing based in path"""
result = []
files = os.listdir(path)
for x in files:
fullpath = os.path.join(path, x);
if os.path.isdir(fullpath) and not os.path.islink(fullpath):
result.append(fullpath)
result.extend(recursiveListing(fullpath))
else:
result.append(fullpath)
return result
def fixVersion(file, argsHash):
if not argsHash.has_key('version'):
print "fixVersion unable to execute, no version information available"
return
version = argsHash['version']
libversion = argsHash['dllversion']
origfile = file + ".orig"
os.rename(file, origfile)
oldFile = open(origfile, "r")
newFile = open(file, "w")
line = oldFile.read();
line = re.sub("@ver@", version, line)
line = re.sub("@libver@", libversion, line)
newFile.write(line)
newFile.close()
oldFile.close()
os.remove(origfile)
class FileSpecWorker:
def __init__(self, id, source, dest, processors):
self.id = id
self.source = source
self.dest = dest
self.include = []
self.exclude = []
self.explicit = []
self.processors = []
for p in processors:
if globals().has_key(p.strip()):
self.processors.append(p.strip())
else:
print "Possible component configuration error:"
print " Section %s is configured with non-existant processor %s." % (id, p.trim())
def add(self, filename):
parts = filename.split("=")
if len(parts) < 2:
#
# This line doesn"t have a separator and so assume its an
# explicit listing
#
self.explicit.append(filename)
return
if parts[0].startswith("include"):
self.include.append(parts[1].strip())
elif parts[0].startswith("exclude"):
self.exclude.append(parts[1].strip())
elif parts[0].startswith("expect"):
pass
else:
raise FileSpecError("Line \'%s\' does not match filespec schema." % filename)
def execute(self, defaults, fake = False):
"""Copy all of the specified files."""
recursiveIncludes = []
recursiveExcludes = []
midmatchIncludes = []
midmatchExcludes = []
localIncludes = []
localExcludes = []
for f in self.include:
if f.startswith("**/"):
if f.endswith("/**"):
midmatchIncludes.append(".*%s.*" %f[3:len(f) -3].replace('/', '.'))
else:
recursiveIncludes.append("*%s" % f[3:])
else:
if f.endswith("/**"):
midmatchIncludes.append("%s.*" %f[0:len(f) -3].replace('/', '.'))
else:
localIncludes.append(f)
for f in self.exclude:
if f.startswith("**/"):
if f.endswith("/**"):
midmatchExcludes.append(".*%s.*" % f[3:len(f) -3].replace('/', '.'))
else:
recursiveExcludes.append("*%s" % f[3:])
else:
if f.endswith("/**"):
midmatchExcludes.append("%s.*" %f[0:len(f) -3].replace('/', '.'))
else:
localExcludes.append(f)
logging.debug('localIncludes: ' + str(localIncludes))
logging.debug('localExcludes: ' + str(localExcludes))
logging.debug('recursiveIncludes: ' + str(recursiveIncludes))
logging.debug('recursiveExcludes: ' + str(recursiveExcludes))
logging.debug('midmatchIncludes: ' + str(midmatchIncludes))
logging.debug('midmatchExcludes: ' + str(midmatchExcludes))
fullListing = []
result = []
files = os.listdir(self.source)
for f in files:
fullpath = os.path.join(self.source, f);
if os.path.isdir(fullpath) and not os.path.islink(fullpath):
fullListing.extend(recursiveListing(fullpath))
continue
for p in localIncludes + recursiveIncludes:
if fnmatch.fnmatch(f, p):
found = False
for x in localExcludes + recursiveExcludes:
if fnmatch.fnmatch(f, x):
found = True
break
if not found: result.append(f)
inmatches = []
for p in recursiveIncludes:
inmatches.extend(fnmatch.filter(fullListing, p))
inSet = set(inmatches)
for p in midmatchIncludes:
r = re.compile(p)
inmatches = []
for f in fullListing(f):
rel = f[len(self.source):].strip('\\/')
if not r.match(rel) == None:
inmatches.append(f)
inSet = inSet.union(set(inmatches))
outmatches = []
for x in recursiveExcludes:
outmatches.extend(fnmatch.filter(fullListing, x))
outSet = set(outmatches)
for x in midmatchExcludes:
r = re.compile(x)
outmatches = []
for f in fullListing:
rel = f[len(self.source):].strip('\\/')
if not r.match(rel) == None:
outmatches.append(f)
outSet = outSet.union(set(outmatches))
#
# Using sets is the "easiest" way to do this. If Python's set
# implementation is/gets buggy then this needs to be written
# "longhand".
#
diff = inSet - outSet
result.extend(list(diff))
for i in range(0, len(result)):
if result[i].startswith(self.source):
result[i] = result[i][len(self.source):].strip('\\/')
result.sort()
result.extend(self.explicit)
if fake:
for f in result:
print "Copying %s from %s to %s" % (f, self.source, self.dest)
return
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
logging.debug("Files to be copied:")
for f in result:
logging.debug(f)
#
# Detect changes in file list and confirm that changes are okay.
#
cachedResults = None
cacheFilename = os.path.join(os.path.dirname(__file__), "%s.cache" % self.id)
if os.path.exists(cacheFilename):
cacheFile = open(cacheFilename, "r+b")
cachedResults = cacheFile.readlines()
cacheFile.close()
if cachedResults != None:
for i in range(0, len(cachedResults)):
cachedResults[i] = cachedResults[i].rstrip()
previous = set(cachedResults)
current = set(result)
added = current - previous
removed = previous - current
if len(added) > 0:
print "Additions detected:"
pprint.pprint(list(added))
print "Accept (y/n):",
while True:
answer = sys.stdin.read(1)
if answer in ["Y", "y"]:
break
elif answer in ["N", "n"]:
print "\nAborting..."
rejects = open("%s.rejects" % self.id, "w")
rejects.write("Added:\n")
rejects.writelines(pprint.pprint(list(added)))
rejects.write("Deleted:\n")
rejects.writelines(pprint.pprint(list(removed)))
rejects.close()
sys.exit(1)
if len(removed) > 0:
print "Deletions detected:"
pprint.pprint(list(removed))
print "Accept (y/n):",
while True:
answer = sys.stdin.read(1)
if answer in ["Y", "y"]:
break
elif answer in ["N", "n"]:
print "\nAborting..."
rejects = open("%s.rejects" % self.id, "w")
rejects.write("Added:\n")
rejects.writelines(pprint.pprint(list(added)))
rejects.write("Deleted:\n")
rejects.writelines(pprint.pprint(list(removed)))
rejects.close()
sys.exit(1)
cacheFile = open(cacheFilename, "w+b")
for f in result:
cacheFile.write(f)
cacheFile.write("\n")
cacheFile.close()
#
# Scan filename to see if matches one of our designated
# 'convert to dos file format' name patterns. (These are
# regex patterns, not patterns for filename globbing).
#
textFiles = [".*README.*", ".*Makefile.mak", ".*LICENSE.*"]
textFileScanner = None
expression = ""
for p in textFiles:
if expression != "":
expression = expression + "|"
expression = expression + p
textFileScanner = re.compile(expression)
for f in result:
#
# an f, prefix means flatten.
#
flatten = False
current = f
if f.startswith('f,'):
flatten = True
current = current[2:]
current = current % defaults
targetDirectory = self.dest
targetFile = os.path.basename(current)
if not flatten:
targetDirectory = os.path.join(self.dest, os.path.dirname(current))
if not os.path.exists(targetDirectory):
os.makedirs(targetDirectory)
s = os.path.join(self.source, current)
d = os.path.join(targetDirectory, targetFile)
try:
if os.path.isdir(s):
os.mkdir(d)
else:
shutil.copy2(s, d)
isTextFile = (textFileScanner.search(d) != None)
if isTextFile:
# So how do I do the conversion.
tmp = open(d + ".bak", "w")
tmp.write(open(d, "rU").read())
tmp.close()
shutil.copy2(d + ".bak", d)
os.remove(d + ".bak")
if self.processors != None and len(self.processors) > 0:
for p in self.processors:
e = globals()[p.strip()]
e(d, defaults)
except IOError, e:
logging.info('Copying %s to %s failed: %s' % (s, d, str(e)))
raise
def stage(filename, componentdir, stageDirectory, group, defaults):
cfg = ConfigParser.SafeConfigParser()
cfg.read(filename)
if not cfg.has_section(group):
raise StageFileError("Section %s is missing from component list file." % group)
sections = []
for entry in cfg.options(group):
section = cfg.get(group, entry)
if not cfg.has_section(section):
raise StageFileError("Section %s is missing from component list file." % section)
sections.append((entry, section))
for stageLoc, section in sections:
try:
currentBase = stageDirectory
for f in stageLoc.split('-'):
currentBase = os.path.join(currentBase, f)
if not os.path.exists(currentBase):
os.makedirs(currentBase)
elementCount = cfg.getint(section, "elements")
if elementCount < 0:
raise StageFileError("Section %s elements field is negative value" % section)
for i in range(1, elementCount + 1):
try:
if cfg.has_option(section, "targets"):
target = cfg.get(section, "targets", False, defaults).strip()
if "~" + defaults["target"] in target:
continue
elif not defaults["target"] in target:
continue
source = cfg.get(section, "source%d" % i, False, defaults)
filelist = cfg.get(section, "filelist%d" % i, False, defaults)
dest = cfg.get(section, "dest%d" % i, False, defaults)
#
# Most configurations won"t have file templates. These are usually only used so that file lists can
# be reused but need to be slightly modified for each use case.
#
template = None
mapping = None
if cfg.has_option(section, "filetemplate%d" % i):
#
# We need to get the raw value!
#
template = cfg.get(section, "filetemplate%d" %i, True)
mapping = defaults
processors = []
if cfg.has_option(section, "processor%d" % i):
processors = cfg.get(section, "processor%d" % i).split(',')
filename = os.path.join(componentdir, filelist)
if not (os.path.exists(filename) and os.path.isfile(filename)):
raise StageFileError("Component file %s does not exist or is not a file" % filename)
componentFile = file(filename, "r")
try:
worker = FileSpecWorker("%s.%s.%d" % (filename, section.replace(" ", "_"), i), source, os.path.join(currentBase, dest), processors)
for line in componentFile:
current = line.strip()
if line.startswith('#'):
continue
if len(current) == 0:
continue
if not template == None:
mapping['name'] = current
try:
computedName = template % mapping
except:
print "Mapping exception occurred with " + template + " and " + current
raise
logging.log(logging.DEBUG, 'Adding templatized name %s' % computedName)
worker.add(computedName)
else:
try:
worker.add(current % defaults)
except Exception, e:
print str(e) + ": occured while adding %s to worker in element %d in %s" % \
(current, i, section)
if worker == None:
msg = "Component file %s is empty." % filename
logging.warning(msg)
else:
#
# NOTE: set fake to true while debugging.
#
worker.execute(defaults, False)
finally:
componentFile.close()
except ConfigParser.NoOptionError:
raise StageFileError("Section %s has invalid value for element %d" % (section, i))
except ConfigParser.NoOptionError:
raise StageFileError("Section %s has invalid or missing elements field" % section)
if __name__ == "__main__":
print 'components'
|