aboutsummaryrefslogtreecommitdiff
blob: 5273192f6834b794bd472fb10a7ec7476951446c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3

import os
import sys
from collections import defaultdict
from itertools import chain
from textwrap import dedent

from setuptools import setup
from distutils import log
from distutils.command import build_clib as dst_build_clib
from distutils.command import install_data as dst_install_data
from distutils.command import install_lib as dst_install_lib
from distutils.util import byte_compile
from snakeoil.dist import distutils_extensions as pkgdist

pkgdist_setup, pkgdist_cmds = pkgdist.setup()

DATA_INSTALL_OFFSET = 'share/pkgcheck'

use_system_tree_sitter_bash = bool(os.environ.get('USE_SYSTEM_TREE_SITTER_BASH', False))


class install_lib(dst_install_lib.install_lib):
    """Wrapper to install bash parsing library."""

    def run(self):
        super().run()
        if not use_system_tree_sitter_bash:
            build_clib = self.reinitialize_command('build_clib')
            build_clib.ensure_finalized()
            self.copy_tree(build_clib.build_clib, self.install_dir)


class install(pkgdist.install):
    """Install wrapper to generate and install pkgcheck-related files."""

    def finalize_options(self):
        """Force platlib install since non-python libraries are included."""
        super().finalize_options()
        self.install_lib = self.install_platlib

    def run(self):
        super().run()
        target = self.install_data
        root = self.root or '/'
        if target.startswith(root):
            target = os.path.join('/', os.path.relpath(target, root))
        target = os.path.abspath(target)

        if not self.dry_run:
            # Install configuration data so the program can find its content,
            # rather than assuming it is running from a tarball/git repo.
            write_obj_lists(self.install_lib, target)


def write_obj_lists(python_base, install_prefix):
    """Generate config file of keyword, check, and other object lists."""
    objects_path = os.path.join(python_base, pkgdist.MODULE_NAME, "_objects.py")
    os.makedirs(os.path.dirname(objects_path), exist_ok=True)
    log.info(f'writing config to {objects_path!r}')

    wheel_install = (
        install_prefix != os.path.abspath(sys.prefix)
        and not install_prefix.startswith(pkgdist.REPODIR)
    )

    # hack to drop quotes on modules in generated files
    class _kls:

        def __init__(self, module):
            self.module = module

        def __repr__(self):
            return self.module

    with pkgdist.syspath(pkgdist.PACKAGEDIR):
        from pkgcheck import objects

    modules = defaultdict(set)
    objs = defaultdict(list)
    for obj in ('KEYWORDS', 'CHECKS', 'REPORTERS'):
        for name, cls in getattr(objects, obj).items():
            parent, module = cls.__module__.rsplit('.', 1)
            modules[parent].add(module)
            objs[obj].append((name, _kls(f'{module}.{name}')))

    keywords = tuple(objs['KEYWORDS'])
    checks = tuple(objs['CHECKS'])
    reporters = tuple(objs['REPORTERS'])

    with open(objects_path, 'w') as f:
        os.chmod(objects_path, 0o644)
        for k, v in sorted(modules.items()):
            f.write(f"from {k} import {', '.join(sorted(v))}\n")
        f.write(dedent(f"""\
            KEYWORDS = {keywords}
            CHECKS = {checks}
            REPORTERS = {reporters}
        """))

    const_path = os.path.join(python_base, pkgdist.MODULE_NAME, "_const.py")
    with open(const_path, 'w') as f:
        os.chmod(const_path, 0o644)
        # write install path constants to config
        if wheel_install:
            # write more dynamic _const file for wheel installs
            f.write(dedent("""\
                import os.path as osp
                import sys
                INSTALL_PREFIX = osp.abspath(sys.prefix)
                DATA_PATH = osp.join(INSTALL_PREFIX, {!r})
            """.format(DATA_INSTALL_OFFSET)))
        else:
            f.write("INSTALL_PREFIX=%r\n" % install_prefix)
            f.write("DATA_PATH=%r\n" %
                    os.path.join(install_prefix, DATA_INSTALL_OFFSET))
            f.close()

            # byte compile generated modules
            for path in (const_path, objects_path):
                byte_compile([path], prefix=python_base)
                byte_compile([path], optimize=1, prefix=python_base)
                byte_compile([path], optimize=2, prefix=python_base)


class install_data(dst_install_data.install_data):
    """Generate data files for install.

    Currently this includes keyword, check, and reporter name lists.
    """

    def run(self):
        self._generate_files()
        super().run()

    def _generate_files(self):
        with pkgdist.syspath(pkgdist.PACKAGEDIR):
            from pkgcheck import base, objects
            from pkgcheck.addons import caches

        os.makedirs(os.path.join(pkgdist.REPODIR, '.generated'), exist_ok=True)
        files = []

        # generate available scopes
        path = os.path.join(pkgdist.REPODIR, '.generated', 'scopes')
        with open(path, 'w') as f:
            f.write('\n'.join(base.scopes) + '\n')
        files.append(os.path.join('.generated', 'scopes'))

        # generate available cache types
        path = os.path.join(pkgdist.REPODIR, '.generated', 'caches')
        cache_objs = caches.CachedAddon.caches.values()
        with open(path, 'w') as f:
            f.write('\n'.join(x.type for x in cache_objs))
        files.append(os.path.join('.generated', 'caches'))

        # generate available object lists
        for obj in ('KEYWORDS', 'CHECKS', 'REPORTERS'):
            log.info(f'Generating {obj.lower()} list')
            path = os.path.join(pkgdist.REPODIR, '.generated', obj.lower())
            with open(path, 'w') as f:
                f.write('\n'.join(getattr(objects, obj)) + '\n')
            files.append(os.path.join('.generated', obj.lower()))
        self.data_files.append(('share/pkgcheck', files))


class build_clib(dst_build_clib.build_clib):
    """Build bash parsing library."""

    def run(self):
        if not use_system_tree_sitter_bash:
            with pkgdist.syspath(pkgdist.PACKAGEDIR):
                from pkgcheck.bash import build_library
            path = os.path.join(self.build_clib, 'pkgcheck', 'bash', 'lang.so')
            build_library(path, ['tree-sitter-bash'])


class build(pkgdist.build):
    """Force build_clib to run to build bash parsing library."""

    sub_commands = pkgdist.build.sub_commands[:]
    sub_commands.append(('build_clib', None))


setup(**dict(
    pkgdist_setup,
    license='BSD',
    author='Tim Harder',
    author_email='radhermit@gmail.com',
    description='pkgcore-based QA utility for ebuild repos',
    url='https://github.com/pkgcore/pkgcheck',
    data_files=list(chain(
        pkgdist.data_mapping('share/bash-completion/completions', 'completion/bash'),
        pkgdist.data_mapping('share/zsh/site-functions', 'completion/zsh'),
        pkgdist.data_mapping(DATA_INSTALL_OFFSET, 'data'),
    )),
    cmdclass=dict(
        pkgdist_cmds,
        install_data=install_data,
        install_lib=install_lib,
        install=install,
        build_clib=build_clib,
        build=build,
    ),
    classifiers=[
        'License :: OSI Approved :: BSD License',
        'Programming Language :: Python :: 3.9',
        'Programming Language :: Python :: 3.10',
        'Programming Language :: Python :: 3.11',
    ],
    extras_require={
        'network': ['requests'],
    },
    distclass=pkgdist.BinaryDistribution,
))