0

I am working on a program that will find some files and provide the file information to a NSIS script. The NSIS script accepts the command line as follows

makensis.exe /DON="This is one" /DOD="c:\path1\path2 to dir\path 3" scriptfile.nsi

The values of the switches will change on each execution of the program. I have tried to get this to execute using subprocess.call and subprocess.Popen. The issue I am having has to do with quoting.

First of all the subprocess calls seem to put the entire argument statement between double quotes making NSIS see them as one argument. Second I am having some difficulty getting the individual switches properly quoted on the command line. Here is a snippet of what my program currently looks like.

subprocess.Popen([setup.profile['NSISExe'], ' /DON="' + setup.profile['DESC'] + '" /DOD="' + setup.profile['InstallDir'] + \
   '" /DMT="' + app.machine_type.get() + '" /DSD="' + os.path.join(WinShellVar.LOCAL_APPDATA, 'MLC CAD', appname) + \
   '" /DXV=X6 ' + setup.profile['NSISScript']])

And here is the output from NSIS

    Can't open script " /DON="Mastercam X6 Standard" /DOD="C:\Users\John\Desktop" /D
MT="mill" /DSD="C:\Users\John\AppData\Local\MLC CAD\mcdeftool" /DXV=X6 bin\packa
ge.002.nsi"

As you can see I am using a mixed bag of data, getting some bits for dicts and some from class calls (be easy on me if my terms are somewhat incorrect, I have been learning python for about 4 days now, correct me please just nicely). If using this data like this is "unpythonic" let me know.

Looking forward to your input

2
  • As a note of style, you don't need a trailing \ to continue a line in python if there is some sort of unterminated bracket hanging around. e.g. ( [ { Commented Feb 13, 2013 at 2:24
  • re: my previous comment -- Further explanation can be found in the python style guide Commented Feb 13, 2013 at 2:29

1 Answer 1

2

disclaimer -- I don't use windows


I think you probably want something like:

subprocess.Popen([setup.profile['NSISExe'], '/DON=' + setup.profile['DESC'],
                  '/DOD=' + setup.profile['InstallDir'],
                  '/DMT=' + app.machine_type.get(),
                  '/DSD=' + os.path.join(WinShellVar.LOCAL_APPDATA, 'MLC CAD', appname), 
                  '/DXV=X6',
                  setup.profile['NSISScript']])

When the shell reads the commandline, it splits on non-quoted, non-escaped whitespace. When you pass a list to Popen, it expects the list elements to be the way it would look after the shell split the arguments. The other option is to pass a string (instead of a list) exactly as you would put it into the windows shell and pass shell=True to Popen. But that method isn't preferred as it is much more vulnerable to shell-injection insecurities.

Sign up to request clarification or add additional context in comments.

2 Comments

This works! I see the problem was I was trying to give python to much help. What an awesome language, loving it so far!
@JohnMcCord -- It only gets better :). I've been working with it for maybe 2 years now and I grow to enjoy it more and more each day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.