views:

332

answers:

4

The following code:

gb = self.request.form['groupby']
typ = self.request.form['type']
tbl = self.request.form['table']

primary = self.request.form.get('primary', None)

if primary is not None:
    create = False
else:
create = True

mdb = tempfile.NamedTemporaryFile()
mdb.write(self.request.form['mdb'].read())
mdb.seek(0)

csv = tempfile.TemporaryFile()
conversion = subprocess.Popen(("/Users/jondoe/development/mdb-export", mdb.name, tbl,),stdout=csv)

Causes the this error when calling the last line i.e. 'conversion =' in OS X.

Traceback (innermost last):
  Module ZPublisher.Publish, line 119, in publish
  Module ZPublisher.mapply, line 88, in mapply
  Module ZPublisher.Publish, line 42, in call_object
  Module circulartriangle.mdbtoat.mdb, line 62, in __call__
  Module subprocess, line 543, in __init__
  Module subprocess, line 975, in _execute_child
OSError: [Errno 13] Permission denied

I've tried chmod 777 /Users/jondoe/development/mdb-export - what else might be required?

A: 

You also need to ensure read and execute permissions for the user running that code on the directories up the chain - /Users, /Users/jondoe and /Users/jondoe/development.

samtregar
I made another folder /mdb-export in root (didn't fancy setting chmod 777 on /Users) and chmod'ed it to 777. Same error still.
Jon Hadley
+1  A: 

Can you feed "sudo" to subprocess? See this SO thread.

@Jon Hadley, from the interpreter:

>>> import subprocess
>>> p = subprocess.call(['sudo','/usr/bin/env'])
PASSWORD:
[snip]

USER=root
USERNAME=root
SUDO_COMMAND=/usr/bin/env
SUDO_USER=telliott99
SUDO_UID=501
SUDO_GID=20

From Terminal on OS X, I have to do sudo when I run the script:

$ sudo python test.py

then this (in test.py) gives the same output as before:

import subprocess
p = subprocess.Popen('/usr/bin/env')

Getting subprocess to directly handle the authentication from a script is probably not a good idea, since it hides the privilege escalation. But you could look at pexpect and this SO answer.

telliott99
Giving that a try. Is there a way to see what user my Python process is running as?
Jon Hadley
@Jon Hadley>>> import os>>> os.getenv('USER')'telliott99'
telliott99
A: 

Assuming that permissions on parent folders are correct (i.e. all parent folders should have +x permission), try adding:

shell=True

to the Popen command such as:

subprocess.Popen(("/Users/jondoe/development/mdb-export", mdb.name, tbl,), stdout=csv, shell=True)
Unode
A: 

It seems the 'Permissions denied error' was orginally coming from Popen trying to execute mdb-export from the wrong location (and to compound things, with the wrong permissions).

If mdbtools is installed, the following works fine and inherits the correct permissions without the need for sudo etc.

subprocess.Popen(("mdb-export", mdb.name, tbl,),stdout=csv)

(Worth noting, I got myself into a muddle for a while, having forgotten that Popen is for opening executables, not folders or non-exectable files in folders)

Thanks for all your responses, they all made for interesting reading regardless :)

Jon Hadley