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

pybuilder / pybuilder / 17147403269

22 Aug 2025 05:59AM UTC coverage: 83.684%. First build
17147403269

Pull #932

github

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

2156 of 2674 branches covered (80.63%)

Branch coverage included in aggregate %.

11 of 12 new or added lines in 1 file covered. (91.67%)

5517 of 6495 relevant lines covered (84.94%)

20.87 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
25✔
20
import os
25✔
21
import re
25✔
22
import string
25✔
23
from datetime import datetime
25✔
24
from textwrap import dedent
25✔
25

26
from pybuilder import pip_utils
25✔
27
from pybuilder.core import (after,
25✔
28
                            before,
29
                            use_plugin,
30
                            init,
31
                            task,
32
                            RequirementsFile,
33
                            Dependency)
34
from pybuilder.errors import BuildFailedException, MissingPrerequisiteException
25✔
35
from pybuilder.python_utils import StringIO
25✔
36
from pybuilder.utils import (as_list,
25✔
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")
25✔
44

45
LEADING_TAB_RE = re.compile(r'^(\t*)')
25✔
46
DATA_FILES_PROPERTY = "distutils_data_files"
25✔
47
SETUP_TEMPLATE = string.Template("""#!/usr/bin/env python
25✔
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=""):
25✔
105
    if value is None:
25✔
106
        return default
25✔
107
    return value
25✔
108

109

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

113

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

121
    project.set_property_if_unset("distutils_commands", ["sdist", "wheel"])
25✔
122
    project.set_property_if_unset("distutils_command_options", None)
25✔
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)
25✔
127
    project.set_property_if_unset("distutils_classifiers", [
25✔
128
        "Development Status :: 3 - Alpha",
129
        "Programming Language :: Python"
130
    ])
131
    project.set_property_if_unset("distutils_fail_on_warnings", False)
25✔
132

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

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

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

154

155
@after("prepare")
25✔
156
def set_description(project, logger, reactor):
25✔
157
    if project.get_property("distutils_readme_description"):
25✔
158
        description = None
19✔
159
        if project.get_property("distutils_readme_file_convert"):
19!
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",
19✔
167
                         encoding=project.get_property("distutils_readme_file_encoding")) as f:
168
                description = f.read()
19✔
169

170
        if description:
19!
171
            if (not hasattr(project, "summary") or
19!
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
19!
177
                    project.description is None or
178
                    project.get_property("distutils_description_overwrite")):
179
                setattr(project, "description", description)
19✔
180

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

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

190
    warn = False
25✔
191
    if len(project.summary) >= 512:
25!
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:
25!
196
        logger.warn("Project summary SHOULD NOT contain new-line characters per PEP-426")
×
197
        warn = True
×
198

199
    if len(project.summary) >= 2048:
25!
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"):
25!
203
        raise BuildFailedException("Distutil plugin warnings caused a build failure. Please see warnings above.")
×
204

205

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

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

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

217

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

224
    template_values = {
25✔
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)
25✔
261

262

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

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

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

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

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

280

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

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

291

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

296
    _prepare_reports_dir(project)
25✔
297
    outfile_name = project.expand_path("$dir_reports", "distutils",
25✔
298
                                       "pip_install_%s" % datetime.utcnow().strftime("%Y%m%d%H%M%S"))
299
    pip_utils.pip_install(
25✔
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.")
25✔
313
def upload(project, logger, reactor):
25✔
314
    repository = project.get_property("distutils_upload_repository")
25✔
315
    repository_args = []
25✔
316
    if repository:
25✔
317
        repository_args = ["--repository-url", repository]
25✔
318
    else:
319
        repository_key = project.get_property("distutils_upload_repository_key")
25✔
320
        if repository_key:
25✔
321
            repository_args = ["--repository", repository_key]
25✔
322

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

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

336
    skip_existing = project.get_property("distutils_upload_skip_existing")
25✔
337
    logger.info("Uploading project %s-%s%s%s%s%s", project.name, project.version,
25✔
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
25✔
344
    if skip_existing:
25!
345
        upload_cmd_args.append("--skip-existing")
×
346

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

349

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

354

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

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

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

365
    return manifest_content.getvalue()
25✔
366

367

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

379

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

384
    for command in distutils_commands:
25✔
385
        if is_string(command):
25✔
386
            out_file = os.path.join(reports_dir, safe_log_file_name(command))
25✔
387
        else:
388
            out_file = os.path.join(reports_dir, safe_log_file_name("__".join(command)))
25✔
389
        with open(out_file, "w") as out_f:
25✔
390
            commands = python_env.executable + ["-c",
25✔
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"):
25✔
395
                commands.append("-v")
19✔
396
            if is_string(command):
25✔
397
                commands.extend(command.split())
25✔
398
            else:
399
                commands.extend(command)
25✔
400
            commands.append(setup_script_dir)
25✔
401
            logger.debug("Executing distutils command: %s", commands)
25✔
402
            return_code = python_env.run_process_and_wait(commands, project.expand_path("$dir_dist"), out_f)
25✔
403
            if return_code != 0:
25!
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):
25✔
410
    reports_dir = _prepare_reports_dir(project)
25✔
411
    dist_artifact_dir, artifacts = _get_generated_artifacts(project, logger)
25✔
412

413
    if command == "register":
25✔
414
        for artifact in artifacts:
25✔
415
            out_file = os.path.join(reports_dir,
25✔
416
                                    safe_log_file_name("twine_%s_%s.log" % (command, os.path.basename(artifact))))
417
            _execute_twine(project, logger, python_env,
25✔
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))
25✔
421
        _execute_twine(project, logger, python_env,
25✔
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):
25✔
426
    with open(out_file, "w") as out_f:
25✔
427
        commands = python_env.executable + ["-m", "twine"] + command
25✔
428
        logger.debug("Executing Twine: %s", commands)
25✔
429
        return_code = python_env.run_process_and_wait(commands, work_dir, out_f)
25✔
430
        if return_code != 0:
25!
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):
25✔
436
    return [requirement for requirement in requirements
25✔
437
            if not requirement.strip().startswith("#")]
438

439

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

443

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

447

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

454

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

458

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

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

475
    dependencies.extend(flattened_requirements_without_editables)
25✔
476

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

481
    return build_string_from_array(dependencies)
25✔
482

483

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

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

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

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

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

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

511
    return build_string_from_array(all_dependency_links)
25✔
512

513

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

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

521
    return build_string_from_array(scripts)
25✔
522

523

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

535
    result = "[\n"
25✔
536

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

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

546

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

552
    indent = 8
25✔
553

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

556
    result = "{\n"
25✔
557

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

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

568
    return result
25✔
569

570

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

575
    indent = 8
25✔
576

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

579
    result = "{\n"
25✔
580

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

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

588
    return result
25✔
589

590

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

594

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

598

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

602

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

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

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

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

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

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

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

628
    return result
25✔
629

630

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

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

639
    return repr(setup_keywords)
25✔
640

641

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

646

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

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

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

675
    return result
25✔
676

677

678
def build_string_from_dict(d, indent=12):
25✔
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):
25✔
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):
25✔
707
    def replace_tabs(match):
25✔
708
        return " " * (len(match.groups(0)) * indent)
25✔
709

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

712

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

718

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

725

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

731
    if not file_type:
25!
732
        if project.get_property("distutils_readme_description"):
25✔
733
            readme_file_ci = project.get_property("distutils_readme_file").lower()
19✔
734
            if readme_file_ci.endswith("md"):
19!
735
                file_type = "text/markdown"
19✔
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:
25!
742
        file_encoding = file_encoding.upper()
×
743

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

748
    if file_type:
25✔
749
        return "%s%s%s" % (file_type,
19✔
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):
25✔
755
    dist_artifact_dir = project.expand_path("$dir_dist", "dist")
25✔
756

757
    artifacts = [os.path.join(dist_artifact_dir, artifact) for artifact in list(os.walk(dist_artifact_dir))[0][2]]
25✔
758
    return dist_artifact_dir, artifacts
25✔
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