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