Created
November 19, 2019 09:56
-
-
Save boegel/fd9a636d652aa5c8e57778088e9c0a21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/enb python | |
import json | |
import sys | |
from pprint import pprint | |
if len(sys.argv) != 3: | |
sys.stderr.write("ERROR: Usage: %s <path to JSON file with output of 'pipdeptree -j <pkg>'> <pkg name>\n") | |
sys.exit(1) | |
json_file, target_pkg = sys.argv[1:] | |
with open(json_file) as fp: | |
deptree = json.loads(fp.read()) | |
def find_deps(pkgs): | |
res = [] | |
for pkg in pkgs: | |
for entry in deptree: | |
if entry['package']['package_name'] == pkg: | |
res.append((pkg, entry['package']['installed_version'])) | |
deps = [dep['package_name'] for dep in entry['dependencies']] | |
res.extend(find_deps(deps)) | |
break | |
return res | |
deps = find_deps([target_pkg]) | |
modnames = { | |
'attrs': 'attr', | |
'enum34': 'enum', | |
'futures': 'concurrent.futures', | |
'ipython': 'IPython', | |
'python-dateutil': 'dateutil', | |
'pyzmq': 'zmq', | |
} | |
# iterate over deps in reverse order, get rid of duplicates along the way | |
# also filter out Python packages that are already installed in current environment | |
res = [] | |
skipped = [] | |
for dep in deps[::-1]: | |
if dep not in res + skipped: | |
if dep[0] in modnames: | |
modname = modnames[dep[0]] | |
else: | |
modname = dep[0].replace('-', '_') | |
try: | |
__import__(modname) | |
skipped.append(dep) | |
except ImportError: | |
res.append(dep) | |
pprint(res, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pkg=qiskit # tweak this according to what you want to install | |
# load modules for Python + additional Python packages (like SciPy-bundle, matplotlib, ...) | |
module load SciPy-bundle/2019.03-foss-2019a | |
# prevent pip from (ab)using $HOME/.cache/pip | |
export XDG_CACHE_HOME=/tmp/$USER/pip_cache | |
# create virtualenv, install package in it | |
virtualenv --system-site-packages /tmp/$USER/$pkg | |
source /tmp/$USER/$pkg/bin/activate | |
pip install $pkg | |
# install pipdeptree, figure out dependency tree for installed package | |
pip install pipdeptree | |
pipdeptree -j -p $pkg > ${pkg}_deptree.json | |
deactivate | |
# use script to generate exts_list | |
python deptree2exts_list.py ${pkg}_deptree.json $pkg | |
rm -rf /tmp/$USER/$pkg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment