subprocess.Popen on android

1,934 views
Skip to first unread message

Matthias

unread,
Jul 13, 2012, 7:30:51 PM7/13/12
to python-...@googlegroups.com
Hi!

I am trying to write a android app using kivy and python for android. It is great to see how easy it is finally to program in python for android. Thanks for this project! However, I am experiencing a problem at the moment: I want to execute another program from my python code. The other program is packed within the app (it is "git" from the Terminal IDE app, I just copied the executables). I install the app onto the phone, not on the SDcard, as it is not possible to execute from the SDcard (see Terminal IDE). However, I have not managed to execute git. I try to run it like this:

    output,error = subprocess.Popen(['./git', 'status'],stdout=PIPE,stderr=PIPE).communicate()

However, I get an "permission denied" error:

    I/python  ( 9649):  OSError: [Errno 13] Permission denied

the executable is found and is executable (755). Do you have an idea why this fails? Any help would be great! If it helps: I am using a Galaxy S running Android 2.3.7.

Regards,
  Matthias

Mathieu Virbel

unread,
Jul 14, 2012, 5:39:02 AM7/14/12
to python-...@googlegroups.com
Hi,

I've absolutely no idea, and don't even know if you can do that. The
whole android system/application are based on java security. Fork is
allowed (but you must be careful about it), but about the ability to
start a new process, maybe it's just forbidden by android.

Could you search more about it?

Mathieu

Matthias

unread,
Jul 14, 2012, 5:57:45 PM7/14/12
to python-...@googlegroups.com
Hi!


Am Samstag, 14. Juli 2012 11:39:02 UTC+2 schrieb Mathieu Virbel:
I've absolutely no idea, and don't even know if you can do that. The
whole android system/application are based on java security. Fork is
allowed (but you must be careful about it), but about the ability to
start a new process, maybe it's just forbidden by android.

Could you search more about it?
Well, it should be possible, as Terminal IDE manages to start executables. For another example see [1]. Also on the market page of Terminal IDE they write:
  "This app CANNOT be installed onto the SDCard because you do not get 'Execute' permissions on that mount. It must run in this app's private memory on the device" [2]. On [3] the precise android commands to execute subprocesses are listed. Thus it should be definitly possible to start other commands. I have no idea how python's subprocess module is compiled for android. Maybe here something more sophisticated is needed? I.e., the use of android's runtime object?

By the way, I also tried to use os.system('./git status &> test.log'). It returned with code 0, but test.log did not contain anything. Thus this doesn't seem to work either.

Regards,
  Matthias

[1] http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App
[2] https://play.google.com/store/apps/details?id=com.spartacusrex.spartacuside&hl=de
[3] http://developer.android.com/reference/java/lang/Runtime.html

qua-non

unread,
Jul 15, 2012, 6:51:53 AM7/15/12
to python-...@googlegroups.com

Hello Matthias,

    My own experience has been a bit confusing in this regard, I remember trying out my console app
(used subprocess.popen) in android quite a while ago and was flabbergasted when it just worked
(without setting any permissions or anything if I remember correctly). So it works and this was
 just a simple subproess.popen without accessing any Java functions. As it was just a test and I
 got busy with other things,  I never followed it up.

After looking at this thread I was reminded of the console app. Since then the console app had been significantly
re-factored, with no tests on android. Also the android build chain has changed significantly and has evolved into
python for android. So when I tried packaging it, this time; it doesn't work. (gives me permission denied on
commands that don't exist, and gets stuck trying to execute commands like `ls`).

I don't know the reason why it doesn't work now, could be a number of things, the re factoring of the console app
to use pipes or something about the new build chain, or maybe just about giving the right permissions.

I'm including a link to the original test apk that still works. https://docs.google.com/file/d/0B1WO07-OL50_cXp1Vm8yNUt0aHc/edit
To test, select the text input on the bottom and type command like `ls` or `ps aux` or `whoami` ...

Hope this helps.

qua-non

unread,
Jul 24, 2012, 4:48:44 PM7/24/12
to python-...@googlegroups.com
I think I misunderstood what you guys were talking about. I see now that it's a lot easy to run the built in available commands in `/system` directory hence the commands like ls or `uname` or `whoami`... work fine but running your own bundled command is a whole other deal.

qua-non

unread,
Jul 25, 2012, 3:58:46 PM7/25/12
to python-...@googlegroups.com
It works :),  I am able to run `git` executable. These are the steps I followed.

1) Copied the system folder from TerminalIDE to 'python for android/dist/default/private' folder.
2) Rebuild the apk and install
3) While running the apk "cd" into the `/data/data/org.test.k_console/files/system/bin`.
    (`org.test.k_console` is the name I gave to the build.py). Note Terminal IDE also uses
    apps private directory to put the system directory in).
4) Run './git' <- this gives a permission denied error.
5) Run chmod 744 git. Now running ./git prints out the usage/help text.

I didn't change the permissions before putting the files in the private dir so am assuming that
If I changed the file permissions before hand I wouldn't need to run chmod at run time.

Matthias

unread,
Aug 31, 2012, 11:12:54 AM8/31/12
to python-...@googlegroups.com
Hi!

Thanks for your help. It does work now for me, too. Probably, I messed up something with the permissions. Now the next problem arises: How can I git tell to use ssh with some public key? I did not yet manage to do this, but I will keep trying. Apparently, there is an environmental variable for this, but so far it does not work...

Regards,
  Matthias
Reply all
Reply to author
Forward
0 new messages