Debian Codegraph @

dhpython/build/plugin_distutils.py

raw symbols SCIP
1 # Copyright © 2012-2013 Piotr Ożarowski <[email protected]g>
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 # THE SOFTWARE.
20
21 import logging
22 from collections.abc import Callable
23 from functools import wraps
24 from glob import glob1
25 from os import remove
26 from os.path import exists, isdir, join
27 from shutil import rmtree, move
28 from typing import (
29 TYPE_CHECKING,
30 Concatenate,
31 Literal,
32 ParamSpec,
33 TypeVar,
34 override
35 )
36
37 from dhpython.build.base import Base, shell_command, copy_test_files, create_setuptools_parallel_cfg
38 from dhpython.build.types import Args, Context
39
40 if TYPE_CHECKING:
41 from functools import _Wrapped
42
43
44 log = logging.getLogger('dhpython')
45 _setup_tpl = 'setup.py|setup-3.py'
46
47
48 P = ParamSpec('P')
49 T = TypeVar('T')
50
51 def create_pydistutils_cfg(
52 func: Callable[Concatenate["BuildSystem", Context, Args, P], T]
53 ) -> "_Wrapped[Concatenate[BuildSystem, Context, Args, P], T, Concatenate[BuildSystem, Context, Args, P], T]":
54 """distutils doesn't have sane command-line API - this decorator creates
55 .pydistutils.cfg file to workaround it
56
57 hint: if you think this is plain stupid, please don't read
58 distutils/setuptools/distribute sources
59 """
60
61 @wraps(func)
62 def wrapped_func(
63 self: "BuildSystem",
64 context: Context,
65 args: Args,
66 *oargs: P.args,
67 **kwargs: P.kwargs
68 ) -> T:
69 fpath = join(args.home_dir, '.pydistutils.cfg')
70 if not exists(fpath):
71 with open(fpath, 'w', encoding='utf-8') as fp:
72 lines = ['[clean]\n',
73 'all=1\n',
74 '[build]\n',
75 'build_lib={}\n'.format(args.build_dir),
76 '[install]\n',
77 'force=1\n',
78 'install_layout=deb\n',
79 'install_scripts=$base/bin\n',
80 'install_lib={}\n'.format(args.install_dir),
81 'prefix=/usr\n']
82 log.debug('pydistutils config file:\n%s', ''.join(lines))
83 fp.writelines(lines)
84 context.ENV['HOME'] = args.home_dir
85 return func(self, context, args, *oargs, **kwargs)
86
87 return wrapped_func
88
89
90 class BuildSystem(Base):
91 DESCRIPTION = 'Distutils build system'
92 SUPPORTED_INTERPRETERS = {'python', 'python3', 'python{version}',
93 'python-dbg', 'python3-dbg', 'python{version}-dbg'}
94 REQUIRED_FILES = [_setup_tpl]
95 OPTIONAL_FILES = {'setup.cfg': 1,
96 'requirements.txt': 1,
97 'PKG-INFO': 10,
98 '*.egg-info': 10}
99 CLEAN_FILES = Base.CLEAN_FILES | {'build'}
100
101 @override
102 def detect(self, context: Context) -> int:
103 result = super().detect(context)
104 if _setup_tpl in self.DETECTED_REQUIRED_FILES:
105 context.args['setup_py'] = self.DETECTED_REQUIRED_FILES[_setup_tpl][0]
106 else:
107 context.args['setup_py'] = 'setup.py'
108 return result
109
110 @shell_command
111 @create_pydistutils_cfg
112 @override
113 def clean(self, context: Context, args: Args) -> str | Literal[0]:
114 super().clean(context, args)
115 if exists(args.interpreter.binary()):
116 return '{interpreter} {setup_py} clean {args}'
117 return 0 # no need to invoke anything
118
119 @shell_command
120 @create_pydistutils_cfg
121 @override
122 def configure(self, context: Context, args: Args) -> str:
123 return '{interpreter} {setup_py} config {args}'
124
125 @shell_command
126 @create_pydistutils_cfg
127 @override
128 def build(self, context: Context, args: Args) -> str:
129 return '{interpreter.binary_dv} {setup_py} build {args}'
130
131 @shell_command
132 @create_setuptools_parallel_cfg()
133 def _bdist_wheel(self, context: Context, args: Args) -> str:
134 # pylint: disable=unused-argument
135 try:
136 # pylint: disable=unused-import
137 import wheel # type: ignore
138 except ImportError:
139 raise Exception("wheel is required to build wheels for distutils/setuptools packages. Build-Depend on python3-wheel.")
140 # Don't build a wheel in the deb install layout
141 fpath = join(args.home_dir, '.pydistutils.cfg')
142 remove(fpath)
143 return '{interpreter.binary_dv} -c "import setuptools, runpy; runpy.run_path(\'{setup_py}\')" bdist_wheel {args}'
144
145 @override
146 def build_wheel(self, context: Context, args: Args) -> None:
147 self._bdist_wheel(context, args)
148 dist_dir = join(args.dir, 'dist')
149 wheels = glob1(dist_dir, '*.whl')
150 n_wheels = len(wheels)
151 if n_wheels != 1:
152 raise Exception(f"Expected 1 wheel, found {n_wheels}")
153 move(join(dist_dir, wheels[0]), args.home_dir)
154
155 @shell_command
156 @create_pydistutils_cfg
157 @override
158 def install(self, context: Context, args: Args) -> str:
159 # remove egg-info dirs from build_dir
160 for fname in glob1(args.build_dir, '*.egg-info'):
161 fpath = join(args.build_dir, fname)
162 if isdir(fpath):
163 rmtree(fpath)
164 else:
165 remove(fpath)
166
167 return '{interpreter.binary_dv} {setup_py} install --root {destdir} {args}'
168
169 @shell_command
170 @create_pydistutils_cfg
171 @copy_test_files()
172 @override
173 def test(self, context: Context, args: Args) -> str:
174 return super().test_cmd(context, args)