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