find_restricted_dependencies.py (1373B)
1 #!/usr/bin/env python3 2 import sys 3 4 try: 5 import requests 6 except ImportError as e: 7 sys.exit(f"Error: {str(e)}. Try 'sudo python3 -m pip install <module-name>'") 8 9 10 def check_restriction(p, r): 11 # See: https://www.python.org/dev/peps/pep-0496/ 12 # Hopefully we don't need to parse the whole microlanguage 13 if "extra" in r and "[" not in p: 14 return False 15 for marker in ["os_name", "platform_release", "sys_platform", "platform_system"]: 16 if marker in r: 17 return True 18 19 20 for p in sys.stdin.read().split(): 21 p = p.strip() 22 if not p: 23 continue 24 assert "==" in p, "This script expects a list of packages with pinned version, e.g. package==1.2.3, not {}".format(p) 25 p, v = p.rsplit("==", 1) 26 try: 27 data = requests.get("https://pypi.org/pypi/{}/{}/json".format(p, v)).json()["info"] 28 except ValueError: 29 raise Exception("Package could not be found: {}=={}".format(p, v)) 30 try: 31 for r in data["requires_dist"]: 32 if ";" not in r: 33 continue 34 d, restricted = r.split(";", 1) 35 if check_restriction(d, restricted): 36 print(d, sep=" ") 37 print("Installing {} from {} although it is only needed for {}".format(d, p, restricted), file=sys.stderr) 38 except TypeError: 39 # Has no dependencies at all 40 continue 41