blob: 01a803ab61331b8d5be5e881541592b1c6aa6509 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001# 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
7import os
8import re
9import subprocess
10import shutil
11import tempfile
12
13from abc import ABCMeta, abstractmethod
14
15class 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:
Brad Bishopf86d0552018-12-04 14:18:15 -080020 self.tempdirobj = tempfile.TemporaryDirectory(prefix='buildproject-')
21 tmpdir = self.tempdirobj.name
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022 self.localarchive = os.path.join(tmpdir, self.archive)
23 self.dl_dir = dl_dir
24 if foldername:
25 self.fname = foldername
26 else:
27 self.fname = re.sub(r'\.tar\.bz2$|\.tar\.gz$|\.tar\.xz$', '', self.archive)
Brad Bishop977dc1a2019-02-06 16:01:43 -050028 self.needclean = False
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029
30 # Download self.archive to self.localarchive
31 def _download_archive(self):
Brad Bishop977dc1a2019-02-06 16:01:43 -050032
33 self.needclean = True
Brad Bishop6e60e8b2018-02-01 10:27:11 -050034 if self.dl_dir and os.path.exists(os.path.join(self.dl_dir, self.archive)):
35 shutil.copyfile(os.path.join(self.dl_dir, self.archive), self.localarchive)
36 return
37
38 cmd = "wget -O %s %s" % (self.localarchive, self.uri)
39 subprocess.check_output(cmd, shell=True)
40
41 # This method should provide a way to run a command in the desired environment.
42 @abstractmethod
43 def _run(self, cmd):
44 pass
45
46 # The timeout parameter of target.run is set to 0 to make the ssh command
47 # run with no timeout.
48 def run_configure(self, configure_args='', extra_cmds=''):
49 return self._run('cd %s; gnu-configize; %s ./configure %s' % (self.targetdir, extra_cmds, configure_args))
50
51 def run_make(self, make_args=''):
52 return self._run('cd %s; make %s' % (self.targetdir, make_args))
53
54 def run_install(self, install_args=''):
55 return self._run('cd %s; make install %s' % (self.targetdir, install_args))
56
57 def clean(self):
Brad Bishop977dc1a2019-02-06 16:01:43 -050058 if not self.needclean:
59 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 self._run('rm -rf %s' % self.targetdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061 subprocess.check_call('rm -f %s' % self.localarchive, shell=True)