python - Are there any modules to read shell scripts? -


i'm making python script right now, , need use environment variables set in bash shell script.

the bash script like:

#! /bin/sh  #sets names: export distro="unified" #export distro="other"  #number of parallel builds export bb_num_threads=2  #set build dir export builddir=$pwd 

normally, source script in bash, go builds. i'm trying wrap python around whole process management of output want remove manual source ./this_script.sh step.

what want read script python , use os.environ set variables within it. (i know not affect parent, current running python instance , that's fine)

so make work easier, i'm trying find out there modules can "parse" bash script , make use of environment variables found within? i'm doing hand , it's bit of pain.

if no such module exists exactly want, there more pythonic (read: easier/shorter) way of manually parsing file in general, right i'm doing this:

def parse_bash_script(fn):   open(fn) f:     line in f:       if not line[:1] == '#':   #ignore comments         if "export" in line:           line = line.replace(" ","").strip()           var = line[6:line.find("=")]           val = line[line.find("=")+1:len(line)]           if "\"" in val:             val = val[1:-1]           os.environ[var]=val 

there no module want, shlex lot of want. in particular, quoting, etc. right without having worry (which hardest part of this), skipping comments, etc. thing won't handle export keywords.

the easy way around preprocess:

with open(fn) f:     processed = f.read().replace('export ', '') line in shlex.split(processed):     var, _ value = line.partition('=')     os.environ[var] = val 

it's bit hackier, can bit less verbosely post-processing. in particular, shlex treat export foo="bar spam eggs" 2 values: export , foo="bar spam eggs", , can skip ones == 'export', or partition finds nothing, or… example:

with open(fn) f:     line in shlex.split(f.read()):     var, eq, value = line.partition('=')     if eq:         os.environ[var] = val 

if want fancier, can construct shlex object , (a) drive parser directly file, , (b) control parsing @ finer-grained level. however, don't think that's necessary here.


meanwhile, if want handle environment substitution (as builddir=$pwd implies), won't magically take care of you. can make configparser extendedinterpolation feature, you'll need trick configparser handling shlex syntax, @ point… why bother.

you can of course manually writing own interpolator, that's hard right. need know shell's rules why $pwd-foo same ${pwd}-foo, $pwd_foo same ${pwd_foo}, etc.

a better solution @ point—assuming script safe run—would use shell you. example:

with open('script.sh') f:     script = f.read() script += b'\nenv' subprocess.popen(['sh'], stdin=subprocess.pipe, stdout=subprocess.pipe) p:     result = p.communicate(script) line in result.splitlines():     var, _, value = line.partition('=')     os.environ[var] = value 

of course override things _=/usr/bin/env, not care about.


Comments