Skip to content

Instantly share code, notes, and snippets.

@kergoth
Created February 26, 2020 23:16
Show Gist options
  • Save kergoth/09de93347bb62238ad5bede58e41bcd4 to your computer and use it in GitHub Desktop.
Save kergoth/09de93347bb62238ad5bede58e41bcd4 to your computer and use it in GitHub Desktop.
def shell_get_vars(d, bbvars):
"""Return var/value pairs for use in a shell while loop.
Example usage:
while read -r var value; do
done <<END
$'{@shell_get_vars(d, 'FOO BAR')}
END
"""
if isinstance(bbvars, str):
bbvars = bbvars.split()
lines = []
for var in bbvars:
lines.append('{0} {1}\n'.format(var, d.getVar(var, True)))
return ''.join(lines)
def shell_vars(d, bbvars, indent=' '):
"""Return var=value lines to set them as shell variables."""
import subprocess
if isinstance(bbvars, str):
bbvars = bbvars.split()
lines = []
for var in bbvars:
value = d.getVar(var, True)
lines.append('{0}{1}={2}\n'.format(indent, var, subprocess.list2cmdline([value])))
return ''.join(lines)[len(indent):]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment