From 92b75d2e2495e2afcbc0bf151047901e89a63b81 Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Tue, 8 Mar 2022 17:32:15 -0500 Subject: [PATCH] Normalize python package names Co-authored-by: Matt Martz --- ansible_builder/requirements.py | 3 +++ test/data/ansible_collections/other/reqfile/requirements.txt | 1 + test/data/ansible_collections/test/reqfile/requirements.txt | 1 + test/integration/test_introspect_cli.py | 4 +++- test/unit/test_introspect.py | 5 ++++- 5 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 test/data/ansible_collections/other/reqfile/requirements.txt diff --git a/ansible_builder/requirements.py b/ansible_builder/requirements.py index 2c853176..5b99a764 100644 --- a/ansible_builder/requirements.py +++ b/ansible_builder/requirements.py @@ -1,5 +1,6 @@ import logging import requirements +from pkg_resources import safe_name logger = logging.getLogger(__name__) @@ -37,6 +38,8 @@ def sanitize_requirements(collection_py_reqs): for collection, lines in collection_py_reqs.items(): try: for req in requirements.parse('\n'.join(lines)): + if req.specifier: + req.name = safe_name(req.name) req.collections = [collection] # add backref for later if req.name is None: consolidated.append(req) diff --git a/test/data/ansible_collections/other/reqfile/requirements.txt b/test/data/ansible_collections/other/reqfile/requirements.txt new file mode 100644 index 00000000..3f3d8ac7 --- /dev/null +++ b/test/data/ansible_collections/other/reqfile/requirements.txt @@ -0,0 +1 @@ +python_dateutil # intentional underscore diff --git a/test/data/ansible_collections/test/reqfile/requirements.txt b/test/data/ansible_collections/test/reqfile/requirements.txt index 694334b8..81590b68 100644 --- a/test/data/ansible_collections/test/reqfile/requirements.txt +++ b/test/data/ansible_collections/test/reqfile/requirements.txt @@ -1,2 +1,3 @@ pytz +python-dateutil>=2.8.2 # intentional dash -r extra_req.txt diff --git a/test/integration/test_introspect_cli.py b/test/integration/test_introspect_cli.py index 7852acda..f21f73bf 100644 --- a/test/integration/test_introspect_cli.py +++ b/test/integration/test_introspect_cli.py @@ -35,6 +35,7 @@ def test_introspect_write_python(cli, data_dir, tmp_path): assert dest_file.read_text() == '\n'.join([ 'pyvcloud>=14 # from collection test.metadata', 'pytz # from collection test.reqfile', + 'python-dateutil>=2.8.2 # from collection test.reqfile', 'tacacs_plus # from collection test.reqfile', 'pyvcloud>=18.0.10 # from collection test.reqfile', '', @@ -48,7 +49,8 @@ def test_introspect_write_python_and_sanitize(cli, data_dir, tmp_path): assert dest_file.read_text() == '\n'.join([ 'pyvcloud>=14,>=18.0.10 # from collection test.metadata,test.reqfile', 'pytz # from collection test.reqfile', - 'tacacs_plus # from collection test.reqfile', + 'python-dateutil>=2.8.2 # from collection test.reqfile', + 'tacacs-plus # from collection test.reqfile', '', ]) diff --git a/test/unit/test_introspect.py b/test/unit/test_introspect.py index 027d1419..d950a041 100644 --- a/test/unit/test_introspect.py +++ b/test/unit/test_introspect.py @@ -13,7 +13,10 @@ def test_multiple_collection_metadata(data_dir): assert files == {'python': [ 'pyvcloud>=14,>=18.0.10 # from collection test.metadata,test.reqfile', 'pytz # from collection test.reqfile', - 'tacacs_plus # from collection test.reqfile' + # python-dateutil should appear only once even though referenced in + # multiple places, once with a dash and another with an underscore in the name. + 'python-dateutil>=2.8.2 # from collection test.reqfile', + 'tacacs-plus # from collection test.reqfile' ], 'system': [ 'subversion [platform:rpm] # from collection test.bindep', 'subversion [platform:dpkg] # from collection test.bindep'