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