21 from __future__
import print_function
30 ROOT_ENV_KEY =
'DH_VIRTUALENV_INSTALL_ROOT' 31 DEFAULT_INSTALL_DIR =
'/opt/venvs/' 32 PYTHON_INTERPRETERS = [
'python',
'pypy',
'ipy',
'jython']
33 _PYTHON_INTERPRETERS_REGEX =
r'\(' +
r'\|'.join(PYTHON_INTERPRETERS) +
r'\)' 39 return subprocess.check_call(cmd, *args, **kwargs)
57 extra_virtualenv_arg=[],
58 use_system_packages=
False,
61 log_file=tempfile.NamedTemporaryFile().name,
63 requirements_filename=
'requirements.txt'):
67 install_root = os.environ.get(ROOT_ENV_KEY, DEFAULT_INSTALL_DIR)
75 if install_suffix
is None:
111 self.pip_args.append(
'-v')
114 self.pip_args.append(
'--index-url={0}'.format(index_url))
115 self.pip_args.extend([
116 '--extra-index-url={0}'.format(url)
for url
in extra_urls
122 self.pip_args.append(
'--log={0}'.format(os.path.abspath(self.
log_file)))
128 self.pip_args.extend(extra_pip_arg)
135 verbose = options.verbose
or os.environ.get(
'DH_VERBOSE') ==
'1' 137 extra_urls=options.extra_index_url,
138 preinstall=options.preinstall,
139 pip_tool=options.pip_tool,
140 upgrade_pip=options.upgrade_pip,
141 index_url=options.index_url,
142 setuptools=options.setuptools,
143 python=options.python,
144 builtin_venv=options.builtin_venv,
145 builtin_pip=options.builtin_pip,
146 sourcedirectory=options.sourcedirectory,
148 extra_pip_arg=options.extra_pip_arg,
149 extra_virtualenv_arg=options.extra_virtualenv_arg,
150 use_system_packages=options.use_system_packages,
151 skip_install=options.skip_install,
152 install_suffix=options.install_suffix,
153 requirements_filename=options.requirements_filename)
160 virtualenv = [self.
python,
'-m',
'venv']
163 virtualenv.append(
'--system-site-packages')
166 virtualenv = [
'virtualenv']
169 virtualenv.extend((
'--python', self.
python))
172 virtualenv.append(
'--system-site-packages')
174 virtualenv.append(
'--no-site-packages')
177 virtualenv.append(
'--setuptools')
180 virtualenv.append(
'--verbose')
190 return os.path.abspath(os.path.join(self.
bin_dir, binary_name))
213 if os.path.exists(requirements_path):
219 if os.path.exists(setup_py):
223 """Find list of files containing python shebangs in the bin directory""" 224 command = [
'grep',
'-l',
'-r',
225 '-e',
r'^#!.*bin/\(env \)\?{0}'.format(_PYTHON_INTERPRETERS_REGEX),
226 '-e',
r"^'''exec.*bin/{0}".format(_PYTHON_INTERPRETERS_REGEX),
228 grep_proc = subprocess.Popen(command, stdout=subprocess.PIPE)
229 files, stderr = grep_proc.communicate()
230 return set(f
for f
in files.decode(
'utf-8').strip().split(
'\n')
if f)
233 """Translate /usr/bin/python and /usr/bin/env python shebang 234 lines to point to our virtualenv python. 239 r's-^#!.*bin/\(env \)\?{names}\"\?-#!{pythonpath}-;' 240 r"s-^'''exec'.*bin/{names}-'''exec' {pythonpath}-" 241 ).format(names=_PYTHON_INTERPRETERS_REGEX, pythonpath=re.escape(pythonpath))
245 """Replace the `VIRTUAL_ENV` path in bin/activate to reflect the 246 post-install path of the virtualenv. 248 activate_settings = [
256 r'^setenv VIRTUAL_ENV.*$',
261 r'^set -gx VIRTUAL_ENV.*$',
266 for activate_args
in activate_settings:
267 virtualenv_path = activate_args[0]
268 pattern = re.compile(activate_args[1], flags=re.M)
269 activate_file = activate_args[2]
271 with open(self.
venv_bin(activate_file),
'r+')
as fh:
272 content = pattern.sub(virtualenv_path, fh.read())
286 local_dir = os.path.join(self.
package_dir,
"local")
287 if not os.path.isdir(local_dir):
289 elif os.path.samefile(self.
package_dir, local_dir):
292 os.symlink(
".", local_dir)
295 for d
in os.listdir(local_dir):
296 path = os.path.join(local_dir, d)
297 if not os.path.islink(path):
300 existing_target = os.readlink(path)
301 if not os.path.isabs(existing_target):
306 new_target = os.path.relpath(existing_target, local_dir)
308 os.symlink(new_target, path)
def fix_local_symlinks(self)
def find_script_files(self)
def from_options(cls, package, options)
def venv_bin(self, binary_name)
def create_virtualenv(self)
def pip_preinstall(self, args)
def install_package(self)
def check_call(cmd, args, kwargs)
def install_dependencies(self)
def __init__(self, package, extra_urls=[], preinstall=[], pip_tool='pip', upgrade_pip=False, index_url=None, setuptools=False, python=None, builtin_venv=False, builtin_pip=False, sourcedirectory=None, verbose=False, extra_pip_arg=[], extra_virtualenv_arg=[], use_system_packages=False, skip_install=False, install_suffix=None, log_file=tempfile.NamedTemporaryFile().name, pip_version=None, requirements_filename='requirements.txt')
def fix_activate_path(self)