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