Commit 7d73caa0 authored by Julien Muchembled's avatar Julien Muchembled

Optimize wrapper to scripts with long shebangs

parent 339b320e
......@@ -409,16 +409,18 @@ class Recipe(object):
continue
for f in os.listdir(dir_abspath):
f_abspath = os.path.join(dir_abspath, f)
if not os.path.isfile(f_abspath):
st_mode = os.stat(f_abspath).st_mode
if not stat.S_ISREG(st_mode):
continue
header = file(f_abspath).readline(128)
m = re.match('^#! ?(/.*)', header)
if not m or len(header) < 128:
continue
shebang_abspath = f_abspath + '.shebang'
os.rename(f_abspath, shebang_abspath)
file(f_abspath, 'w').write('''#!/bin/sh
shebang=$(head -1 "$0.shebang")
exec ${shebang#\#!} "$0.shebang" "$@"
with open(f_abspath) as f:
header = f.readline(128)
if header.startswith('#!') and header[-1] != '\n':
os.rename(f_abspath, f_abspath + '.shebang')
with open(f_abspath, 'w') as f:
os.fchmod(f.fileno(), stat.S_IMODE(st_mode))
f.write('''#!/bin/sh -e
read -r EXE ARG < "$0.shebang"
EXE=${EXE#\\#!}
[ "$EXE" ] || read -r _ EXE ARG < "$0.shebang"
exec $EXE ${ARG:+"$ARG"} "$0.shebang" "$@"
''')
os.chmod(f_abspath, stat.S_IMODE(os.stat(shebang_abspath).st_mode))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment