Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1 | # Copyright (C) 2013-2016 Intel Corporation |
| 2 | # |
| 3 | # Released under the MIT license (see COPYING.MIT) |
| 4 | |
| 5 | # Provides a class for automating build tests for projects |
| 6 | |
| 7 | import os |
| 8 | import re |
| 9 | import subprocess |
| 10 | import shutil |
| 11 | import tempfile |
| 12 | |
| 13 | from abc import ABCMeta, abstractmethod |
| 14 | |
| 15 | class BuildProject(metaclass=ABCMeta): |
| 16 | def __init__(self, uri, foldername=None, tmpdir=None, dl_dir=None): |
| 17 | self.uri = uri |
| 18 | self.archive = os.path.basename(uri) |
| 19 | if not tmpdir: |
| 20 | tmpdir = tempfile.mkdtemp(prefix='buildproject') |
| 21 | self.localarchive = os.path.join(tmpdir, self.archive) |
| 22 | self.dl_dir = dl_dir |
| 23 | if foldername: |
| 24 | self.fname = foldername |
| 25 | else: |
| 26 | self.fname = re.sub(r'\.tar\.bz2$|\.tar\.gz$|\.tar\.xz$', '', self.archive) |
| 27 | |
| 28 | # Download self.archive to self.localarchive |
| 29 | def _download_archive(self): |
| 30 | if self.dl_dir and os.path.exists(os.path.join(self.dl_dir, self.archive)): |
| 31 | shutil.copyfile(os.path.join(self.dl_dir, self.archive), self.localarchive) |
| 32 | return |
| 33 | |
| 34 | cmd = "wget -O %s %s" % (self.localarchive, self.uri) |
| 35 | subprocess.check_output(cmd, shell=True) |
| 36 | |
| 37 | # This method should provide a way to run a command in the desired environment. |
| 38 | @abstractmethod |
| 39 | def _run(self, cmd): |
| 40 | pass |
| 41 | |
| 42 | # The timeout parameter of target.run is set to 0 to make the ssh command |
| 43 | # run with no timeout. |
| 44 | def run_configure(self, configure_args='', extra_cmds=''): |
| 45 | return self._run('cd %s; gnu-configize; %s ./configure %s' % (self.targetdir, extra_cmds, configure_args)) |
| 46 | |
| 47 | def run_make(self, make_args=''): |
| 48 | return self._run('cd %s; make %s' % (self.targetdir, make_args)) |
| 49 | |
| 50 | def run_install(self, install_args=''): |
| 51 | return self._run('cd %s; make install %s' % (self.targetdir, install_args)) |
| 52 | |
| 53 | def clean(self): |
| 54 | self._run('rm -rf %s' % self.targetdir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 55 | subprocess.check_call('rm -f %s' % self.localarchive, shell=True) |