setup.py 8.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/env python3
#
#  Copyright (C) 2016 Codethink Limited
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
#  Authors:
#        Tristan Van Berkom <[email protected]>
20

21 22
import os
import shutil
23 24
import sys

25 26
if sys.version_info[0] != 3 or sys.version_info[1] < 4:
    print("BuildStream requires Python >= 3.4")
27 28
    sys.exit(1)

29 30
try:
    from setuptools import setup, find_packages
31
    from setuptools.command.easy_install import ScriptWriter
32 33 34 35 36
except ImportError:
    print("BuildStream requires setuptools in order to build. Install it using"
          " your package manager (usually python3-setuptools) or via pip (pip3"
          " install setuptools).")
    sys.exit(1)
37

38

39 40 41 42 43 44 45 46 47 48 49 50 51 52
##################################################################
# Bubblewrap requirements
##################################################################
def assert_bwrap():
    platform = os.environ.get('BST_FORCE_BACKEND', '') or sys.platform
    if platform.startswith('linux'):
        bwrap_path = shutil.which('bwrap')
        if not bwrap_path:
            print("Bubblewrap not found: BuildStream requires Bubblewrap (bwrap) for"
                  " sandboxing the build environment. Install it using your package manager"
                  " (usually bwrap or bubblewrap)")
            sys.exit(1)


53
##################################################################
54
# OSTree version requirements
55
##################################################################
56 57 58 59
REQUIRED_OSTREE_YEAR = 2017
REQUIRED_OSTREE_RELEASE = 8


60
def exit_ostree(reason):
61 62 63
    print(reason +
          "\nBuildStream requires OSTree >= v{}.{} with Python bindings. "
          .format(REQUIRED_OSTREE_YEAR, REQUIRED_OSTREE_RELEASE) +
64 65 66
          "Install it using your package manager (usually ostree or gir1.2-ostree-1.0).")
    sys.exit(1)

67 68 69 70 71 72 73 74 75 76 77 78 79 80

def assert_ostree_version():
    platform = os.environ.get('BST_FORCE_BACKEND', '') or sys.platform
    if platform.startswith('linux'):
        try:
            import gi
        except ImportError:
            print("BuildStream requires PyGObject (aka PyGI). Install it using"
                  " your package manager (usually pygobject3 or python-gi).")
            sys.exit(1)

        try:
            gi.require_version('OSTree', '1.0')
            from gi.repository import OSTree
81
        except ValueError:
82 83 84 85 86 87 88 89 90 91
            exit_ostree("OSTree not found")

        try:
            if OSTree.YEAR_VERSION < REQUIRED_OSTREE_YEAR or \
               (OSTree.YEAR_VERSION == REQUIRED_OSTREE_YEAR and
                OSTree.RELEASE_VERSION < REQUIRED_OSTREE_RELEASE):
                exit_ostree("OSTree v{}.{} is too old."
                            .format(OSTree.YEAR_VERSION, OSTree.RELEASE_VERSION))
        except AttributeError:
            exit_ostree("OSTree is too old.")
92

93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
###########################################
# List the pre-built man pages to install #
###########################################
#
# Man pages are automatically generated however it was too difficult
# to integrate with setuptools as a step of the build (FIXME !).
#
# To update the man pages in tree before a release, you need to
# ensure you have the 'click_man' package installed, and run:
#
# python3 setup.py --command-packages=click_man.commands man_pages
#
# Then commit the result.
#
def list_man_pages():
    bst_dir = os.path.dirname(os.path.abspath(__file__))
    man_dir = os.path.join(bst_dir, 'man')
    man_pages = os.listdir(man_dir)
    return [os.path.join('man', page) for page in man_pages]


115
#####################################################
116
#                Conditional Checks                 #
117
#####################################################
118 119 120 121 122
#
# Because setuptools... there is no way to pass an option to
# the setup.py explicitly at install time.
#
# So screw it, lets just use an env var.
123 124 125 126 127 128
bst_install_entry_points = {
    'console_scripts': [
        'bst-artifact-receive = buildstream._artifactcache.pushreceive:receive_main'
    ],
}

129 130 131 132 133
if not os.environ.get('BST_ARTIFACTS_ONLY', ''):
    assert_bwrap()
    assert_ostree_version()
    bst_install_entry_points['console_scripts'] += [
        'bst = buildstream._frontend:cli'
134 135
    ]

136 137 138 139 140 141
_version_template = """\
# coding: utf-8
# file generated by setuptools_scm
# don't change, don't track in version control
__version__ = {version!r}
"""
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#####################################################
#    Monkey-patching setuptools for performance     #
#####################################################
#
# The template of easy_install.ScriptWriter is inefficient in our case as it
# imports pkg_resources. Patching the template only doesn't work because of the
# old string formatting used (%). This forces us to overwrite the class function
# as well.
#
# The patch was inspired from https://github.com/ninjaaron/fast-entry_points
# which we believe was also inspired from the code from `setuptools` project.
TEMPLATE = '''\
# -*- coding: utf-8 -*-
import sys

from {0} import {1}

if __name__ == '__main__':
    sys.exit({2}())'''


164 165 166 167
# Modify the get_args() function of the ScriptWriter class
# Note: the pylint no-member warning has been disabled as the functions: get_header(),
# ensure_safe_name() and _get_script_args() are all members of this class.
# pylint: disable=no-member
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
@classmethod
def get_args(cls, dist, header=None):
    if header is None:
        header = cls.get_header()
    for name, ep in dist.get_entry_map('console_scripts').items():
        cls._ensure_safe_name(name)
        script_text = TEMPLATE.format(ep.module_name, ep.attrs[0], '.'.join(ep.attrs))
        args = cls._get_script_args('console', name, header, script_text)
        for res in args:
            yield res


ScriptWriter.get_args = get_args


183 184 185
#####################################################
#             Main setup() Invocation               #
#####################################################
186
setup(name='BuildStream',
187
      description='A framework for modelling build pipelines in YAML',
188
      license='LGPL',
189 190 191 192
      use_scm_version={
          'write_to': "buildstream/__version__.py",
          'write_to_template': _version_template,
      },
193
      packages=find_packages(),
Tristan Maat's avatar
Tristan Maat committed
194
      package_data={'buildstream': ['plugins/*/*.py', 'plugins/*/*.yaml',
195
                                    'data/*.yaml', 'data/*.sh.in']},
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
      data_files=[
          # This is a weak attempt to integrate with the user nicely,
          # installing things outside of the python package itself with pip is
          # not recommended, but there seems to be no standard structure for
          # addressing this; so just installing this here.
          #
          # These do not get installed in developer mode (`pip install --user -e .`)
          #
          # The completions are ignored by bash unless it happens to be installed
          # in the right directory; this is more like a weak statement that we
          # attempt to install bash completion scriptlet.
          #
          ('share/man/man1', list_man_pages()),
          ('share/bash-completion/completions', [
              os.path.join('buildstream', 'data', 'bst')
          ])
      ],
213
      install_requires=[
214
          'setuptools',
215
          'psutil',
216 217
          'ruamel.yaml',
          'pluginbase',
218
          'Click',
219
          'blessings',
220
          'jinja2 >= 2.10',
221
      ],
222
      entry_points=bst_install_entry_points,
223
      setup_requires=['pytest-runner', 'setuptools_scm'],
224
      tests_require=['pep8',
225 226
                     # Pin coverage to 4.2 for now, we're experiencing
                     # random crashes with 4.4.2
227
                     'coverage == 4.4.0',
228
                     'pytest-datafiles',
229
                     'pytest-env',
230
                     'pytest-pep8',
231
                     'pytest-pylint',
232
                     'pytest-cov',
233 234
                     # Provide option to run tests in parallel, less reliable
                     'pytest-xdist',
235
                     'pytest >= 3.1.0'],
236
      zip_safe=False)