Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-28-2004, 11:47 AM
|
#1
|
|
Member
Registered: Oct 2003
Distribution: Mandrake 10
Posts: 160
Rep:
|
Python question
Hello,
I'm attempting to modify Xtart to provide a logout and a halt option. Once these are working, I'll add in a loop-until-exit. My idea is that a user can run this from text mode as a pseudo-dm (like gdm); it could even be invoked from the .login file.
The file is below; I bolded my changes from the original.
I don't know Python. My questions are
1) What is the correct os.execvp syntax for logout? (It complains that it can't find "logout", since logout is in fact an internal bash command.) Is there an alternative I can use? (By logout, I mean logout to the text mode login prompt.)
2) What's the correct os.execvp syntax for shutdown? The syntax below succesfully runs sudo; however, sudo then complains that the user isn't in the sudoers file, which isn't the case.
Throw in any other useful mods you come up with.
Thanks!
Code:
#!/usr/bin/env python
# This is a modification of Xtart, distributed with Mandrake 10, to provide a
# shutdown/logout option.
# This is an official linux-mandrake extension of the X windowing system for
# the benefit of Declan Moriarty Copyright 2001 by civileme@mandrakesoft all
# rights reserved. Use is hereby granted under the GNU General Publoic
# License version 2 or any future version of said license at the user's option
#
# No Warranty of course.
# This program is designed for those who want to work in concole and
# occasionally boot into window managers or who want to avoid theme
# persistence from one WM to another.
#
#
import sys
import os
import popen2
import string
def complain():
print
print 'What do you expect me to do with that?'
print
sys.exit(0)
return
print
print ' WELCOME to Sessions Selector'
print
print 'Press h to shutdown, l to logout, or choose a number to load a wm'
print
fspc=os.path.expanduser('~')+'/'
io=popen2.Popen3('ls /etc/X11/wmsession.d',1)
L=io.fromchild.readlines()
Sessions={}
Sessionkeys=[]
for k in L:
primekey=int(k[0:2])
name=k[2:-1]
j='cat /etc/X11/wmsession.d/'+k[:-1]
declan=popen2.Popen3(j,1)
M=declan.fromchild.readlines()
for cc in M:
if string.find(cc,'EXEC=') > -1:
command=cc[5:-1]
break
Sessions[primekey]=(name, command)
Sessionkeys=Sessions.keys()
Sessionkeys.sort()
for j in Sessionkeys:
if os.system('[ -x '+Sessions[j][1]+' ]') == 0:
print j, Sessions[j][0]
pass
print
sys.stdin.flush()
s=raw_input('Select number, l, h :')
if s == '0':
print
print 'You have selected X with no WM'
print
sys.stdin.flush()
s=raw_input('Type another 0 to confirm')
if s == '0':
os.execvp('xinit',('xinit','/usr/X11R6/bin/xvt'))
if len(s)==0:
s='1'
selct=1
if len(s)>2:
print "Be Reasonable! That number is too big."
complain()
if len(s)==1:
if s == 'l':
os.execvp('logout',['logout'])
if s == 'h':
os.execvp('sudo', ['sudo', '/sbin/shutdown -h now'])
if len(s)==2:
if string.find(string.digits,s[1]) == -1:
print s[1] + " isn't a digit."
complain()
if string.find(string.digits,s[0]) == -1:
print s[0] + " isn't a digit."
complain()
well=int(s)
wellformed=Sessions.has_key(well)
if wellformed:
os.execvp('xinit',Sessions[well])
else:
print
print 'All that work and you mistyped the number'
print 'Hmph!'
print
complain()
Last edited by bluefire; 08-28-2004 at 11:50 AM.
|
|
|
|
08-29-2004, 06:36 AM
|
#2
|
|
Moderator
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,088
|
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
|
|
|
|
08-29-2004, 06:43 AM
|
#3
|
|
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794
Rep: 
|
I dunno for the logout part (maybe exit ?) but for shutdown/reboot try /usr/bin/halt and /usr/bin/reboot 
Last edited by Proud; 08-29-2004 at 06:45 AM.
|
|
|
|
08-29-2004, 11:37 AM
|
#4
|
|
Member
Registered: Oct 2003
Distribution: Mandrake 10
Posts: 160
Original Poster
Rep:
|
Hello, I got restart and shutdown working.
However, I still need to get 1) logout working and 2) the loop working (so that when you exit the window manager, the menu is there for you to choose from.)
To get halt and reboot working, I just had to break up their arguments into the array as per below.
Here's my revised code (bolded are my changed from the original Xtart):
Code:
#!/usr/bin/env python
# This is a modification of Xtart, distributed with Mandrake 10, to provide a
# shutdown/logout option.
# This is an official linux-mandrake extension of the X windowing system for
# the benefit of Declan Moriarty Copyright 2001 by civileme@mandrakesoft all
# rights reserved. Use is hereby granted under the GNU General Publoic
# License version 2 or any future version of said license at the user's option
#
# No Warranty of course.
# This program is designed for those who want to work in concole and
# occasionally boot into window managers or who want to avoid theme
# persistence from one WM to another.
#
#
import sys
import os
import popen2
import string
def complain():
print
print 'What do you expect me to do with that?'
print
sys.exit(0)
return
exittoshell = 0
while exittoshell == 0:
print
print ' WELCOME to Sessions Selector'
print
print 'Type h to shutdown, '
print ' r to reboot, '
print ' l to logout,'
print ' x to exit to shell,'
print ' or choose a number to load a wm'
print
fspc=os.path.expanduser('~')+'/'
io=popen2.Popen3('ls /etc/X11/wmsession.d',1)
L=io.fromchild.readlines()
Sessions={}
Sessionkeys=[]
for k in L:
primekey=int(k[0:2])
name=k[2:-1]
j='cat /etc/X11/wmsession.d/'+k[:-1]
declan=popen2.Popen3(j,1)
M=declan.fromchild.readlines()
for cc in M:
if string.find(cc,'EXEC=') > -1:
command=cc[5:-1]
break
Sessions[primekey]=(name, command)
Sessionkeys=Sessions.keys()
Sessionkeys.sort()
for j in Sessionkeys:
if os.system('[ -x '+Sessions[j][1]+' ]') == 0:
print j, Sessions[j][0]
pass
print
sys.stdin.flush()
s=raw_input('Select number, l, h :')
if s == '0':
print
print 'You have selected X with no WM'
print
sys.stdin.flush()
s=raw_input('Type another 0 to confirm')
if s == '0':
os.execvp('xinit',('xinit','/usr/X11R6/bin/xvt'))
if len(s)==0:
s='1'
selct=1
if len(s)>2:
print "Be Reasonable! That number is too big."
complain()
if len(s)==1:
if s == 'l':
os.execvp('logout',['logout'])
if s == 'h':
os.execvp('sudo', ['sudo', '/sbin/shutdown', '-h', 'now'])
if s == 'r':
os.execvp('sudo', ['sudo', '/sbin/shutdown', '-r', 'now'])
if s == 'x':
print 'Type ./Xtartstop to load menu again'
sys.exit(0)
if len(s)==2:
if string.find(string.digits,s[1]) == -1:
print s[1] + " isn't a digit."
complain()
if string.find(string.digits,s[0]) == -1:
print s[0] + " isn't a digit."
complain()
well=int(s)
wellformed=Sessions.has_key(well)
if wellformed:
os.execvp('xinit',Sessions[well])
else:
print
print 'All that work and you mistyped the number'
print 'Hmph!'
print
complain()
Logout I can live with for now. But why doesn't the loop work? Does os.execvp end the script?
Thanks
|
|
|
|
08-30-2004, 01:50 PM
|
#5
|
|
Member
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569
Rep:
|
One comment on the code, use elif instead of a bunch of consecutive if clauses.
|
|
|
|
09-28-2004, 12:30 PM
|
#6
|
|
Member
Registered: Jun 2004
Posts: 129
Rep:
|
If you knew the pid of the shell you wanted to logout of you could use os.kill()
Here's a link to some python docs that might help you http://docs.python.org/lib/os-process.html
You could also check out snakehandlers too!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:01 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|