Re: [Tutor] Correct Way to Start a Python Program Under Linux

2007-01-19 Thread pytutor . 20 . 247ob
 pytutor.20.247ob at spamgourmet.com wrote
 
 I'm not sure what you mean by a filelauncher.
 In my window manager I just add the file path to my menu
 by editing a dialog box or config file. which environment are you 
 using?

Im using gnome and creating a 'custom application launcher' by rt click
a panel and choosing 'add to panel'. It is just a case of adding a file
path, the only option is you can run in a terminal (which works but
fires up a terminal window which I do not want)

  my program uses subprocess.Popen({command string}, etc)
  and these calls to Popen do not work, only if I run the program
 from 
  a
  shell window.
 
 How do you know it doesn't work? Do you get an error or just silence?
 If you launch a program that writes to stdout then you had better
 be sure it has a stdout to write to! Are you sure its not just
 dumping its output to /dev/null?

when it's working you can see the processes created by doing 'ps -ef'
in another shell window, the python program spawns a child process
running tcpdump.

 OTOH it could also be that the
 import
 path is not set correctly(try checking your PYTHONPATH environment
 variable)

from a shell window typing echo $PYTHONPATH returns nothing, is this
normal, should I be setting this or is it optional? 
from within a python session importing sys and typing  sys.path
I get a long list of paths which I assume is right as its searching and
finding all the standard library modules.

 Can you give us a bit more info about what you mean by the
 statement that it doesn't work?

I mean the call to subprocess.Popen  should create a new process
when viewed with 'ps -ef' from another command window, normally I see
the new process running the command 'tcpdump .' but when running
the application from the launcher this just does not happen. The main
pygtk program is running fine, just the new process does not start.

 As a check try writing a very simple program with no external
 dependencies and see if it works...

You have helped crack it, I just wrote a small program like this, it
reads 400 packets from the eth0 interface saving it to a file:

code
#!/usr/bin/env python
#test prog
import curses.ascii, sys, os, subprocess, tempfile
def Readtraffic():
myerr = tempfile.TemporaryFile()
myout = tempfile.TemporaryFile()
command = sudo /usr/sbin/tcpdump -c 400 -q -t -s 192 -i eth0 -w
/var/tmp/testdump -Z wayne
myproc = subprocess.Popen(command, executable=/bin/bash,
shell=True, stderr=myerr, stdout=myout)
return (myproc)

proc = Readtraffic()
stat = proc.poll()
print stat =  + str(stat)

while stat == None:
stat = proc.poll()
print stat =  + str(stat)
print finished
/code

This works fine, using another shell window I can see the subprocess
and tcpdump running under a new pid as expected, it's VERY similar to
the code in the program (which has been chopped about quite a bit by
now) with the difference being I changed the variable 'command' to be a
pure text string instead of a concatenation of variables and strings.
 
Substituting the code above into my program works! 
Now the puzzling bit identifying the actual error,

the code below works until I un-comment the line below '#oldcommand
...', I have even changed the variable name to 'oldcommand' so in
effect it's not even used, but just uncommenting it causes the problem,
ie, the subprocess does not start up when run from an application
launcher (yet still works when started from a shell!).

code
#oldcommand = sudo  + self.exe['tcpdump'] +  -c  + str(packets) + 
-q -t -s  + str(packetsize) \
#+  -i  + self.nic +  -w  + self.savepath + self.dumpfilename + 
-Z  + os.getlogin()
command = sudo /usr/sbin/tcpdump -c 400 -q -t -s 192 -i eth0 -w
/var/tmp/testdump -Z wayne
myproc = subprocess.Popen(command, executable=/bin/bash, shell=True,
stderr=myerr, stdout=myout)
return (myproc)
/code

After going through the command piece by piece I found the problem, the
call to os.getlogin() works fine when run from a shell BUT fails when
run from a launcher, back to the docs I found:

user = pwd.getpwuid(os.getuid()) [0]

and this works!!!

Thanks for your help,

Wayne.




___ 
What kind of emailer are you? Find out today - get a free analysis of your 
email personality. Take the quiz at the Yahoo! Mail Championship. 
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Correct Way to Start a Python Program Under Linux

2007-01-18 Thread pytutor . 20 . 247ob
Hi,
Please help I'm at my wits end...

My program comprises of 2 modules and runs a gui written in pygtk2, I
have at the beginning of each:

#!/bin/env python

I made it executable (chmod a+x)

I can run them from a terminal window like

$ /path/to/file.py

this works fine and dandy no probs at all but ultimately I want to
share this program and I want people to launch the program directly
from a menu and not a shell.

now if I create a file launcher on the task bar that points directly to
the same file it runs and seems to be ok except..
my program uses subprocess.Popen({command string}, etc)
and these calls to Popen do not work, only if I run the program from a
shell window.

So am I doing something wrong please tell me? 
I have spent hours and hours debugging and changing the code in
different ways and always the same, I cannot get a subprocess to spawn
into another process unless I run the program from a shell window
,please help!

Wayne.



___ 
What kind of emailer are you? Find out today - get a free analysis of your 
email personality. Take the quiz at the Yahoo! Mail Championship. 
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Subprocess popen problem depending on how I start program

2007-01-16 Thread pytutor . 20 . 247ob
Hi,
I'm writing a program that downloads audio streams from the net, to do
this I use mplayer and tcpdump, which I kick off as detached processes
(if thats the correct terminology), I do it this way because i'm using
pygtk and I dont want the program to wait for the processes to finish
otherwise the app would appear to freeze.
I have written 2 modules, one contains a class and methods that do all
the work, the other is the gui module, I've got it working fairly well
at least when I start the program from within a bash shell (Im using
Ubuntu Linux BTW) The modules both have a line '#!/usr/bin/env python'
at the top and are set as executable too so I can run them from a bash
shell.
Now I have one eye on distributing the modules in the future I'm
thinking how people will start the app, and I discover that if I use
the 'run application' program (alt F2 in most linux's I think) to call
the module it starts ok and works normally until I attempt to use the
part that starts the subprocess in the background, it just does not
work and the gui continues to respond as if nothing ever happened. When
I check the processes running I can see that it has not started a sub
shell to run the command. also tried other things like creating an
application launcher, this will work if I tick 'run in terminal' but
not otherwise.
I've also started learning about distutils and installed the modules on
my system using it, then created a python script (made it executable)
and tried using this to start the program, same problem.

I'm scratching around trying to find an answer but I'm at a loss now so
would sure appreciate some help, thanks.

Wayne..
ps. sorry if it's wordy, I want to give enough info. if you need
anymore info just say.





___ 
New Yahoo! Mail is the ultimate force in competitive emailing. Find out more at 
the Yahoo! Mail Championships. Plus: play games and win prizes. 
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor