LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python question (https://www.linuxquestions.org/questions/programming-9/python-question-223522/)

bluefire 08-28-2004 11:47 AM

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()


XavierP 08-29-2004 06:36 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

Proud 08-29-2004 06:43 AM

I dunno for the logout part (maybe exit ?) but for shutdown/reboot try /usr/bin/halt and /usr/bin/reboot :)

bluefire 08-29-2004 11:37 AM

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

Strike 08-30-2004 01:50 PM

One comment on the code, use elif instead of a bunch of consecutive if clauses.

SheldonPlankton 09-28-2004 12:30 PM

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!


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