run-benchmark should support SunSpider, Kraken and Octane
[WebKit-https.git] / Tools / Scripts / webkitpy / benchmark_runner / benchmark_builder / generic_benchmark_builder.py
1 #!/usr/bin/env python
2
3 import logging
4 import tempfile
5 import os
6 import urllib
7 import shutil
8 import subprocess
9
10 from zipfile import ZipFile
11 from webkitpy.benchmark_runner.utils import getPathFromProjectRoot, forceRemove
12
13
14 _log = logging.getLogger(__name__)
15
16
17 class GenericBenchmarkBuilder(object):
18
19     def prepare(self, name, benchmarkPath, archiveURL, patch, createScript):
20         self.name = name
21         self.webRoot = tempfile.mkdtemp()
22         self.dest = os.path.join(self.webRoot, self.name)
23         if benchmarkPath:
24             self._copyBenchmarkToTempDir(benchmarkPath)
25         else:
26             assert(archiveURL)
27             self._fetchRemoteArchive(archiveURL)
28
29         _log.info('Copied the benchmark into: %s' % self.dest)
30         try:
31             if createScript:
32                 self._runCreateScript(createScript)
33             return self._applyPatch(patch)
34         except:
35             self.clean()
36             raise
37
38     def _runCreateScript(self, createScript):
39         oldWorkingDirectory = os.getcwd()
40         os.chdir(self.dest)
41         _log.debug('Running %s in %s' % (createScript, self.dest))
42         errorCode = subprocess.call(createScript)
43         os.chdir(oldWorkingDirectory)
44         if errorCode:
45             raise Exception('Cannot create the benchmark', errorCode)
46
47     def _copyBenchmarkToTempDir(self, benchmarkPath):
48         shutil.copytree(getPathFromProjectRoot(benchmarkPath), self.dest)
49
50     def _fetchRemoteArchive(self, archiveURL):
51         archivePath = os.path.join(self.webRoot, 'archive.zip')
52         _log.info('Downloading %s to %s' % (archiveURL, archivePath))
53         urllib.urlretrieve(archiveURL, archivePath)
54
55         with ZipFile(archivePath, 'r') as archive:
56             archive.extractall(self.dest)
57
58         unarchivedFiles = filter(lambda name: not name.startswith('.'), os.listdir(self.dest))
59         if len(unarchivedFiles) == 1:
60             firstFile = os.path.join(self.dest, unarchivedFiles[0])
61             if os.path.isdir(firstFile):
62                 shutil.move(firstFile, self.webRoot)
63                 os.rename(os.path.join(self.webRoot, unarchivedFiles[0]), self.dest)
64
65     def _applyPatch(self, patch):
66         if not patch:
67             return self.webRoot
68         oldWorkingDirectory = os.getcwd()
69         os.chdir(self.dest)
70         errorCode = subprocess.call(['patch', '-p1', '-f', '-i', getPathFromProjectRoot(patch)])
71         os.chdir(oldWorkingDirectory)
72         if errorCode:
73             raise Exception('Cannot apply patch, will skip current benchmarkPath')
74         return self.webRoot
75
76     def clean(self):
77         _log.info('Cleanning Benchmark')
78         if self.webRoot:
79             forceRemove(self.webRoot)