Skip to content

Instantly share code, notes, and snippets.

@geblanco
Last active November 20, 2023 12:14
Show Gist options
  • Save geblanco/8317420b806c95ee76b5cced7c825a86 to your computer and use it in GitHub Desktop.
Save geblanco/8317420b806c95ee76b5cced7c825a86 to your computer and use it in GitHub Desktop.
Script to automatically extract dependencies from a pyproject.toml file.
#!/bin/bash
# If a requirements.txt file is supplied, it will be updated with the latest versions of each package.
# If a pyproject.toml is required, package upgrades will just be printed.
echo "Careful:" >&2
echo " - Virtual Env is required" >&2
echo " - If any of your PYPY packages contain the strin 'http', it is possible that it gets astray, manually inspect it" >&2
def_deps_key="dependencies"
deps_key="${def_deps_key}"
pyproj_file=""
output=/dev/stdout
function usage(){
echo "pyprojreqs [-p|--pyproject <pyproject.toml>] [-k|--key <key to extract from optional-depencencies>] [-o|--output <FD (default to stdout)>]"
}
function convert_pyproj(){
file=$1; shift
key=$1; shift
python3 -m venv /tmp/pyprojreqs >/dev/null
/tmp/pyprojreqs/bin/pip3 install toml pip-tools >/dev/null
/tmp/pyprojreqs/bin/python3 -c "$(cat <<EOF
import toml
from packaging.requirements import InvalidRequirement, Requirement
proj = toml.load(open("${file}"))
if "${dev}" == "":
deps = proj["project"]["dependencies"]
else:
deps = proj["project"]["optional-dependencies"]["dev"]
parsed_deps = []
for item in deps:
try:
# only accept valid dependencies and those that do not depend on a url (i.e.: from pypi)
req = Requirement(item)
if req.url is None:
parsed_deps.append(item)
except:
pass
print("\n".join(parsed_deps))
EOF
)"
}
if [[ $# -eq 0 ]]; then
usage
exit 0
fi
while [[ $# -gt 0 ]]; do
case $1 in
-p|--pyproject)
pyproj_file="$2"
shift; shift
;;
-k|--key)
deps_key="$2"
shift; shift
;;
-o|--output)
output="$2"
shift; shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option $1"
exit 1
;;
esac
done
if [[ "${pyproj_file}" == "" ]]; then
usage
exit 0
fi
# set -e
# set -o xtrace
convert_pyproj $pyproj_file $deps_key > $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment