-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest2.py
83 lines (60 loc) · 2.21 KB
/
test2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import argparse
import logging
import re
import subprocess
def pip_show(package_name, packages=[]):
"""
Recursively checks the dependencies of a given Python package using pip show command.
Args:
package_name (str): Name of the Python package to check the dependencies for.
packages (list, optional): List of packages already checked to avoid checking the same package twice if multiple packages depend on it. Defaults to an empty list.
Returns:
None
Raises:
None
"""
if package_name in packages:
return # avoid checking the same package twice if multiple packages depends on it.
packages.append(package_name)
result = subprocess.run(["pip", "show", package_name], stdout=subprocess.PIPE)
if result.returncode != 0:
logging.error("pip show %s failed", package_name)
show_stdout = result.stdout.decode("utf-8")
print(package_name + "==" + get_version(show_stdout))
for dependency in get_dependencies(show_stdout):
pip_show(dependency, packages=packages)
def get_version(show_stdout):
"""
Returns the version of a Python package by parsing the output of pip show command.
Args:
show_stdout (str): Output of pip show command for a Python package.
Returns:
str: Version of the Python package.
Raises:
None
"""
for line in show_stdout.split("\n"):
m = re.match(r"^Version:\s(?P<version>.+)$", line)
if m:
return m.group("version")
return "not found"
def get_dependencies(show_stdout):
"""
Returns the list of dependencies of a Python package by parsing the output of pip show command.
Args:
show_stdout (str): Output of pip show command for a Python package.
Returns:
list: List of dependencies of the Python package.
Raises:
None
"""
for line in show_stdout.split("\n"):
m = re.match(r"^Requires:\s(?P<requires>.+)$", line)
if m:
return m.group("requires").split(", ")
return []
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("package", type=str, help="package name")
args = parser.parse_args()
pip_show(args.package)