0

I am trying the solution given by xApple in this given link: Calling the "source" command from subprocess.Popen

I am calling the shell_source() generator in the script as follows:

def shell_source():
    statements
shell_source("./myscript.sh")

when I run the python script, errors I am getting:

./script.sh: 88: Syntax error: end of file unexpected (expecting "then")
Traceback (most recent call last):
  File "trial.py", line 20, in <module>
    shell_source('./myscript.sh')
  File "trial.py", line 17, in shell_source
    env = dict((line.split("=", 1) for line in output.split('\x00')))
ValueError: dictionary update sequence element #0 has length 1; 2 is required

but its not working. Am I doing correct? Also, I went through the comments to do editings but not that clear. A snapshot of the execution will help me a lot.

On request, python script(trial.py):

#!/usr/bin/python
import os
import subprocess
import commands

def shell_source(script):
    import subprocess, os
    pipe = subprocess.Popen("source %s; env" % script, stdout=subprocess.PIPE, executable="/bin/csh" ,shell=True)
    output = pipe.communicate()[0]
    env = dict((line.split("=", 1) for line in output.splitlines()))
    os.environ.update(env)
shell_source('myscript.sh')

myscript.sh:

setenv PROJ_ROOT $PWD
setenv PROJ_OS freebsd
setenv OS freebsd
Community
  • 1
  • 1
Dr. Essen
  • 603
  • 2
  • 9
  • 25
  • What are the last few lines of `./script.sh` ? Sounds like there's an `if` statement without a `then` statement. – Mark Plotnick Aug 05 '16 at 20:46
  • if `uname -r | grep -c "8.4-RELEASE"` == 1 setenv ICP_WITHOUT_THREAD 1 if `uname -r | grep -c "8.4-RELEASE"` == 1 setenv 32_USER_64_KERNEL 1 cd ./quickteck/freebsd/sys/modules/common/ ./linux_links.sh mkdir _linux cd _linux/ tar -xvf ../linux_files.tar.gz cd $ROOT ln -s /usr/src/sys/i386/include $BUILDSYSTEM_PATH/machine Above lines are present in the code. when I do source ./script, it works fine. – Dr. Essen Aug 06 '16 at 10:13
  • I think thats not an issue, a/c to this , my script is correct. – Dr. Essen Aug 06 '16 at 10:25
  • 1
    Ah, you have a csh or tcsh script, but Popen is going to use /bin/sh by default. In the linked question, J.F. Sebastian suggests adding an `executable=` parameter to the Popen call, so try `executable="/bin/tcsh"` (or whatever your shell's pathname is) – Mark Plotnick Aug 06 '16 at 17:47
  • Am I calling the generator correct? passing filename? like shell_source("./myscript.sh") ? – Dr. Essen Aug 08 '16 at 15:00
  • 2
    You're calling shell_source correctly, but it assumes you're running /bin/sh. Edit it to change `subprocess.Popen(". %s; env" % script, stdout=subprocess.PIPE, shell=True)` to `subprocess.Popen("source %s; env" % script, stdout=subprocess.PIPE, executable="/bin/tcsh", shell=True)` (where /bin/tcsh is the path to your csh or tcsh shell) – Mark Plotnick Aug 08 '16 at 23:48
  • It worked, but some commands are working except lines containing 'setenv' in the script. :@#%$ :/ I am missing something but don't know what? Does "setenv" works like this? – Dr. Essen Aug 09 '16 at 06:45
  • Can you edit your question to include all the code in `shell_source` and a small portion of your `./myscript.sh` file that demonstrates the problem? – Mark Plotnick Aug 09 '16 at 13:43
  • @MarkPlotnick, added. – Dr. Essen Aug 10 '16 at 04:48
  • Hmm. Your code works fine for me, after moving the line `shell_source('myscript.sh')` out of the function definition, moving it to top level. For instance, `print os.environ['PROJ_OS']` gave the desired output. – Mark Plotnick Aug 10 '16 at 19:45
  • Sorry for the indentation error in the question. but I ran correctly as you did. Hey, Isn't it related to OS, like I am running this on freeBSD. – Dr. Essen Aug 11 '16 at 11:30
  • Your code runs correctly for me - the environment gets set based on the setenv commands in the csh script - on both Ubuntu 14.04 Linux and on FreeBSD 10.3. Both using python 2.7. What OS version are you running where things are not working as expected? – Mark Plotnick Aug 11 '16 at 12:24
  • I am working in FreeBSD 10.2. – Dr. Essen Aug 23 '16 at 07:20
  • Your shell and python script work correctly for me on FreeBSD 10.2 as well, using the python2.7 from ports; if I add `print os.environ['PROJ_OS']` to the end of the python script, it prints `freebsd`. Sorry I couldn't reproduce your problem. – Mark Plotnick Aug 23 '16 at 22:19
  • It's fine. I will try some other alternative. Thanks! – Dr. Essen Aug 24 '16 at 09:33

0 Answers0