custom.py 6.47 KB
Newer Older
1 2
##############################################################################
#
3
# Copyright (c) 2006 Zope Foundation and Contributors.
4 5 6 7 8 9 10 11 12 13 14 15
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Install packages as eggs
"""
Jim Fulton's avatar
Jim Fulton committed
16 17 18
import logging
import os
import sys
Tres Seaver's avatar
Tres Seaver committed
19

20 21
import zc.buildout.easy_install

22 23
logger = logging.getLogger(__name__)

Christian Zagrodnick's avatar
Christian Zagrodnick committed
24

25
class Base:
26 27

    def __init__(self, buildout, name, options):
28 29 30 31 32 33 34 35 36
        self.name, self.options = name, options

        options['_d'] = buildout['buildout']['develop-eggs-directory']

        self.build_ext = build_ext(buildout, options)

    def update(self):
        return self.install()

Christian Zagrodnick's avatar
Christian Zagrodnick committed
37

38 39 40 41 42
class Custom(Base):

    def __init__(self, buildout, name, options):
        Base.__init__(self, buildout, name, options)

43 44 45 46 47 48 49 50 51 52 53 54 55 56
        links = options.get('find-links',
                            buildout['buildout'].get('find-links'))
        if links:
            links = links.split()
            options['find-links'] = '\n'.join(links)
        else:
            links = ()
        self.links = links

        index = options.get('index', buildout['buildout'].get('index'))
        if index is not None:
            options['index'] = index
        self.index = index

57
        environment_section = options.get('environment')
58
        if environment_section:
59 60 61
            self.environment = buildout[environment_section]
        else:
            self.environment = {}
62
        environment_data = list(self.environment.items())
63 64
        environment_data.sort()
        options['_environment-data'] = repr(environment_data)
65

66 67
        options['_e'] = buildout['buildout']['eggs-directory']

68 69
        if buildout['buildout'].get('offline') == 'true':
            self.install = lambda: ()
70

71 72
        self.newest = buildout['buildout'].get('newest') == 'true'

73 74
    def install(self):
        options = self.options
75 76 77 78 79 80 81
        distribution = options.get('egg')
        if distribution is None:
            distribution = options.get('eggs')
            if distribution is None:
                distribution = self.name
            else:
                logger.warn("The eggs option is deprecated. Use egg instead")
Christian Zagrodnick's avatar
Christian Zagrodnick committed
82 83


84 85
        distribution = options.get('egg', options.get('eggs', self.name)
                                   ).strip()
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

        setup_eggs = [
            r.strip()
            for r in options.get('setup-eggs', '').split('\n')
            if r.strip()]
        if setup_eggs:
            ws = zc.buildout.easy_install.install(
                setup_eggs, options['_e'],
                links=self.links,
                index=self.index,
                executable=sys.executable,
                path=[options['_d'], options['_e']],
                newest=self.newest,
                )
            extra_path = os.pathsep.join(ws.entries)
            self.environment['PYTHONEXTRAPATH'] = os.environ['PYTHONEXTRAPATH'] = extra_path

103 104 105 106
        self._set_environment()
        try:
            return zc.buildout.easy_install.build(
                distribution, options['_d'], self.build_ext,
Jim Fulton's avatar
Jim Fulton committed
107 108
                self.links, self.index, sys.executable,
                [options['_e']], newest=self.newest,
109 110 111 112 113 114 115
                )
        finally:
            self._restore_environment()


    def _set_environment(self):
        self._saved_environment = {}
116
        for key, value in list(self.environment.items()):
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            if key in os.environ:
                self._saved_environment[key] = os.environ[key]
            # Interpolate value with variables from environment. Maybe there
            # should be a general way of doing this in buildout with something
            # like ${environ:foo}:
            os.environ[key] = value % os.environ

    def _restore_environment(self):
        for key in self.environment:
            if key in self._saved_environment:
                os.environ[key] = self._saved_environment[key]
            else:
                try:
                    del os.environ[key]
                except KeyError:
                    pass
Christian Zagrodnick's avatar
Christian Zagrodnick committed
133 134


135 136 137 138 139 140 141 142 143 144
class Develop(Base):

    def __init__(self, buildout, name, options):
        Base.__init__(self, buildout, name, options)
        options['setup'] = os.path.join(buildout['buildout']['directory'],
                                        options['setup'])

    def install(self):
        options = self.options
        return zc.buildout.easy_install.develop(
Jim Fulton's avatar
Jim Fulton committed
145
            options['setup'], options['_d'], self.build_ext)
Christian Zagrodnick's avatar
Christian Zagrodnick committed
146

147 148 149

def build_ext(buildout, options):
    result = {}
150
    for be_option in ('include-dirs', 'library-dirs'):
151 152 153 154 155 156 157 158 159 160 161 162 163 164
        value = options.get(be_option)
        if value is None:
            continue
        value = [
            os.path.join(
                buildout['buildout']['directory'],
                v.strip()
                )
            for v in value.strip().split('\n')
            if v.strip()
        ]
        result[be_option] = os.pathsep.join(value)
        options[be_option] = os.pathsep.join(value)

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    # rpath has special symbolic dirnames which must not be prefixed
    # with the buildout dir.  See:
    # http://man7.org/linux/man-pages/man8/ld.so.8.html
    RPATH_SPECIAL = [
        '$ORIGIN', '$LIB', '$PLATFORM', '${ORIGIN}', '${LIB}', '${PLATFORM}']
    def _prefix_non_special(x):
        x = x.strip()
        for special in RPATH_SPECIAL:
            if x.startswith(special):
                return x
        return os.path.join( buildout['buildout']['directory'], x)

    value = options.get('rpath')
    if value is not None:
        values = [_prefix_non_special(v)
                    for v in value.strip().split('\n') if v.strip()]
Tres Seaver's avatar
Typo.  
Tres Seaver committed
181 182
        result['rpath'] = os.pathsep.join(values)
        options['rpath'] = os.pathsep.join(values)
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197
    swig = options.get('swig')
    if swig:
        options['swig'] = result['swig'] = os.path.join(
            buildout['buildout']['directory'],
            swig,
            )

    for be_option in ('define', 'undef', 'libraries', 'link-objects',
                      'debug', 'force', 'compiler', 'swig-cpp', 'swig-opts',
                      ):
        value = options.get(be_option)
        if value is None:
            continue
        result[be_option] = value
Jim Fulton's avatar
Jim Fulton committed
198

199
    return result