Skip to content

Instantly share code, notes, and snippets.

@lcarva
Last active February 6, 2026 21:53
Show Gist options
  • Select an option

  • Save lcarva/67798b2b5e583cc21004c6fdc3806ba2 to your computer and use it in GitHub Desktop.

Select an option

Save lcarva/67798b2b5e583cc21004c6fdc3806ba2 to your computer and use it in GitHub Desktop.
setup-rhtl-python.sh
#!/bin/bash
set -euo pipefail
show_help() {
cat << EOF
Setup Red Hat Trusted Libraries for Python/pip
This script configures pip to use Red Hat's trusted-libraries package index.
Prerequisites:
Python's keyring must be configured with the password for the Red Hat package registry.
The script assumes this has already been set up and will use the keyring for authentication.
Usage: $(basename "$0") [OPTIONS]
Options:
-h, --help Show this help message and exit
-l, --location LOCATION Set pip config location
Valid values: user, global, site
If not specified, automatically detects virtual environment
(site if in venv, user otherwise)
See: https://pip.pypa.io/en/stable/topics/configuration/#location
-u, --username USERNAME Set username for authentication (default: extracted from Docker config)
Examples:
$(basename "$0") # Use defaults
$(basename "$0") --location user # Configure for current user only
$(basename "$0") --username myuser # Use specific username
EOF
}
# Default values
LOCATION=""
USERNAME=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-l|--location)
LOCATION="$2"
shift 2
;;
-u|--username)
USERNAME="$2"
shift 2
;;
*)
echo "Error: Unknown option: $1" >&2
echo "Use --help for usage information" >&2
exit 1
;;
esac
done
# Auto-detect location if not specified
if [[ -z "$LOCATION" ]]; then
if [[ -n "${VIRTUAL_ENV:-}" ]]; then
LOCATION="site"
else
LOCATION="user"
fi
fi
PIP_CONFIG="pip config --${LOCATION}"
# Extract username from Docker config if not provided
if [[ -z "$USERNAME" ]]; then
USERNAME="$(< ~/.docker/config.json jq '.auths["registry.redhat.io"].auth | @base64d' -r | cut -d: -f1)"
fi
# 'subprocess' instead of 'import' so it doesn't require the keyring library to be installed in
# every virtual environment.
$PIP_CONFIG set global.keyring-provider subprocess
$PIP_CONFIG set global.index-url "https://${USERNAME}@packages.redhat.com/trusted-libraries"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment