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

pybuilder / pybuilder / 16613314016

30 Jul 2025 04:14AM UTC coverage: 84.008% (-0.1%) from 84.146%
16613314016

push

github

arcivanov
Release 0.13.16

2167 of 2671 branches covered (81.13%)

Branch coverage included in aggregate %.

5534 of 6496 relevant lines covered (85.19%)

36.25 hits per line

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

17.92
/src/main/python/pybuilder/plugins/python/coveralls_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 logging
22✔
20
import random
22✔
21
from os.path import normcase as nc
22✔
22
from time import sleep
22✔
23

24
from pybuilder.core import init, use_plugin, finalize
22✔
25
from pybuilder.errors import BuildFailedException
22✔
26
from pybuilder.execution import ExecutionManager
22✔
27
from pybuilder.plugins.python._coverage_util import patch_coverage
22✔
28

29
use_plugin("python.core")
22✔
30
use_plugin("analysis")
22✔
31
use_plugin("python.coverage")
22✔
32

33

34
@init(environments="ci")
22✔
35
def init_coveralls_properties(project):
22✔
36
    project.plugin_depends_on("coveralls", "~=3.0")
×
37

38
    project.set_property_if_unset("coveralls_dry_run", False)
×
39
    project.set_property_if_unset("coveralls_report", False)
×
40
    project.set_property_if_unset("coveralls_token_required", True)
×
41
    project.set_property_if_unset("coveralls_retry_delay_min", 3)
×
42
    project.set_property_if_unset("coveralls_retry_delay_max", 10)
×
43

44

45
@finalize(environments="ci")
22✔
46
def finalize_coveralls(project, logger, reactor):
22✔
47
    em = reactor.execution_manager  # type: ExecutionManager
×
48

49
    if not em.is_task_in_current_execution_plan("coverage"):
×
50
        return
×
51

52
    patch_coverage()
×
53

54
    from coveralls.api import Coveralls, CoverallReporter, CoverallsException
×
55
    from coverage import coverage, files
×
56

57
    class PybCoveralls(Coveralls):
×
58
        def get_coverage(self):
×
59
            coverage_config = project.get_property("__coverage_config")
×
60

61
            workman = coverage(**coverage_config)
×
62
            workman.load()
×
63

64
            if hasattr(workman, '_harvest_data'):
×
65
                workman._harvest_data()  # pylint: disable=W0212
×
66
            else:
67
                workman.get_data()
×
68

69
            return CoverallReporter(workman, workman.config).coverage
×
70

71
    coveralls_logger = logging.getLogger("coveralls")
×
72
    coveralls_logger.addHandler(logger)
×
73

74
    try:
×
75
        dry_run = project.get_property("coveralls_dry_run")
×
76
        report = project.get_property("coveralls_report")
×
77
        token_required = project.get_property("coveralls_token_required") and not dry_run and not report
×
78

79
        old_relative_dir = files.RELATIVE_DIR
×
80
        files.RELATIVE_DIR = nc(project.expand_path(project.get_property("coverage_source_path")))
×
81
        try:
×
82
            pyb_coveralls = PybCoveralls(token_required=token_required)
×
83
            try:
×
84
                staging = False
×
85
                if report:
×
86
                    report_file = project.expand_path("$dir_reports", "%s.coveralls.json" % project.name)
×
87
                    pyb_coveralls.save_report(report_file)
×
88
                    logger.info("Written Coveralls report into %r", report_file)
×
89
                    staging = True
×
90

91
                if dry_run:
×
92
                    pyb_coveralls.wear(dry_run=True)
×
93
                    logger.info("Coveralls dry-run coverage test has been completed!")
×
94
                    staging = True
×
95

96
                if staging:
×
97
                    return
×
98

99
                while True:
100
                    try:
×
101
                        report_result = pyb_coveralls.wear()
×
102
                        break
×
103
                    except CoverallsException as e:
×
104
                        if (hasattr(e.__cause__, "response") and
×
105
                                hasattr(e.__cause__.response, "status_code")):
106
                            status_code = e.__cause__.response.status_code
×
107
                            if status_code in (408, 429, 502, 503, 504):
×
108
                                # Retry after a random delay
109
                                logger.warn("Coveralls failed while uploading results, will retry: %s", e.__cause__)
×
110
                                sleep(random.randint(int(project.get_property("coveralls_retry_delay_min")),
×
111
                                                     int(project.get_property("coveralls_retry_delay_max"))))
112
                            elif status_code == 422 and pyb_coveralls.config["service_name"] == "github-actions":
×
113
                                # https://github.com/TheKevJames/coveralls-python/issues/252
114
                                pyb_coveralls = PybCoveralls(token_required=token_required, service_name="github")
×
115
                            else:
116
                                raise
×
117
                        else:
118
                            raise
×
119

120
                logger.debug("Coveralls result: %r", report_result)
×
121
                logger.info("Coveralls coverage successfully submitted! %s @ %s",
×
122
                            report_result["message"],
123
                            report_result["url"])
124
            except CoverallsException as e:
×
125
                raise BuildFailedException("Failed to upload Coveralls coverage: %s", e)
×
126
        finally:
127
            files.RELATIVE_DIR = old_relative_dir
×
128
    finally:
129
        coveralls_logger.removeHandler(logger)
×
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

© 2025 Coveralls, Inc