• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pybuilder / pybuilder / 17164080924

22 Aug 2025 07:21PM UTC coverage: 84.026%. First build
17164080924

Pull #932

github

web-flow
Merge bed1f1933 into 7d30a872e
Pull Request #932: Migrate from `python setup.py sdist/bdist_wheel` to `build`

2169 of 2672 branches covered (81.18%)

Branch coverage included in aggregate %.

12 of 13 new or added lines in 2 files covered. (92.31%)

5532 of 6493 relevant lines covered (85.2%)

39.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.05
/src/main/python/pybuilder/plugins/python/distutils_plugin.py
1
#   -*- coding: utf-8 -*-
2
#
3
#   This file is part of PyBuilder
4
#
5
#   Copyright 2011-2020 PyBuilder Team
6
#
7
#   Licensed under the Apache License, Version 2.0 (the "License");
8
#   you may not use this file except in compliance with the License.
9
#   You may obtain a copy of the License at
10
#
11
#       http://www.apache.org/licenses/LICENSE-2.0
12
#
13
#   Unless required by applicable law or agreed to in writing, software
14
#   distributed under the License is distributed on an "AS IS" BASIS,
15
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
#   See the License for the specific language governing permissions and
17
#   limitations under the License.
18

19
import io
48✔
20
import os
48✔
21
import re
48✔
22
import string
48✔
23
from datetime import datetime
48✔
24
from textwrap import dedent
48✔
25

26
from pybuilder import pip_utils
48✔
27
from pybuilder.core import (after,
48✔
28
                            before,
29
                            use_plugin,
30
                            init,
31
                            task,
32
                            RequirementsFile,
33
                            Dependency)
34
from pybuilder.errors import BuildFailedException, MissingPrerequisiteException
48✔
35
from pybuilder.python_utils import StringIO
48✔
36
from pybuilder.utils import (as_list,
48✔
37
                             is_string,
38
                             is_notstr_iterable,
39
                             get_dist_version_string,
40
                             safe_log_file_name,
41
                             tail_log)
42

43
use_plugin("python.core")
48✔
44

45
LEADING_TAB_RE = re.compile(r'^(\t*)')
48✔
46
DATA_FILES_PROPERTY = "distutils_data_files"
48✔
47
SETUP_TEMPLATE = string.Template("""#!/usr/bin/env python
48✔
48
#   -*- coding: utf-8 -*-
49
$remove_hardlink_capabilities_for_shared_filesystems
50
from $module import setup
51
from $module.command.install import install as _install
52

53
class install(_install):
54
    def pre_install_script(self):
55
$preinstall_script
56

57
    def post_install_script(self):
58
$postinstall_script
59

60
    def run(self):
61
        self.pre_install_script()
62

63
        _install.run(self)
64

65
        self.post_install_script()
66

67
if __name__ == '__main__':
68
    setup(
69
        name = $name,
70
        version = $version,
71
        description = $summary,
72
        long_description = $description,
73
        long_description_content_type = $description_content_type,
74
        classifiers = $classifiers,
75
        keywords = $setup_keywords,
76

77
        author = $author,
78
        author_email = $author_email,
79
        maintainer = $maintainer,
80
        maintainer_email = $maintainer_email,
81

82
        license = $license,
83

84
        url = $url,
85
        project_urls = $project_urls,
86

87
        scripts = $scripts,
88
        packages = $packages,
89
        namespace_packages = $namespace_packages,
90
        py_modules = $modules,
91
        entry_points = $entry_points,
92
        data_files = $data_files,
93
        package_data = $package_data,
94
        install_requires = $dependencies,
95
        dependency_links = $dependency_links,
96
        zip_safe = $zip_safe,
97
        cmdclass = {'install': install},
98
        python_requires = $python_requires,
99
        obsoletes = $obsoletes,
100
    )
101
""")
102

103

104
def default(value, default=""):
48✔
105
    if value is None:
48✔
106
        return default
48✔
107
    return value
48✔
108

109

110
def as_str(value):
48✔
111
    return repr(str(value))
48✔
112

113

114
@init
48✔
115
def initialize_distutils_plugin(project):
48✔
116
    project.plugin_depends_on("pypandoc", "~=1.4")
48✔
117
    project.plugin_depends_on("twine", ">=1.15.0")
48✔
118
    project.plugin_depends_on("setuptools", ">=76.0", eager_update=False)
48✔
119
    project.plugin_depends_on("build", ">=1.3.0", eager_update=False)
48✔
120

121
    project.set_property_if_unset("distutils_commands", ["sdist", "wheel"])
48✔
122
    project.set_property_if_unset("distutils_command_options", None)
48✔
123

124
    # Workaround for http://bugs.python.org/issue8876 , unable to build a bdist
125
    # on a filesystem that does not support hardlinks
126
    project.set_property_if_unset("distutils_issue8876_workaround_enabled", False)
48✔
127
    project.set_property_if_unset("distutils_classifiers", [
48✔
128
        "Development Status :: 3 - Alpha",
129
        "Programming Language :: Python"
130
    ])
131
    project.set_property_if_unset("distutils_fail_on_warnings", False)
48✔
132

133
    project.set_property_if_unset("distutils_upload_register", False)
48✔
134
    project.set_property_if_unset("distutils_upload_repository", None)
48✔
135
    project.set_property_if_unset("distutils_upload_repository_key", None)
48✔
136
    project.set_property_if_unset("distutils_upload_sign", False)
48✔
137
    project.set_property_if_unset("distutils_upload_sign_identity", None)
48✔
138
    project.set_property_if_unset("distutils_upload_skip_existing", False)
48✔
139

140
    project.set_property_if_unset("distutils_readme_description", False)
48✔
141
    project.set_property_if_unset("distutils_readme_file", "README.md")
48✔
142
    project.set_property_if_unset("distutils_readme_file_convert", False)
48✔
143
    project.set_property_if_unset("distutils_readme_file_type", None)
48✔
144
    project.set_property_if_unset("distutils_readme_file_encoding", None)
48✔
145
    project.set_property_if_unset("distutils_readme_file_variant", None)
48✔
146
    project.set_property_if_unset("distutils_summary_overwrite", False)
48✔
147
    project.set_property_if_unset("distutils_description_overwrite", False)
48✔
148

149
    project.set_property_if_unset("distutils_console_scripts", None)
48✔
150
    project.set_property_if_unset("distutils_entry_points", None)
48✔
151
    project.set_property_if_unset("distutils_setup_keywords", None)
48✔
152
    project.set_property_if_unset("distutils_zip_safe", True)
48✔
153

154

155
@after("prepare")
48✔
156
def set_description(project, logger, reactor):
48✔
157
    if project.get_property("distutils_readme_description"):
48✔
158
        description = None
24✔
159
        if project.get_property("distutils_readme_file_convert"):
24!
160
            try:
×
161
                reactor.pybuilder_venv.verify_can_execute(["pandoc", "--version"], "pandoc", "distutils")
×
162
                description = doc_convert(project, logger)
×
163
            except (MissingPrerequisiteException, ImportError):
×
164
                logger.warn("Was unable to find pandoc or pypandoc and did not convert the documentation")
×
165
        else:
166
            with io.open(project.expand_path("$distutils_readme_file"), "rt",
24✔
167
                         encoding=project.get_property("distutils_readme_file_encoding")) as f:
168
                description = f.read()
24✔
169

170
        if description:
24!
171
            if (not hasattr(project, "summary") or
24!
172
                    project.summary is None or
173
                    project.get_property("distutils_summary_overwrite")):
174
                setattr(project, "summary", description.splitlines()[0].strip())
×
175

176
            if (not hasattr(project, "description") or
24!
177
                    project.description is None or
178
                    project.get_property("distutils_description_overwrite")):
179
                setattr(project, "description", description)
24✔
180

181
    if (not hasattr(project, "description") or
48✔
182
            not project.description):
183
        if hasattr(project, "summary") and project.summary:
48!
184
            description = project.summary
×
185
        else:
186
            description = project.name
48✔
187

188
        setattr(project, "description", description)
48✔
189

190
    warn = False
48✔
191
    if len(project.summary) >= 512:
48!
192
        logger.warn("Project summary SHOULD be shorter than 512 characters per PEP-426")
×
193
        warn = True
×
194

195
    if "\n" in project.summary or "\r" in project.summary:
48!
196
        logger.warn("Project summary SHOULD NOT contain new-line characters per PEP-426")
×
197
        warn = True
×
198

199
    if len(project.summary) >= 2048:
48!
200
        raise BuildFailedException("Project summary MUST NOT be shorter than 2048 characters per PEP-426")
×
201

202
    if warn and project.get_property("distutils_fail_on_warnings"):
48!
203
        raise BuildFailedException("Distutil plugin warnings caused a build failure. Please see warnings above.")
×
204

205

206
@after("package")
48✔
207
def write_setup_script(project, logger):
48✔
208
    setup_script = project.expand_path("$dir_dist", "setup.py")
48✔
209
    logger.info("Writing setup.py as %s", setup_script)
48✔
210

211
    with io.open(setup_script, "wt", encoding="utf-8") as setup_file:
48✔
212
        script = render_setup_script(project)
48✔
213
        setup_file.write(script)
48✔
214

215
    os.chmod(setup_script, 0o755)
48✔
216

217

218
def render_setup_script(project):
48✔
219
    author = ", ".join(map(lambda a: a.name, project.authors))
48✔
220
    author_email = ", ".join(map(lambda a: a.email, project.authors))
48✔
221
    maintainer = ", ".join(map(lambda a: a.name, project.maintainers))
48✔
222
    maintainer_email = ",".join(map(lambda a: a.email, project.maintainers))
48✔
223

224
    template_values = {
48✔
225
        "module": "setuptools",
226
        "name": as_str(project.name),
227
        "version": as_str(project.dist_version),
228
        "summary": as_str(default(project.summary)),
229
        "description": as_str(default(project.description)),
230
        "description_content_type": repr(_get_description_content_type(project)),
231
        "author": as_str(author),
232
        "author_email": as_str(author_email),
233
        "maintainer": as_str(maintainer),
234
        "maintainer_email": as_str(maintainer_email),
235
        "license": as_str(default(project.license)),
236
        "url": as_str(default(project.url)),
237
        "project_urls": build_map_string(project.urls),
238
        "scripts": build_scripts_string(project),
239
        "packages": build_packages_string(project),
240
        "namespace_packages": build_namespace_packages_string(project),
241
        "modules": build_modules_string(project),
242
        "classifiers": build_classifiers_string(project),
243
        "entry_points": build_entry_points_string(project),
244
        "data_files": build_data_files_string(project),
245
        "package_data": build_package_data_string(project),
246
        "dependencies": build_install_dependencies_string(project),
247
        "dependency_links": build_dependency_links_string(project),
248
        "remove_hardlink_capabilities_for_shared_filesystems": (
249
            "import os\ndel os.link"
250
            if project.get_property("distutils_issue8876_workaround_enabled")
251
            else ""),
252
        "preinstall_script": _normalize_setup_post_pre_script(project.setup_preinstall_script or "pass"),
253
        "postinstall_script": _normalize_setup_post_pre_script(project.setup_postinstall_script or "pass"),
254
        "setup_keywords": build_setup_keywords(project),
255
        "python_requires": as_str(default(project.requires_python)),
256
        "obsoletes": build_string_from_array(project.obsoletes),
257
        "zip_safe": project.get_property("distutils_zip_safe")
258
    }
259

260
    return SETUP_TEMPLATE.substitute(template_values)
48✔
261

262

263
@after("package")
48✔
264
def write_manifest_file(project, logger):
48✔
265
    if len(project.manifest_included_files) == 0 and len(project.manifest_included_directories) == 0:
48✔
266
        logger.debug("No data to write into MANIFEST.in")
48✔
267
        return
48✔
268

269
    logger.debug("Files included in MANIFEST.in: %s" %
48✔
270
                 project.manifest_included_files)
271

272
    manifest_filename = project.expand_path("$dir_dist", "MANIFEST.in")
48✔
273
    logger.info("Writing MANIFEST.in as %s", manifest_filename)
48✔
274

275
    with open(manifest_filename, "w") as manifest_file:
48✔
276
        manifest_file.write(render_manifest_file(project))
48✔
277

278
    os.chmod(manifest_filename, 0o664)
48✔
279

280

281
@before("publish")
48✔
282
def build_binary_distribution(project, logger, reactor):
48✔
283
    logger.info("Building binary distribution in %s",
48✔
284
                project.expand_path("$dir_dist"))
285

286
    commands = [build_command_with_options(cmd, project.get_property("distutils_command_options"))
48✔
287
                for cmd in as_list(project.get_property("distutils_commands"))]
288
    execute_distutils(project, logger, reactor.pybuilder_venv, commands)
48✔
289
    upload_check(project, logger, reactor)
48✔
290

291

292
@task("install")
48✔
293
def install_distribution(project, logger, reactor):
48✔
294
    logger.info("Installing project %s-%s", project.name, project.version)
48✔
295

296
    _prepare_reports_dir(project)
48✔
297
    outfile_name = project.expand_path("$dir_reports", "distutils",
48✔
298
                                       "pip_install_%s" % datetime.utcnow().strftime("%Y%m%d%H%M%S"))
299
    pip_utils.pip_install(
48✔
300
        install_targets=project.expand_path("$dir_dist"),
301
        python_env=reactor.python_env_registry["system"],
302
        index_url=project.get_property("install_dependencies_index_url"),
303
        extra_index_url=project.get_property("install_dependencies_extra_index_url"),
304
        force_reinstall=True,
305
        logger=logger,
306
        verbose=project.get_property("pip_verbose"),
307
        cwd=".",
308
        outfile_name=outfile_name,
309
        error_file_name=outfile_name)
310

311

312
@task("upload", description="Upload a project to PyPi.")
48✔
313
def upload(project, logger, reactor):
48✔
314
    repository = project.get_property("distutils_upload_repository")
48✔
315
    repository_args = []
48✔
316
    if repository:
48✔
317
        repository_args = ["--repository-url", repository]
48✔
318
    else:
319
        repository_key = project.get_property("distutils_upload_repository_key")
48✔
320
        if repository_key:
48✔
321
            repository_args = ["--repository", repository_key]
48✔
322

323
    upload_sign = project.get_property("distutils_upload_sign")
48✔
324
    sign_identity = project.get_property("distutils_upload_sign_identity")
48✔
325
    upload_sign_args = []
48✔
326
    if upload_sign:
48✔
327
        upload_sign_args = ["--sign"]
48✔
328
        if sign_identity:
48✔
329
            upload_sign_args += ["--identity", sign_identity]
48✔
330

331
    if project.get_property("distutils_upload_register"):
48✔
332
        logger.info("Registering project %s-%s%s", project.name, project.version,
48✔
333
                    (" into repository '%s'" % repository) if repository else "")
334
        execute_twine(project, logger, reactor.pybuilder_venv, repository_args, "register")
48✔
335

336
    skip_existing = project.get_property("distutils_upload_skip_existing")
48✔
337
    logger.info("Uploading project %s-%s%s%s%s%s", project.name, project.version,
48✔
338
                (" to repository '%s'" % repository) if repository else "",
339
                get_dist_version_string(project, " as version %s"),
340
                (" signing%s" % (" with %s" % sign_identity if sign_identity else "")) if upload_sign else "",
341
                (", will skip existing" if skip_existing else ""))
342

343
    upload_cmd_args = repository_args + upload_sign_args
48✔
344
    if skip_existing:
48!
345
        upload_cmd_args.append("--skip-existing")
×
346

347
    execute_twine(project, logger, reactor.pybuilder_venv, upload_cmd_args, "upload")
48✔
348

349

350
def upload_check(project, logger, reactor):
48✔
351
    logger.info("Running Twine check for generated artifacts")
48✔
352
    execute_twine(project, logger, reactor.pybuilder_venv, [], "check")
48✔
353

354

355
def render_manifest_file(project):
48✔
356
    manifest_content = StringIO()
48✔
357

358
    for included_file in project.manifest_included_files:
48✔
359
        manifest_content.write("include %s\n" % included_file)
48✔
360

361
    for directory, pattern_list in project.manifest_included_directories:
48✔
362
        patterns = ' '.join(pattern_list)
48✔
363
        manifest_content.write("recursive-include %s %s\n" % (directory, patterns))
48✔
364

365
    return manifest_content.getvalue()
48✔
366

367

368
def build_command_with_options(command, distutils_command_options=None):
48✔
369
    if command == "bdist_wheel":
48!
NEW
370
        command = "wheel"
×
371
    commands = [f"--{command}"]
48✔
372
    if distutils_command_options:
48✔
373
        try:
48✔
374
            commands.extend([f"-C{cmd}" for cmd in as_list(distutils_command_options[command])])
48✔
375
        except KeyError:
48✔
376
            pass
48✔
377
    return commands
48✔
378

379

380
def execute_distutils(project, logger, python_env, distutils_commands):
48✔
381
    reports_dir = _prepare_reports_dir(project)
48✔
382
    setup_script_dir = project.expand_path("$dir_dist")
48✔
383

384
    for command in distutils_commands:
48✔
385
        if is_string(command):
48✔
386
            out_file = os.path.join(reports_dir, safe_log_file_name(command))
48✔
387
        else:
388
            out_file = os.path.join(reports_dir, safe_log_file_name("__".join(command)))
48✔
389
        with open(out_file, "w") as out_f:
48✔
390
            commands = python_env.executable + ["-c",
48✔
391
                                                "import sys; del sys.path[0]; "
392
                                                "import runpy; runpy.run_module('build.__main__', run_name='__main__')"
393
                                                ]
394
            if project.get_property("verbose"):
48✔
395
                commands.append("-v")
24✔
396
            if is_string(command):
48✔
397
                commands.extend(command.split())
48✔
398
            else:
399
                commands.extend(command)
48✔
400
            commands.append(setup_script_dir)
48✔
401
            logger.debug("Executing distutils command: %s", commands)
48✔
402
            return_code = python_env.run_process_and_wait(commands, project.expand_path("$dir_dist"), out_f)
48✔
403
            if return_code != 0:
48!
404
                raise BuildFailedException(
×
405
                    "Error while executing setup command %s. See %s for full details:\n%s",
406
                    command, out_file, tail_log(out_file))
407

408

409
def execute_twine(project, logger, python_env, command_args, command):
48✔
410
    reports_dir = _prepare_reports_dir(project)
48✔
411
    dist_artifact_dir, artifacts = _get_generated_artifacts(project, logger)
48✔
412

413
    if command == "register":
48✔
414
        for artifact in artifacts:
48✔
415
            out_file = os.path.join(reports_dir,
48✔
416
                                    safe_log_file_name("twine_%s_%s.log" % (command, os.path.basename(artifact))))
417
            _execute_twine(project, logger, python_env,
48✔
418
                           [command] + command_args + [artifact], dist_artifact_dir, out_file)
419
    else:
420
        out_file = os.path.join(reports_dir, safe_log_file_name("twine_%s.log" % command))
48✔
421
        _execute_twine(project, logger, python_env,
48✔
422
                       [command] + command_args + artifacts, dist_artifact_dir, out_file)
423

424

425
def _execute_twine(project, logger, python_env, command, work_dir, out_file):
48✔
426
    with open(out_file, "w") as out_f:
48✔
427
        commands = python_env.executable + ["-m", "twine"] + command
48✔
428
        logger.debug("Executing Twine: %s", commands)
48✔
429
        return_code = python_env.run_process_and_wait(commands, work_dir, out_f)
48✔
430
        if return_code != 0:
48!
431
            raise BuildFailedException(
×
432
                "Error while executing Twine %s. See %s for full details:\n%s", command, out_file, tail_log(out_file))
433

434

435
def strip_comments(requirements):
48✔
436
    return [requirement for requirement in requirements
48✔
437
            if not requirement.strip().startswith("#")]
438

439

440
def quote(requirements):
48✔
441
    return ['"%s"' % requirement for requirement in requirements]
48✔
442

443

444
def is_editable_requirement(requirement):
48✔
445
    return "-e " in requirement or "--editable " in requirement
48✔
446

447

448
def flatten_and_quote(requirements_file):
48✔
449
    with open(requirements_file.name, 'r') as requirements_file:
48✔
450
        requirements = [requirement.strip("\n") for requirement in requirements_file.readlines()]
48✔
451
        requirements = [requirement for requirement in requirements if requirement]
48✔
452
        return quote(strip_comments(requirements))
48✔
453

454

455
def format_single_dependency(dependency):
48✔
456
    return '%s%s' % (dependency.name, pip_utils.build_dependency_version_string(dependency))
48✔
457

458

459
def build_install_dependencies_string(project):
48✔
460
    dependencies = [
48✔
461
        dependency for dependency in project.dependencies
462
        if isinstance(dependency, Dependency) and not dependency.url]
463
    requirements = [
48✔
464
        requirement for requirement in project.dependencies
465
        if isinstance(requirement, RequirementsFile)]
466
    if not dependencies and not requirements:
48✔
467
        return "[]"
48✔
468

469
    dependencies = [format_single_dependency(dependency) for dependency in dependencies]
48✔
470
    requirements = [strip_comments(flatten_and_quote(requirement)) for requirement in requirements]
48✔
471
    flattened_requirements = [dependency for dependency_list in requirements for dependency in dependency_list]
48✔
472
    flattened_requirements_without_editables = [
48✔
473
        requirement for requirement in flattened_requirements if not is_editable_requirement(requirement)]
474

475
    dependencies.extend(flattened_requirements_without_editables)
48✔
476

477
    for i, dep in enumerate(dependencies):
48✔
478
        if dep.startswith('"') and dep.endswith('"'):
48✔
479
            dependencies[i] = dep[1:-1]
48✔
480

481
    return build_string_from_array(dependencies)
48✔
482

483

484
def build_dependency_links_string(project):
48✔
485
    dependency_links = [
48✔
486
        dependency for dependency in project.dependencies
487
        if isinstance(dependency, Dependency) and dependency.url]
488
    requirements = [
48✔
489
        requirement for requirement in project.dependencies
490
        if isinstance(requirement, RequirementsFile)]
491

492
    editable_links_from_requirements = []
48✔
493
    for requirement in requirements:
48✔
494
        editables = [editable for editable in flatten_and_quote(requirement) if is_editable_requirement(editable)]
48✔
495
        editable_links_from_requirements.extend(
48✔
496
            [editable.replace("--editable ", "").replace("-e ", "") for editable in editables])
497

498
    if not dependency_links and not requirements:
48✔
499
        return "[]"
48✔
500

501
    def format_single_dependency(dependency):
48✔
502
        return '%s' % dependency.url
48✔
503

504
    all_dependency_links = [link for link in map(format_single_dependency, dependency_links)]
48✔
505
    all_dependency_links.extend(editable_links_from_requirements)
48✔
506

507
    for i, dep in enumerate(all_dependency_links):
48✔
508
        if dep.startswith('"') and dep.endswith('"'):
48✔
509
            all_dependency_links[i] = dep[1:-1]
48✔
510

511
    return build_string_from_array(all_dependency_links)
48✔
512

513

514
def build_scripts_string(project):
48✔
515
    scripts = [script for script in project.list_scripts()]
48✔
516

517
    scripts_dir = project.get_property("dir_dist_scripts")
48✔
518
    if scripts_dir:
48✔
519
        scripts = list(map(lambda s: '{}/{}'.format(scripts_dir, s), scripts))
48✔
520

521
    return build_string_from_array(scripts)
48✔
522

523

524
def build_data_files_string(project):
48✔
525
    indent = 8
48✔
526
    """
40✔
527
    data_files = [
528
      ('bin', ['foo','bar','hhrm'])
529
    ]
530
    """
531
    data_files = project.files_to_install
48✔
532
    if not len(data_files):
48✔
533
        return '[]'
48✔
534

535
    result = "[\n"
48✔
536

537
    for dataType, dataFiles in data_files:
48✔
538
        result += (" " * (indent + 4)) + "('%s', ['" % dataType
48✔
539
        result += "', '".join(dataFiles)
48✔
540
        result += "']),\n"
48✔
541

542
    result = result[:-2] + "\n"
48✔
543
    result += " " * indent + "]"
48✔
544
    return result
48✔
545

546

547
def build_package_data_string(project):
48✔
548
    package_data = project.package_data
48✔
549
    if not package_data:
48✔
550
        return "{}"
48✔
551

552
    indent = 8
48✔
553

554
    sorted_keys = sorted(project.package_data.keys())
48✔
555

556
    result = "{\n"
48✔
557

558
    for pkgType in sorted_keys:
48✔
559
        result += " " * (indent + 4)
48✔
560
        result += "'%s': " % pkgType
48✔
561
        result += "['"
48✔
562
        result += "', '".join(package_data[pkgType])
48✔
563
        result += "'],\n"
48✔
564

565
    result = result[:-2] + "\n"
48✔
566
    result += " " * indent + "}"
48✔
567

568
    return result
48✔
569

570

571
def build_map_string(m):
48✔
572
    if not m:
48✔
573
        return "{}"
48✔
574

575
    indent = 8
48✔
576

577
    sorted_keys = sorted(m.keys())
48✔
578

579
    result = "{\n"
48✔
580

581
    for k in sorted_keys:
48✔
582
        result += " " * (indent + 4)
48✔
583
        result += "%r: %r,\n" % (k, m[k])
48✔
584

585
    result = result[:-2] + "\n"
48✔
586
    result += " " * indent + "}"
48✔
587

588
    return result
48✔
589

590

591
def build_namespace_packages_string(project):
48✔
592
    return build_string_from_array([pkg for pkg in project.explicit_namespaces])
48✔
593

594

595
def build_packages_string(project):
48✔
596
    return build_string_from_array([pkg for pkg in project.list_packages()])
48✔
597

598

599
def build_modules_string(project):
48✔
600
    return build_string_from_array([mod for mod in project.list_modules()])
48✔
601

602

603
def build_entry_points_string(project):
48✔
604
    console_scripts = project.get_property('distutils_console_scripts')
48✔
605
    entry_points = project.get_property('distutils_entry_points')
48✔
606
    if console_scripts is not None and entry_points is not None:
48✔
607
        raise BuildFailedException("'distutils_console_scripts' cannot be combined with 'distutils_entry_points'")
48✔
608

609
    if entry_points is None:
48✔
610
        entry_points = dict()
48✔
611

612
    if console_scripts is not None:
48✔
613
        entry_points['console_scripts'] = console_scripts
48✔
614

615
    if len(entry_points) == 0:
48✔
616
        return '{}'
48✔
617

618
    indent = 8
48✔
619
    result = "{\n"
48✔
620

621
    for k in sorted(entry_points.keys()):
48✔
622
        result += " " * (indent + 4)
48✔
623
        result += "'%s': %s" % (k, build_string_from_array(as_list(entry_points[k]), indent + 8)) + ",\n"
48✔
624

625
    result = result[:-2] + "\n"
48✔
626
    result += (" " * indent) + "}"
48✔
627

628
    return result
48✔
629

630

631
def build_setup_keywords(project):
48✔
632
    setup_keywords = project.get_property("distutils_setup_keywords")
48✔
633
    if not setup_keywords or not len(setup_keywords):
48✔
634
        return repr("")
48✔
635

636
    if isinstance(setup_keywords, (list, tuple)):
48✔
637
        return repr(" ".join(setup_keywords))
48✔
638

639
    return repr(setup_keywords)
48✔
640

641

642
def build_classifiers_string(project):
48✔
643
    classifiers = project.get_property("distutils_classifiers", [])
48✔
644
    return build_string_from_array(classifiers, indent=12)
48✔
645

646

647
def build_string_from_array(arr, indent=12):
48✔
648
    result = ""
48✔
649

650
    if len(arr) == 1:
48✔
651
        """
652
        arrays with one item contained on one line
653
        """
654
        if len(arr[0]) > 0:
48✔
655
            if is_notstr_iterable(arr[0]):
48✔
656
                result += "[" + build_string_from_array(arr[0], indent + 4) + "]"
48✔
657
            else:
658
                result += "['%s']" % arr[0]
48✔
659
        else:
660
            result = '[[]]'
48✔
661
    elif len(arr) > 1:
48✔
662
        result = "[\n"
48✔
663

664
        for item in arr:
48✔
665
            if is_notstr_iterable(item):
48✔
666
                result += (" " * indent) + build_string_from_array(item, indent + 4) + ",\n"
48✔
667
            else:
668
                result += (" " * indent) + "'" + item + "',\n"
48✔
669
        result = result[:-2] + "\n"
48✔
670
        result += " " * (indent - 4)
48✔
671
        result += "]"
48✔
672
    else:
673
        result = '[]'
48✔
674

675
    return result
48✔
676

677

678
def build_string_from_dict(d, indent=12):
48✔
679
    element_separator = ",\n"
×
680
    element_separator += " " * indent
×
681
    map_elements = []
×
682

683
    for k, v in d.items():
×
684
        map_elements.append("'%s': '%s'" % (k, v))
×
685

686
    result = ""
×
687

688
    if len(map_elements) > 0:
×
689
        result += "{\n"
×
690
        result += " " * indent
×
691
        result += element_separator.join(map_elements)
×
692
        result += "\n"
×
693
        result += " " * (indent - 4)
×
694
        result += "}"
×
695

696
    return result
×
697

698

699
def doc_convert(project, logger):
48✔
700
    import pypandoc
×
701
    readme_file = project.expand_path("$distutils_readme_file")
×
702
    logger.debug("Converting %s into RST format for PyPi documentation...", readme_file)
×
703
    return pypandoc.convert_file(readme_file, "rst")
×
704

705

706
def _expand_leading_tabs(s, indent=4):
48✔
707
    def replace_tabs(match):
48✔
708
        return " " * (len(match.groups(0)) * indent)
48✔
709

710
    return "".join([LEADING_TAB_RE.sub(replace_tabs, line) for line in s.splitlines(True)])
48✔
711

712

713
def _normalize_setup_post_pre_script(s, indent=8):
48✔
714
    indent_str = " " * indent
48✔
715
    return "".join([indent_str + line if len(str.rstrip(line)) > 0 else line for line in
48✔
716
                    dedent(_expand_leading_tabs(s)).splitlines(True)])
717

718

719
def _prepare_reports_dir(project):
48✔
720
    reports_dir = project.expand_path("$dir_reports", "distutils")
48✔
721
    if not os.path.exists(reports_dir):
48✔
722
        os.mkdir(reports_dir)
48✔
723
    return reports_dir
48✔
724

725

726
def _get_description_content_type(project):
48✔
727
    file_type = project.get_property("distutils_readme_file_type")
48✔
728
    file_encoding = project.get_property("distutils_readme_file_encoding")
48✔
729
    file_variant = project.get_property("distutils_readme_file_variant")
48✔
730

731
    if not file_type:
48!
732
        if project.get_property("distutils_readme_description"):
48✔
733
            readme_file_ci = project.get_property("distutils_readme_file").lower()
24✔
734
            if readme_file_ci.endswith("md"):
24!
735
                file_type = "text/markdown"
24✔
736
            elif readme_file_ci.endswith("rst"):
×
737
                file_type = "text/x-rst"
×
738
            else:
739
                file_type = "text/plain"
×
740

741
    if file_encoding:
48!
742
        file_encoding = file_encoding.upper()
×
743

744
    if file_type == "text/markdown":
48✔
745
        if file_variant:
24!
746
            file_variant = file_variant.upper()
×
747

748
    if file_type:
48✔
749
        return "%s%s%s" % (file_type,
24✔
750
                           "; charset=%s" % file_encoding if file_encoding else "",
751
                           "; variant=%s" % file_variant if file_variant else "")
752

753

754
def _get_generated_artifacts(project, logger):
48✔
755
    dist_artifact_dir = project.expand_path("$dir_dist", "dist")
48✔
756

757
    artifacts = [os.path.join(dist_artifact_dir, artifact) for artifact in list(os.walk(dist_artifact_dir))[0][2]]
48✔
758
    return dist_artifact_dir, artifacts
48✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc