Created
November 26, 2019 16:11
-
-
Save rcastill/dab85c234dd10fa7af56755116c75aee to your computer and use it in GitHub Desktop.
Convert Pipfile.lock into requirements.txt
This file contains hidden or 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/env python | |
""" | |
Convert Pipfile.lock into requirements.txt | |
It generates an output similar to `pipenv lock -r` | |
without the need to actually locking the file. | |
See https://github.com/pypa/pipenv/issues/3493 | |
This script was created to include the definition of sources | |
at the beginning of the requirements file. Useful when | |
working with private sources. | |
""" | |
import json | |
import sys | |
from os.path import expandvars as exenv | |
if __name__ == "__main__": | |
fname = "./Pipfile.lock" | |
if len(sys.argv) == 2: | |
fname = sys.argv[1] | |
elif len(sys.argv) > 2: | |
print("Usage: {} [Pipfile.lock]".format(sys.argv[0])) | |
sys.exit(1) | |
lines = [] | |
with open(fname) as f: | |
lock = json.load(f) | |
# add sources | |
sources = lock.get("_meta", dict()).get("sources", []) | |
if len(sources) > 0: | |
lines.append("-i {}".format(exenv(sources[0]["url"]))) | |
for extra in sources[1:]: | |
lines.append("--extra-index-url {}" | |
.format(exenv(extra["url"]))) | |
# requirements | |
pkgs = lock.get("default", dict()) | |
for pkg, meta in pkgs.items(): | |
lines.append(pkg + meta.get("version", "")) | |
# write iff went OK | |
print("\n".join(lines)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment