Re: [Tutor] Confirmation if command worked

2011-08-25 Thread Christian Witts

On 2011/08/25 07:51 AM, Johan Geldenhuys wrote:

Hi all,


I have the following code that uses pexpect to execute a system command. My
problem is, I don't know how to identify if the command was successfully
executed.
The code works as it is to execute the SCP command, but it executes
regardless if the SCP session can actually connect to something.

Thanks

def doScp(files):


 logger.log(Files to get:  + `files`)
 fNames = ' '.join(files)

 cmd = 'scp %s %s@%s:%s%s' % (fNames,
 SCP_USERNAME,
 SCP_HOST,
 SCP_IMG_FILE_DIR,
 SCP_IMG_FILE_PATH)

 logger.log(Sending:  + cmd)

 child = pexpect.spawn(cmd)

 i = child.expect(['assword:', 'yes/no'], timeout=30)
 if i == 0:
 child.sendline(SCP_PASSWD)

 elif i == 1:
 child.sendline(yes)
 child.expect(assword:, timeout=30)
 child.sendline(SCP_PASSWD)

 data = child.read()

 if data != None:
 success = True
 else:
 success = False
 child.close()

 logger.log(Files sent to SCP host)

 return success





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




Once you call child.close() the exit and signal status will be stored in 
.exitstatus and .signalstatus, for a normal exit of the program 
.exitstatus will store the return code from SCP as per [1] [2] [3] and 
.signalstatus will be None.  If SCP was terminated with a signal then 
.exitstatus will be None and .signalstatus will contain the signal 
value.  Info found in the docs [4].


So the changes to your code will be:

snip
data = child.read()
child.close()

if child.exitstatus and child.exitstatus == 0:
success = True
else:
success = False
snip

I'll leave putting in handling of failed error codes and abnormal 
termination to you.


[1] http://support.attachmate.com/techdocs/2116.html
[2] http://support.attachmate.com/techdocs/2487.html
[3] http://support.attachmate.com/techdocs/2285.html
[4] http://pexpect.sourceforge.net/pexpect.html

--

Christian Witts
Python Developer

//
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Confirmation if command worked

2011-08-25 Thread Johan Geldenhuys
Hi Christian,

 

Thanks for that. I'll give it a shot and see if I can catch the error.

 

Lekker dag

 

 

Johan

 

From: Christian Witts [mailto:cwi...@compuscan.co.za] 
Sent: Thursday, 25 August 2011 4:25 PM
To: Johan Geldenhuys
Cc: tutor@python.org
Subject: Re: [Tutor] Confirmation if command worked

 

On 2011/08/25 07:51 AM, Johan Geldenhuys wrote: 

Hi all,
 
 
I have the following code that uses pexpect to execute a system command. My
problem is, I don't know how to identify if the command was successfully
executed.
The code works as it is to execute the SCP command, but it executes
regardless if the SCP session can actually connect to something.
 
Thanks
 
def doScp(files):


logger.log(Files to get:  + `files`)
fNames = ' '.join(files)

cmd = 'scp %s %s@%s:%s%s' % (fNames,
SCP_USERNAME,
SCP_HOST,
SCP_IMG_FILE_DIR,
SCP_IMG_FILE_PATH)

logger.log(Sending:  + cmd)

child = pexpect.spawn(cmd)

i = child.expect(['assword:', 'yes/no'], timeout=30)
if i == 0:
child.sendline(SCP_PASSWD)

elif i == 1:
child.sendline(yes)
child.expect(assword:, timeout=30)
child.sendline(SCP_PASSWD)

data = child.read()

if data != None:
success = True
else:
success = False
child.close()

logger.log(Files sent to SCP host)

return success
 
 
 
 
 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
 
 


Once you call child.close() the exit and signal status will be stored in
.exitstatus and .signalstatus, for a normal exit of the program .exitstatus
will store the return code from SCP as per [1] [2] [3] and .signalstatus
will be None.  If SCP was terminated with a signal then .exitstatus will be
None and .signalstatus will contain the signal value.  Info found in the
docs [4].

So the changes to your code will be:

snip
data = child.read()
child.close()

if child.exitstatus and child.exitstatus == 0:
success = True
else:
success = False
snip

I'll leave putting in handling of failed error codes and abnormal
termination to you.

[1] http://support.attachmate.com/techdocs/2116.html
[2] http://support.attachmate.com/techdocs/2487.html
[3] http://support.attachmate.com/techdocs/2285.html
[4] http://pexpect.sourceforge.net/pexpect.html

-- 

Christian Witts
Python Developer

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Confirmation if command worked

2011-08-25 Thread Alan Gauld

On 25/08/11 07:25, Christian Witts wrote:


Once you call child.close() the exit and signal status will be stored in
.exitstatus and .signalstatus, for a normal exit of the program
.exitstatus will store the return code from SCP as per [1] [2] [3] and
.signalstatus will be None.  If SCP was terminated with a signal then
.exitstatus will be None and .signalstatus will contain the signal
value.  Info found in the docs [4].

 data = child.read()
 child.close()

 if child.exitstatus and child.exitstatus == 0:
 success = True
 else:
 success = False


Won't that if statement always evaluate to false?

If exitstatus is 0 the first part of the and will be false and so the 
entire and expression is false.


If exitstatus is not 0 then the second part of the and will be false and 
again the and result will be false.


Did you mean to use exitstatus for both tests?

Or am I missing something subtle here?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Confirmation if command worked

2011-08-25 Thread Christian Witts

On 2011/08/25 10:19 AM, Alan Gauld wrote:

On 25/08/11 07:25, Christian Witts wrote:


Once you call child.close() the exit and signal status will be stored in
.exitstatus and .signalstatus, for a normal exit of the program
.exitstatus will store the return code from SCP as per [1] [2] [3] and
.signalstatus will be None.  If SCP was terminated with a signal then
.exitstatus will be None and .signalstatus will contain the signal
value.  Info found in the docs [4].

 data = child.read()
 child.close()

 if child.exitstatus and child.exitstatus == 0:
 success = True
 else:
 success = False


Won't that if statement always evaluate to false?

If exitstatus is 0 the first part of the and will be false and so the 
entire and expression is false.


If exitstatus is not 0 then the second part of the and will be false 
and again the and result will be false.


Did you mean to use exitstatus for both tests?

Or am I missing something subtle here?



Good catch, it should be `if child.exitstatus != None and 
child.exitstatus == 0:` or removing the first part entirely.  The 
zero-evaluating-as-False has bitten me before and for some reason I 
still on occasion forget it even though I use it often for data extracts.


--

Christian Witts
Python Developer

//
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Confirmation if command worked

2011-08-25 Thread Steve Willoughby

On 25-Aug-11 01:37, Christian Witts wrote:

Good catch, it should be `if child.exitstatus != None and
child.exitstatus == 0:`


It's better form to say
if child.exitstatus is not None
instead of comparing for equality to None with the != operator.


--
Steve Willoughby / st...@alchemy.com
A ship in harbor is safe, but that is not what ships are built for.
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Confirmation if command worked

2011-08-25 Thread Steven D'Aprano

Christian Witts wrote:


if child.exitstatus and child.exitstatus == 0:
success = True
else:
success = False



There is never any need to write Python code that looks like that. (Or 
in any other language I'm familiar with either.) Anything of the form:



if some_condition:
flag = True
else:
flag = False

is better written as:

flag = some_condition

In the above example, you should write:

success = child.exitstatus and child.exitstatus == 0


except that I think you have the condition wrong... if exitstatus is 
zero, you get False and True = False, but if exitstatus is non-zero, 
you get True and False = False. So you will always get success = False.





--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Confirmation if command worked

2011-08-25 Thread Steven D'Aprano

Steven D'Aprano wrote:


if some_condition:
flag = True
else:
flag = False

is better written as:

flag = some_condition



Actually, that's a slight over-simplification.

some_condition may not actually be a bool. If you don't mind flag also 
being a non-bool, that's fine, but if you want to ensure that it is one 
of True/False, then you can call:


flag = bool(some_condition)

to force flag to be one of True or False. But that's generally not 
necessary unless you care about what flag looks like when printed.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Confirmation if command worked

2011-08-24 Thread Johan Geldenhuys
Hi all,


I have the following code that uses pexpect to execute a system command. My
problem is, I don't know how to identify if the command was successfully
executed.
The code works as it is to execute the SCP command, but it executes
regardless if the SCP session can actually connect to something.

Thanks

def doScp(files):


logger.log(Files to get:  + `files`)
fNames = ' '.join(files)

cmd = 'scp %s %s@%s:%s%s' % (fNames,
SCP_USERNAME,
SCP_HOST,
SCP_IMG_FILE_DIR,
SCP_IMG_FILE_PATH)

logger.log(Sending:  + cmd)

child = pexpect.spawn(cmd)

i = child.expect(['assword:', 'yes/no'], timeout=30)
if i == 0:
child.sendline(SCP_PASSWD)

elif i == 1:
child.sendline(yes)
child.expect(assword:, timeout=30)
child.sendline(SCP_PASSWD)

data = child.read()

if data != None:
success = True
else:
success = False
child.close()

logger.log(Files sent to SCP host)

return success





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor