Skip to content

Commit 39588f1

Browse files
authored
Merge pull request #117 from bennati/pr-116
Fail gracefully at parsing setup.py with no deps.
2 parents 9e765ec + 093275f commit 39588f1

14 files changed

+429
-289
lines changed

src/python_inspector/resolution.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

10+
import ast
1011
import operator
1112
import os
13+
import re
1214
import tarfile
1315
from typing import Dict
1416
from typing import Generator
@@ -281,7 +283,7 @@ def get_requirements_from_python_manifest(
281283
"""
282284
Return a list of parsed requirements from the ``sdist_location`` sdist location
283285
"""
284-
# Look in requirements file if and only if thy are refered in setup.py or setup.cfg
286+
# Look in requirements file if and only if they are refered in setup.py or setup.cfg
285287
# And no deps have been yielded by requirements file.
286288
requirements = list(
287289
get_reqs_from_requirements_file_in_sdist(
@@ -299,11 +301,48 @@ def get_requirements_from_python_manifest(
299301
)
300302

301303
else:
302-
# We should not raise exception here as we may have a setup.py that does not
303-
# have any dependencies. We should not fail in this case.
304-
raise Exception(
305-
f"Unable to collect setup.py dependencies securely: {setup_py_location}"
306-
)
304+
# Do not raise exception here as we may have a setup.py that does not
305+
# have any dependencies.
306+
with (open(setup_py_location)) as sf:
307+
file_contents = sf.read()
308+
node = ast.parse(file_contents)
309+
setup_fct = [
310+
elem
311+
for elem in ast.walk(node)
312+
if (
313+
isinstance(elem, ast.Expr)
314+
and isinstance(elem.value, ast.Call)
315+
and isinstance(elem.value.func, ast.Name)
316+
and elem.value.func.id == "setup"
317+
)
318+
]
319+
if len(setup_fct) == 0:
320+
raise Exception(
321+
f"Unable to collect setup.py dependencies securely: {setup_py_location}"
322+
)
323+
if len(setup_fct) > 1:
324+
print(
325+
f"Warning: identified multiple definitions of 'setup()' in {setup_py_location}, "
326+
"defaulting to the first occurrence"
327+
)
328+
setup_fct = setup_fct[0]
329+
install_requires = [
330+
k.value for k in setup_fct.value.keywords if k.arg == "install_requires"
331+
]
332+
if len(install_requires) == 0:
333+
raise Exception(
334+
f"Unable to collect setup.py dependencies securely: {setup_py_location}"
335+
)
336+
if len(install_requires) > 1:
337+
print(
338+
f"Warning: identified multiple definitions of 'install_requires' in "
339+
"{setup_py_location}, defaulting to the first occurrence"
340+
)
341+
install_requires = install_requires[0].elts
342+
if len(install_requires) != 0:
343+
raise Exception(
344+
f"Unable to collect setup.py dependencies securely: {setup_py_location}"
345+
)
307346

308347

309348
DEFAULT_ENVIRONMENT = utils_pypi.Environment.from_pyver_and_os(

tests/data/azure-devops.req-310-expected.json

Lines changed: 40 additions & 38 deletions
Large diffs are not rendered by default.

tests/data/azure-devops.req-38-expected.json

Lines changed: 40 additions & 38 deletions
Large diffs are not rendered by default.

tests/data/pinned-pdt-requirements.txt-expected.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,12 +2831,12 @@
28312831
"type": "pypi",
28322832
"namespace": null,
28332833
"name": "openpyxl",
2834-
"version": "3.1.0",
2834+
"version": "3.1.1",
28352835
"qualifiers": {},
28362836
"subpath": null,
28372837
"primary_language": "Python",
28382838
"description": "A Python library to read/write Excel 2010 xlsx/xlsm files\n.. image:: https://coveralls.io/repos/bitbucket/openpyxl/openpyxl/badge.svg?branch=default\n :target: https://coveralls.io/bitbucket/openpyxl/openpyxl?branch=default\n :alt: coverage status\n\nIntroduction\n------------\n\nopenpyxl is a Python library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.\n\nIt was born from lack of existing library to read/write natively from Python\nthe Office Open XML format.\n\nAll kudos to the PHPExcel team as openpyxl was initially based on PHPExcel.\n\n\nSecurity\n--------\n\nBy default openpyxl does not guard against quadratic blowup or billion laughs\nxml attacks. To guard against these attacks install defusedxml.\n\nMailing List\n------------\n\nThe user list can be found on http://groups.google.com/group/openpyxl-users\n\n\nSample code::\n\n from openpyxl import Workbook\n wb = Workbook()\n\n # grab the active worksheet\n ws = wb.active\n\n # Data can be assigned directly to cells\n ws['A1'] = 42\n\n # Rows can also be appended\n ws.append([1, 2, 3])\n\n # Python types will automatically be converted\n import datetime\n ws['A2'] = datetime.datetime.now()\n\n # Save the file\n wb.save(\"sample.xlsx\")\n\n\nDocumentation\n-------------\n\nThe documentation is at: https://openpyxl.readthedocs.io\n\n* installation methods\n* code examples\n* instructions for contributing\n\nRelease notes: https://openpyxl.readthedocs.io/en/stable/changes.html",
2839-
"release_date": "2023-01-31T14:40:28",
2839+
"release_date": "2023-02-13T16:51:26",
28402840
"parties": [
28412841
{
28422842
"type": "person",
@@ -2860,11 +2860,11 @@
28602860
"Programming Language :: Python :: 3.9"
28612861
],
28622862
"homepage_url": "https://openpyxl.readthedocs.io",
2863-
"download_url": "https://files.pythonhosted.org/packages/0d/89/f78a9a895e221ec8b13ae7f9495f340a0fb43563b13e2891b5df134f20ea/openpyxl-3.1.0-py2.py3-none-any.whl",
2864-
"size": 250043,
2863+
"download_url": "https://files.pythonhosted.org/packages/9e/57/1d3c2ce7f6f783be9b21569fc468a9f3660e35cc17017abfbbc26d3bd061/openpyxl-3.1.1-py2.py3-none-any.whl",
2864+
"size": 249839,
28652865
"sha1": null,
2866-
"md5": "66351b61736b19d3c88cd108908447d1",
2867-
"sha256": "24d7d361025d186ba91eff58135d50855cf035a84371b891e58fb6eb5125660f",
2866+
"md5": "864e1e1ea061fe056ade64f4e7bbaf22",
2867+
"sha256": "a0266e033e65f33ee697254b66116a5793c15fc92daf64711080000df4cfe0a8",
28682868
"sha512": null,
28692869
"bug_tracking_url": "https://foss.heptapod.net/openpyxl/openpyxl/-/issues",
28702870
"code_view_url": "https://foss.heptapod.net/openpyxl/openpyxl",
@@ -2884,20 +2884,20 @@
28842884
"dependencies": [],
28852885
"repository_homepage_url": null,
28862886
"repository_download_url": null,
2887-
"api_data_url": "https://pypi.org/pypi/openpyxl/3.1.0/json",
2887+
"api_data_url": "https://pypi.org/pypi/openpyxl/3.1.1/json",
28882888
"datasource_id": null,
2889-
"purl": "pkg:pypi/[email protected].0"
2889+
"purl": "pkg:pypi/[email protected].1"
28902890
},
28912891
{
28922892
"type": "pypi",
28932893
"namespace": null,
28942894
"name": "openpyxl",
2895-
"version": "3.1.0",
2895+
"version": "3.1.1",
28962896
"qualifiers": {},
28972897
"subpath": null,
28982898
"primary_language": "Python",
28992899
"description": "A Python library to read/write Excel 2010 xlsx/xlsm files\n.. image:: https://coveralls.io/repos/bitbucket/openpyxl/openpyxl/badge.svg?branch=default\n :target: https://coveralls.io/bitbucket/openpyxl/openpyxl?branch=default\n :alt: coverage status\n\nIntroduction\n------------\n\nopenpyxl is a Python library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.\n\nIt was born from lack of existing library to read/write natively from Python\nthe Office Open XML format.\n\nAll kudos to the PHPExcel team as openpyxl was initially based on PHPExcel.\n\n\nSecurity\n--------\n\nBy default openpyxl does not guard against quadratic blowup or billion laughs\nxml attacks. To guard against these attacks install defusedxml.\n\nMailing List\n------------\n\nThe user list can be found on http://groups.google.com/group/openpyxl-users\n\n\nSample code::\n\n from openpyxl import Workbook\n wb = Workbook()\n\n # grab the active worksheet\n ws = wb.active\n\n # Data can be assigned directly to cells\n ws['A1'] = 42\n\n # Rows can also be appended\n ws.append([1, 2, 3])\n\n # Python types will automatically be converted\n import datetime\n ws['A2'] = datetime.datetime.now()\n\n # Save the file\n wb.save(\"sample.xlsx\")\n\n\nDocumentation\n-------------\n\nThe documentation is at: https://openpyxl.readthedocs.io\n\n* installation methods\n* code examples\n* instructions for contributing\n\nRelease notes: https://openpyxl.readthedocs.io/en/stable/changes.html",
2900-
"release_date": "2023-01-31T14:40:31",
2900+
"release_date": "2023-02-13T16:51:28",
29012901
"parties": [
29022902
{
29032903
"type": "person",
@@ -2921,11 +2921,11 @@
29212921
"Programming Language :: Python :: 3.9"
29222922
],
29232923
"homepage_url": "https://openpyxl.readthedocs.io",
2924-
"download_url": "https://files.pythonhosted.org/packages/3d/73/bb87810cdde809f69fef11d31e77297894e58710d47626dc5e5b3ff8f92a/openpyxl-3.1.0.tar.gz",
2925-
"size": 186306,
2924+
"download_url": "https://files.pythonhosted.org/packages/10/bf/950ea7896f3c42ab04073cd2903f0a190ba77ef28bdf76191f6f86373712/openpyxl-3.1.1.tar.gz",
2925+
"size": 185802,
29262926
"sha1": null,
2927-
"md5": "b7ba597b801b9a102f27599b2fa227b3",
2928-
"sha256": "eccedbe1cdd8b2494057e73959b496821141038dbb7eb9266ea59e3f34208231",
2927+
"md5": "0b1a5d776707ef471810f61c7bf77a2d",
2928+
"sha256": "f06d44e2c973781068bce5ecf860a09bcdb1c7f5ce1facd5e9aa82c92c93ae72",
29292929
"sha512": null,
29302930
"bug_tracking_url": "https://foss.heptapod.net/openpyxl/openpyxl/-/issues",
29312931
"code_view_url": "https://foss.heptapod.net/openpyxl/openpyxl",
@@ -2945,9 +2945,9 @@
29452945
"dependencies": [],
29462946
"repository_homepage_url": null,
29472947
"repository_download_url": null,
2948-
"api_data_url": "https://pypi.org/pypi/openpyxl/3.1.0/json",
2948+
"api_data_url": "https://pypi.org/pypi/openpyxl/3.1.1/json",
29492949
"datasource_id": null,
2950-
"purl": "pkg:pypi/[email protected].0"
2950+
"purl": "pkg:pypi/[email protected].1"
29512951
},
29522952
{
29532953
"type": "pypi",
@@ -5025,7 +5025,7 @@
50255025
{
50265026
"key": "openpyxl",
50275027
"package_name": "openpyxl",
5028-
"installed_version": "3.1.0",
5028+
"installed_version": "3.1.1",
50295029
"dependencies": [
50305030
{
50315031
"key": "et-xmlfile",

tests/data/pinned-requirements.txt-expected.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,12 +2831,12 @@
28312831
"type": "pypi",
28322832
"namespace": null,
28332833
"name": "openpyxl",
2834-
"version": "3.1.0",
2834+
"version": "3.1.1",
28352835
"qualifiers": {},
28362836
"subpath": null,
28372837
"primary_language": "Python",
28382838
"description": "A Python library to read/write Excel 2010 xlsx/xlsm files\n.. image:: https://coveralls.io/repos/bitbucket/openpyxl/openpyxl/badge.svg?branch=default\n :target: https://coveralls.io/bitbucket/openpyxl/openpyxl?branch=default\n :alt: coverage status\n\nIntroduction\n------------\n\nopenpyxl is a Python library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.\n\nIt was born from lack of existing library to read/write natively from Python\nthe Office Open XML format.\n\nAll kudos to the PHPExcel team as openpyxl was initially based on PHPExcel.\n\n\nSecurity\n--------\n\nBy default openpyxl does not guard against quadratic blowup or billion laughs\nxml attacks. To guard against these attacks install defusedxml.\n\nMailing List\n------------\n\nThe user list can be found on http://groups.google.com/group/openpyxl-users\n\n\nSample code::\n\n from openpyxl import Workbook\n wb = Workbook()\n\n # grab the active worksheet\n ws = wb.active\n\n # Data can be assigned directly to cells\n ws['A1'] = 42\n\n # Rows can also be appended\n ws.append([1, 2, 3])\n\n # Python types will automatically be converted\n import datetime\n ws['A2'] = datetime.datetime.now()\n\n # Save the file\n wb.save(\"sample.xlsx\")\n\n\nDocumentation\n-------------\n\nThe documentation is at: https://openpyxl.readthedocs.io\n\n* installation methods\n* code examples\n* instructions for contributing\n\nRelease notes: https://openpyxl.readthedocs.io/en/stable/changes.html",
2839-
"release_date": "2023-01-31T14:40:28",
2839+
"release_date": "2023-02-13T16:51:26",
28402840
"parties": [
28412841
{
28422842
"type": "person",
@@ -2860,11 +2860,11 @@
28602860
"Programming Language :: Python :: 3.9"
28612861
],
28622862
"homepage_url": "https://openpyxl.readthedocs.io",
2863-
"download_url": "https://files.pythonhosted.org/packages/0d/89/f78a9a895e221ec8b13ae7f9495f340a0fb43563b13e2891b5df134f20ea/openpyxl-3.1.0-py2.py3-none-any.whl",
2864-
"size": 250043,
2863+
"download_url": "https://files.pythonhosted.org/packages/9e/57/1d3c2ce7f6f783be9b21569fc468a9f3660e35cc17017abfbbc26d3bd061/openpyxl-3.1.1-py2.py3-none-any.whl",
2864+
"size": 249839,
28652865
"sha1": null,
2866-
"md5": "66351b61736b19d3c88cd108908447d1",
2867-
"sha256": "24d7d361025d186ba91eff58135d50855cf035a84371b891e58fb6eb5125660f",
2866+
"md5": "864e1e1ea061fe056ade64f4e7bbaf22",
2867+
"sha256": "a0266e033e65f33ee697254b66116a5793c15fc92daf64711080000df4cfe0a8",
28682868
"sha512": null,
28692869
"bug_tracking_url": "https://foss.heptapod.net/openpyxl/openpyxl/-/issues",
28702870
"code_view_url": "https://foss.heptapod.net/openpyxl/openpyxl",
@@ -2884,20 +2884,20 @@
28842884
"dependencies": [],
28852885
"repository_homepage_url": null,
28862886
"repository_download_url": null,
2887-
"api_data_url": "https://pypi.org/pypi/openpyxl/3.1.0/json",
2887+
"api_data_url": "https://pypi.org/pypi/openpyxl/3.1.1/json",
28882888
"datasource_id": null,
2889-
"purl": "pkg:pypi/[email protected].0"
2889+
"purl": "pkg:pypi/[email protected].1"
28902890
},
28912891
{
28922892
"type": "pypi",
28932893
"namespace": null,
28942894
"name": "openpyxl",
2895-
"version": "3.1.0",
2895+
"version": "3.1.1",
28962896
"qualifiers": {},
28972897
"subpath": null,
28982898
"primary_language": "Python",
28992899
"description": "A Python library to read/write Excel 2010 xlsx/xlsm files\n.. image:: https://coveralls.io/repos/bitbucket/openpyxl/openpyxl/badge.svg?branch=default\n :target: https://coveralls.io/bitbucket/openpyxl/openpyxl?branch=default\n :alt: coverage status\n\nIntroduction\n------------\n\nopenpyxl is a Python library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.\n\nIt was born from lack of existing library to read/write natively from Python\nthe Office Open XML format.\n\nAll kudos to the PHPExcel team as openpyxl was initially based on PHPExcel.\n\n\nSecurity\n--------\n\nBy default openpyxl does not guard against quadratic blowup or billion laughs\nxml attacks. To guard against these attacks install defusedxml.\n\nMailing List\n------------\n\nThe user list can be found on http://groups.google.com/group/openpyxl-users\n\n\nSample code::\n\n from openpyxl import Workbook\n wb = Workbook()\n\n # grab the active worksheet\n ws = wb.active\n\n # Data can be assigned directly to cells\n ws['A1'] = 42\n\n # Rows can also be appended\n ws.append([1, 2, 3])\n\n # Python types will automatically be converted\n import datetime\n ws['A2'] = datetime.datetime.now()\n\n # Save the file\n wb.save(\"sample.xlsx\")\n\n\nDocumentation\n-------------\n\nThe documentation is at: https://openpyxl.readthedocs.io\n\n* installation methods\n* code examples\n* instructions for contributing\n\nRelease notes: https://openpyxl.readthedocs.io/en/stable/changes.html",
2900-
"release_date": "2023-01-31T14:40:31",
2900+
"release_date": "2023-02-13T16:51:28",
29012901
"parties": [
29022902
{
29032903
"type": "person",
@@ -2921,11 +2921,11 @@
29212921
"Programming Language :: Python :: 3.9"
29222922
],
29232923
"homepage_url": "https://openpyxl.readthedocs.io",
2924-
"download_url": "https://files.pythonhosted.org/packages/3d/73/bb87810cdde809f69fef11d31e77297894e58710d47626dc5e5b3ff8f92a/openpyxl-3.1.0.tar.gz",
2925-
"size": 186306,
2924+
"download_url": "https://files.pythonhosted.org/packages/10/bf/950ea7896f3c42ab04073cd2903f0a190ba77ef28bdf76191f6f86373712/openpyxl-3.1.1.tar.gz",
2925+
"size": 185802,
29262926
"sha1": null,
2927-
"md5": "b7ba597b801b9a102f27599b2fa227b3",
2928-
"sha256": "eccedbe1cdd8b2494057e73959b496821141038dbb7eb9266ea59e3f34208231",
2927+
"md5": "0b1a5d776707ef471810f61c7bf77a2d",
2928+
"sha256": "f06d44e2c973781068bce5ecf860a09bcdb1c7f5ce1facd5e9aa82c92c93ae72",
29292929
"sha512": null,
29302930
"bug_tracking_url": "https://foss.heptapod.net/openpyxl/openpyxl/-/issues",
29312931
"code_view_url": "https://foss.heptapod.net/openpyxl/openpyxl",
@@ -2945,9 +2945,9 @@
29452945
"dependencies": [],
29462946
"repository_homepage_url": null,
29472947
"repository_download_url": null,
2948-
"api_data_url": "https://pypi.org/pypi/openpyxl/3.1.0/json",
2948+
"api_data_url": "https://pypi.org/pypi/openpyxl/3.1.1/json",
29492949
"datasource_id": null,
2950-
"purl": "pkg:pypi/[email protected].0"
2950+
"purl": "pkg:pypi/[email protected].1"
29512951
},
29522952
{
29532953
"type": "pypi",
@@ -4976,7 +4976,7 @@
49764976
"pkg:pypi/[email protected]",
49774977
"pkg:pypi/[email protected]",
49784978
"pkg:pypi/[email protected]",
4979-
"pkg:pypi/[email protected].0",
4979+
"pkg:pypi/[email protected].1",
49804980
"pkg:pypi/[email protected]",
49814981
"pkg:pypi/[email protected]"
49824982
]
@@ -5066,7 +5066,7 @@
50665066
"dependencies": []
50675067
},
50685068
{
5069-
"package": "pkg:pypi/[email protected].0",
5069+
"package": "pkg:pypi/[email protected].1",
50705070
"dependencies": [
50715071
"pkg:pypi/[email protected]"
50725072
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Copyright 2018 Matthew Aynalem
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
from distutils.core import setup
17+
18+
from setuptools import find_packages
19+
20+
setup(
21+
name="packer.py",
22+
version="0.3.0",
23+
author="Matthew Aynalem",
24+
author_email="[email protected]",
25+
packages=["packerpy"],
26+
url="https://github.com/mayn/packer.py",
27+
license="Apache License 2.0",
28+
description="packer.py - python library to run hashicorp packer CLI commands",
29+
keywords="hashicorp packer",
30+
long_description=open("README.rst").read(),
31+
install_requires=[],
32+
classifiers=[
33+
"License :: OSI Approved :: Apache Software License",
34+
"Programming Language :: Python :: 2",
35+
"Programming Language :: Python :: 2.7",
36+
"Programming Language :: Python :: 3",
37+
"Programming Language :: Python :: 3.4",
38+
"Programming Language :: Python :: 3.5",
39+
"Programming Language :: Python :: 3.6",
40+
],
41+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Copyright 2018 Matthew Aynalem
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
from distutils.core import setup
17+
18+
from setuptools import find_packages
19+
20+
setup(
21+
name="packer.py",
22+
version="0.3.0",
23+
author="Matthew Aynalem",
24+
author_email="[email protected]",
25+
packages=["packerpy"],
26+
url="https://github.com/mayn/packer.py",
27+
license="Apache License 2.0",
28+
description="packer.py - python library to run hashicorp packer CLI commands",
29+
keywords="hashicorp packer",
30+
long_description=open("README.rst").read(),
31+
classifiers=[
32+
"License :: OSI Approved :: Apache Software License",
33+
"Programming Language :: Python :: 2",
34+
"Programming Language :: Python :: 2.7",
35+
"Programming Language :: Python :: 3",
36+
"Programming Language :: Python :: 3.4",
37+
"Programming Language :: Python :: 3.5",
38+
"Programming Language :: Python :: 3.6",
39+
],
40+
)

0 commit comments

Comments
 (0)