Skip to content

Commit b1acadc

Browse files
abitrollyhenryiii
andauthored
main: disable colorama on Linux and flush output (#494)
* Disable `colorama` on Linux (fixes #493) [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * `colorama` is a required dependency on Windows * Prevent output buffering * Fix `cprint()` * Make `cprint()` private * Get back `ModuleNotFoundError` * `cprint`: don't try to format message `{` are present in tracebacks leading to errors * Add type annotation Fix type annotaion errors * tests: remove colorama on path test * tests: move path test up to normal tests Signed-off-by: Henry Schreiner <[email protected]> * tox: include coverage on path job Signed-off-by: Henry Schreiner <[email protected]> * ci: combine coverage from the run Signed-off-by: Henry Schreiner <[email protected]> Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]>
1 parent a1de450 commit b1acadc

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ jobs:
5151

5252
steps:
5353
- uses: actions/checkout@v3
54+
with:
55+
fetch-depth: 0
5456

5557
- name: Setup python for test ${{ matrix.py }}
5658
uses: actions/setup-python@v4
@@ -91,10 +93,13 @@ jobs:
9193
if: matrix.tox-target == 'min'
9294
run: tox -e ${{env.BASE}}-${{ matrix.tox-target }}
9395

94-
- name: Rename coverage report file
95-
if: matrix.tox-target == 'tox'
96-
run: mv ".tox/coverage.${BASE}.xml" .tox/coverage.xml
97-
shell: bash
96+
- name: Run path test
97+
if: matrix.tox-target == 'tox' && matrix.py == '3.10'
98+
run: tox -e path
99+
100+
- name: Combine coverage files
101+
if: always()
102+
run: tox -e coverage
98103

99104
- uses: codecov/codecov-action@v3
100105
if: always()
@@ -106,10 +111,6 @@ jobs:
106111
env_vars: PYTHON
107112
name: ${{ matrix.py }} - ${{ matrix.os }}
108113

109-
- name: Run path test
110-
if: matrix.tox-target == 'tox' && matrix.py == '3.10'
111-
run: tox -e path
112-
113114
type:
114115
runs-on: ubuntu-latest
115116
env:

src/build/__main__.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import argparse
55
import contextlib
66
import os
7+
import platform
78
import shutil
89
import subprocess
910
import sys
@@ -46,6 +47,10 @@ def _init_colors() -> Dict[str, str]:
4647
_STYLES = _init_colors()
4748

4849

50+
def _cprint(fmt: str = '', msg: str = '') -> None:
51+
print(fmt.format(msg, **_STYLES), flush=True)
52+
53+
4954
def _showwarning(
5055
message: Union[Warning, str],
5156
category: Type[Warning],
@@ -54,18 +59,19 @@ def _showwarning(
5459
file: Optional[TextIO] = None,
5560
line: Optional[str] = None,
5661
) -> None: # pragma: no cover
57-
print('{yellow}WARNING{reset} {}'.format(message, **_STYLES))
62+
_cprint('{yellow}WARNING{reset} {}', str(message))
5863

5964

6065
def _setup_cli() -> None:
6166
warnings.showwarning = _showwarning
6267

63-
try:
64-
import colorama
65-
except ModuleNotFoundError:
66-
pass
67-
else:
68-
colorama.init() # fix colors on windows
68+
if platform.system() == 'Windows':
69+
try:
70+
import colorama
71+
72+
colorama.init()
73+
except ModuleNotFoundError:
74+
pass
6975

7076

7177
def _error(msg: str, code: int = 1) -> NoReturn: # pragma: no cover
@@ -75,20 +81,20 @@ def _error(msg: str, code: int = 1) -> NoReturn: # pragma: no cover
7581
:param msg: Error message
7682
:param code: Error code
7783
"""
78-
print('{red}ERROR{reset} {}'.format(msg, **_STYLES))
84+
_cprint('{red}ERROR{reset} {}', msg)
7985
raise SystemExit(code)
8086

8187

8288
class _ProjectBuilder(ProjectBuilder):
8389
@staticmethod
8490
def log(message: str) -> None:
85-
print('{bold}* {}{reset}'.format(message, **_STYLES))
91+
_cprint('{bold}* {}{reset}', message)
8692

8793

8894
class _IsolatedEnvBuilder(IsolatedEnvBuilder):
8995
@staticmethod
9096
def log(message: str) -> None:
91-
print('{bold}* {}{reset}'.format(message, **_STYLES))
97+
_cprint('{bold}* {}{reset}', message)
9298

9399

94100
def _format_dep_chain(dep_chain: Sequence[str]) -> str:
@@ -119,7 +125,7 @@ def _build_in_current_env(
119125
missing = builder.check_dependencies(distribution)
120126
if missing:
121127
dependencies = ''.join('\n\t' + dep for deps in missing for dep in (deps[0], _format_dep_chain(deps[1:])) if dep)
122-
print()
128+
_cprint()
123129
_error(f'Missing dependencies:{dependencies}')
124130

125131
return builder.build(distribution, outdir, config_settings or {})
@@ -147,7 +153,7 @@ def _handle_build_error() -> Iterator[None]:
147153
_error(str(e))
148154
except BuildBackendException as e:
149155
if isinstance(e.exception, subprocess.CalledProcessError):
150-
print()
156+
_cprint()
151157
_error(str(e))
152158

153159
if e.exc_info:
@@ -160,7 +166,7 @@ def _handle_build_error() -> Iterator[None]:
160166
tb = ''.join(tb_lines)
161167
else:
162168
tb = traceback.format_exc(-1)
163-
print('\n{dim}{}{reset}\n'.format(tb.strip('\n'), **_STYLES))
169+
_cprint('\n{dim}{}{reset}\n', tb.strip('\n'))
164170
_error(str(e))
165171

166172

@@ -370,10 +376,10 @@ def main(cli_args: Sequence[str], prog: Optional[str] = None) -> None: # noqa:
370376
artifact_list = _natural_language_list(
371377
['{underline}{}{reset}{bold}{green}'.format(artifact, **_STYLES) for artifact in built]
372378
)
373-
print('{bold}{green}Successfully built {}{reset}'.format(artifact_list, **_STYLES))
379+
_cprint('{bold}{green}Successfully built {}{reset}', artifact_list)
374380
except Exception as e: # pragma: no cover
375381
tb = traceback.format_exc().strip('\n')
376-
print('\n{dim}{}{reset}\n'.format(tb, **_STYLES))
382+
_cprint('\n{dim}{}{reset}\n', tb)
377383
_error(str(e))
378384

379385

tox.ini

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ commands =
4747
description = verify build can run from source (bootstrap)
4848
setenv =
4949
PYTHONPATH = {toxinidir}/src
50-
commands =
51-
python -E -m pip uninstall -y build
52-
pytest -ra {posargs:-n auto}
50+
COVERAGE_FILE = {toxworkdir}/.coverage.{envname}
51+
commands_pre =
52+
python -E -m pip uninstall -y build colorama
5353

5454
[testenv:type]
5555
description = run type check on code base
@@ -60,9 +60,8 @@ commands =
6060
[testenv:{py311, py310, py39, py38, py37, py36, pypy37, pypy38, pypy39}-min]
6161
description = check minimum versions required of all dependencies
6262
skip_install = true
63-
commands =
63+
commands_pre =
6464
pip install .[test] -c tests/constraints.txt
65-
pytest -ra {posargs:-n auto}
6665

6766
[testenv:docs]
6867
description = build documentations
@@ -90,19 +89,18 @@ description = combine coverage from test environments
9089
passenv =
9190
DIFF_AGAINST
9291
setenv =
93-
COVERAGE_FILE = {toxworkdir}/.coverage
9492
skip_install = true
9593
deps =
96-
coverage>=5.1
94+
coverage[toml]>=5.1
9795
diff_cover>=3
9896
parallel_show_output = true
9997
commands =
100-
coverage combine
98+
coverage combine {toxworkdir}
10199
coverage report --skip-covered --show-missing -i
102100
coverage xml -o {toxworkdir}/coverage.xml -i
103101
coverage html -d {toxworkdir}/htmlcov -i
104102
python -m diff_cover.diff_cover_tool --compare-branch {env:DIFF_AGAINST:origin/main} {toxworkdir}/coverage.xml
105-
depends = {py311, py310, py39, py38, py37, py36, pypy37, pypy38, pypy39}
103+
depends = {py311, py310, py39, py38, py37, py36, pypy37, pypy38, pypy39}{,-min}, path
106104

107105
[flake8]
108106
max-line-length = 127

0 commit comments

Comments
 (0)