Skip to content

Commit 52c8f7b

Browse files
committed
Add API function for using cleanly as a library #39
Signed-off-by: Tushar Goel <[email protected]>
1 parent f2eaad4 commit 52c8f7b

24 files changed

+15465
-15458
lines changed

src/python_inspector/api.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class Resolution(NamedTuple):
5858

5959
def to_dict(self):
6060
return {
61-
"resolution": self.resolution,
62-
"packages": [package for package in self.packages],
6361
"files": self.files,
62+
"packages": [package for package in self.packages],
63+
"resolution": self.resolution,
6464
}
6565

6666

@@ -83,7 +83,14 @@ def resolver_api(
8383
"""
8484
Resolve the dependencies for the package requirements listed in one or
8585
more ``requirement_files``, one or more ``specifiers`` and one setuptools
86-
``setup_py_file`` file and save the results as JSON to FILE.
86+
``setup_py_file`` file.
87+
88+
Resolve the dependencies for the requested ``python_version`` PYVER and
89+
``operating_system`` OS combination defaulting Python version 3.8 and
90+
linux OS.
91+
92+
Download from the provided PyPI simple index_urls INDEX(s) URLs defaulting
93+
to PyPI.org
8794
"""
8895

8996
if verbose:
@@ -198,7 +205,11 @@ def resolver_api(
198205
)
199206

200207
if not direct_dependencies:
201-
raise Exception("Error: no requirements requested.")
208+
return Resolution(
209+
packages=[],
210+
resolution={},
211+
files=files,
212+
)
202213

203214
if verbose:
204215
printer("direct_dependencies:")
@@ -266,7 +277,7 @@ def resolver_api(
266277
packages=packages,
267278
resolution=resolution,
268279
files=files,
269-
).to_dict()
280+
)
270281

271282

272283
def resolve(
@@ -345,6 +356,9 @@ def get_resolved_dependencies(
345356
def get_requirements_from_direct_dependencies(
346357
direct_dependencies: List[DependentPackage], environment_marker: Dict
347358
) -> List[Requirement]:
359+
"""
360+
Yield Requirements from a list of DependentPackages.
361+
"""
348362
for dependency in direct_dependencies:
349363
# FIXME We are skipping editable requirements
350364
# and other pip options for now

src/python_inspector/resolution.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,9 @@ def get_requirements_from_distribution(
8181
if not os.path.exists(location):
8282
return []
8383
reqs = []
84-
try:
85-
for package_data in handler.parse(location):
86-
dependencies = package_data.dependencies
87-
reqs.extend(get_requirements_from_dependencies(dependencies=dependencies))
88-
except Exception as e:
89-
import subprocess
90-
91-
subprocess.check_call(["zip", "-T", location])
92-
raise Exception(
93-
f"Could not get requirements from pypi package at: {location!r}: {e}"
94-
) from e
84+
for package_data in handler.parse(location):
85+
dependencies = package_data.dependencies
86+
reqs.extend(get_requirements_from_dependencies(dependencies=dependencies))
9587
return reqs
9688

9789

src/python_inspector/resolve_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ def resolve_dependencies(
253253
)
254254
output = dict(
255255
headers=headers,
256-
resolved_dependencies_graph=resolution_result.get("resolution"),
257-
files=resolution_result.get("files"),
258-
packages=resolution_result.get("packages"),
256+
files=resolution_result.files,
257+
packages=resolution_result.packages,
258+
resolved_dependencies_graph=resolution_result.resolution,
259259
)
260260
write_output_in_file(
261261
output=output,

src/python_inspector/setup_py_live_eval.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def iter_requirements(level, extras, setup_file):
6060
with open(setup_file) as sf:
6161
exec(sf.read(), g)
6262
sys.path.pop()
63+
# removing the assertion `assert g["setup"]`` since this is not true for all cases
64+
# for example when setuptools.setup() is called instead of setup()
6365

6466
mock_args, mock_kwargs = mock_setup.call_args
6567
install_requires = mock_kwargs.get("install_requires", install_requires)

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

Lines changed: 103 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -14,109 +14,6 @@
1414
"warnings": [],
1515
"errors": []
1616
},
17-
"resolved_dependencies_graph": [
18-
{
19-
"package": "pkg:pypi/[email protected]",
20-
"dependencies": [
21-
"pkg:pypi/[email protected]",
22-
"pkg:pypi/[email protected]",
23-
"pkg:pypi/[email protected]"
24-
]
25-
},
26-
{
27-
"package": "pkg:pypi/[email protected]",
28-
"dependencies": [
29-
"pkg:pypi/[email protected]"
30-
]
31-
},
32-
{
33-
"package": "pkg:pypi/[email protected]",
34-
"dependencies": [
35-
"pkg:pypi/[email protected]",
36-
"pkg:pypi/[email protected]",
37-
"pkg:pypi/[email protected]"
38-
]
39-
},
40-
{
41-
"package": "pkg:pypi/[email protected]",
42-
"dependencies": []
43-
},
44-
{
45-
"package": "pkg:pypi/[email protected]",
46-
"dependencies": [
47-
"pkg:pypi/[email protected]"
48-
]
49-
},
50-
{
51-
"package": "pkg:pypi/[email protected]",
52-
"dependencies": []
53-
},
54-
{
55-
"package": "pkg:pypi/[email protected]",
56-
"dependencies": []
57-
},
58-
{
59-
"package": "pkg:pypi/[email protected]",
60-
"dependencies": [
61-
"pkg:pypi/[email protected]"
62-
]
63-
},
64-
{
65-
"package": "pkg:pypi/[email protected]",
66-
"dependencies": []
67-
},
68-
{
69-
"package": "pkg:pypi/[email protected]",
70-
"dependencies": [
71-
"pkg:pypi/[email protected]"
72-
]
73-
},
74-
{
75-
"package": "pkg:pypi/[email protected]",
76-
"dependencies": [
77-
"pkg:pypi/[email protected]",
78-
"pkg:pypi/[email protected]",
79-
"pkg:pypi/[email protected]",
80-
"pkg:pypi/[email protected]"
81-
]
82-
},
83-
{
84-
"package": "pkg:pypi/[email protected]",
85-
"dependencies": []
86-
},
87-
{
88-
"package": "pkg:pypi/[email protected]",
89-
"dependencies": []
90-
},
91-
{
92-
"package": "pkg:pypi/[email protected]",
93-
"dependencies": [
94-
"pkg:pypi/[email protected]",
95-
"pkg:pypi/[email protected]"
96-
]
97-
},
98-
{
99-
"package": "pkg:pypi/[email protected]",
100-
"dependencies": [
101-
"pkg:pypi/[email protected]",
102-
"pkg:pypi/[email protected]",
103-
"pkg:pypi/[email protected]",
104-
"pkg:pypi/[email protected]"
105-
]
106-
},
107-
{
108-
"package": "pkg:pypi/[email protected]",
109-
"dependencies": []
110-
},
111-
{
112-
"package": "pkg:pypi/[email protected]",
113-
"dependencies": []
114-
},
115-
{
116-
"package": "pkg:pypi/[email protected]",
117-
"dependencies": []
118-
}
119-
],
12017
"files": [
12118
{
12219
"type": "file",
@@ -2011,5 +1908,108 @@
20111908
"datasource_id": null,
20121909
"purl": "pkg:pypi/[email protected]"
20131910
}
1911+
],
1912+
"resolved_dependencies_graph": [
1913+
{
1914+
"package": "pkg:pypi/[email protected]",
1915+
"dependencies": [
1916+
"pkg:pypi/[email protected]",
1917+
"pkg:pypi/[email protected]",
1918+
"pkg:pypi/[email protected]"
1919+
]
1920+
},
1921+
{
1922+
"package": "pkg:pypi/[email protected]",
1923+
"dependencies": [
1924+
"pkg:pypi/[email protected]"
1925+
]
1926+
},
1927+
{
1928+
"package": "pkg:pypi/[email protected]",
1929+
"dependencies": [
1930+
"pkg:pypi/[email protected]",
1931+
"pkg:pypi/[email protected]",
1932+
"pkg:pypi/[email protected]"
1933+
]
1934+
},
1935+
{
1936+
"package": "pkg:pypi/[email protected]",
1937+
"dependencies": []
1938+
},
1939+
{
1940+
"package": "pkg:pypi/[email protected]",
1941+
"dependencies": [
1942+
"pkg:pypi/[email protected]"
1943+
]
1944+
},
1945+
{
1946+
"package": "pkg:pypi/[email protected]",
1947+
"dependencies": []
1948+
},
1949+
{
1950+
"package": "pkg:pypi/[email protected]",
1951+
"dependencies": []
1952+
},
1953+
{
1954+
"package": "pkg:pypi/[email protected]",
1955+
"dependencies": [
1956+
"pkg:pypi/[email protected]"
1957+
]
1958+
},
1959+
{
1960+
"package": "pkg:pypi/[email protected]",
1961+
"dependencies": []
1962+
},
1963+
{
1964+
"package": "pkg:pypi/[email protected]",
1965+
"dependencies": [
1966+
"pkg:pypi/[email protected]"
1967+
]
1968+
},
1969+
{
1970+
"package": "pkg:pypi/[email protected]",
1971+
"dependencies": [
1972+
"pkg:pypi/[email protected]",
1973+
"pkg:pypi/[email protected]",
1974+
"pkg:pypi/[email protected]",
1975+
"pkg:pypi/[email protected]"
1976+
]
1977+
},
1978+
{
1979+
"package": "pkg:pypi/[email protected]",
1980+
"dependencies": []
1981+
},
1982+
{
1983+
"package": "pkg:pypi/[email protected]",
1984+
"dependencies": []
1985+
},
1986+
{
1987+
"package": "pkg:pypi/[email protected]",
1988+
"dependencies": [
1989+
"pkg:pypi/[email protected]",
1990+
"pkg:pypi/[email protected]"
1991+
]
1992+
},
1993+
{
1994+
"package": "pkg:pypi/[email protected]",
1995+
"dependencies": [
1996+
"pkg:pypi/[email protected]",
1997+
"pkg:pypi/[email protected]",
1998+
"pkg:pypi/[email protected]",
1999+
"pkg:pypi/[email protected]"
2000+
]
2001+
},
2002+
{
2003+
"package": "pkg:pypi/[email protected]",
2004+
"dependencies": []
2005+
},
2006+
{
2007+
"package": "pkg:pypi/[email protected]",
2008+
"dependencies": []
2009+
},
2010+
{
2011+
"package": "pkg:pypi/[email protected]",
2012+
"dependencies": []
2013+
}
20142014
]
20152015
}

0 commit comments

Comments
 (0)