Skip to content

Commit

Permalink
Merge pull request #33 from paparomeo/avoid-temporary-file
Browse files Browse the repository at this point in the history
Avoid temporary file
  • Loading branch information
edc committed Aug 24, 2016
2 parents cba21b3 + 1970a38 commit 3e4c13c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
66 changes: 33 additions & 33 deletions functions/__bass.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import json
import subprocess
import sys
import tempfile


BASH = 'bash'
Expand All @@ -28,7 +27,7 @@ def gen_script():
output = subprocess.check_output(args, universal_newlines=True)
old_env = output.strip()

command = '{}; echo "{}"; {}'.format(' '.join(sys.argv[1:]), divider, env_reader)
command = '{} && (echo "{}"; {})'.format(' '.join(sys.argv[1:]), divider, env_reader)
args = [BASH, '-c', command]
output = subprocess.check_output(args, universal_newlines=True)
stdout, new_env = output.split(divider, 1)
Expand All @@ -39,42 +38,43 @@ def gen_script():

skips = ['PS1', 'SHLVL', 'XPC_SERVICE_NAME']

with tempfile.NamedTemporaryFile('w', delete=False) as f:
for line in stdout.splitlines():
f.write("printf '%s\\n'\n" % line)
for k, v in new_env.items():
if k in skips:
script_lines = []

for line in stdout.splitlines():
script_lines.append("printf '%s\\n'" % line)
for k, v in new_env.items():
if k in skips:
continue
v1 = old_env.get(k)
if not v1:
script_lines.append('# adding %s=%s' % (k, v))
elif v1 != v:
script_lines.append('# updating %s=%s -> %s' % (k, v1, v))
# process special variables
if k == 'PWD':
script_lines.append('cd "%s"' % v)
continue
v1 = old_env.get(k)
if not v1:
f.write('# adding %s=%s\n' % (k, v))
elif v1 != v:
f.write('# updating %s=%s -> %s\n' % (k, v1, v))
# process special variables
if k == 'PWD':
f.write('cd "%s"' % v)
continue
else:
continue
if k == 'PATH':
# use json.dumps to reliably escape quotes and backslashes
value = ' '.join([json.dumps(directory)
for directory in v.split(':')])
else:
# use json.dumps to reliably escape quotes and backslashes
value = json.dumps(v)
f.write('set -g -x %s %s\n' % (k, value))

return f.name
else:
continue
if k == 'PATH':
# use json.dumps to reliably escape quotes and backslashes
value = ' '.join([json.dumps(directory)
for directory in v.split(':')])
else:
# use json.dumps to reliably escape quotes and backslashes
value = json.dumps(v)
script_lines.append('set -g -x %s %s' % (k, value))
script = '\n'.join(script_lines)
return script

if not sys.argv[1:]:
print('__usage')
print('__usage', end='')
sys.exit(0)

try:
name = gen_script()
script = gen_script()
except Exception as e:
sys.stderr.write(str(e) + '\n')
print('__error')
print('exit code:', e.returncode, file=sys.stderr)
print('__error', end='')
else:
print(name)
print(script, end='')
11 changes: 5 additions & 6 deletions functions/bass.fish
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ function bass
set -e __bash_args[1]
end

set -l __script (python (dirname (status -f))/__bass.py $__bash_args)
if test $__script = '__usage'
python (dirname (status -f))/__bass.py $__bash_args | read -z __script
if test "$__script" = '__usage'
echo "Usage: bass [-d] <bash-command>"
else if test $__script = '__error'
else if test "$__script" = '__error'
echo "Bass encountered an error!"
else
source $__script
echo -e "$__script" | source -
if set -q __bass_debug
cat $__script
echo "$__script"
end
rm -f $__script
end
end

0 comments on commit 3e4c13c

Please sign in to comment.