Debian Codegraph @

setup.py

raw
1 # setup.py
2 # Part of ‘dput’, a Debian package upload toolkit.
3 #
4 # This is free software, and you are welcome to redistribute it under
5 # certain conditions; see the end of this file for copyright
6 # information, grant of license, and disclaimer of warranty.
7
8 """ Distribution setup for ‘dput’ library. """
9
10 import email.utils
11 import os.path
12 import pydoc
13 import re
14
15 import debian.changelog
16 import debian.copyright
17 import debian.deb822
18 import packaging.version
19 from setuptools import setup
20
21
22 debian_version_regex = re.compile(
23 r"""
24 # Anchor at start of string.
25 ^
26 # Optional: Epoch (digit sequence), followed by ‘:’.
27 ((?P<epoch>\d+):)?
28 # The version string of the release.
29 (?P<release_version>
30 # Upstream version string.
31 (?P<upstream_version>
32 # Match only if this segment begins with a digit.
33 (?=\d)
34 # Characters including ‘-’, ‘~’, ‘+’, only if all of those
35 # characters follow.
36 ([\w.~+-]+(?=[-])(?![-]+)(?=[~])(?![~-]+)(?=[+])(?![~+-]+))
37 # Alternately, match the next group.
38 |
39 # Characters including ‘-’, ‘~’, only if all of those
40 # characters follow.
41 ([\w.~-]+(?=[-])(?![-]+)(?=[~])(?![~-]+))
42 # Alternately, match the next group.
43 |
44 # Characters including ‘-’, ‘+’, only if all of those
45 # characters follow.
46 ([\w.+-]+(?=[-])(?![-]+)(?=[+])(?![~+-]+))
47 # Alternately, match the next group.
48 |
49 # Characters including ‘-’, only if that character follows.
50 ([\w.-]+(?=[-])(?![-]+))
51 # Alternately, match the next group.
52 |
53 # Characters including ‘~’, ‘+’, only if all of those
54 # characters follow.
55 ([\w.~+]+(?=[~])(?![~-]+)(?=[+])(?![~+-]+))
56 # Alternately, match the next group.
57 |
58 # Characters including ‘~’, only if that character follows.
59 ([\w.~]+(?=[~])(?![~-]+))
60 # Alternately, match the next group.
61 |
62 # Characters including ‘+’, only if that character follows.
63 ([\w.+]+(?=[+])(?![~+-]+))
64 # Alternately, match the next group.
65 |
66 # Characters excluding all of ‘-’, ‘~’, ‘+’.
67 ([\w.]+)
68 )
69 # Optional: ‘-’ followed by Debian release number (alphanumerics).
70 (-(?P<debian_revision>
71 # Characters including ‘~’, ‘+’, only if all of those
72 # characters follow.
73 ([\w.~+]+(?=[~])(?![~]+)(?=[+])(?![~+]+))
74 # Alternately, match the next group.
75 |
76 # Characters including ‘~’, only if that character follows.
77 ([\w.~]+(?=[~])(?![~]+))
78 # Alternately, match the next group.
79 |
80 # Characters including ‘+’, only if that character follows.
81 ([\w.+]+(?=[+])(?![+]+))
82 # Alternately, match the next group.
83 |
84 # Characters excluding all of ‘-’, ‘~’, ‘+’.
85 ([\w.]+)
86 ))?
87 )
88 # Optional: ‘~’ followed by prerelease segment (indicator this version
89 # is *prior* to `release_version`).
90 (~(?P<prerelease_segment>
91 # Sequence of ASCII alphabetic characters.
92 (?P<prerelease_prefix>[A-Za-z]+)
93 # Sequence of ASCII digits.
94 (?P<prerelease_ordinal>\d+)
95 ))?
96 # Optional: Post-release annotation of the version after release,
97 # identifying subsequent re-builds, etc.
98 # ‘+’ followed by alphanumerics and other version characters.
99 (\+(?P<annotation>[\w.+:-]+))?
100 # Anchor at end of string.
101 $
102 """,
103 (re.VERBOSE | re.ASCII))
104 """ Regular expression pattern for a Debian package version.
105
106 The match groups allow extracting specifically those parts relevant for
107 composing the corresponding Python package version.
108
109 <URL:https://www.debian.org/doc/debian-policy/ch-controlfields.html#version>
110 """
111
112 python_prerelease_regex = re.compile(
113 r"""
114 # Anchor at start of string.
115 ^
116 # Specific values allowed for pre-release segment prefix.
117 (?P<prefix>a|alpha|b|beta|rc|c|pre|preview)
118 # Sequence of digits.
119 (?P<ordinal>\d+)
120 # Anchor at end of string.
121 $
122 """,
123 (re.VERBOSE | re.ASCII))
124 """ Regular expression pattern for a PyPA conformant pre-release segment.
125
126 <URL:https://packaging.python.org/specifications/version-specifiers/>
127 """
128
129
130 def packaging_version_from_debian_version(debian_version):
131 """ Make valid Python PEP 440 version, from `debian_version`.
132
133 :param debian_version: A `debian.changelog.Version` object representing
134 the version of the Debian package.
135 :return: A valid Python `packaging` version text representing the
136 `debian_version` as a valid PEP 440 version text.
137 """
138 match = debian_version_regex.match(str(debian_version))
139 if match is None:
140 raise ValueError(
141 "Debian package version {version!r}"
142 " does not match expected pattern {pattern!r}".format(
143 version=debian_version,
144 pattern=debian_version_regex,
145 )
146 )
147 prerelease_segment = match.group('prerelease_segment')
148 if prerelease_segment is not None:
149 prerelease_match = python_prerelease_regex.match(
150 str(prerelease_segment))
151 if prerelease_match is None:
152 raise ValueError(
153 "Prerelease segment {prerelease!r}"
154 " does not match expected pattern {pattern!r}".format(
155 prerelease=prerelease_segment,
156 pattern=python_prerelease_regex,
157 ))
158 packaging_prerelease = (
159 "" if prerelease_segment is None
160 else prerelease_segment if prerelease_match
161 else "dev{}".format(match.group('prerelease_ordinal'))
162 )
163 packaging_version = "{release}{prerelease}".format(
164 release=match.group('release_version'),
165 prerelease=packaging_prerelease,
166 )
167 # Verify that `packaging_version` is a valid Python package version.
168 try:
169 __ = packaging.version.Version(packaging_version)
170 except packaging.version.InvalidVersion as exc:
171 raise ValueError(
172 "Package release version {version!r}"
173 " is not a valid Python package version".format(
174 version=packaging_version)) from exc
175 return packaging_version
176
177
178 setup_dir = os.path.dirname(__file__)
179
180 readme_file_path = os.path.join(setup_dir, "README")
181 with open(readme_file_path, encoding='UTF-8') as readme_file:
182 (synopsis, long_description) = pydoc.splitdoc(readme_file.read())
183
184 changelog_file_path = os.path.join(setup_dir, "debian", "changelog")
185 with open(changelog_file_path, encoding='UTF-8') as changelog_file:
186 changelog = debian.changelog.Changelog(changelog_file, max_blocks=1)
187 (author_name, author_email) = email.utils.parseaddr(changelog.author)
188 version = packaging_version_from_debian_version(changelog.version)
189
190 control_file_path = os.path.join(setup_dir, "debian", "control")
191 with open(control_file_path, encoding='UTF-8') as control_file:
192 control_structure = debian.deb822.Deb822(control_file)
193 (maintainer_name, maintainer_email) = email.utils.parseaddr(
194 control_structure['maintainer'])
195
196 copyright_file_path = os.path.join(setup_dir, "debian", "copyright")
197 with open(copyright_file_path, encoding='UTF-8') as copyright_file:
198 copyright_structure = debian.copyright.Copyright(copyright_file)
199 general_files_paragraph = copyright_structure.find_files_paragraph("*")
200 license = general_files_paragraph.license
201
202
203 setup_kwargs = dict(
204 # Core project metadata.
205 version=version,
206 description=synopsis,
207 long_description=long_description,
208 author=author_name,
209 author_email=author_email,
210 maintainer=maintainer_name,
211 maintainer_email=maintainer_email,
212 license=license.synopsis,
213 url=control_structure['homepage'],
214 )
215
216
217 if __name__ == '__main__': # pragma: nocover
218 setup(**setup_kwargs)
219
220
221 # Copyright © 2008–2026 Ben Finney <[email protected]>
222 #
223 # This is free software: you may copy, modify, and/or distribute this work
224 # under the terms of the GNU General Public License as published by the
225 # Free Software Foundation; version 3 of that license or any later version.
226 # No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details.
227
228
229 # Local variables:
230 # coding: utf-8
231 # mode: python
232 # End:
233 # vim: fileencoding=utf-8 filetype=python :