6ac26f34aadd3bf6068508f3783af5750692bf55
[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 shutil
7 import subprocess
8
9 from webkitpy.benchmark_runner.utils import getPathFromProjectRoot, forceRemove
10
11
12 _log = logging.getLogger(__name__)
13
14
15 class GenericBenchmarkBuilder(object):
16
17     def prepare(self, benchmarkPath, patch):
18         self._copyBenchmarkToTempDir(benchmarkPath)
19         return self._applyPatch(patch)
20
21     def _copyBenchmarkToTempDir(self, benchmarkPath):
22         self.webRoot = tempfile.mkdtemp()
23         _log.debug('Servering at webRoot: %s' % self.webRoot)
24         self.dest = os.path.join(self.webRoot, os.path.split(benchmarkPath)[1])
25         shutil.copytree(getPathFromProjectRoot(benchmarkPath), self.dest)
26
27     def _applyPatch(self, patch):
28         if not patch:
29             return self.webRoot
30         oldWorkingDirectory = os.getcwd()
31         os.chdir(self.webRoot)
32         errorCode = subprocess.call(['patch', '-p1', '-f', '-i', getPathFromProjectRoot(patch)])
33         os.chdir(oldWorkingDirectory)
34         if errorCode:
35             _log.error('Cannot apply patch, will skip current benchmarkPath')
36             self.clean()
37             return None
38         return self.webRoot
39
40     def clean(self):
41         _log.info('Cleanning Benchmark')
42         if self.webRoot:
43             forceRemove(self.webRoot)