-
Notifications
You must be signed in to change notification settings - Fork 975
/
Copy pathpip_list_versions.py
42 lines (32 loc) · 1.24 KB
/
pip_list_versions.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
import argparse
import logging
import re
import subprocess
def pip_show(package_name, packages=[]):
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):
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):
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)