Skip to content

Instantly share code, notes, and snippets.

@Erotemic
Created October 25, 2017 17:24
Show Gist options
  • Save Erotemic/a4c437d823ccc1f3097d68d0f43fc39b to your computer and use it in GitHub Desktop.
Save Erotemic/a4c437d823ccc1f3097d68d0f43fc39b to your computer and use it in GitHub Desktop.
#!/bin/bash
codeblock()
{
if [ "-h" == "$1" ] || [ "--help" == "$1" ]; then
# Use codeblock to show the usage of codeblock, so you can use
# codeblock while you codeblock.
echo "$(codeblock '
Unindents code before its executed so you can maintain a pretty
indentation in your code file. Multiline strings simply begin
with
"$(codeblock "
and end with
")"
Example:
echo "$(codeblock "
a long
multiline string.
this is the last line that will be considered.
")"
')"
else
# Prevents python indentation errors in bash
#python -c "from textwrap import dedent; print(dedent('''$1''').strip('\n'))"
echo "$1" | python -c "import sys; from textwrap import dedent; print(dedent(sys.stdin.read()).strip('\n'))"
fi
}
patch_venv_with_libs(){
# References:
# https://github.com/pypa/virtualenv/pull/1045
ACTIVATE_SCRIPT=$VIRTUAL_ENV/bin/activate
# apply the patch
python -c "$(codeblock "
import textwrap
text = open('$ACTIVATE_SCRIPT', 'r').read()
lines = text.splitlines(True)
def absindent(text, prefix=''):
text = textwrap.dedent(text).lstrip()
text = prefix + prefix.join(text.splitlines(True))
return text.splitlines(True)
if len(lines) > 80:
# hacky way of preventing extra patches
print('file is already patched')
else:
print('patching')
new_lines = []
for old_lineno, line in enumerate(lines, start=1):
new_lines.append(line)
if old_lineno == 10:
new_lines.extend(absindent('''
C_INCLUDE_PATH=\"\$_OLD_C_INCLUDE_PATH\"
LD_LIBRARY_PATH=\"\$_OLD_VIRTUAL_LD_LIBRARY_PATH\"
''', ' ' * 8))
elif old_lineno == 11:
new_lines.extend(absindent('''
export C_INCLUDE_PATH
export LD_LIBRARY_PATH
unset _OLD_C_INCLUDE_PATH
unset _OLD_VIRTUAL_LD_LIBRARY_PATH
''', ' ' * 8))
elif old_lineno == 46:
new_lines.extend(absindent('''
_OLD_C_INCLUDE_PATH=\"\$C_INCLUDE_PATH\"
_OLD_VIRTUAL_LD_LIBRARY_PATH=\"\$LD_LIBRARY_PATH\"
'''))
elif old_lineno == 47:
new_lines.extend(absindent('''
C_INCLUDE_PATH=\"\$C_INCLUDE_PATH/include:\$C_INCLUDE_PATH\"
LD_LIBRARY_PATH=\"\$VIRTUAL_ENV/lib:\$LD_LIBRARY_PATH\"
'''))
elif old_lineno == 48:
new_lines.extend(absindent('''
export C_INCLUDE_PATH
export LD_LIBRARY_PATH
'''))
new_text = ''.join(new_lines)
open('$ACTIVATE_SCRIPT', 'w').write(new_text)
")"
}
patch_venv_with_libs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment