aboutsummaryrefslogtreecommitdiff
blob: f6e96a36be4854e8595704c8a913e5bbd7faa03e (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
import logging
import os
import sys
from collections import defaultdict
from contextlib import contextmanager
from pathlib import Path
from textwrap import dedent

from setuptools import setup, Command
from setuptools.command.build import SubCommand, build as orig_build
from setuptools.command.install import install as orig_install
from setuptools.command.sdist import sdist as orig_sdist
from wheel.bdist_wheel import bdist_wheel as orig_bdist_wheel


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


@contextmanager
def sys_path():
    orig_path = sys.path[:]
    sys.path.insert(0, str(Path.cwd() / "src"))
    try:
        yield
    finally:
        sys.path = orig_path


class build_treesitter(Command, SubCommand):
    description = "build tree-sitter-bash library"

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def get_source_files(self):
        cwd = Path(__file__).parent / "tree-sitter-bash/src"
        return [
            str(cwd / "GNUmakefile"),
            str(cwd / "tree_sitter/parser.h"),
            str(cwd / "parser.c"),
            str(cwd / "scanner.cc"),
        ]

    library_path = Path(__file__).parent / "src/pkgcheck/bash/lang.so"

    def run(self):
        if not use_system_tree_sitter_bash:
            if not self.library_path.exists():
                logging.info("building tree-sitter-bash library")
                with sys_path():
                    from pkgcheck.bash import build_library
                build_library(self.library_path, ["tree-sitter-bash"])


class build(orig_build):
    sub_commands = orig_build.sub_commands + [("build_treesitter", None)]


class install(orig_install):
    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()
        self.write_obj_lists()
        self.generate_files()

        self.copy_tree("data", self.install_data)

        install_dir = Path(self.install_lib)
        if not use_system_tree_sitter_bash:
            self.reinitialize_command("build").ensure_finalized()
            (dst := install_dir / "pkgcheck/bash").mkdir(parents=True, exist_ok=True)
            self.copy_file(
                build_treesitter.library_path,
                dst / "lang.so",
                preserve_mode=True,
                preserve_times=False,
            )

    def write_obj_lists(self):
        """Generate config file of keyword, check, and other object lists."""
        (base_dir := Path(self.install_lib) / "pkgcheck").mkdir(parents=True, exist_ok=True)
        objects_path = base_dir / "_objects.py"
        const_path = base_dir / "_const.py"
        verinfo_path = base_dir / "_verinfo.py"

        # 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 sys_path():
            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"])

        logging.info(f"writing objects to {objects_path!r}")
        with objects_path.open("w") as f:
            objects_path.chmod(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}
                    """
                )
            )

        logging.info(f"writing path constants to {const_path!r}")
        with const_path.open("w") as f:
            const_path.chmod(0o644)
            f.write(
                dedent(
                    """\
                        from os.path import abspath, exists, join
                        import sys
                        INSTALL_PREFIX = abspath(sys.prefix)
                        if not exists(join(INSTALL_PREFIX, 'lib/pkgcore')):
                            INSTALL_PREFIX = abspath(sys.base_prefix)
                        DATA_PATH = join(INSTALL_PREFIX, 'share/pkgcheck')
                    """
                )
            )

        logging.info("generating version info")
        from snakeoil.version import get_git_version

        verinfo_path.write_text(f"version_info={get_git_version(Path(__file__).parent)!r}")

    def generate_files(self):
        with sys_path():
            from pkgcheck import base, objects
            from pkgcheck.addons import caches

        (dst := Path(self.install_data) / "share/pkgcheck").mkdir(parents=True, exist_ok=True)

        logging.info("Generating available scopes")
        (dst / "scopes").write_text("\n".join(base.scopes) + "\n")

        logging.info("Generating available cache types")
        cache_objs = caches.CachedAddon.caches.values()
        (dst / "caches").write_text("\n".join(x.type for x in cache_objs) + "\n")

        for obj in ("KEYWORDS", "CHECKS", "REPORTERS"):
            logging.info(f"Generating {obj.lower()} list")
            (dst / obj.lower()).write_text("\n".join(getattr(objects, obj)) + "\n")


class bdist_wheel(orig_bdist_wheel):
    def finalize_options(self):
        super().finalize_options()
        self.root_is_pure = False  # Mark us as not a pure python package

    def get_tag(self):
        _, _, plat = super().get_tag()
        # We don't contain any python source, nor any python extensions
        return "py3", "none", plat


class sdist(orig_sdist):
    def make_release_tree(self, base_dir, files):
        super().make_release_tree(base_dir, files)
        base_dir = Path(base_dir)

        if (man_page := Path(__file__).parent / "build/sphinx/man/pkgcheck.1").exists():
            (base_dir / "man").mkdir(parents=True, exist_ok=True)
            self.copy_file(
                man_page, base_dir / "man/pkgcheck.1", preserve_mode=False, preserve_times=False
            )

        logging.info("generating version info")
        from snakeoil.version import get_git_version

        (base_dir / "src/pkgcheck/_verinfo.py").write_text(
            f"version_info={get_git_version(Path(__file__).parent)!r}"
        )


setup(
    cmdclass={
        "bdist_wheel": bdist_wheel,
        "build": build,
        "build_treesitter": build_treesitter,
        "install": install,
        "sdist": sdist,
    }
)