Skip to content

Commit

Permalink
Update dh-virtualenv so it supports specifying custom pip binary to copy
Browse files Browse the repository at this point in the history
to and use inside the created dh virtual environment.

In addition to that, update create_virtualenv() step to print actual
versions of the tools which are installed into virtualenv.

This new --pip-binary flag should be used in combination with
--virtualenv-extra-args="--no-pip" where we want to use a correct system
version of pip is used instead of the one bundled with virtual
environment.

Also add --force-pip-version which allows us to install a specific
version of pip inside venv.
  • Loading branch information
Kami committed Mar 5, 2021
1 parent bab5d2e commit 1cf92b6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dh_virtualenv/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ def get_default_parser():
"you'll install this program by using the --preinstall "
"argument. The replacement is expected to be found in "
"the virtualenv's bin/ directory.")
parser.add_option('--pip-binary', default=None,
help="Path to custom pip binary which should be used "
"and copied to dh virtualenv. Should be used in "
"combination with virtualenv --no-pip flag")
parser.add_option('--force-pip-version', default=None,
help="Force upgrade a specific pip version in dh "
"dh-virtualenv")
parser.add_option('--upgrade-pip', action='store_true', default=False,
help='Upgrade pip to the latest available version')
parser.add_option('--extra-pip-arg', action='append',
Expand Down
39 changes: 39 additions & 0 deletions dh_virtualenv/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self,
extra_urls=[],
preinstall=[],
pip_tool='pip',
pip_binary=None,
force_pip_version=None,
upgrade_pip=False,
index_url=None,
setuptools=False,
Expand Down Expand Up @@ -66,6 +68,9 @@ def __init__(self,
self.bin_dir = os.path.join(self.package_dir, 'bin')
self.local_bin_dir = os.path.join(self.package_dir, 'local', 'bin')

self.pip_binary = pip_binary
self.force_pip_version = force_pip_version

self.preinstall = preinstall
self.upgrade_pip = upgrade_pip
self.extra_virtualenv_arg = extra_virtualenv_arg
Expand Down Expand Up @@ -108,6 +113,8 @@ def from_options(cls, package, options):
extra_urls=options.extra_index_url,
preinstall=options.preinstall,
pip_tool=options.pip_tool,
pip_binary=options.pip_binary,
force_pip_version=options.force_pip_version,
upgrade_pip=options.upgrade_pip,
index_url=options.index_url,
setuptools=options.setuptools,
Expand Down Expand Up @@ -152,6 +159,38 @@ def create_virtualenv(self):
virtualenv.append(self.package_dir)
subprocess.check_call(virtualenv)

if self.force_pip_version:
venv_pip_path = self.venv_bin('pip')
print("Forcing / upgrading pip version inside venv to %s..." % (self.force_pip_version))
output = subprocess.check_output([venv_pip_path, 'install', '--upgrade',
'pip==%s' % (self.force_pip_version)]).strip()
print('Output: %s' % (output))

if self.pip_binary:
dest_path = self.venv_bin('pip')
print("Copying %s pip binary into dh virtualenv: %s..." % (self.pip_binary, dest_path))
shutil.copy(self.pip_binary, dest_path)

# To make troubleshooting, etc. easier, we also print which versions
# were copied and used into venv
venv_python_path = self.venv_bin('python')
venv_virtualenv_path = self.venv_bin('virtualenv')
venv_pip_path = self.venv_bin('pip')

print('dh-virualenv versions info')

if os.path.exists(venv_python_path):
output = subprocess.check_output([venv_python_path, '--version']).strip()
print('Using Python: %s' % (output))

if os.path.exists(venv_virtualenv_path):
output = subprocess.check_output([venv_virtualenv_path, '--version']).strip()
print('Using virtualenv: %s' % (output))

if os.path.exists(venv_pip_path):
output = subprocess.check_output([venv_pip_path, '--version']).strip()
print('Using pip: %s' % (output))

def venv_bin(self, binary_name):
return os.path.abspath(os.path.join(self.bin_dir, binary_name))

Expand Down

0 comments on commit 1cf92b6

Please sign in to comment.