Skip to content

Commit a998176

Browse files
authored
chore: add colour to the advisory generation output. (#109)
I find the output from the Advisory generation to be an unreadable wall of text. This adds a scattering of colour to the notices for SAs that have had an action applied to them. This is manage through the addition of [Colorama](https://pypi.org/project/colorama/). I selected this one as it manages the colours and styles while taking into account the platform it's run on, is actively maintained, and has been around for more than a decade. <img width="1125" height="1072" alt="image" src="https://github.com/user-attachments/assets/90966c1a-3884-448a-b302-f831648b2e3c" /> I note that the `poetry.lock` has an unexpected removal. Could this be because it is included via the new Colourama?
1 parent 746f4e5 commit a998176

5 files changed

Lines changed: 47 additions & 8 deletions

File tree

poetry.lock

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name = "drupal-advisory-database"
33
requires-python = ">= 3.13"
44

55
[tool.poetry.dependencies]
6+
colorama = "^0.4.6"
67
jsonschema = "^4.23.0"
78
markdownify = "^1.1.0"
89
mypy = "^1.15.0"
@@ -12,6 +13,7 @@ ruff = "~0.14.0"
1213
semver = "^3.0.4"
1314
types-jsonschema = "^4.23.0.20241208"
1415
types-requests = "^2.32.0.20250328"
16+
types-colorama = "^0.4.15.20240311"
1517

1618
[tool.poetry]
1719
package-mode = false

scripts/generate_osv_advisories.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import requests
1818
import semver
19+
from helpers import text_is
1920
from markdownify import markdownify
2021

2122
from typings import drupal, osv
@@ -350,10 +351,13 @@ def patch_advisory(sa_id: str, sa_advisory: drupal.Advisory) -> bool:
350351

351352
if before == sa_advisory['field_affected_versions']:
352353
sa_advisory['field_affected_versions'] = after
353-
print(' \\- patched affected versions')
354+
print(' \\- ' + text_is.success('patched affected versions'))
354355
return True
355356
print(
356-
f' \\- skipped patching as affected version is now "{sa_advisory["field_affected_versions"]}"'
357+
' \\- '
358+
+ text_is.warning(
359+
f'skipped patching as affected version is now "{sa_advisory["field_affected_versions"]}"'
360+
)
357361
)
358362
return False
359363

@@ -375,14 +379,14 @@ def build_osv_advisory(
375379
# we expect that the downloader has excluded PSA type entries, but
376380
# we still guard against them here just in case one slips through
377381
if sa_advisory['field_is_psa'] == '1':
378-
print(' \\- skipping as it is a psa? (this should not happen)')
382+
print(' \\-' + text_is.error('skipping as it is a psa? (this should not happen)'))
379383
return None
380384

381385
# there's not really much we can do if there isn't an affected version
382386
# todo: since build_affected_ranges throws if this isn't present, it might
383387
# make more sense to use that, with a custom exception class
384388
if sa_advisory['field_affected_versions'] is None:
385-
print(' \\- skipping as we do not have any affected versions')
389+
print(' \\- ' + text_is.notice('skipping as we do not have any affected versions'))
386390
return None
387391

388392
osv_advisory: osv.Vulnerability = {
@@ -461,7 +465,10 @@ def generate_osv_advisories() -> None:
461465
os.makedirs(f'advisories/{name}', exist_ok=True)
462466
if is_existing_advisory_ahead(name, sa_id, osv_advisory['modified']):
463467
print(
464-
' \\- error: current modified date is ahead of the proposed modified date (is your cache up to date?)'
468+
' \\- '
469+
+ text_is.error(
470+
'error: current modified date is ahead of the proposed modified date (is your cache up to date?)'
471+
)
465472
)
466473
exit(1)
467474

scripts/helpers/__init__.py

Whitespace-only changes.

scripts/helpers/text_is.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from colorama import Fore, Style, just_fix_windows_console
2+
3+
just_fix_windows_console()
4+
5+
6+
def notice(message: str) -> str:
7+
return f'{Fore.YELLOW}{Style.DIM}{message}{Style.RESET_ALL}'
8+
9+
10+
def success(message: str) -> str:
11+
return f'{Fore.GREEN}{message}{Style.RESET_ALL}'
12+
13+
14+
def warning(message: str) -> str:
15+
return f'{Fore.YELLOW}{message}{Style.RESET_ALL}'
16+
17+
18+
def error(message: str) -> str:
19+
return f'{Fore.RED}{message}{Style.RESET_ALL}'

0 commit comments

Comments
 (0)