Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # Copyright (C) 2013 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 bb.utils |
| 10 | import subprocess |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 11 | import tempfile |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | from abc import ABCMeta, abstractmethod |
| 13 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 14 | class BuildProject(metaclass=ABCMeta): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 16 | def __init__(self, d, uri, foldername=None, tmpdir=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | self.d = d |
| 18 | self.uri = uri |
| 19 | self.archive = os.path.basename(uri) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 20 | if not tmpdir: |
| 21 | tmpdir = self.d.getVar('WORKDIR') |
| 22 | if not tmpdir: |
| 23 | tmpdir = tempfile.mkdtemp(prefix='buildproject') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | self.localarchive = os.path.join(tmpdir,self.archive) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | if foldername: |
| 26 | self.fname = foldername |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 27 | else: |
| 28 | self.fname = re.sub(r'\.tar\.bz2$|\.tar\.gz$|\.tar\.xz$', '', self.archive) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | |
| 30 | # Download self.archive to self.localarchive |
| 31 | def _download_archive(self): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 32 | dl_dir = self.d.getVar("DL_DIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | if dl_dir and os.path.exists(os.path.join(dl_dir, self.archive)): |
| 34 | bb.utils.copyfile(os.path.join(dl_dir, self.archive), self.localarchive) |
| 35 | return |
| 36 | |
| 37 | exportvars = ['HTTP_PROXY', 'http_proxy', |
| 38 | 'HTTPS_PROXY', 'https_proxy', |
| 39 | 'FTP_PROXY', 'ftp_proxy', |
| 40 | 'FTPS_PROXY', 'ftps_proxy', |
| 41 | 'NO_PROXY', 'no_proxy', |
| 42 | 'ALL_PROXY', 'all_proxy', |
| 43 | 'SOCKS5_USER', 'SOCKS5_PASSWD'] |
| 44 | |
| 45 | cmd = '' |
| 46 | for var in exportvars: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 47 | val = self.d.getVar(var) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | if val: |
| 49 | cmd = 'export ' + var + '=\"%s\"; %s' % (val, cmd) |
| 50 | |
| 51 | cmd = cmd + "wget -O %s %s" % (self.localarchive, self.uri) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 52 | subprocess.check_output(cmd, shell=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | |
| 54 | # This method should provide a way to run a command in the desired environment. |
| 55 | @abstractmethod |
| 56 | def _run(self, cmd): |
| 57 | pass |
| 58 | |
| 59 | # The timeout parameter of target.run is set to 0 to make the ssh command |
| 60 | # run with no timeout. |
| 61 | def run_configure(self, configure_args='', extra_cmds=''): |
| 62 | return self._run('cd %s; %s ./configure %s' % (self.targetdir, extra_cmds, configure_args)) |
| 63 | |
| 64 | def run_make(self, make_args=''): |
| 65 | return self._run('cd %s; make %s' % (self.targetdir, make_args)) |
| 66 | |
| 67 | def run_install(self, install_args=''): |
| 68 | return self._run('cd %s; make install %s' % (self.targetdir, install_args)) |
| 69 | |
| 70 | def clean(self): |
| 71 | self._run('rm -rf %s' % self.targetdir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 72 | subprocess.check_call('rm -f %s' % self.localarchive, shell=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | pass |
| 74 | |
| 75 | class TargetBuildProject(BuildProject): |
| 76 | |
| 77 | def __init__(self, target, d, uri, foldername=None): |
| 78 | self.target = target |
| 79 | self.targetdir = "~/" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 80 | BuildProject.__init__(self, d, uri, foldername) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | |
| 82 | def download_archive(self): |
| 83 | |
| 84 | self._download_archive() |
| 85 | |
| 86 | (status, output) = self.target.copy_to(self.localarchive, self.targetdir) |
| 87 | if status != 0: |
| 88 | raise Exception("Failed to copy archive to target, output: %s" % output) |
| 89 | |
| 90 | (status, output) = self.target.run('tar xf %s%s -C %s' % (self.targetdir, self.archive, self.targetdir)) |
| 91 | if status != 0: |
| 92 | raise Exception("Failed to extract archive, output: %s" % output) |
| 93 | |
| 94 | #Change targetdir to project folder |
| 95 | self.targetdir = self.targetdir + self.fname |
| 96 | |
| 97 | # The timeout parameter of target.run is set to 0 to make the ssh command |
| 98 | # run with no timeout. |
| 99 | def _run(self, cmd): |
| 100 | return self.target.run(cmd, 0)[0] |
| 101 | |
| 102 | |
| 103 | class SDKBuildProject(BuildProject): |
| 104 | |
| 105 | def __init__(self, testpath, sdkenv, d, uri, foldername=None): |
| 106 | self.sdkenv = sdkenv |
| 107 | self.testdir = testpath |
| 108 | self.targetdir = testpath |
| 109 | bb.utils.mkdirhier(testpath) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | self.datetime = d.getVar('DATETIME') |
| 111 | self.testlogdir = d.getVar("TEST_LOG_DIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | bb.utils.mkdirhier(self.testlogdir) |
| 113 | self.logfile = os.path.join(self.testlogdir, "sdk_target_log.%s" % self.datetime) |
| 114 | BuildProject.__init__(self, d, uri, foldername, tmpdir=testpath) |
| 115 | |
| 116 | def download_archive(self): |
| 117 | |
| 118 | self._download_archive() |
| 119 | |
| 120 | cmd = 'tar xf %s%s -C %s' % (self.targetdir, self.archive, self.targetdir) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 121 | subprocess.check_output(cmd, shell=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | |
| 123 | #Change targetdir to project folder |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 124 | self.targetdir = os.path.join(self.targetdir, self.fname) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 126 | def run_configure(self, configure_args='', extra_cmds=' gnu-configize; '): |
| 127 | return super(SDKBuildProject, self).run_configure(configure_args=(configure_args or '$CONFIGURE_FLAGS'), extra_cmds=extra_cmds) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | |
| 129 | def run_install(self, install_args=''): |
| 130 | return super(SDKBuildProject, self).run_install(install_args=(install_args or "DESTDIR=%s/../install" % self.targetdir)) |
| 131 | |
| 132 | def log(self, msg): |
| 133 | if self.logfile: |
| 134 | with open(self.logfile, "a") as f: |
| 135 | f.write("%s\n" % msg) |
| 136 | |
| 137 | def _run(self, cmd): |
| 138 | self.log("Running . %s; " % self.sdkenv + cmd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 139 | return subprocess.check_call(". %s; " % self.sdkenv + cmd, shell=True) |