Skip to content

Commit

Permalink
Merge pull request spotify#317 from StackStorm/upstream
Browse files Browse the repository at this point in the history
Also fix 'python' shebang lines
  • Loading branch information
nailor authored Dec 13, 2020
2 parents f57cd42 + 77d8b42 commit 609bb49
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions dh_virtualenv/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,20 @@ def run_tests(self):
def find_script_files(self):
"""Find list of files containing python shebangs in the bin directory"""
command = ['grep', '-l', '-r',
'-e', r'^#!.*bin/\(env \)\?{0}'.format(_PYTHON_INTERPRETERS_REGEX),
'-e', r'^#!\({0}\|.*bin/\(env \)\?{0}\)'.format(_PYTHON_INTERPRETERS_REGEX),
'-e', r"^'''exec.*bin/{0}".format(_PYTHON_INTERPRETERS_REGEX),
self.bin_dir]
grep_proc = subprocess.Popen(command, stdout=subprocess.PIPE)
files, stderr = grep_proc.communicate()
return set(f for f in files.decode('utf-8').strip().split('\n') if f)

def fix_shebangs(self):
"""Translate /usr/bin/python and /usr/bin/env python shebang
"""Translate '/usr/bin/python', '/usr/bin/env python', and 'python' shebang
lines to point to our virtualenv python.
"""
pythonpath = os.path.join(self.virtualenv_install_dir, 'bin/python')
for f in self.find_script_files():
regex = (r's-^#!.*bin/\(env \)\?{names}\"\?-#!{pythonpath}-;'
regex = (r's-^#!\({names}\|.*bin/\(env \)\?{names}\"\?\)-#!{pythonpath}-;'
r"s-^'''exec'.*bin/{names}-'''exec' {pythonpath}-"
).format(names=_PYTHON_INTERPRETERS_REGEX, pythonpath=re.escape(pythonpath))
p = subprocess.Popen(
Expand Down
5 changes: 5 additions & 0 deletions doc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Unreleased
==========

* Fix --verbose and --setuptools command line argument usage together with --builtin-venv
* Also fix ``#!python`` shebang lines
(`#317 <https://github.com/spotify/dh-virtualenv/pull/317>`_)
[`@blag <https://github.com/blag>`_, `@Kami <https://github.com/Kami>`_,
`@dennybaa <https://github.com/dennybaa>`_, and
`@StackStorm contributors <https://github.com/StackStorm>`_]

1.2.2
=====
Expand Down
8 changes: 8 additions & 0 deletions test/test_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ def check_shebangs_fix(interpreter, path):

deployment.fix_shebangs()

with open(temp.name) as f:
eq_(f.readline(), expected_shebang)

with open(temp.name, 'w') as f:
f.write('#!{0}\n'.format(interpreter))

deployment.fix_shebangs()

with open(temp.name) as f:
eq_(f.readline(), expected_shebang)

Expand Down

0 comments on commit 609bb49

Please sign in to comment.