| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2013 - 2014 Spotify AB |
| 3 |
|
| 4 |
# This file is part of dh-virtualenv. |
| 5 |
|
| 6 |
# dh-virtualenv is free software: you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU General Public License as |
| 8 |
# published by the Free Software Foundation, either version 2 of the |
| 9 |
# License, or (at your option) any later version. |
| 10 |
|
| 11 |
# dh-virtualenv is distributed in the hope that it will be useful, but |
| 12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# General Public License for more details. |
| 15 |
|
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with dh-virtualenv. If not, see |
| 18 |
# <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import os |
| 21 |
import re |
| 22 |
import shutil |
| 23 |
import subprocess |
| 24 |
import tempfile |
| 25 |
|
| 26 |
ROOT_ENV_KEY = 'DH_VIRTUALENV_INSTALL_ROOT' |
| 27 |
DEFAULT_INSTALL_DIR = '/opt/venvs/' |
| 28 |
PYTHON_INTERPRETERS = ['python', 'pypy', 'ipy', 'jython'] |
| 29 |
_PYTHON_INTERPRETERS_REGEX = r'\(' + r'\|'.join(PYTHON_INTERPRETERS) + r'\)' |
| 30 |
|
| 31 |
|
| 32 |
class Deployment(object): |
| 33 |
def __init__(self, |
| 34 |
package, |
| 35 |
extra_urls=[], |
| 36 |
preinstall=[], |
| 37 |
extras=[], |
| 38 |
pip_tool='pip', |
| 39 |
upgrade_pip=False, |
| 40 |
index_url=None, |
| 41 |
setuptools=False, |
| 42 |
python=None, |
| 43 |
builtin_venv=False, |
| 44 |
sourcedirectory=None, |
| 45 |
verbose=False, |
| 46 |
extra_pip_arg=[], |
| 47 |
extra_virtualenv_arg=[], |
| 48 |
use_system_packages=False, |
| 49 |
skip_install=False, |
| 50 |
install_suffix=None, |
| 51 |
requirements_filename='requirements.txt', |
| 52 |
upgrade_pip_to='', |
| 53 |
): |
| 54 |
|
| 55 |
self.package = package |
| 56 |
install_root = os.environ.get(ROOT_ENV_KEY, DEFAULT_INSTALL_DIR) |
| 57 |
self.install_suffix = install_suffix |
| 58 |
|
| 59 |
self.debian_root = os.path.join( |
| 60 |
'debian', package, install_root.lstrip('/')) |
| 61 |
|
| 62 |
if install_suffix is None: |
| 63 |
self.virtualenv_install_dir = os.path.join(install_root, self.package) |
| 64 |
self.package_dir = os.path.join(self.debian_root, package) |
| 65 |
else: |
| 66 |
self.virtualenv_install_dir = os.path.join(install_root, install_suffix) |
| 67 |
self.package_dir = os.path.join(self.debian_root, install_suffix) |
| 68 |
|
| 69 |
self.bin_dir = os.path.join(self.package_dir, 'bin') |
| 70 |
self.local_bin_dir = os.path.join(self.package_dir, 'local', 'bin') |
| 71 |
|
| 72 |
self.preinstall = preinstall |
| 73 |
self.extras = extras |
| 74 |
self.upgrade_pip = upgrade_pip |
| 75 |
self.upgrade_pip_to = upgrade_pip_to |
| 76 |
self.extra_virtualenv_arg = extra_virtualenv_arg |
| 77 |
self.log_file = tempfile.NamedTemporaryFile() |
| 78 |
self.verbose = verbose |
| 79 |
self.setuptools = setuptools |
| 80 |
self.python = python |
| 81 |
self.builtin_venv = builtin_venv |
| 82 |
self.sourcedirectory = '.' if sourcedirectory is None else sourcedirectory |
| 83 |
self.use_system_packages = use_system_packages |
| 84 |
self.skip_install = skip_install |
| 85 |
self.requirements_filename = requirements_filename |
| 86 |
|
| 87 |
# We need to prefix the pip run with the location of python |
| 88 |
# executable. Otherwise it would just blow up due to too long |
| 89 |
# shebang-line. |
| 90 |
python = self.venv_bin('python') |
| 91 |
self.pip_preinstall_prefix = [python, self.venv_bin('pip')] |
| 92 |
self.pip_prefix = [python, self.venv_bin(pip_tool)] |
| 93 |
self.pip_args = ['install'] |
| 94 |
|
| 95 |
if self.verbose: |
| 96 |
self.pip_args.append('-v') |
| 97 |
|
| 98 |
if index_url: |
| 99 |
self.pip_args.append('--index-url={0}'.format(index_url)) |
| 100 |
self.pip_args.extend([ |
| 101 |
'--extra-index-url={0}'.format(url) for url in extra_urls |
| 102 |
]) |
| 103 |
self.pip_args.append('--log={0}'.format(os.path.abspath(self.log_file.name))) |
| 104 |
# Keep a copy with well-suported options only (for upgrading pip itself) |
| 105 |
self.pip_upgrade_args = self.pip_args[:] |
| 106 |
# Add in any user supplied pip args |
| 107 |
self.pip_args.extend(extra_pip_arg) |
| 108 |
|
| 109 |
@classmethod |
| 110 |
def from_options(cls, package, options): |
| 111 |
verbose = options.verbose or os.environ.get('DH_VERBOSE') == '1' |
| 112 |
return cls(package, |
| 113 |
extra_urls=options.extra_index_url, |
| 114 |
preinstall=options.preinstall, |
| 115 |
extras=options.extras, |
| 116 |
pip_tool=options.pip_tool, |
| 117 |
upgrade_pip=options.upgrade_pip, |
| 118 |
index_url=options.index_url, |
| 119 |
setuptools=options.setuptools, |
| 120 |
python=options.python, |
| 121 |
builtin_venv=options.builtin_venv, |
| 122 |
sourcedirectory=options.sourcedirectory, |
| 123 |
verbose=verbose, |
| 124 |
extra_pip_arg=options.extra_pip_arg, |
| 125 |
extra_virtualenv_arg=options.extra_virtualenv_arg, |
| 126 |
use_system_packages=options.use_system_packages, |
| 127 |
skip_install=options.skip_install, |
| 128 |
install_suffix=options.install_suffix, |
| 129 |
requirements_filename=options.requirements_filename, |
| 130 |
upgrade_pip_to=options.upgrade_pip_to, |
| 131 |
) |
| 132 |
|
| 133 |
def clean(self): |
| 134 |
shutil.rmtree(self.debian_root) |
| 135 |
|
| 136 |
def create_virtualenv(self): |
| 137 |
# Specify interpreter and virtual environment options |
| 138 |
if self.builtin_venv: |
| 139 |
virtualenv = [self.python, '-m', 'venv'] |
| 140 |
|
| 141 |
if self.use_system_packages: |
| 142 |
virtualenv.append('--system-site-packages') |
| 143 |
|
| 144 |
else: |
| 145 |
virtualenv = ['virtualenv'] |
| 146 |
|
| 147 |
if self.use_system_packages: |
| 148 |
virtualenv.append('--system-site-packages') |
| 149 |
|
| 150 |
if self.python: |
| 151 |
virtualenv.extend(('--python', self.python)) |
| 152 |
|
| 153 |
if self.setuptools: |
| 154 |
virtualenv.append('--setuptools') |
| 155 |
|
| 156 |
if self.verbose: |
| 157 |
virtualenv.append('--verbose') |
| 158 |
|
| 159 |
# Add in any user supplied virtualenv args |
| 160 |
if self.extra_virtualenv_arg: |
| 161 |
virtualenv.extend(self.extra_virtualenv_arg) |
| 162 |
|
| 163 |
virtualenv.append(self.package_dir) |
| 164 |
subprocess.check_call(virtualenv) |
| 165 |
|
| 166 |
# Due to Python bug https://bugs.python.org/issue24875 |
| 167 |
# venv doesn't bootstrap pip/setuptools in the virtual |
| 168 |
# environment with --system-site-packages . |
| 169 |
# The workaround is to reconfigure it with this option |
| 170 |
# after it has been created. |
| 171 |
if self.builtin_venv and self.use_system_packages: |
| 172 |
virtualenv.append('--system-site-packages') |
| 173 |
subprocess.check_call(virtualenv) |
| 174 |
|
| 175 |
def venv_bin(self, binary_name): |
| 176 |
return os.path.abspath(os.path.join(self.bin_dir, binary_name)) |
| 177 |
|
| 178 |
def pip_preinstall(self, *args): |
| 179 |
return self.pip_preinstall_prefix + self.pip_args + list(args) |
| 180 |
|
| 181 |
def pip(self, *args): |
| 182 |
return self.pip_prefix + self.pip_args + list(args) |
| 183 |
|
| 184 |
def install_dependencies(self): |
| 185 |
# Install preinstall stage packages. This is handy if you need |
| 186 |
# a custom package to install dependencies (think something |
| 187 |
# along lines of setuptools), but that does not get installed |
| 188 |
# by default virtualenv. |
| 189 |
if self.upgrade_pip or self.upgrade_pip_to: |
| 190 |
# First, bootstrap pip with a reduced option set (well-supported options) |
| 191 |
cmd = self.pip_preinstall_prefix + self.pip_upgrade_args |
| 192 |
if not self.upgrade_pip_to or self.upgrade_pip_to == 'latest': |
| 193 |
cmd += ['-U', 'pip'] |
| 194 |
else: |
| 195 |
cmd += ['pip==' + self.upgrade_pip_to] |
| 196 |
subprocess.check_call(cmd) |
| 197 |
if self.preinstall: |
| 198 |
subprocess.check_call(self.pip_preinstall(*self.preinstall)) |
| 199 |
|
| 200 |
requirements_path = os.path.join(self.sourcedirectory, self.requirements_filename) |
| 201 |
if os.path.exists(requirements_path): |
| 202 |
subprocess.check_call(self.pip('-r', requirements_path)) |
| 203 |
|
| 204 |
def run_tests(self): |
| 205 |
python = self.venv_bin('python') |
| 206 |
setup_py = os.path.join(self.sourcedirectory, 'setup.py') |
| 207 |
if os.path.exists(setup_py): |
| 208 |
subprocess.check_call([python, 'setup.py', 'test'], cwd=self.sourcedirectory) |
| 209 |
|
| 210 |
def find_script_files(self): |
| 211 |
"""Find list of files containing python shebangs in the bin directory""" |
| 212 |
command = ['grep', '-l', '-r', |
| 213 |
'-e', r'^#!.*bin/\(env \)\?{0}'.format(_PYTHON_INTERPRETERS_REGEX), |
| 214 |
'-e', r"^'''exec.*bin/{0}".format(_PYTHON_INTERPRETERS_REGEX), |
| 215 |
self.bin_dir] |
| 216 |
grep_proc = subprocess.Popen(command, stdout=subprocess.PIPE) |
| 217 |
files, stderr = grep_proc.communicate() |
| 218 |
return set(f for f in files.decode('utf-8').strip().split('\n') if f) |
| 219 |
|
| 220 |
def fix_shebangs(self): |
| 221 |
"""Translate /usr/bin/python and /usr/bin/env python shebang |
| 222 |
lines to point to our virtualenv python. |
| 223 |
""" |
| 224 |
pythonpath = os.path.join(self.virtualenv_install_dir, 'bin/python') |
| 225 |
for f in self.find_script_files(): |
| 226 |
regex = (r's-^#!.*bin/\(env \)\?{names}\"\?-#!{pythonpath}-;' |
| 227 |
r"s-^'''exec'.*bin/{names}-'''exec' {pythonpath}-" |
| 228 |
).format(names=_PYTHON_INTERPRETERS_REGEX, pythonpath=re.escape(pythonpath)) |
| 229 |
p = subprocess.Popen( |
| 230 |
['sed', '-i', regex, f], |
| 231 |
stdout=subprocess.PIPE, |
| 232 |
stderr=subprocess.PIPE, |
| 233 |
) |
| 234 |
subprocess.check_call(['sed', '-i', regex, f]) |
| 235 |
|
| 236 |
def fix_activate_path(self): |
| 237 |
"""Replace the `VIRTUAL_ENV` path in bin/activate to reflect the |
| 238 |
post-install path of the virtualenv. |
| 239 |
""" |
| 240 |
activate_settings = [ |
| 241 |
[ |
| 242 |
'VIRTUAL_ENV="{0}"'.format(self.virtualenv_install_dir), |
| 243 |
r'^VIRTUAL_ENV=.*$', |
| 244 |
"activate" |
| 245 |
], |
| 246 |
[ |
| 247 |
'setenv VIRTUAL_ENV "{0}"'.format(self.virtualenv_install_dir), |
| 248 |
r'^setenv VIRTUAL_ENV.*$', |
| 249 |
"activate.csh" |
| 250 |
], |
| 251 |
[ |
| 252 |
'set -gx VIRTUAL_ENV "{0}"'.format(self.virtualenv_install_dir), |
| 253 |
r'^set -gx VIRTUAL_ENV.*$', |
| 254 |
"activate.fish" |
| 255 |
], |
| 256 |
] |
| 257 |
|
| 258 |
for activate_args in activate_settings: |
| 259 |
virtualenv_path = activate_args[0] |
| 260 |
pattern = re.compile(activate_args[1], flags=re.M) |
| 261 |
activate_file = activate_args[2] |
| 262 |
|
| 263 |
with open(self.venv_bin(activate_file), 'r+') as fh: |
| 264 |
content = pattern.sub(virtualenv_path, fh.read()) |
| 265 |
fh.seek(0) |
| 266 |
fh.truncate() |
| 267 |
fh.write(content) |
| 268 |
|
| 269 |
def install_package(self): |
| 270 |
if not self.skip_install: |
| 271 |
package = '.[{}]'.format(','.join(self.extras)) if self.extras else '.' |
| 272 |
subprocess.check_call(self.pip(package), cwd=os.path.abspath(self.sourcedirectory)) |
| 273 |
|
| 274 |
def fix_local_symlinks(self): |
| 275 |
# The virtualenv might end up with a local folder that points outside the package |
| 276 |
# Specifically it might point at the build environment that created it! |
| 277 |
# Make those links relative |
| 278 |
# See https://github.com/pypa/virtualenv/commit/5cb7cd652953441a6696c15bdac3c4f9746dfaa1 |
| 279 |
local_dir = os.path.join(self.package_dir, "local") |
| 280 |
if not os.path.isdir(local_dir): |
| 281 |
return |
| 282 |
elif os.path.samefile(self.package_dir, local_dir): |
| 283 |
# "local" points directly to its containing directory |
| 284 |
os.unlink(local_dir) |
| 285 |
os.symlink(".", local_dir) |
| 286 |
return |
| 287 |
|
| 288 |
for d in os.listdir(local_dir): |
| 289 |
path = os.path.join(local_dir, d) |
| 290 |
if not os.path.islink(path): |
| 291 |
continue |
| 292 |
|
| 293 |
existing_target = os.readlink(path) |
| 294 |
if not os.path.isabs(existing_target): |
| 295 |
# If the symlink is already relative, we don't |
| 296 |
# want to touch it. |
| 297 |
continue |
| 298 |
|
| 299 |
new_target = os.path.relpath(existing_target, local_dir) |
| 300 |
os.unlink(path) |
| 301 |
os.symlink(new_target, path) |