relocate.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013 - 2014 Spotify AB
3 # Copyright (c) 2020 Locus Robotics
4 
5 # This file is part of dh-virtualenv.
6 
7 # dh-virtualenv is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation, either version 2 of the
10 # License, or (at your option) any later version.
11 
12 # dh-virtualenv is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 
17 # You should have received a copy of the GNU General Public License
18 # along with dh-virtualenv. If not, see
19 # <http://www.gnu.org/licenses/>.
20 
21 import os
22 import re
23 import subprocess
24 
25 from . import run_command
26 
27 PYTHON_INTERPRETERS = ["python", "pypy", "ipy", "jython"]
28 _PYTHON_INTERPRETERS_REGEX = r"\(" + r"\|".join(PYTHON_INTERPRETERS) + r"\)"
29 
30 
31 def find_script_files(venv_dir):
32  """Find list of files containing python shebangs in the bin directory. """
33  command = [
34  "grep",
35  "-l",
36  "-r",
37  "-e",
38  r"^#!.*bin/\(env \)\?{0}".format(_PYTHON_INTERPRETERS_REGEX),
39  "-e",
40  r"^'''exec.*bin/{0}".format(_PYTHON_INTERPRETERS_REGEX),
41  os.path.join(venv_dir, "bin"),
42  ]
43  files = run_command(command, check=True, capture_output=True).stdout
44  return {f for f in files.decode("utf-8").strip().split("\n") if f}
45 
46 
47 def fix_shebangs(venv_dir, target_dir):
48  """Translate /usr/bin/python and /usr/bin/env python shebang
49  lines to point to our virtualenv python.
50  """
51  pythonpath = os.path.join(target_dir, "bin/python")
52  for f in find_script_files(venv_dir):
53  regex = (
54  r"s-^#!.*bin/\(env \)\?{names}\"\?-#!{pythonpath}-;" r"s-^'''exec'.*bin/{names}-'''exec' {pythonpath}-"
55  ).format(names=_PYTHON_INTERPRETERS_REGEX, pythonpath=re.escape(pythonpath))
56  run_command(["sed", "-i", regex, f], check=True)
57 
58 
59 def fix_activate_path(venv_dir, target_dir):
60  """Replace the `VIRTUAL_ENV` path in bin/activate to reflect the
61  post-install path of the virtualenv.
62  """
63  activate_settings = [
64  ['VIRTUAL_ENV="{0}"'.format(target_dir), r"^VIRTUAL_ENV=.*$", "activate"],
65  ['setenv VIRTUAL_ENV "{0}"'.format(target_dir), r"^setenv VIRTUAL_ENV.*$", "activate.csh"],
66  ['set -gx VIRTUAL_ENV "{0}"'.format(target_dir), r"^set -gx VIRTUAL_ENV.*$", "activate.fish"],
67  ]
68 
69  for activate_args in activate_settings:
70  virtualenv_path = activate_args[0]
71  pattern = re.compile(activate_args[1], flags=re.M)
72  activate_file = activate_args[2]
73 
74  with open(os.path.join(venv_dir, "bin", activate_file), "r+") as fh:
75  content = pattern.sub(virtualenv_path, fh.read())
76  fh.seek(0)
77  fh.truncate()
78  fh.write(content)
79 
80 
81 def fix_local_symlinks(venv_dir):
82  # The virtualenv might end up with a local folder that points outside the package
83  # Specifically it might point at the build environment that created it!
84  # Make those links relative
85  # See https://github.com/pypa/virtualenv/commit/5cb7cd652953441a6696c15bdac3c4f9746dfaa1
86  local_dir = os.path.join(venv_dir, "local")
87  if not os.path.isdir(local_dir):
88  return
89  elif os.path.samefile(venv_dir, local_dir):
90  # "local" points directly to its containing directory
91  os.unlink(local_dir)
92  os.symlink(".", local_dir)
93  return
94 
95  for d in os.listdir(local_dir):
96  path = os.path.join(local_dir, d)
97  if not os.path.islink(path):
98  continue
99 
100  existing_target = os.readlink(path)
101  if not os.path.isabs(existing_target):
102  # If the symlink is already relative, we don't
103  # want to touch it.
104  continue
105 
106  new_target = os.path.relpath(existing_target, local_dir)
107  os.unlink(path)
108  os.symlink(new_target, path)
def find_script_files(venv_dir)
Definition: relocate.py:31
def fix_activate_path(venv_dir, target_dir)
Definition: relocate.py:59
def fix_local_symlinks(venv_dir)
Definition: relocate.py:81
def run_command(cmd, args, kwargs)
Definition: __init__.py:39
def fix_shebangs(venv_dir, target_dir)
Definition: relocate.py:47


catkin_virtualenv
Author(s): Paul Bovbel
autogenerated on Thu Apr 1 2021 02:36:27