forked from PiRK/ElectrumABC
/
find_restricted_dependencies.py
executable file
·51 lines (46 loc) · 1.66 KB
/
find_restricted_dependencies.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
#!/usr/bin/env python3
import sys
try:
import requests
except ImportError as e:
sys.exit(f"Error: {str(e)}. Try 'sudo python3 -m pip install <module-name>'")
def check_restriction(p, r):
# See: https://www.python.org/dev/peps/pep-0496/
# Hopefully we don't need to parse the whole microlanguage
if "extra" in r and "[" not in p:
return False
for marker in ["os_name", "platform_release", "sys_platform", "platform_system"]:
if marker in r:
return True
for p in sys.stdin.read().split():
p = p.strip()
if not p:
continue
assert "==" in p, (
"This script expects a list of packages with pinned version, e.g. "
f"package==1.2.3, not {p}"
)
p, v = p.rsplit("==", 1)
try:
data = requests.get(f"https://pypi.org/pypi/{p}/{v}/json").json()["info"]
except ValueError:
raise Exception("Package could not be found: {}=={}".format(p, v))
try:
for r in data["requires_dist"]: # type: str
if ";" not in r:
continue
# example value for "r" at this point: "pefile (>=2017.8.1) ; sys_platform == \"win32\""
dep, restricted = r.split(";", 1)
dep = dep.strip()
restricted = restricted.strip()
dep_basename = dep.split(" ")[0]
if check_restriction(dep, restricted):
print(dep_basename, sep=" ")
print(
f"Installing {dep} from {p} although it is only needed for "
f"{restricted}",
file=sys.stderr,
)
except TypeError:
# Has no dependencies at all
continue