From c3ec362fbf677974aac51baac7046d668778b936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Wed, 10 Jan 2024 20:10:46 +0100 Subject: [PATCH] Sort requirements by canonical distribution name --- news/134.bugfix | 1 + src/pip_deepfreeze/sync.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 news/134.bugfix diff --git a/news/134.bugfix b/news/134.bugfix new file mode 100644 index 0000000..5c9d04e --- /dev/null +++ b/news/134.bugfix @@ -0,0 +1 @@ +Sort requirement files by canonical requirement name to help ensure stability and comparability. diff --git a/src/pip_deepfreeze/sync.py b/src/pip_deepfreeze/sync.py index 0f8b991..b87a93d 100644 --- a/src/pip_deepfreeze/sync.py +++ b/src/pip_deepfreeze/sync.py @@ -13,7 +13,7 @@ from .project_name import get_project_name from .req_file_parser import OptionsLine, parse as parse_req_file from .req_merge import prepare_frozen_reqs_for_upgrade -from .req_parser import get_req_names +from .req_parser import get_req_name, get_req_names from .utils import ( HttpFetcher, get_temp_path_in_dir, @@ -28,6 +28,13 @@ ) +def _req_line_sort_key(req_line: str) -> str: + req_name = get_req_name(req_line) + if req_name is None: + return req_line + return req_name + + def sync( python: str, upgrade_all: bool, @@ -78,8 +85,9 @@ def sync( ): if isinstance(parsed_req_line, OptionsLine): print(parsed_req_line.raw_line, file=f) - # output frozen dependencies of project - for req_line in frozen_reqs: + # output frozen dependencies of project, + # sorted by canonical requirement name + for req_line in sorted(frozen_reqs, key=_req_line_sort_key): print(normalize_req_line(req_line), file=f) # uninstall unneeded dependencies, if asked to do so unneeded_req_names = sorted(