Skip to content

Instantly share code, notes, and snippets.

@njoyce
Created September 30, 2020 09:15
Show Gist options
  • Save njoyce/4d61938b8d2757bf3e613d4a125544ba to your computer and use it in GitHub Desktop.
Save njoyce/4d61938b8d2757bf3e613d4a125544ba to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import toml
import subprocess
def get_root_packages():
pipdeptree = subprocess.run(
"pipdeptree",
shell=True,
capture_output=True,
check=True
)
root_packages = []
for line in pipdeptree.stdout.decode("utf-8").split(os.linesep):
if line.startswith(" "):
# we only care about root packages
continue
root_packages.append(line.strip())
return root_packages
def get_pipenv_packages():
with open("Pipfile", "rt") as fp:
pipenv = toml.loads(fp.read())
return pipenv["packages"]
if __name__ == "__main__":
root_packages = get_root_packages()
for package, version in get_pipenv_packages():
for root_pkg in root_packages:
if root_pkg.startswith(package + "=="):
print(f"{package} = \"{version}\"")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment