LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 08-28-2004, 11:47 AM   #1
bluefire
Member
 
Registered: Oct 2003
Distribution: Mandrake 10
Posts: 160

Rep: Reputation: 30
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.
 
Old 08-29-2004, 06:36 AM   #2
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-29-2004, 06:43 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.
 
Old 08-29-2004, 11:37 AM   #4
bluefire
Member
 
Registered: Oct 2003
Distribution: Mandrake 10
Posts: 160

Original Poster
Rep: Reputation: 30
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
 
Old 08-30-2004, 01:50 PM   #5
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
One comment on the code, use elif instead of a bunch of consecutive if clauses.
 
Old 09-28-2004, 12:30 PM   #6
SheldonPlankton
Member
 
Registered: Jun 2004
Posts: 129

Rep: Reputation: 15
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python variable question bendeco13 Programming 6 02-03-2005 07:22 AM
Python question gamehack Programming 5 01-03-2005 05:21 AM
python question ywchen2000 Programming 1 04-20-2004 11:46 PM
A Python question gamehack Programming 2 03-17-2004 09:09 PM
Python question m4rccd Programming 1 08-24-2003 06:04 AM


All times are GMT -5. The time now is 06:47 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration