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
|
#!/usr/bin/env python
# CSS Test Suite Build Script
# Copyright 2011 Hewlett-Packard Development Company, L.P.
# Initial code by fantasai, joint copyright 2010 W3C and Microsoft
# Licensed under BSD 3-Clause: <http://www.w3.org/Consortium/Legal/2008/03-bsd-license>
import sys
import os
import json
import optparse
import shutil
from collections import defaultdict
from apiclient import apiclient
from w3ctestlib import Sources, Utils, Suite, Indexer
from mercurial import ui
class Builder(object):
def __init__(self, ui, outputPath, backupPath, ignorePaths, onlyCache):
self.reset(onlyCache)
self.ui = ui
self.skipDirs = ('support')
self.rawDirs = {'other-formats': 'other'}
self.sourceTree = Sources.SourceTree()
self.sourceCache = Sources.SourceCache(self.sourceTree)
self.cacheDir = 'tools/cache'
self.outputPath = outputPath.rstrip('/') if (outputPath) else 'dist'
self.backupPath = backupPath.rstrip('/') if (backupPath) else None
self.ignorePaths = [path.rstrip('/') for path in ignorePaths] if (ignorePaths) else []
self.workPath = 'build-temp'
self.ignorePaths += (self.outputPath, self.backupPath, self.workPath)
def reset(self, onlyCache):
self.useCacheOnly = onlyCache
self.shepherd = apiclient.apiclient.APIClient('https://api.csswg.org/shepherd/', version = 'vnd.csswg.shepherd.v1') if (not onlyCache) else None
self.cacheData = False
self.testSuiteData = {}
self.specificationData = {}
self.flagData = {}
self.specNames = {}
self.specAnchors = {}
self.buildSuiteNames = []
self.buildSpecNames = {}
self.testSuites = {}
def _loadShepherdData(self, apiName, description, **kwargs):
self.ui.status("Loading ", description, " information\n")
cacheFile = os.path.join(self.cacheDir, apiName + '.json')
if (not self.useCacheOnly or (not os.path.exists(cacheFile))):
result = self.shepherd.get(apiName, **kwargs)
if (result and (200 == result.status)):
data = {}
for name in result.data: # trim leading _
data[name[1:]] = result.data[name]
with open(cacheFile, 'w') as file:
json.dump(data, file)
return data
self.ui.status("Shepherd API call failed, result: ", result.status if result else 'None', "\n")
if (os.path.exists(cacheFile)):
self.ui.status("Loading cached data.\n")
try:
with open(cacheFile, 'r') as file:
return json.load(file)
except:
pass
return None
def _addAnchors(self, anchors, specName):
for anchor in anchors:
self.specAnchors[specName].add(anchor['uri'].lower())
if ('children' in anchor):
self._addAnchors(anchor['children'], specName)
def _normalizeScheme(self, url):
if (url and url.startswith('http:')):
return 'https:' + url[5:]
return url
def getSpecName(self, url):
if (not self.specNames):
for specName in self.specificationData:
specData = self.specificationData[specName]
officialURL = self._normalizeScheme(specData.get('base_uri'))
if (officialURL):
if (officialURL.endswith('/')):
officialURL = officialURL[:-1]
self.specNames[officialURL.lower()] = specName
draftURL = self._normalizeScheme(specData.get('draft_uri'))
if (draftURL):
if (draftURL.endswith('/')):
draftURL = draftURL[:-1]
self.specNames[draftURL.lower()] = specName
self.specAnchors[specName] = set()
if ('anchors' in specData):
self._addAnchors(specData['anchors'], specName)
if ('draft_anchors' in specData):
self._addAnchors(specData['draft_anchors'], specName)
url = self._normalizeScheme(url.lower())
for specURL in self.specNames:
if (url.startswith(specURL) and
((url == specURL) or
url.startswith(specURL + '/') or
url.startswith(specURL + '#'))):
anchorURL = url[len(specURL):]
if (anchorURL.startswith('/')):
anchorURL = anchorURL[1:]
specName = self.specNames[specURL]
if (anchorURL in self.specAnchors[specName]):
return (specName, anchorURL)
return (specName, None)
return (None, None)
def gatherTests(self, dir):
dirName = os.path.basename(dir)
if (dirName in self.skipDirs):
return
self.ui.note("Scanning directory: ", dir, "\n")
suiteFileNames = defaultdict(set)
for fileName in Utils.listfiles(dir):
filePath = os.path.join(dir, fileName)
if not self.sourceTree.isTestCase(filePath):
continue
source = self.sourceCache.generateSource(filePath, fileName)
if not source.isTest():
continue
try:
metaData = source.getMetadata(True)
except Exception as error:
self.ui.warn("Exception parsing '", filePath, "': ", error, "\n")
continue
if not metaData:
if (source.errors):
self.ui.warn("Error parsing '", filePath, "': ", ' '.join(source.errors), "\n")
else:
self.ui.warn("No metadata available for '", filePath, "'\n")
continue
for specURL in metaData['links']:
specName, anchorURL = self.getSpecName(specURL)
if not specName:
self.ui.note("Unknown specification URL: ", specURL, "\n in: ", filePath, "\n")
continue
if not specName in self.buildSpecNames:
continue
if not anchorURL:
self.ui.warn("Test links to unknown specification anchor: ", specURL, "\n in: ", filePath, "\n")
continue
for testSuiteName in self.buildSpecNames[specName]:
formats = self.testSuiteData[testSuiteName].get('formats')
if (formats):
for formatName in formats:
if (((formatName) in self.formatData) and
(self.formatData[formatName].get('mime_type') == source.mimetype)):
suiteFileNames[testSuiteName].add(fileName)
break
else:
self.ui.note("Test not in acceptable format: ", source.mimetype, "\n for: ", filePath, "\n")
for testSuiteName in suiteFileNames:
if (dirName in self.rawDirs):
self.testSuites[testSuiteName].addRaw(dir, self.rawDirs[dirName])
else:
self.testSuites[testSuiteName].addTestsByList(dir, suiteFileNames[testSuiteName])
for subDir in Utils.listdirs(dir):
subDirPath = os.path.join(dir, subDir)
if (not (self.sourceTree.isIgnoredDir(subDirPath) or (subDirPath in self.ignorePaths))):
self.gatherTests(subDirPath)
def _findSections(self, baseURL, anchors, sectionData, parentSectionName = ''):
if (anchors):
for anchor in anchors:
if ('section' in anchor):
sectionData.append((baseURL + anchor['uri'], anchor['name'],
anchor['title'] if 'title' in anchor else 'Untitled'))
else:
sectionData.append((baseURL + anchor['uri'], parentSectionName + '.#' + anchor['name'], None))
if ('children' in anchor):
self._findSections(baseURL, anchor['children'], sectionData, anchor['name'])
return sectionData
def getSections(self, specName):
specData = self.specificationData[specName]
specURL = specData['base_uri'] if ('base_uri' in specData) else specData.get('draft_uri')
anchorData = specData['anchors'] if ('anchors' in specData) else specData['draft_anchors']
sectionData = []
self._findSections(specURL, anchorData, sectionData)
return sectionData
def _user(self, user):
if (user):
data = user['full_name']
if ('organization' in user):
data += ', ' + user['organization']
if ('uri' in user):
data += ', ' + user['uri']
elif ('email' in user):
data += ', <' + user['email'].replace('@', ' @') + '>'
return data
return 'None Yet'
def getSuiteData(self):
data = {}
for testSuiteName in self.testSuiteData:
testSuiteData = self.testSuiteData[testSuiteName]
specData = self.specificationData[testSuiteData['specs'][0]]
data[testSuiteName] = {
'title': testSuiteData['title'] if ('title' in testSuiteData) else 'Untitled',
'spec': specData['title'] if ('title' in specData) else specData['name'],
'specroot': specData['base_uri'] if ('base_uri' in specData) else specData.get('draft_uri'),
'draftroot': specData['draft_uri'] if ('draft_uri' in specData) else specData.get('base_uri'),
'owner': self._user(testSuiteData['owners'][0] if ('owners' in testSuiteData) else None),
'harness': testSuiteName,
'status': testSuiteData['status'] if ('status' in testSuiteData) else 'Unknown'
}
return data
def getFlags(self):
data = {}
for flag in self.flagData:
flagData = self.flagData[flag]
data[flag] = {
'title': flagData['description'] if ('description' in flagData) else 'Unknown',
'abbr': flagData['title'] if ('title' in flagData) else flag
}
return data
def build(self, testSuiteNames):
try:
os.makedirs(self.cacheDir)
except:
pass
self.testSuiteData = self._loadShepherdData('test_suites', 'test suite', repo = 'css')
if (not self.testSuiteData):
self.ui.warn("ERROR: Unable to load test suite information.\n")
return -1
if (testSuiteNames):
self.buildSuiteNames = []
for testSuiteName in testSuiteNames:
if (testSuiteName in self.testSuiteData):
self.buildSuiteNames.append(testSuiteName)
else:
self.ui.status("Unknown test suite: ", testSuiteName, "\n")
else:
self.buildSuiteNames = [testSuiteName for testSuiteName in self.testSuiteData if self.testSuiteData[testSuiteName].get('build')]
self.buildSpecNames = defaultdict(list)
if (self.buildSuiteNames):
self.specificationData = self._loadShepherdData('specifications', 'specification', anchors = True, draft = True)
if (not self.specificationData):
self.ui.warn("ERROR: Unable to load specification information.\n")
return -2
for testSuiteName in self.buildSuiteNames:
specNames = self.testSuiteData[testSuiteName].get('specs')
if (specNames):
for specName in specNames:
if (specName in self.specificationData):
self.buildSpecNames[specName].append(testSuiteName)
else:
self.ui.warn("WARNING: Test suite '", testSuiteName, "' references unknown specification: '", specName, "'.\n")
else:
self.ui.warn("ERROR: Test suite '", testSuiteName, "' does not have target specifications.\n")
return -6
else:
self.ui.status("No test suites identified\n")
return 0
if (not self.buildSpecNames):
self.ui.status("No target specifications identified\n")
return -3
self.flagData = self._loadShepherdData('test_flags', 'test flag')
if (not self.flagData):
self.ui.warn("ERROR: Unable to load flag information\n")
return -4
self.formatData = self._loadShepherdData('test_formats', 'test format')
if (not self.formatData):
self.ui.warn("ERROR: Unable to load format information\n")
return -5
self.buildSuiteNames.sort()
for testSuiteName in self.buildSuiteNames:
data = self.testSuiteData[testSuiteName]
specData = self.specificationData[data['specs'][0]]
specURL = specData['base_uri'] if ('base_uri' in specData) else specData.get('draft_uri')
draftURL = specData['draft_uri'] if ('draft_uri' in specData) else specData.get('base_uri')
self.testSuites[testSuiteName] = Suite.TestSuite(testSuiteName, data['title'], specURL, draftURL, self.sourceCache, self.ui) # XXX need to support multiple specs
if ('formats' in data):
self.testSuites[testSuiteName].setFormats(data['formats'])
self.ui.status("Scanning test files\n")
for dir in Utils.listdirs('.'):
if (not (self.sourceTree.isIgnoredDir(dir) or (dir in self.ignorePaths))):
self.gatherTests(dir)
if (os.path.exists(self.workPath)):
self.ui.note("Clearing work path: ", self.workPath, "\n")
shutil.rmtree(self.workPath)
suiteData = self.getSuiteData()
flagData = self.getFlags()
templatePath = os.path.join('tools', 'templates')
for testSuiteName in self.buildSuiteNames:
testSuite = self.testSuites[testSuiteName]
self.ui.status("Building ", testSuiteName, "\n")
specSections = self.getSections(self.testSuiteData[testSuiteName]['specs'][0])
indexer = Indexer.Indexer(testSuite, specSections, suiteData, flagData, True,
templatePathList = [templatePath],
extraData = {'devel' : False, 'official' : True })
workPath = os.path.join(self.workPath, testSuiteName)
testSuite.buildInto(workPath, indexer)
# move from work path to output path
for testSuiteName in self.buildSuiteNames:
workPath = os.path.join(self.workPath, testSuiteName)
outputPath = os.path.join(self.outputPath, testSuiteName)
backupPath = os.path.join(self.backupPath, testSuiteName) if (self.backupPath) else None
if (os.path.exists(workPath)):
if (os.path.exists(outputPath)):
if (backupPath):
if (os.path.exists(backupPath)):
self.ui.note("Removing ", backupPath, "\n")
shutil.rmtree(backupPath) # delete old backup
self.ui.note("Backing up ", outputPath, " to ", backupPath, "\n")
shutil.move(outputPath, backupPath) # backup old output
else:
self.ui.note("Removing ", outputPath, "\n")
shutil.rmtree(outputPath) # no backups, delete old output
self.ui.note("Moving ", workPath, " to ", outputPath, "\n")
shutil.move(workPath, outputPath)
if (os.path.exists(self.workPath)):
shutil.rmtree(self.workPath)
return 0
if __name__ == "__main__": # called from the command line
parser = optparse.OptionParser(usage = "usage: %prog [options] test_suite [...]")
parser.add_option("-q", "--quiet",
action = "store_true", dest = "quiet", default = False,
help = "don't print status messages to stdout")
parser.add_option("-d", "--debug",
action = "store_true", dest = "debug", default = False,
help = "print detailed debugging information to stdout")
parser.add_option("-v", "--verbose",
action = "store_true", dest = "verbose", default = False,
help = "print more detailed debugging information to stdout")
parser.add_option("-o", "--output", dest = "output", metavar = "OUTPUT_PATH",
help = "Path to build into (default 'dist')")
parser.add_option("-b", "--backup", dest = "backup", metavar = "BACKUP_PATH",
help = "Path to preserve old version to")
parser.add_option("-i", "--ignore",
action = "append", dest = "ignore", metavar = "IGNORE_PATH",
help = "Ignore files in this path")
parser.add_option("-c", "--cache",
action = "store_true", dest = "cache", default = False,
help = "use cached test suite and specification data only")
(options, args) = parser.parse_args()
ui = ui.ui()
ui.setconfig('ui', 'debug', str(options.debug))
ui.setconfig('ui', 'quiet', str(options.quiet))
ui.setconfig('ui', 'verbose', str(options.verbose))
builder = Builder(ui, options.output, options.backup, options.ignore, options.cache)
result = builder.build(args)
quit(result)
|