Re: Fwd: Converting a script to Python 3 - having trouble.

2009-09-15 Thread Russell Jackson
I received a reply from the help group that suggested I added a call to
flush in there, and that fixed it. The working code looks like this:
def setpassword(user):
password = "passworD\n"
try:
cmd = ' passwd {0}'.format(user)
pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE,
stderr=PIPE, universal_newlines=True)
stderr = pipe.stdin.write(password)
pipe.stdin.flush()
time.sleep(2)
stderr = pipe.stdin.write(password)
pipe.stdin.flush()
pipe.stdin.close()
if pipe.wait() != 0:
log("ERROR", "Password reset failed.\n{0}{1} generated the
following error: {2}".format(p4, cmd, stderr))
except OSError as err:
log("ERROR", "Execution failed: {0}".format(err))

Thanks,
Rusty


On Tue, Sep 15, 2009 at 6:37 PM, Chris Rebert  wrote:

> On Tue, Sep 15, 2009 at 4:58 PM, Russell Jackson
>  wrote:
> > I just get an errorlevel from the executable when I read stdout, but I
> can't
> > tell what is going on because, of course, I can't tell what Popen is
> > actually doing. I never see the prompt from the executable that I would
> > expect to see when I read stdout.
> > I originally had the function like this:
> > def setpassword(user):
> > password = "passworD\n"
> > try:
> > cmd = ' passwd {0}'.format(user)
> > pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE,
> > stderr=PIPE, universal_newlines=True)
> > stdout = pipe.stdout.readline()
> > stderr = pipe.stdin.write(password)
> > time.sleep(1)
> > stdout = pipe.stdout.readline()
> > stderr = pipe.stdin.write(password)
> > if pipe.stdin.close != 0:
> > log("ERROR", "Password reset failed.\n{0}{1} generated the
> > following error: {2}".format(p4, cmd, stderr))
> > except OSError as err:
> > log("ERROR", "Execution failed: {0}".format(err))
> > but, the script just hung when I did that. I think it was stuck on the
> > readline, and never got anything from the process.
> > I didn't think that the if statement was incorrect based on examples I
> saw
> > in the docs,
>
> I'm unable to locate any example in the subprocess docs using a
> similar "if". The closest is this code snippet:
>
> rc = pipe.close()
> if  rc != None and rc % 256:
>print "There were some errors"
>
> ...but that's used as an example of code *using os.popen()* in the
> context of how to translate it to use subprocess.Popen().
>
> > and the fact that it didn't complain about that part,
>
> Well, you're just comparing the .close method of a file object for
> equality with 0, which is valid, meaningful, and well-defined (hence,
> no error), but not useful or relevant in this context.
>
> >but I'll try the close():
>
> I agree with Rhodri that that "if" statement is definitely bunk.
> Rereading the docs, I think what was intended is:
>
> pipe.stdin.close()
> if pipe.wait() != 0:
>log(...)
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>



-- 
Rusty

775-636-7402 Office
775-851-1982 Fax
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a script to Python 3 - having trouble.

2009-09-15 Thread Russell Jackson
Yes, I don't think the problem is that isn't running the command, I think it
is just that I don't know how to communicate with it in the way that I need
to in order to make this work. I have tried finding examples of working with
Popen, but they are few and far between. I am not sure what was wrong with
os.popen that they felt it needed to be thrown away, but it certainly was
MUCH easier to work with than subprocess.Popen.
I even see examples of code where people are using the communicate call, and
passing in strings, but don't have universal_newline set to true. I can't
get that to work at all without the universal_newline being set to True. The
docs on the subprocess module need a lot of enhancement in my opinion given
that is is supposed to be the replacement for all the os.popen stuff in the
prior versions.

Thanks,
Rusty

On Tue, Sep 15, 2009 at 5:11 PM, Chris Rebert  wrote:

> On Tue, Sep 15, 2009 at 5:07 PM, Chris Rebert  wrote:
> > On Tue, Sep 15, 2009 at 4:01 PM, Russell Jackson
> >  wrote:
> > 
> >> Attempted code in Python 3: (Doesn't work either)
> > 
> >> cmd = ' passwd {0}'.format(user)
> >> pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE,
> >> stderr=PIPE, universal_newlines=True)
> 
>
> Scratch that, I neglected to notice the shell=True option.
>
> Cheers,
> Chris
>



-- 
Rusty

775-636-7402 Office
775-851-1982 Fax
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Converting a script to Python 3 - having trouble.

2009-09-15 Thread Russell Jackson
I just get an errorlevel from the executable when I read stdout, but I can't
tell what is going on because, of course, I can't tell what Popen is
actually doing. I never see the prompt from the executable that I would
expect to see when I read stdout.
I originally had the function like this:

def setpassword(user):
password = "passworD\n"
try:
cmd = ' passwd {0}'.format(user)
pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE,
stderr=PIPE, universal_newlines=True)
stdout = pipe.stdout.readline()
stderr = pipe.stdin.write(password)
time.sleep(1)
stdout = pipe.stdout.readline()
stderr = pipe.stdin.write(password)
if pipe.stdin.close != 0:
log("ERROR", "Password reset failed.\n{0}{1} generated the
following error: {2}".format(p4, cmd, stderr))
except OSError as err:
log("ERROR", "Execution failed: {0}".format(err))

but, the script just hung when I did that. I think it was stuck on the
readline, and never got anything from the process.

I didn't think that the if statement was incorrect based on examples I saw
in the docs, and the fact that it didn't complain about that part, but I'll
try the close():

Thanks,
Rusty



On Tue, Sep 15, 2009 at 4:24 PM, Rhodri James
wrote:

> On Wed, 16 Sep 2009 00:01:17 +0100, Russell Jackson <
> ru...@rcjacksonconsulting.com> wrote:
>
>  Hi,
>> I have the following code that works fine in Python 2.x, but I can't seem
>> to
>> get it to work in Python 3 with Popen. Can you please tell me how to get
>> the
>> same functionality out of Python 3? The gist of what I doing is in the
>> setpassword function. I have tried numerous ways to get this to work, and
>> just can't figure it out, and the docs on Popen are no help whatsoever on
>> how to use the now open process. The examples completely skip over what to
>> do with the process after you open it.
>>
>
> So how did it fail?
>
> ###
>> def setpassword(user):
>>password = "passworD\n"
>>try:
>>cmd = ' passwd {0}'.format(user)
>>pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE,
>> stderr=PIPE, universal_newlines=True)
>>stderr = pipe.stdin.write(password)
>>time.sleep(1)
>>stderr = pipe.stdin.write(password)
>>if pipe.stdin.close != 0:
>>
>
> Did you perhaps mean "if pipe.stdin.close():" ?
> Does it help if you read stdout rather than sleeping for arbitrary periods?
>
> --
> Rhodri James *-* Wildebeest Herder to the Masses
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Rusty

775-636-7402 Office
775-851-1982 Fax
-- 
http://mail.python.org/mailman/listinfo/python-list


Fwd: Converting a script to Python 3 - having trouble.

2009-09-15 Thread Russell Jackson
Hi,
I have the following code that works fine in Python 2.x, but I can't seem to
get it to work in Python 3 with Popen. Can you please tell me how to get the
same functionality out of Python 3? The gist of what I doing is in the
setpassword function. I have tried numerous ways to get this to work, and
just can't figure it out, and the docs on Popen are no help whatsoever on
how to use the now open process. The examples completely skip over what to
do with the process after you open it.

The command I am running is a Perforce "p4 passwd username" command. It
prompts for the password twice, and I have to enter it twice to reset a
user's password. I would appreciate your help on getting this to work under
Python 3.

Rusty


Working code in Python 2.x (Does not work in Python 3.)

import os
import string
import sys
import time


###
def setpassword(user):
password = "passworD\n"

p4passwd = os.popen( "p4 passwd %s" % user, 'w' )
time.sleep(1)
 p4passwd.write( password )
time.sleep(1)
p4passwd.write( password )

if p4passwd.close() == 1:
print "Password reset failed.\n"
 sys.exit( 1 )

###
if __name__ == '__main__':
 if len (sys.argv) <= 1:
print "Read the usage section at the top of the script for required
parameters."
 sys.exit(1)

user = sys.argv[1]

setpassword(user)





Attempted code in Python 3: (Doesn't work either)



import os
import string
import sys
import time
import platform
from subprocess import *

if platform.system() == "Windows":
p4="p4.exe"
else:
p4="/p4/1/bin/p4_1"

###
def log(msglevel="DEBUG", message=""):
if msglevel == "TEST":
print("Running in test mode. Command run would have been:\n",
message)
elif msglevel == "ERROR":
print(message)
sys.exit(1)
elif (verbosity == "3"):
print(message)
elif (verbosity == "2" and msglevel == "INFO"):
print(message)

###
def setpassword(user):
password = "passworD\n"
try:
cmd = ' passwd {0}'.format(user)
pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE,
stderr=PIPE, universal_newlines=True)
stderr = pipe.stdin.write(password)
time.sleep(1)
stderr = pipe.stdin.write(password)
if pipe.stdin.close != 0:
log("ERROR", "Password reset failed.\n{0}{1} generated the
following error: {2}".format(p4, cmd, stderr))
except OSError as err:
log("ERROR", "Execution failed: {0}".format(err))

###
if __name__ == '__main__':
 if len (sys.argv) <= 1:
print ("Read the usage section at the top of the script for required
parameters.")
 sys.exit(1)

user = sys.argv[1]

setpassword(user)




-- 
Rusty

775-636-7402 Office
775-851-1982 Fax



-- 
Rusty

775-636-7402 Office
775-851-1982 Fax
-- 
http://mail.python.org/mailman/listinfo/python-list