run-benchmark should either checkout JetStream 1.0.1 or support running the latest...
[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, plan):
20         self.name = name
21         self.webRoot = tempfile.mkdtemp()
22         self.dest = os.path.join(self.webRoot, self.name)
23         if 'local_copy' in plan:
24             self._copyBenchmarkToTempDir(plan['local_copy'])
25         elif 'remote_archive' in plan:
26             self._fetchRemoteArchive(plan['remote_archive'])
27         elif 'svn_source' in plan:
28             self._checkoutWithSubverion(plan['svn_source'])
29         else:
30             raise Exception('The benchmark location was not specified')
31
32         _log.info('Copied the benchmark into: %s' % self.dest)
33         try:
34             if 'create_script' in plan:
35                 self._runCreateScript(plan['create_script'])
36             if 'benchmark_patch' in plan:
37                 self._applyPatch(plan['benchmark_patch'])
38             return self.webRoot
39         except Exception:
40             self.clean()
41             raise
42
43     def _runCreateScript(self, createScript):
44         oldWorkingDirectory = os.getcwd()
45         os.chdir(self.dest)
46         _log.debug('Running %s in %s' % (createScript, self.dest))
47         errorCode = subprocess.call(createScript)
48         os.chdir(oldWorkingDirectory)
49         if errorCode:
50             raise Exception('Cannot create the benchmark - Error: %s' % errorCode)
51
52     def _copyBenchmarkToTempDir(self, benchmarkPath):
53         shutil.copytree(getPathFromProjectRoot(benchmarkPath), self.dest)
54
55     def _fetchRemoteArchive(self, archiveURL):
56         archivePath = os.path.join(self.webRoot, 'archive.zip')
57         _log.info('Downloading %s to %s' % (archiveURL, archivePath))
58         urllib.urlretrieve(archiveURL, archivePath)
59
60         with ZipFile(archivePath, 'r') as archive:
61             archive.extractall(self.dest)
62
63         unarchivedFiles = filter(lambda name: not name.startswith('.'), os.listdir(self.dest))
64         if len(unarchivedFiles) == 1:
65             firstFile = os.path.join(self.dest, unarchivedFiles[0])
66             if os.path.isdir(firstFile):
67                 shutil.move(firstFile, self.webRoot)
68                 os.rename(os.path.join(self.webRoot, unarchivedFiles[0]), self.dest)
69
70     def _checkoutWithSubverion(self, subversionURL):
71         _log.info('Checking out %s to %s' % (subversionURL, self.dest))
72         errorCode = subprocess.call(['svn', 'checkout', subversionURL, self.dest])
73         if errorCode:
74             raise Exception('Cannot checkout the benchmark - Error: %s' % errorCode)
75
76     def _applyPatch(self, patch):
77         oldWorkingDirectory = os.getcwd()
78         os.chdir(self.dest)
79         errorCode = subprocess.call(['patch', '-p1', '-f', '-i', getPathFromProjectRoot(patch)])
80         os.chdir(oldWorkingDirectory)
81         if errorCode:
82             raise Exception('Cannot apply patch, will skip current benchmarkPath - Error: %s' % errorCode)
83
84     def clean(self):
85         _log.info('Cleaning Benchmark')
86         if self.webRoot:
87             forceRemove(self.webRoot)