-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·128 lines (108 loc) · 4.3 KB
/
setup.py
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
#!/usr/bin/env python3
import errno
from itertools import chain
import os
import sys
from distutils import log
from distutils.util import byte_compile
from setuptools import setup
from snakeoil.dist import distutils_extensions as pkgdist
pkgdist_setup, pkgdist_cmds = pkgdist.setup()
# These offsets control where we install the config and service files.
DATA_INSTALL_OFFSET = os.path.join('share', pkgdist.MODULE)
CONFIG_INSTALL_OFFSET = os.path.join(DATA_INSTALL_OFFSET, 'config')
class install(pkgdist.install):
"""Install wrapper to generate and install lookup files."""
def run(self):
pkgdist.install.run(self)
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_lookup_config(self.install_purelib, target)
def write_lookup_config(python_base, install_prefix):
"""Generate file of install path constants."""
path = os.path.join(python_base, pkgdist.MODULE, "_const.py")
try:
os.makedirs(os.path.dirname(path))
except OSError as e:
if e.errno != errno.EEXIST:
raise
log.info("writing lookup config to %r" % path)
with pkgdist.syspath(pkgdist.PACKAGEDIR):
from bite import const
clients = tuple(sorted(const.CLIENTS.items()))
services = tuple(sorted(const.SERVICES.items()))
service_opts = tuple(sorted(const.SERVICE_OPTS.items()))
import textwrap
with open(path, "w") as f:
os.chmod(path, 0o644)
# write more dynamic file for wheel installs
if install_prefix != os.path.abspath(sys.prefix):
f.write(textwrap.dedent(f"""\
import os.path as osp
import sys
INSTALL_PREFIX = osp.abspath(sys.prefix)
DATA_PATH = osp.join(INSTALL_PREFIX, {DATA_INSTALL_OFFSET!r})
CONFIG_PATH = osp.join(INSTALL_PREFIX, {CONFIG_INSTALL_OFFSET!r})
CLIENTS = {clients}
SERVICES = {services}
SERVICE_OPTS = {service_opts}
"""))
else:
data_path = os.path.join(install_prefix, DATA_INSTALL_OFFSET),
config_path = os.path.join(install_prefix, CONFIG_INSTALL_OFFSET),
f.write(textwrap.dedent(f"""\
INSTALL_PREFIX = {install_prefix!r}
DATA_PATH = {data_path!r}
CONFIG_PATH = {config_path!r}
CLIENTS = {clients!r}
SERVICES = {services!r}
SERVICE_OPTS = {service_opts!r}
"""))
f.close()
byte_compile([path], prefix=python_base)
byte_compile([path], optimize=2, prefix=python_base)
class test(pkgdist.pytest):
"""Test wrapper to enforce testing against built version."""
def run(self):
# This is fairly hacky, but is done to ensure that the tests
# are ran purely from what's in build, reflecting back to the source
# only for misc bash scripts or config data.
key = 'BITE_OVERRIDE_DATA_PATH'
original = os.environ.get(key)
try:
os.environ[key] = os.path.dirname(os.path.realpath(__file__))
return super().run()
finally:
if original is not None:
os.environ[key] = original
else:
os.environ.pop(key, None)
setup(
description='bug, issue, and ticket extraction library and command line tool',
author='Tim Harder',
author_email='[email protected]',
url='https://github.com/bite/bite/',
license='BSD',
platforms=['any'],
data_files=list(chain(
pkgdist.data_mapping(CONFIG_INSTALL_OFFSET, 'config'),
pkgdist.data_mapping(os.path.join(DATA_INSTALL_OFFSET, 'services'), 'services'),
)),
cmdclass=dict(
pkgdist_cmds,
install=install,
test=test),
classifiers=(
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
),
**pkgdist_setup
)