diff options
author | Andoni Morales Alastruey <ylatuya@gmail.com> | 2012-07-13 19:42:40 +0200 |
---|---|---|
committer | Andoni Morales Alastruey <ylatuya@gmail.com> | 2012-08-08 13:02:27 +0200 |
commit | 3aa0eefa960e4ff6cb912b8639e91f535fefd08a (patch) | |
tree | 395023b179459f6bb75f09b5d74ef8cf2004816a | |
parent | a650cb1315643c7fd3ec247a535f5c10456db73c (diff) |
Add an easier way to modify the environment in recipes
-rw-r--r-- | cerbero/build/build.py | 75 | ||||
-rw-r--r-- | test/test_cerbero_build_build.py | 89 |
2 files changed, 142 insertions, 22 deletions
diff --git a/cerbero/build/build.py b/cerbero/build/build.py index d423e7d7..6c227733 100644 --- a/cerbero/build/build.py +++ b/cerbero/build/build.py @@ -71,15 +71,17 @@ class CustomBuild(Build): pass -def system_libs(func): - ''' Decorator to use system libs''' +def modify_environment(func): + ''' Decorator to modify the build environment ''' def call(*args): self = args[0] + append_env = self.append_env + new_env = {a:b for a, b in self.new_env.iteritems()} if self.use_system_libs and self.config.allow_system_libs: - self._add_system_libs() + self._add_system_libs(new_env) + old_env = self._modify_env(append_env, new_env) res = func(*args) - if self.use_system_libs and self.config.allow_system_libs: - self._restore_pkg_config_path() + self._restore_env(old_env) return res call.func_name = func.func_name @@ -102,17 +104,19 @@ class MakefilesBase (Build): use_system_libs = False allow_parallel_build = True srcdir = '.' + append_env = {} + new_env = {} def __init__(self): - self._with_system_libs = False Build.__init__(self) self.make_dir = os.path.abspath(os.path.join(self.build_dir, self.srcdir)) if self.config.allow_parallel_build and self.allow_parallel_build \ and self.config.num_of_cpus > 1: self.make += ' -j%d' % self.config.num_of_cpus + self._old_env = None - @system_libs + @modify_environment def configure(self): shell.call(self.configure_tpl % {'config-sh': self.config_sh, 'prefix': to_unixpath(self.config.prefix), @@ -123,38 +127,65 @@ class MakefilesBase (Build): 'options': self.configure_options}, self.make_dir) - @system_libs + @modify_environment def compile(self): shell.call(self.make, self.make_dir) - @system_libs + @modify_environment def install(self): shell.call(self.make_install, self.make_dir) - @system_libs + @modify_environment def clean(self): shell.call(self.make_clean, self.make_dir) - @system_libs + @modify_environment def check(self): if self.make_check: shell.call(self.make_check, self.build_dir) - def _add_system_libs(self): - if self._with_system_libs: - # Don't mess the env too much + def _modify_env(self, append_env, new_env): + ''' + Modifies the build environment appending the values in + append_env or replacing the values in new_env + ''' + if self._old_env is not None: + return None + + self._old_env = {} + for var in append_env.keys() + new_env.keys(): + self._old_env[var] = os.environ[var] + + for var, val in append_env.iteritems(): + os.environ[var] += val + + for var, val in new_env.iteritems(): + if val is None: + del os.environ[var] + else: + os.environ[var] = val + return self._old_env + + def _restore_env(self, old_env): + ''' Restores the old environment ''' + if old_env is None: return + + for var, val in old_env.iteritems(): + os.environ[var] = val + self._old_env = None + + def _add_system_libs(self, new_env): + ''' + Delete PKG_CONFIG_LIBDIR, pointing to the installation prefix and + appends it to PKG_CONFIG_LIBDIR to allow pkg-config finding libraries + in the default system search path + ''' self.pkgconfiglibdir = os.environ['PKG_CONFIG_LIBDIR'] self.pkgconfigpath = os.environ['PKG_CONFIG_PATH'] - os.environ['PKG_CONFIG_PATH'] = '%s:%s' % (self.pkgconfigpath, + new_env['PKG_CONFIG_PATH'] = '%s:%s' % (self.pkgconfigpath, self.pkgconfiglibdir) - del os.environ['PKG_CONFIG_LIBDIR'] - self._with_system_libs = True - - def _restore_pkg_config_path(self): - os.environ['PKG_CONFIG_PATH'] = self.pkgconfigpath - os.environ['PKG_CONFIG_LIBDIR'] = self.pkgconfiglibdir - self._with_system_libs = False + new_env['PKG_CONFIG_LIBDIR'] = None class Autotools (MakefilesBase): diff --git a/test/test_cerbero_build_build.py b/test/test_cerbero_build_build.py new file mode 100644 index 00000000..b7102080 --- /dev/null +++ b/test/test_cerbero_build_build.py @@ -0,0 +1,89 @@ +# cerbero - a multi-platform build system for Open Source software +# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import unittest +import os + +from test.test_common import DummyConfig +from cerbero.build import build + + +class MakefilesBase(build.MakefilesBase): + + srcdir = '' + build_dir = '' + + def __init__(self, config): + self.config = config + build.MakefilesBase.__init__(self) + + @build.modify_environment + def get_env_var(self, var): + if var not in os.environ: + return None + return os.environ[var] + + @build.modify_environment + def get_env_var_nested(self, var): + return self.get_env_var(var) + + +class ModifyEnvTest(unittest.TestCase): + + def setUp(self): + self.var = 'TEST_VAR' + self.val1 = 'test' + self.val2 = 'test2' + self.mk = MakefilesBase(DummyConfig()) + + def testAppendEnv(self): + os.environ[self.var] = self.val1 + self.mk.append_env = {self.var: self.val2} + val = self.mk.get_env_var(self.var) + self.assertEquals(val, self.val1 + self.val2) + + def testNewEnv(self): + os.environ[self.var] = self.val1 + self.mk.new_env = {self.var: self.val2} + val = self.mk.get_env_var(self.var) + self.assertEquals(val, self.val2) + + def testAppendAndNewEnv(self): + os.environ[self.var] = '' + self.mk.append_env = {self.var: self.val1} + self.mk.new_env = {self.var: self.val2} + val = self.mk.get_env_var(self.var) + self.assertEquals(val, self.val2) + + def testSystemLibs(self): + os.environ['PKG_CONFIG_PATH'] = '/path/1' + os.environ['PKG_CONFIG_LIBDIR'] = '/path/2' + self.mk.config.allow_system_libs = True + self.mk.use_system_libs = True + val = self.mk.get_env_var('PKG_CONFIG_PATH') + self.assertEquals(val, '/path/1:/path/2') + val = self.mk.get_env_var('PKG_CONFIG_LIBDIR') + self.assertIsNone(val) + + def testNestedModif(self): + os.environ[self.var] = self.val1 + self.mk.append_env = {self.var: self.val2} + val = self.mk.get_env_var(self.var) + self.assertEquals(val, self.val1 + self.val2) + val = self.mk.get_env_var_nested(self.var) + self.assertEquals(val, self.val1 + self.val2) |