Re: How do I add users using Python scripts on a Linux machine

2007-01-01 Thread Hari Sekhon
That is shell scripting with a python layer on top. Is there a
specific reason you have to use python? Why not just use shell, that's
what it's designed for? Unless you have some complex maths/networking
requirement or something on top.

-h

On 01/01/07, Daniel Klein [EMAIL PROTECTED] wrote:
 On 1 Jan 2007 11:33:42 -0800, Ramdas [EMAIL PROTECTED] wrote:

 How do I add users using Python scripts on a Linux machine?
 
 Someone has a script?

 This should be as easy as something like:

 os.system(/usr/sbin/useradd -m -d /home/newuser -s /bin/ksh)

 Dan
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
Hari Sekhon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do this?

2006-10-05 Thread Hari Sekhon
Do whichever makes you happy I'd say

The only real difference is coding style and the formatting options of 
the %s way that I can see.

%s is negligibly slower in my tests, but we're talking the tiniest 
fraction of a second over thousands of iterations, not worth considering...

-h

Hari Sekhon



Matthew Warren wrote:
 Ok, not really python focused, but it feels like the people here could
 explain it for me :)

 Now, I started programming when I was 8 with BBC Basic.

 I never took any formal classes however, and I have never become an
 expert programmer. I'm an average/hobbyist programmer with quite a few
 languages under my belt but I cant do any really fancy tricks with any
 of them. (although Python might be nudging me into more advanced things,
 now I'm starting to get what all the __method__ thingies and operators
 are all about)

 I learned over the years to do things like the following, and I like
 doing it like this because of readability, something Python seems to
 focus on :-

 Print There are +number+ ways to skin a +furryanimal

 But nowadays, I see things like this all over the place;

 print(There are %s ways to skin a %s % (number, furryanimal))

 Now I understand there can be additional formatting benefits when
 dealing with numbers, decimal places etc.. But to me, for strings, the
 second case is much harder to read than the first.

 I hope I'm not being dense.

 The result is that I have pathalogically avoided print %s % (thing)
 because it seems to just over complicate things.


 Ta, :)

 Matt.





 This email is confidential and may be privileged. If you are not the intended 
 recipient please notify the sender immediately and delete the email from your 
 computer. 

 You should not copy the email, use it for any purpose or disclose its 
 contents to any other person.
 Please note that any views or opinions presented in this email may be 
 personal to the author and do not necessarily represent the views or opinions 
 of Digica.
 It is the responsibility of the recipient to check this email for the 
 presence of viruses. Digica accepts no liability for any damage caused by any 
 virus transmitted by this email.

 UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
 Reception Tel: + 44 (0) 115 977 1177
 Support Centre: 0845 607 7070
 Fax: + 44 (0) 115 977 7000
 http://www.digica.com

 SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
 Africa
 Tel: + 27 (0) 21 957 4900
 Fax: + 27 (0) 21 948 3135
 http://www.digica.com
   
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: excepthook doesn't give exact line number

2006-10-05 Thread Hari Sekhon




I've tried the sample code you provided but it seems to just hang, it
must be doing something but unfortunately it must take too long, by
which time a second control-c gives an awful dual traceback message
showing the original traceback and the new one from the tbiter() func.

I've tried a few variations since yesterday but unfortunately those 2
funcs take too long to run to make a viable solution and the script
ends up hanging on an uncaught exception.

If anybody has any improvements, suggestions or alternatives for
getting the proper line number and traceback message inside an
excepthook then I'd be grateful to hear them.

-h
Hari Sekhon


Peter Otten wrote:

  Hari Sekhon wrote:

  
  
The problem is that the excepthook gives the line of the topmost called
function rather that the actual line that generated the error the way
you get it with a normal traceback.

  
  
A look into the traceback module shows that tracebacks are stored as a
linked list. Here's a way to get hold of its tail:

def tbiter(tb):
while tb is not None:
yield tb
tb = tb.tb_next

def last(items):
for  item in items:
pass
return item

# example usage
def myexcepthook(type, value, tb):
tb_tail = last(tbiter(tb))
print tb_tail.tb_lineno
 
Peter
  



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: excepthook doesn't give exact line number

2006-10-05 Thread Hari Sekhon




Thanks for the pointer, I've now got this giving me the right line
number when an exception occurs, although I still get an empty stack
trace from 

print "Stack Trace:\n%s\n" % str(traceback.print_exc(2))

inside the excepthook.

Any ideas why this is?

Is there no traceback since the traceback was fed to the excepthook? Is
there another way of getting the traceback like you see when the
exception isn't caught?

Thanks for the help.

-h
Hari Sekhon


Peter Otten wrote:

  Hari Sekhon wrote:

  
  
The problem is that the excepthook gives the line of the topmost called
function rather that the actual line that generated the error the way
you get it with a normal traceback.

  
  
A look into the traceback module shows that tracebacks are stored as a
linked list. Here's a way to get hold of its tail:

def tbiter(tb):
while tb is not None:
yield tb
tb = tb.tb_next

def last(items):
for  item in items:
pass
return item

# example usage
def myexcepthook(type, value, tb):
tb_tail = last(tbiter(tb))
print tb_tail.tb_lineno
 
Peter
  



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Best way to handle large lists?

2006-10-04 Thread Hari Sekhon




So are you saying that using a dict means a faster search since you
only need to look up one value?

I would think that you would have to look through the keys and stop at
the first key that matches since each key has to be uniq, so perhaps if
it is nearer the front of the set of keys then perhaps it would be a
quicker lookup?

On the other hand, if it is nearer the end of the set of keys would it
not be slower?

Does this make it more dependent on the search order whether a list or
a dict is faster? Or am I completely on the wrong track?

-h
Hari Sekhon


Fredrik Lundh wrote:

  Hari Sekhon wrote:

  
  
That is surprising since I read on this list recently that lists were 
faster than dicts

  
  
depends on what you're doing with them, of course.

  
  
It was one reason that was cited as to why local vars are better than

  
global vars.

L[int] is indeed a bit faster than D[string] (but not much), but that 
doesn't mean that you can loop over *all* items in a list faster than 
you can look up a single key in a dictionary.

/F

  



-- 
http://mail.python.org/mailman/listinfo/python-list

excepthook doesn't give exact line number

2006-10-03 Thread Hari Sekhon
Hi,

I'm wondering if anyone can please help me on figuring out a better way 
of doing an excepthook. I have a script which is using an excepthook to 
catch any uncaught exceptions - there usually aren't any except when I 
am messing with the code, like right now  :-) 

The problem is that the excepthook gives the line of the topmost called 
function rather that the actual line that generated the error the way 
you get it with a normal traceback.


import sys, traceback

def myexcepthook(type,value,tb):
do something
   
exception_message = ( \nLine no %s: %s - %s\n
\nStack Trace:\n\n%s\n % 
(tb.tb_lineno,type,value,str(traceback.print_exc(2))) )
   
send me an email etc

sys.excepthook = myexcepthook

now do some work



So the tb object that is passed into the function gives the tb.tb_lineno 
as a line right near the end where the original topmost called function 
happens. This is a bit useless to me since I don't want to go looking 
for the exception manually through the entire code.

As you can see from my example, I have already used the traceback module 
which I usually use to give the normal traceback printout via 
traceback.print_exc(). Here it doesn't seem to work, it always give 
None, likely because the excepthook has taken it or something.

Any guiding wisdom from the masters out there?

Much appreciated, thanks for reading.

-h

-- 
Hari Sekhon

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to handle large lists?

2006-10-03 Thread Hari Sekhon
I don't know much about the python internals either, so this may be the 
blind leading the blind, but aren't dicts much slower to work with than 
lists and therefore wouldn't your suggestion to use dicts be much 
slower? I think it's something to do with the comparative overhead of 
using keys in dicts rather than using positional indexes in lists/arrays...

At least that is what I thought.

Can anyone confirm this?

-h

Hari Sekhon



Bill Williams wrote:
 I don't know enough about Python internals, but the suggested solutions 
 all seem to involve scanning bigList. Can this presumably linear 
 operation be avoided by using dict or similar to find all occurrences of 
 smallist items in biglist and then deleting those occurrences?

 Bill Williams



 In article [EMAIL PROTECTED],
  Chaz Ginger [EMAIL PROTECTED] wrote:

   
 I have a system that has a few lists that are very large (thousands or
 tens of thousands of entries) and some that are rather small. Many times
 I have to produce the difference between a large list and a small one,
 without destroying the integrity of either list. I was wondering if
 anyone has any recommendations on how to do this and keep performance
 high? Is there a better way than

 [ i for i in bigList if i not in smallList ]

 Thanks.
 Chaz
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to handle large lists?

2006-10-03 Thread Hari Sekhon




Jeremy Sanders wrote:

  Chaz Ginger wrote:

  
  
What would sets do for me over lists?

  
  
It's faster to tell whether something is in a set or dict than in a list
(for some minimum size).

Jeremy

  

That is surprising since I read on this list recently that lists were
faster than dicts and that variables stored in lists were faster
referenced/used. It was one reason that was cited as to why local vars
are better than global vars. The guy actually did a looping test and
timed it to show the speed difference.

Can anybody please step in and correct us?

-h

Hari Sekhon


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread Hari Sekhon




Fredrik Lundh wrote:

  Hari Sekhon wrote:

  
  
I'm not sure if that is a very old way of doing it, which is why I was 
reluctant to do it. My way actually uses the process list of the os 
(linux) and counts the number of instances. If it is more than 0 then 
another process is running and the script exits gracefully.

  
  
the code that reliably identifies instances of a given program would be 
interesting to see.

  
  
Also, apart from the fact the using lockfiles feels a bit 1970s, I have

  
found that in real usage of other programs within the company that use
  lockfiles, it sometimes causes a bit of troubleshooting time when
  it stops working due to a stale lockfile.

to minimize that risk, store the pid in the lockfile (and preferrably 
also the host name), and make sure that the program checks that the pid 
is still active before it "stops working".

/F

  


How exactly do you check that the pid is still active in python? Is
there a library or something that will allow me to manipulate system
processes and listings etc the way everybody does in unix shells

I'm a huge fan of shell so I've done my own thing which leans on shell
as follows:

import sys,commands,os

scriptpath = sys.argv[0]
scriptname = os.path.basename(scriptpath)

number_procs=commands.getstatusoutput('ps -ef|grep %s|grep -v grep|wc
-l' % scriptpath)

if number_procs  1:
 print "There appears to be another %s process running." % scriptname
 print "Please do not run more than one instance of this program"
 print "Quitting for safety..."
 sys.exit(200)

This works nicely for me.

You might also want to add a bit of discrimination to the script
(something along the lines of "get a decent os"...since this won't work
on windows)

import platform
if platform.system() != 'Linux':
 print "Sorry but this program can only be run on Linux"
 sys.exit(201)
 #todo: should do a bit extra for our bsd cousins here


Let me know if you think you have a better way. Please provide a code
snippet if so.

-h


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread Hari Sekhon




AMENDMENT:

The line 

number_procs=commands.getstatusoutput('ps -ef|grep %s|grep -v grep|wc
-l' % scriptpath)

was supposed to be

number_procs=int(commands.getstatusoutput('ps -ef|grep %s|grep -v
grep|wc
-l' % scriptpath)[1])

-h

Hari Sekhon


Hari Sekhon wrote:

  
Fredrik Lundh wrote:
  
Hari Sekhon wrote:

  

  I'm not sure if that is a very old way of doing it, which is why I was 
reluctant to do it. My way actually uses the process list of the os 
(linux) and counts the number of instances. If it is more than 0 then 
another process is running and the script exits gracefully.



the code that reliably identifies instances of a given program would be 
interesting to see.

  

  Also, apart from the fact the using lockfiles feels a bit 1970s, I have


  found that in real usage of other programs within the company that use
  lockfiles, it sometimes causes a bit of troubleshooting time when
  it stops working due to a stale lockfile.

to minimize that risk, store the pid in the lockfile (and preferrably 
also the host name), and make sure that the program checks that the pid 
is still active before it "stops working".

/F

  
  
  
How exactly do you check that the pid is still active in python? Is
there a library or something that will allow me to manipulate system
processes and listings etc the way everybody does in unix shells
  
I'm a huge fan of shell so I've done my own thing which leans on shell
as follows:
  
import sys,commands,os
  
scriptpath = sys.argv[0]
scriptname = os.path.basename(scriptpath)
  
number_procs=commands.getstatusoutput('ps -ef|grep %s|grep -v grep|wc
-l' % scriptpath)
  
if number_procs  1:
 print "There appears to be another %s process running." % scriptname
 print "Please do not run more than one instance of this program"
 print "Quitting for safety..."
 sys.exit(200)
  
This works nicely for me.
  
You might also want to add a bit of discrimination to the script
(something along the lines of "get a decent os"...since this won't work
on windows)
  
import platform
if platform.system() != 'Linux':
 print "Sorry but this program can only be run on Linux"
 sys.exit(201)
 #todo: should do a bit extra for our bsd cousins here
  
  
Let me know if you think you have a better way. Please provide a code
snippet if so.
  
-h



-- 
http://mail.python.org/mailman/listinfo/python-list

commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon
I'm running a command like

import commands
result = commands.getstatusoutput('somecommand')
print result[0]
3072


However, this exit code made no sense so I ran it manually from the 
command line in bash on my linux server and it gives the exit code as 
12, not this weird 3072 number.

So I tried os.system('somecommand') in the interactive python shell and 
it too returned the same result for the exit code as the unix shell, 12, 
but re-running the commands.getstatusoutput() with the exact same 
command still gave 3072.


Is commands.getstatusoutput() broken or something?


-h

-- 
Hari Sekhon

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon




I don't quite understand what you are saying here:

2 * 256 is 512,
2 ** 256 is some extremely large number.

2**12 is 4096.

So how does 3072 factor into this?

Could you explain what you mean by "the error in the top half of a
sixteen-bit value"?

This makes no sense to me at this moment.

-h

Hari Sekhon


Steve Holden wrote:

  Hari Sekhon wrote:
  
  
I'm running a command like

import commands
result = commands.getstatusoutput('somecommand')
print result[0]
3072


However, this exit code made no sense so I ran it manually from the 
command line in bash on my linux server and it gives the exit code as 
12, not this weird 3072 number.

So I tried os.system('somecommand') in the interactive python shell and 
it too returned the same result for the exit code as the unix shell, 12, 
but re-running the commands.getstatusoutput() with the exact same 
command still gave 3072.


Is commands.getstatusoutput() broken or something?


-h


  
  No, it's just returning the error code in the top half of a sixteen-bit 
value. You will notice that 3072 == 2 * 256.

That's always been the way the Unix return code has been returned 
programattically, but the shell shifts it down to make it more usab;e.

regards
  Steve
  



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon
I'm sorry, this may seem dense to you but I have to ask. What on earth 
are you talking about?

Why is it shifted 8 bits to the left? Why is there bitshifting at all? 
Why doesn't commands give the same exit value as os.system() and the 
unix cli?

When you choose to exit a program you give it a return value to exit 
with, so why would this change, I exit with the number 1 then expect 
that number to be the exit code, right? Where do these higher numbers 
come into the equation and why?

Please assume that I am not a mind reader and require explanation before 
I can understand.

Perhaps you aren't a mind reader either and don't know why the writers 
of the commands lib chose to do this insanity either? It seems the 
os.system() guys did the right thing, I wonder why 
commands.getstatusoutput doesn't...

Having just tested it manually with a shell script returning 2, 
commands.getstatusoutput did give the exit code as 512, so it does seem 
to generically shift the exit code 8 bits to the left or multiply it by 
256 for those of us who need some more straight talking...

ugg, perhaps it's time to stop using this thing and use a better lib module.

any explanations welcome...

-h

Hari Sekhon



Steve Holden wrote:
 A famous Holden typo - it should have been 12 * 256 == 3072, but 
 really it shouldn't have been beyond you to perform a division of 3072 
 by 12 (given that you already knew the number 12 was potentially 
 involved).

 Basically the value you want is shifted up 8 bits. Perhaps I should 
 more understandably have said:

   12  8 == 3072

 regards
  Steve

 Hari Sekhon wrote:
 I don't quite understand what you are saying here:

 2 * 256 is 512,
 2 ** 256 is some extremely large number.

 2**12 is 4096.

 So how does 3072 factor into this?

 Could you explain what you mean by the error in the top half of a 
 sixteen-bit value?

 This makes no sense to me at this moment.

 -h

 Hari Sekhon



 Steve Holden wrote:

 Hari Sekhon wrote:
  

 I'm running a command like

 import commands
 result = commands.getstatusoutput('somecommand')
 print result[0]
 3072


 However, this exit code made no sense so I ran it manually from the 
 command line in bash on my linux server and it gives the exit code 
 as 12, not this weird 3072 number.

 So I tried os.system('somecommand') in the interactive python shell 
 and it too returned the same result for the exit code as the unix 
 shell, 12, but re-running the commands.getstatusoutput() with the 
 exact same command still gave 3072.


 Is commands.getstatusoutput() broken or something?


 -h

   
 No, it's just returning the error code in the top half of a 
 sixteen-bit value. You will notice that 3072 == 2 * 256.

 That's always been the way the Unix return code has been returned 
 programattically, but the shell shifts it down to make it more usab;e.

 regards
  Steve
  



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon




ok, I was thinking of shifting using subprocess, guess I'd better do
that and forget about this waste of time.

thanks

Hari Sekhon


Fredrik Lundh wrote:

  Hari Sekhon wrote:

  
  
I'm sorry, this may seem dense to you but I have to ask. What on earth 
are you talking about?

  
   
  
  
Why is it shifted 8 bits to the left? Why is there bitshifting at all? 
Why doesn't commands give the same exit value as os.system() and the 
unix cli?

  
  
because that's how Unix's wait() operation returns the status code (as 
mentioned in the "commands" section of the library reference).

you can use the os.WIFEXITED(status) and os.WEXITSTATUS(code) helpers to 
convert between wait return codes and signal numbers/exit codes.  see:

 http://docs.python.org/lib/os-process.html

or you can forget about the obsolete "commands" module, and use the new 
subprocess module instead; e.g.

def getstatusoutput(command):
 from subprocess import Popen, PIPE, STDOUT
 p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
 s = p.stdout.read()
 return p.wait(), s

print getstatusoutput("ls -l /bin/ls")
(0, '-rwxr-xr-x1 root root68660 Aug 12  2003 /bin/ls\n')

the subprocess module is highly flexible; see the library reference for 
details.

/F

  



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Making sure script only runs once instance at a time.

2006-10-02 Thread Hari Sekhon




The name thing is taken care of by the fact that I use the path not
just the script name. The path appears in the command and then is
grepped out by that criteria, so if there was another program with the
same name and in a different path then it would not affect it.

Of course this is defeatable now I think about it if both programs are
called the same thing and both are run as ./program_name. I could use
the args to test too but this is also defeatable if being pedantic, and
I don't want to compound any errors, I'm sure I do that enough without
trying...

So I guess we come back to using the pids in the lockfiles as you
suggested and test against those.

I was thinking that I could just adjust it to find the pid of the
program by location of the program on the filesystem and if there is a
pid for the program then there is a running instance of this exact
program in that directory. This would bypass the lockfile thing again
but I've just tested this and it doesn't work, the pid will be found
for python but not for the python program so it breaks this - dang,
that was a very nice thing to do with binaries, I guess it just won't
work with python in the same way (or any other interpreted language I
expect).

I like your method too, it's very pythonic. I'm still weening off bash
a bit as you can tell...

I guess there is no escaping the lockfile at this stage unless I can
think of something else...

I may rewrite safety to use your lockfile with pid embedded. I've
actually used a very similar method in bash before for something,
saving the pid and env vars and then using the binary kill -0
pid to test if a program is alive by finding out whether a
signal could be sent to it. Works nicely there too.

-h

Hari Sekhon


Fredrik Lundh wrote:

  Hari Sekhon wrote:

  
  
How exactly do you check that the pid is still active in python? Is 
there a library or something that will allow me to manipulate system 
processes and listings etc the way everybody does in unix shells

  
  
by passing zero to the os.kill primitive:

	os.kill(pid, 0)

if this raises an OSError, there's no active process with the given pid.

  
  
I'm a huge fan of shell so I've done my own thing which leans on shell 
as follows:

import sys,commands,os

scriptpath = sys.argv[0]
scriptname = os.path.basename(scriptpath)

number_procs=commands.getstatusoutput('ps -ef|grep %s|grep -v grep|wc 
-l' % scriptpath)

if number_procs  1:
print "There appears to be another %s process running." % scriptname

  
  
what if you have commands with overlapping names (e.g. "bar.py" and 
"foobar.py"), or some other user on the machine happens to run a
command that, on purpose or by accident, contains your script's name 
(e.g. "emacs mybar.py") ?

/F

  



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: commands.getstatusoutput result is not command line exit value!!!

2006-10-02 Thread Hari Sekhon




yes already noted by Steve, thanks. 

I should have spotted that myself straight away but I was too wrapped
up in this whole "I didn't realise there were 2 sets of numbers" thing,
gotta go read some unix programming books it would seem this is a os
function that I am not aware of. 

I still reserve the right to be annoyed at commands for not hiding this
from me like everything else, but then /F is right (as always
it would seem) I should not be using such a deprecated thing like
commands, I will switch to subprocess...

I'm even more surprised since I do so much shell scripting and I've
never even heard of this thing before, I guess only the really
battle-scarred old skool ones may know of it.

-h
Hari Sekhon


Scott David Daniels wrote:

  Steve Holden wrote:
  
  
Hari Sekhon wrote:


  I'm running a command like

import commands
result = commands.getstatusoutput('somecommand')
print result[0]
3072
  

  
  ...
  
  
No, it's just returning the error code in the top half of a sixteen-bit 
value. You will notice that 3072 == 2 * 256.

  
  For the rest of us playing along at home, there is a typo there:
The preceding line should read:
  value. You will notice that 3072 == 12 * 256.

--Scott David Daniels
[EMAIL PROTECTED]
  



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Hari Sekhon
Seeing as there doesn't seem to be a good answer to this (or at least 
not one that we have so far some up with) I have decided to fall back to 
my old friend the unix shell. It's as portable as python, but is very 
flexible and fast at doing real things and will tell me if another 
process by this name is running. If so, print msg and exit. simple.

-h

Hari Sekhon



MaR wrote:
 A very brutal but simple and effective method is to bind() to a socket
 on localhost eg (127.0.0.1, 4711), listen() but never accept().
 Any other process trying to to bind() on the same port will fail..
 When the process dies, the port is released automatically, pending som
 timedelay..

 But this assumes you have an execution environment where this is
 acceptable. A sysadmin may have objections ;o)

   
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Hari Sekhon




I'm not sure if that is a very old way of doing it, which is why I was
reluctant to do it. My way actually uses the process list of the os
(linux) and counts the number of instances. If it is more than 0 then
another process is running and the script exits gracefully.

Also, apart from the fact the using lockfiles feels a bit 1970s, I have
found that in real usage of other programs within the company that use
lockfiles, it sometimes causes a bit of troubleshooting time when it
stops working due to a stale lockfile. This especially happens when the
program is killed, the lockfile remains and causes minor annoyance (to
somebody who knows that is, more annoyance to somebody who doesn't).

-h
Hari Sekhon


Paul Rubin wrote:

  Hari Sekhon [EMAIL PROTECTED] writes:
  
  
Seeing as there doesn't seem to be a good answer to this (or at least
not one that we have so far some up with) I have decided to fall back
to my old friend the unix shell. It's as portable as python, but is
very flexible and fast at doing real things and will tell me if
another process by this name is running. If so, print msg and
exit. simple.

  
  
Huh?  The obvious way to check for another instance is with a lock
file.  Just open the file and use fcntl to set an exclusive lock.  If
the lock acquisition fails, another instance has the file.
  



-- 
http://mail.python.org/mailman/listinfo/python-list

Making sure script only runs once instance at a time.

2006-09-27 Thread Hari Sekhon
I have written a script and I would like to ensure that the script is 
never run more than once at any given time.

What is the best way of testing and exiting if there is another version 
of this script running somewhere on this machine?

I guess what I'm asking is how to handle system processes the way I can 
in shell. I am running this on linux.


Thanks

-h

-- 
Hari Sekhon

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: best way of testing a program exists before using it?

2006-09-12 Thread Hari Sekhon
Tim Williams wrote:
 Alternatively there is os.path.exists which works for files or
 directories, but calling it every time you use the wrapper is probably
 more expensive than using the try statement when the program *does*
 exist.

 import os.path
 if not os.path.exists('/dir1/dir2/filename'):
print_something_and_exit(filename)

 :)

problem with that is that the path may change between installations on 
different machine and I can't guarantee /dir1/dir2 which is why a test 
of all dirs in the path is more portable.

-- 
Hari Sekhon

-- 
http://mail.python.org/mailman/listinfo/python-list


best way of testing a program exists before using it?

2006-09-11 Thread Hari Sekhon
I am writing a wrapper to a binary command to run it and then do 
something with the xml output from it.

What is the best way of making sure that the command is installed on the 
system before I try to execute it, like the python equivalent of the 
unix command which?

Otherwise I'd have to do something like:

if os.system('which somecommand') != 0:
print you don't have %s installed % somecommand
sys.exit(1)

I know that isn't portable which is why a python solution would be 
better (although this will run on unix anyway, but it'd be nice if it 
ran on windows too).


-h

-- 
Hari Sekhon

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: best way of testing a program exists before using it?

2006-09-11 Thread Hari Sekhon




Steve Holden wrote:

  Hari Sekhon wrote:
  
  
I am writing a wrapper to a binary command to run it and then do 
something with the xml output from it.

What is the best way of making sure that the command is installed on the 
system before I try to execute it, like the python equivalent of the 
unix command "which"?

Otherwise I'd have to do something like:

if os.system('which somecommand') != 0:
print "you don't have %s installed" % somecommand
sys.exit(1)

I know that isn't portable which is why a python solution would be 
better (although this will run on unix anyway, but it'd be nice if it 
ran on windows too).


  
  The easiest way to test whether the command will run is to try and run 
it. If the program doesn't exist then you'll get an exception, which you 
can catch. Otherwise you'll be stuck with non-portable mechanisms for 
each platform anyway ...

regards
  Steve
  


Yeah, this occurred to me just after I sent the mail, but I don't
really want to run the program because it will go off and do some work
and take time to come back. If there is a better way then that would be
great. I can't think of anything other than what you have suggested
with a message saying that the program wasn't found in the path which
would be the most appropriate error since the path could also be wrong.

Perhaps I'll just bite the bullet and get the program to do something
as small as possible to test it.

I guess I'll just have to continue to miss my unix commands...

-h

-- 
Hari Sekhon


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: best way of testing a program exists before using it?

2006-09-11 Thread Hari Sekhon




Rob Wolfe wrote:

  Hari Sekhon wrote:
  
  
I am writing a wrapper to a binary command to run it and then do
something with the xml output from it.

What is the best way of making sure that the command is installed on the
system before I try to execute it, like the python equivalent of the
unix command "which"?

Otherwise I'd have to do something like:

if os.system('which somecommand') != 0:
print "you don't have %s installed" % somecommand
sys.exit(1)

I know that isn't portable which is why a python solution would be
better (although this will run on unix anyway, but it'd be nice if it
ran on windows too).

  
  
IMHO this is pretty portable:

  
  

  
def is_on_path(fname):

  

  
  ... for p in os.environ['PATH'].split(os.pathsep):
... if os.path.isfile(os.path.join(p, fname)):
... return True
... return False

HTH,
Rob

  


That works well and is portable, fits nicely. I was actually curious if
somebody already came across this and perhaps put a func like this in
the standard modules...

Thanks.

-h

-- 
Hari Sekhon


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: best way of testing a program exists before using it?

2006-09-11 Thread Hari Sekhon




Steven Bethard wrote:

  Hari Sekhon wrote:
  
  
I am writing a wrapper to a binary command to run it and then do 
something with the xml output from it.

What is the best way of making sure that the command is installed on the 
system before I try to execute it, like the python equivalent of the 
unix command "which"?

  
  
There is the which module:

 http://trentm.com/projects/which/

But I'd probably just try the command and catch the exception, e.g.:

  import subprocess
  try:
... subprocess.call(['foo'])
... except OSError:
... 	print "you don't have foo installed"
...
you don't have foo installed
  try:
... subprocess.call(['svm_learn'])
... except OSError:
... 	print "you don't have svm_learn installed"
...
1

STeVe
  

looks good, although I agree with you on the try it and see simplicity.
I think if the which.py was part of the standard lib then I would use
it, but as it is I will stick to the original path search that you
recommended (I'm not gonna both dealing with the pathext just yet)

-- 
Hari Sekhon


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: best way of testing a program exists before using it?

2006-09-11 Thread Hari Sekhon
Tim Williams wrote:
 On 11/09/06, Hari Sekhon [EMAIL PROTECTED] wrote:

  Steve Holden wrote:
  Hari Sekhon wrote:


  The easiest way to test whether the command will run is to try and run
 it. If the program doesn't exist then you'll get an exception, which you
 can catch. Otherwise you'll be stuck with non-portable mechanisms for
 each platform anyway ...

 regards
  Steve


  Yeah, this occurred to me just after I sent the mail, but I don't 
 really
 want to run the program because it will go off and do some work and take
 time to come back. If there is a better way then that would be great. I
 can't think of anything other than what you have suggested with a 
 message
 saying that the program wasn't found in the path which would be the most
 appropriate error since the path could also be wrong.


 If you run your wrapper and the program exists then you don't have to
 test for it,  so the overall process is quicker and cleaner than
 testing-for *then* running the program

 If you run your wrapper and the program doesn't exist, then you have
 performed your if exists  test without extra code and with very
 little processing, and the raised exception will lead you nicely into
 your not exists scenario.

 try:
run_somecommand
 except:
print you don't have %s installed % somecommand


 HTH :)

The down side to that is the program has to be run which consumes time 
and slows the script down a fair bit (as well as outputting garbage to 
the screen)

-h

-- 
Hari Sekhon

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sending emails using python

2006-09-07 Thread Hari Sekhon




Grant Edwards wrote:

  On 2006-09-07, Sybren Stuvel [EMAIL PROTECTED] wrote:
  
  
Tim Williams enlightened us with:


  Can you send email via it using outlook express or a similar
POP3/IMAP mail client?
  

Wouldn't you use a SMTP client to send email?

  
  
I would, but I don't use exchange server. :)

The one exchange server I used in the past didn't accept SMTP
mail.

  

errr, I used to admin Exchange, if it does accept SMTP then how could
it function as a live mail server?


Hari Sekhon


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: sending emails using python

2006-09-07 Thread Hari Sekhon




Grant Edwards wrote:

  On 2006-09-07, Tim Williams [EMAIL PROTECTED] wrote:

  
  

   Wouldn't you use a SMTP client to send email?

 I would, but I don't use exchange server. :)

The one exchange server I used in the past didn't accept SMTP
mail.

errr, I used to admin Exchange, if it does accept SMTP then
how could it function as a live mail server?
  

Did you mean *doesn't accept* ??

  
  
One presumes he did.

  
  
There are a couple of possibilities at least, of the top of my
head:

* The IMC / IMS (smtp connector) is located on a different
  server within its Exchange organisation, or a DMZ'ed
  exchange server that is only used for SMTP.

* The server doesn't accept SMTP from local IP ranges, only
  from external (ie inbound email)

  
  
I believe it was the latter.  I'm pretty sure there was only
one server.  When I first started at that company it did accept
SMTP connections on it's internal network interface.

  
  
Outlook / Exchange clients use MAPI, internal SMTP is only a
requirement if you have non-MAPI clients sending email through
the server.

  
  
And BOFH was horrified by non-MS software, so he shut off IMAP
support and SMTP support on the internal network as a way to
force everybody to switch to Outlook.

  

I did mean *doesn't accept*, sorry was in a rush...

BOFH? 

lol

If he were a better op then perhaps he would be using unix based system
himself and wouldn't be so discriminating... but then you can't hope
for the world with windows only "techies"... although in fairness,
Exchange and Outlook is a great combination...

you could always try ximian's exchange connector for evolution, I
managed to get some emails with that...

but anyway, ot...


Hari Sekhon




-- 
http://mail.python.org/mailman/listinfo/python-list

Testing a website with HTTPS login and cookies

2006-09-05 Thread Hari Sekhon
Hi everyone,

I want to create a test that will do a proper login test to a web site 
but I need some pointers.

I need to login to the website by accessing an https url and posting to 
that, which should return a very small 302 reply with the address of the 
internal page. I need to take that page href and then access it, if I 
can do all that then the test passes. Of course because I have to access 
two urls I also need cookies for session handling. I've already tried 
bash which would have worked but for the cookie handling and webinject 
which is written in Perl, but this doesn't do exactly what I want in 
terms of output so I'd rather write something myself. (I'm also curious 
as to the best way to do this)

If anybody knows how to do this could they please give me a quick 
pointer and tell me what libraries I need to go read up on?


Thanks

Hari
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: are there any lib for receive hotmail ?

2006-09-05 Thread Hari Sekhon




Tim Chase wrote:

  
And receiving hotmail (or any outher webmail) using scraping
techniques is a daunting task, to say the least - you should
forget about that IMHO.

  
  
There's a perl project called "gotmail" that will do the scraping 
to dump in a local mailbox file (I don't remember whether it's MH 
or mbox format).  You can snag it at

http://gotmail.sf.net

Thus, there's evidence that Hotmail can be scraped, but it seems 
they have a never-ending battle against Hotmail, trying to keep 
up with changes.

I don't know of any Python projects doing the same sort of thing. 
  It might not be too hard to port the Perl project over to 
Python, but one would be an extra level removed from scraping 
Hotmail, playing catchup not only with Hotmail, but then porting 
the latest fixes from Gotmail.

There are a couple other Hotmail-to-mbox scrapers I've heard of, 
but haven't tried.

-tkc




  


I know this isn't technically helping, but why use hotmail when gmail
is so much better and gives you pop access? Even old yahoo gives you
pop access to your mailbox....


Hari Sekhon




-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Open file handles?

2006-08-16 Thread Hari Sekhon




danielx wrote:

  Is there an equivalent in windows?

Jon wrote:
  
  
Perhaps using os you could work with lsof
[http://www.linuxcommand.org/man_pages/lsof8.html]

Jon

Thomas Bartkus wrote:


  This may be more of a Linux question, but I'm doing this from Python. .

How can I know if anything (I don't care who or what!) is in the middle of
using a particular file?

This comes in context of needing to copy a file BUT only if I can verify
that something else doesn't have an open write handle to that file.  IOW - I
need to decline the operation if something else hasn't finished writing to
the file.

How can I know?
Thomas Bartkus
  

  
  
  


yes, handle by sysinternals. 

www.sysinternals.com

not really a python question, more along the lines of bash, which is
why I could answer it

-h




-- 
http://mail.python.org/mailman/listinfo/python-list

Re: os.path.normpath

2006-08-16 Thread Hari Sekhon




[EMAIL PROTECTED] wrote:

  [EMAIL PROTECTED] wrote:
  
  
I am using a windows box and passing a string like "../foo/../foo2" to
normpath which then returns "..\\foo2". But if this string is going
into a webpage link it should really be "../foo".

Is there any way to tell os.path.normpath to act like we are an a unix
style box?

  
  
Use posixpath.normpath() instead.

  

I can't seem to find posixpath in the docs, but I can import posixpath
and a dir shows it does indeed have a normpath.

-h



-- 
http://mail.python.org/mailman/listinfo/python-list

xml.sax problem, help needed.

2006-08-01 Thread Hari Sekhon
I've written an xml parser using xml.sax which works most of the time
but often traces back when trying to read a file. The output of the
traceback is below:

Traceback (most recent call last):
  File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py,
line 271, in run
main()
  File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py,
line 502, in main
body = page_handler(*args, **cherrypy.request.paramMap)
  File netscan.py, line 160, in index
parse()
  File netscan.py, line 117, in parse
parser.parse ( scan_results )
  File /usr/lib/python2.4/xml/sax/expatreader.py, line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
  File /usr/lib/python2.4/xml/sax/xmlreader.py, line 125, in parse
self.close()
  File /usr/lib/python2.4/xml/sax/expatreader.py, line 217, in close
self.feed(, isFinal = 1)
  File /usr/lib/python2.4/xml/sax/expatreader.py, line 211, in feed
self._err_handler.fatalError(exc)
  File /usr/lib/python2.4/xml/sax/handler.py, line 38, in fatalError
raise exception
SAXParseException: /var/log/netscan/scanresults.txt:8:0: no element found


I don't understand why it's telling me that no element is found. It
looks like a problem inside xml.sax, but I'm not sure if I've caused
it or how. The xml file is good and is generated by nmap, it's not
missing tags or anything and is quite small. My script code which has
generated this is below:


#!/usr/bin/env python

import xml.sax
import sys
import os

dir  = '/var/log/netscan'
scan = 'scanresults.txt'
temp = 'tempscan.txt'

scan_results = dir + '/' + scan
temp_results = dir + '/' + temp

if not os.path.isdir(dir):
sys.exit(%s does not exist! exiting... % dir)

network   = [
#{
#status:   ,
#address   :   ,
#hostname  :   ,
#port[0]   :   ,
#protocol[0]   :   ,
#service[0]:   ,
#state[0]  :   ,
#product[0]:   ,
#version[0]:   ,
#extrainfo[0]  :   
#}
]

class scanparser( xml.sax.ContentHandler ):
def __init__(self):
self.host   =  {}
self.host['status'] =  
self.host['address']=  
self.host['hostname']   =  
self.host['port']   =  []
self.host['protocol']   =  []
self.host['service']=  []
self.host['state']  =  []
self.host['product']=  []
self.host['version']=  []
self.host['extrainfo']  =  []


def startElement(self,name,attributes):
global scan_start,scan_stop
if name =='nmaprun':
scan_start = attributes.getValue('startstr')
elif name == 'finished':
scan_stop = attributes.getValue('timestr')
elif name =='status':
self.host['status'] = attributes.getValue('state')

elif name == 'address':
if attributes.getValue('addrtype') == 'ipv4':
self.host['address'] = attributes.getValue('addr')

elif name == 'hostname':
self.host['hostname'] = attributes.getValue('name')

elif name == 'port':
self.host['port'].append( attributes.getValue('portid') )
self.host['protocol'].append( attributes.getValue('protocol') )

elif name == 'service':
self.host['service'].append( attributes.getValue('name') )
if attributes.has_key('product'):
self.host['product'].append( attributes.getValue('product') )
else:
self.host['product'].append()
if attributes.has_key('version'):
self.host['version'].append( attributes.getValue('version') )
else:
self.host['version'].append('')
if attributes.has_key('extrainfo'):
self.host['extrainfo'].append(
attributes.getValue('extrainfo') )
else:
self.host['extrainfo'].append('')

elif name == 'state':
self.host['state'].append( attributes.getValue('state') )


def endElement(self,name):
if name == 'host':
network.append(self.host.copy())
self.host   =  {}
self.host['status'] =  
self.host['address']=  
self.host['hostname']   =  
self.host['port']   =  []
self.host['protocol']   =  []
self.host['service']=  []
self.host['state']  =  []
self.host['product']=  []
self.host['version']=  []
self.host['extrainfo']  =  []


def parse():
global network
parser = xml.sax.make_parser()
parser.setContentHandler( scanparser() )
network = []
parser.parse ( scan_results )


cherrypy tracing back all the time

2006-07-23 Thread Hari Sekhon
I've got a very simple script with cherrypy but for some reason the cherrypy server is constantly tracing back but it stays up, kind of, the performance etc though shows that something is wrong.import cherrypyimport threading
def someFunc(): while 1: print workingthreading._start_new_thread( someFunc , () )class someClass(): def index(self): test=this is a test
 return test index.exposed=Truecherrypy.root=someClass()cherrypy.config.update( file=cherrypyconfig.txt )cherrypy.server.start()The tracebacks I get are:Traceback (most recent call last):
 File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request
 method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run
 request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack
Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request
 method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run
 request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack
Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request
 method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run
 request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack
2006/07/23 17:58:46 INFO Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgi.py, line 110, in wsgiApp environ['wsgi.url_scheme'], File /usr/lib/python2.4/site-packages/cherrypy/_cpserver.py, line 227, in request
 requestLine, headers, rfile, scheme) File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py, line 181, in __init__ self.parseFirstLine() File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py, line 223, in parseFirstLine
 request.method, path, request.protocol = request.requestLine.split()ValueError: too many values to unpackI don't understand, all I've asked the stupid cherrypy thing to do is to print one line...
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: cherrypy tracing back all the time

2006-07-23 Thread Hari Sekhon
On 23/07/06, Hari Sekhon [EMAIL PROTECTED] wrote:
I've got a very simple script with cherrypy but for some reason the cherrypy server is constantly tracing back but it stays up, kind of, the performance etc though shows that something is wrong.import cherrypy
import threading
def someFunc(): while 1: print workingthreading._start_new_thread( someFunc , () )class someClass(): def index(self): test=this is a test
 return test index.exposed=Truecherrypy.root=someClass()cherrypy.config.update( file=cherrypyconfig.txt )cherrypy.server.start()The tracebacks I get are:Traceback (most recent call last):
 File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request
 method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run
 request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack
Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request
 method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run
 request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack
Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request
 method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpackTraceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 242, in run
 request.parse_request() File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgiserver.py, line 134, in parse_request method,path,version = request_line.strip().split( , 2)ValueError: need more than 1 value to unpack
2006/07/23 17:58:46 INFO Traceback (most recent call last): File /usr/lib/python2.4/site-packages/cherrypy/_cpwsgi.py, line 110, in wsgiApp environ['wsgi.url_scheme'], File /usr/lib/python2.4/site-packages/cherrypy/_cpserver.py, line 227, in request
 requestLine, headers, rfile, scheme) File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py, line 181, in __init__ self.parseFirstLine() File /usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py, line 223, in parseFirstLine
 request.method, path, request.protocol = request.requestLine.split()ValueError: too many values to unpackI don't understand, all I've asked the stupid cherrypy thing to do is to print one line...


Ok, when the somefunc() that is executed by threading runs, it doesn't cause the tracing back I showed above, it occurs only when I use one of the other two functions I have in it's place in the script via threading. The thing is, those other two functions both use try excepts inside to make sure there are no exceptions generated beyond the internal function, and neither of those functions can touch anything to do with the class used as the index of the cherrypy document root.
What is going on?
-- 
http://mail.python.org/mailman/listinfo/python-list

threading._start_new_thread executes twice?

2006-07-19 Thread Hari Sekhon
I'm got a script which has a function with a while 1: loop that seems to execute the line it's doing twice instead of just once on each pass when called in a thread...#Script Startimport threading,time,cherrypy
def func(): while 1: print time.ctime() time.sleep(30)threading._start_new_thread( func,() )class somename(): def index(self): test=this is a test
 return test index.exposed=Truecherrypy.root=somename()cherrypy.config.update( file = cherrypy.conf )cherrypy.server.start()#end of script(The contents of the cherrypy.conf
 is below: )[global]server.socketPort=8080server.environment=developmentserver.threadpool = 10The output on the console is at followsWed Jul 19 23:25:34 2006Wed Jul 19 23:25:34 2006
2006/07/19 23:25:34 CONFIG INFO Server parameters:2006/07/19 23:25:34 CONFIG INFO server.environment: development2006/07/19 23:25:34 CONFIG INFO server.logToScreen: True2006/07/19 23:25:34 CONFIG INFO 
server.logFile: 2006/07/19 23:25:34 CONFIG INFO server.protocolVersion: HTTP/1.02006/07/19 23:25:34 CONFIG INFO server.socketHost: 2006/07/19 23:25:34 CONFIG INFO server.socketPort: 80802006/07/19 23:25:34 CONFIG INFO 
server.socketFile: 2006/07/19 23:25:34 CONFIG INFO server.reverseDNS: False2006/07/19 23:25:34 CONFIG INFO server.socketQueueSize: 52006/07/19 23:25:34 CONFIG INFO server.threadPool: 02006/07/19 23:25:34 HTTP INFO Serving HTTP on 
http://localhost:8080/Wed Jul 19 23:26:04 2006Wed Jul 19 23:26:04 2006Wed Jul 19 23:26:34 2006Wed Jul 19 23:26:34 2006If I just replace the threading._start_new_thread line with 
func()func()func()then the time is output only once every thrity seconds, so it seems that threading._start_new_thread is calling func() twice initially?Why is func() executing twice when called from the thread?

-- 
http://mail.python.org/mailman/listinfo/python-list

Progress Bars in python

2006-07-12 Thread Hari Sekhon
Hi,
   I've written a script which backs up a huge bunch of files, but I 
don't want the script to output the file names as it does this as it 
clutters the screen, I only output errors.

So in order to see that the script is working and not stuck, I'd like to 
implement some kind of progress bar or something, perhaps like the 
spinning thing that you often see in linux or freebsd consisting of 
switching / - \ | on the spot to get the appearance of a spinning 
bar I can figure out that the spinning bar is done by switching 
these four chars but I don't know how they get each char to replace the 
last one instead of printing them in succession.

Does anybody have any good suggestions about what the best way of doing 
this or any other suggestions for the best ways to show that the script 
is processing...?


Hari
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Progress Bars in python

2006-07-12 Thread Hari Sekhon
On 12/07/06, Fredrik Lundh [EMAIL PROTECTED] wrote:
 If the output of the script is sent to a logfile, this tends to puke all over the logfile... creating one additional entry per iteration, but it's a good start and I'll look at that link which looks very promising.
there's no way to do this without sending more stuff to stdout than yousee; however, it's easy to *disable* the spinner if you're redirecting theoutput.hint:$ python -c import sys; print sys.stdout.isatty
()True$ python -c import sys; print sys.stdout.isatty() out$ more outFalse/FThanks, that will do nicely. It will be sufficient to do this testing using isatty() and have the progress bar or spinner.
-h
-- 
http://mail.python.org/mailman/listinfo/python-list

catching syntax errors via excepthook?

2006-07-03 Thread Hari Sekhon
I've written an except hook into a script as shown below which works 
well for the most part and catches exceptions.

import sys
def myexcepthook(type,value,tb):
do something

sys.excepthook=myexcepthook
rest of script (now protected by catchall exception hook)


I've been intentionally introducing errors into the code to try to test 
it and while it catches import errors and other things, it doesn't catch 
syntax errors.

Is there a way to get it to catch syntax errors?
Or is there a better way?

-h
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: style question

2006-06-27 Thread Hari Sekhon
On 26/06/06, Claudio Grondi [EMAIL PROTECTED] wrote:
Scott David Daniels wrote: Claudio Grondi wrote: clever stuff to di indentation When necessary to skip first line _and_ indentation: message = 
 This is line 1 This is line 2 This is line 3 .replace('\n', '\n')[1:] # adjust here '\n' to indentation Riffing on this idea:
 message =  This is line 1 This is line 2 This is line 3 .replace( , '\n')[1:]
This was intended as an excercise for the OP in case he likes that kindof solution ...Claudio --Scott David Daniels [EMAIL PROTECTED]
--http://mail.python.org/mailman/listinfo/python-listI've decided to go with

  message = (This is line1. This is line2 This is line3\n)

since I'm declaring many different messages inside various functions I
feel it is important to keep the indentaion and code both aligned
properly so the code looks nice and the output looks nice.

It is easier than doing replace and splicing since I want to keep it as
simple and clean as possible, in true pythonic tradition...

Thanks for the input!

-h

-- 
http://mail.python.org/mailman/listinfo/python-list

how to do -vv for very verbose?

2006-06-27 Thread Hari Sekhon
I'm using optparse.Optionparser to take switches for a script I'm 
writing, but I can't see how to give it -vv for very verbose.

the option for -v is simply set to True by the option parser if present, 
whereas I really want a numeric value, 1 if there is -v and 2 if there 
is -vv.

Any ideas on how to do this?

I think I'll have to stop using  action=store_true.

-h
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: style question

2006-06-27 Thread Hari Sekhon
Claudio Grondi wrote:
 Hari Sekhon wrote:
 On 26/06/06, *Claudio Grondi* [EMAIL PROTECTED] 
 mailto:[EMAIL PROTECTED] wrote:

 Scott David Daniels wrote:
   Claudio Grondi wrote:
   clever stuff to di indentation
  
   When necessary to skip first line _and_ indentation:
 message = 
 This is line 1
 This is line 2
 This is line 3
 .replace('\n  ', '\n')[1:] # adjust here '\n  ' to 
 indentation
  
  
   Riffing on this idea:
   message = 
 This is line 1
 This is line 2
 This is line 3
 .replace(
 , '\n')[1:]

 This was intended as an excercise for the OP in case he likes 
 that kind
 of solution ...

 Claudio

  
   --Scott David Daniels
   [EMAIL PROTECTED] mailto:[EMAIL PROTECTED]
 --
 http://mail.python.org/mailman/listinfo/python-list


 I've decided to go with

message = (
This is line1. 
This is line2 
This is line3\n)

 since I'm declaring many different messages inside various functions 
 I feel it is important to keep the indentaion and code both aligned 
 properly so the code looks nice and the output looks nice.

 It is easier than doing replace and splicing since I want to keep it 
 as simple and clean as possible, in true pythonic tradition...

 Thanks for the input!

 -h

 Thanks for the reply and the explanation what you finally decided to 
 go with.
 I am so happy to have the triple quotes in Python, that I use them 
 whenever possible - this has probably something to do with my bias 
 towards plain text files with human readable content, so as 
 readability is very important to me (and probably to many other 
 Pythonistas) I like most the proposed by me triple quote style as the 
 quotation marks in the style you prefer make the text of the 'message' 
 less easy to read.

 Now we can start a flame war about which style is more 'pythonic' ;-)

 Claudio

I already explained that the triple quotes don't work tidily with 
indentation, either the code is out of alignment or the printout is. 
Either way isn't good.
Triple quotes are best at top level, but since I had to use many 
messages inside functions etc, they broke the layout of the code.

I think they're both best depending on where you are putting them. I'm 
doing very custom messages for each different result, so it would be 
messy to do this at the top level.

-h

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to do -vv for very verbose?

2006-06-27 Thread Hari Sekhon




Fredrik Lundh wrote:

  Hari Sekhon wrote:

  
  
I'm using optparse.Optionparser to take switches for a script I'm
writing, but I can't see how to give it -vv for very verbose.

the option for -v is simply set to True by the option parser if present,
whereas I really want a numeric value, 1 if there is -v and 2 if there
is -vv.

  
  
action="" ?

/F 



  

yeah, I realised this just after I sent it, sort it out.

Thanks.


-h


-- 
http://mail.python.org/mailman/listinfo/python-list

Unbound Local error? How?

2006-06-27 Thread Hari Sekhon
I've got some code as follows:

import re
re_regexname = re.compile('abc')

.
. various function defs
.

def func1():
...
func2()
...

def func2():
if re_regexname.match('abc'):
   do something

if __name__ == '__main__':
func1()


but this returns the Traceback:

UnboundLocalError: local variable 're_regexname' referenced before 
assignment


How?

It was declared in the zero level indentation near the top of the 
script! I don't understand this, isn't a function supposed to be able to 
reference stuff in the containing function or the top level?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unbound Local error? How?

2006-06-27 Thread Hari Sekhon




Diez B. Roggisch wrote:

  Hari Sekhon wrote:

  
  
import re
re_regexname = re.compile('abc')

.
. various function defs
.

def func1():
...
func2()
...

def func2():
if re_regexname.match('abc'):
do something

if __name__ == '__main__':
func1()

  
  

The above clearly is not what you have. See the attached version of the
above that works. So - go check for a typo or something like that.

Diez
  

import re
re_regexname = re.compile('abc')

def func1():
func2()

def func2():
if re_regexname.match('abc'):
print " whohoo!"

if __name__ == '__main__':
func1()
  

you're right, it wasn't that, I was trying to locally override a regex
object as follows:

re_somepattern = re.compile('abc')
re_someotherpattern = re.compile('def')

def func():
    if somecondition:
        re_somepattern = re_someotherpattern

    code block
        re_somepattern.match('something')
    /code.block

not sure how to override this the way I want, it'd be quite messy if I
had to do huge if blocks to handle this inside the other code blocks
that use this re_somepattern.match()

or perhaps I'm going about this all wrong

-h



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Unbound Local error? How?

2006-06-27 Thread Hari Sekhon




Bruno Desthuilliers wrote:

  Hari Sekhon wrote:
  
  
I've got some code as follows:

import re
re_regexname = re.compile('abc')

.
. various function defs
.

def func1():
   ...
   func2()
   ...

def func2():
   if re_regexname.match('abc'):
  do something

if __name__ == '__main__':
   func1()


but this returns the Traceback:

UnboundLocalError: local variable 're_regexname' referenced before
assignment

  
  
this is *not* the traceback. This is only the error message. The
traceback contains all needed informations (or at least all possible
information at this point) to know what happened. But you did not post
the traceback. Nor did you post the minimal runnable code snippet
producing this error.

  
  
How?

  
  
How could we know ?


  


sorry, I know it looks like I was being stingy but the traceback was
not that helpful, not without seeing more a huge amount more of the
code. I was trying to abbreviate.

Traceback (most recent call last):
 File "./backup.py", line 649, in ?
 backup(machine,share)
 File "./backup.py", line 364, in backup
 backupdir(source,destination)
 File "./backup.py", line 398, in backupdir
 (dirlist,filelist) = getdirlisting( source )
 File "./backup.py", line 445, in getdirlisting
 if re_skip_dirs.match(x):
UnboundLocalError: local variable 're_skip_dirs' referenced before
assignment

This doesn't really show that much, I figured the problem was the
following:

def getdirlisting():
  re_skip_dirs = re_skip_top_dirs #Here's the culprit

where both these regex compiled objects were declared at the top level,
it seems that the assignment is trying to use the local variable
re_skip_top_dirs which doesn't exist, that's why I'm getting a
traceback, commenting out this line it runs fine.

-h




-- 
http://mail.python.org/mailman/listinfo/python-list

style question

2006-06-26 Thread Hari Sekhon
Is it better to do:

message = This is line1.
This is line2
This is line3\n

or

message = This is line1.\n
message = message + This is line2\n
message = message + This is line3\n


Since the first method does not follow python's clean and easy looking 
indentation structure but the second just looks crude and ugly anyway.

If I indent the first version so the text is lined up to match code 
indentation then this comes out in the input and isn't aligned there.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: style question

2006-06-26 Thread Hari Sekhon




MTD wrote:

  Hari Sekhon wrote:
  
  
Is it better to do:

message = """This is line1.
This is line2
This is line3\n"""

or

message = "This is line1.\n
message = message + "This is line2\n"
message = message + "This is line3\n"

  
  
Is there any reason you can't do it in one line?

message = "This is line1.\nThis is line2.\nThis is line3.\n"

  

this would also be ugly though, a huge lone line for the paragraph I
want to output, which would wrap around and break the visual
indentation.

otherwise I could do it in one line. but if I was going to do that and
it was going to be so long, I'd just use """..."""


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: What's the best way to wrap a whole script in try..except?

2006-06-22 Thread Hari Sekhon
On 21/06/06, Bruno Desthuilliers [EMAIL PROTECTED] wrote:
Hari Sekhon wrote: I want to wrap a whole script in try ... except. What is the best way of doing this? Consider the following: - try:import modules
CODEdef notifyme(traceback): code to tell me there is a problem except Exception, traceback:notifyme(traceback) Would this code not work because if any part of CODE encounters an
 exception then it won't reach the notifyme() function definition and therefore the whole thing won't work and I won't get notified when a traceback occurs, in fact the call to notifyme() under except will
 itself probably trace back as well!Yes. Do I have to instead do: import a couple of modules def notifyme():code to tell me there is a problem
 try:CODE except Exception, traceback:notifyme(traceback)Would work, but... How you you handle this?I don't put the main logic at the top level - I use a main() function.
import some modulesdef notifyme(e):# code here...def main(*args):try:# code herereturn 0except Exception, e:notifyme(e)return some-non-zero-error-code
if __name__ == '__main__':import syssys.exit(main(*sys.argv))--bruno desthuillierspython -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) forp in '
[EMAIL PROTECTED]'.split('@')])--http://mail.python.org/mailman/listinfo/python-listWhy bother passing the args in def main(*args): and main(*
sys.argv)? Couldn't you just use sys.argv straight off inside main, no passing args aroundI can see you are probably a C programmer as well?-h
-- 
http://mail.python.org/mailman/listinfo/python-list

What's the best way to wrap a whole script in try..except?

2006-06-21 Thread Hari Sekhon
I want to wrap a whole script in try ... except. What is the best way of 
doing this?

Consider the following: -

try:
import modules

CODE

def notifyme(traceback):
   code to tell me there is a problem

except Exception, traceback:
notifyme(traceback)


Would this code not work because if any part of CODE encounters an 
exception then it won't reach the notifyme() function definition and 
therefore the whole thing won't work and I won't get notified when a 
traceback occurs, in fact the call to notifyme() under except will 
itself probably trace back as well!

Do I have to instead do:

import a couple of modules
def notifyme():
code to tell me there is a problem

try:
CODE

except Exception, traceback:
notifyme(traceback)


How you you handle this?


Hari
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: difference between import from mod.func() and x=mod.func()

2006-06-21 Thread Hari Sekhon
I take it that it's not worth listening to that tutorial and just using good old import from, which I suspect is better anyway since it imports less...uneducated premature optimisation - I take it this is insulting a that programmer's progexuality...!
import from it is then unless anybody has anything else to say on the matter.ThanksHariOn 20/06/06, Fredrik Lundh
 [EMAIL PROTECTED] wrote:
Hari Sekhon wrote: What is the difference in terms of efficiency and speed between from os import path and import os path=os.paththe only difference is that the former only sets the path variable, while the
latter leaves both os and path in your namespace. I would think that the import from would be better, just curious since I read somewhere on the web, some guy's code tutorial where he did the
 latter and said it was for efficiency/speed.sounds like uneducated premature optimization to me./F--
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: WinPops

2006-06-21 Thread Hari Sekhon
If you were going to do this you may as well just do something likeif sys.platform='win32': os.system('net send somemachine somemessage')elif sys.platform[:5]='linux' os.system('smblcient -M machine etc...')
This would be more portable and simpler than the alternatives I've seen. It would be better if there was just a cross platform library for this protocol so you could justimport winpopwinpop.send

(host,message)Too much to ask?HariOn 01/06/06, Peter Gsellmann [EMAIL PROTECTED]
 wrote:Roger Upole wrote: Hari Sekhon 
[EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hi, Is there a way of sending winpops (Windows Pop-Up / Net Send messages)
 in python? Perhaps some library or something that I can use under both Windows and Linux? Hari On Windows, you can use win32net.NetMessageBufferSend
. RogerOn Linux, i use the smbclient binary:from subprocess import *q=Popen(['smbclient','-M','maggy'],stdin=PIPE)q.stdin.write('hello!')q.stdin.close()q.wait
()Peter--http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: memory error with zipfile module

2006-06-21 Thread Hari Sekhon
On 20/05/06, Bruno Desthuilliers [EMAIL PROTECTED] wrote:
Roger Miller a écrit : The basic problem is that the zipfile interface only reads and writes whole files, so it may perform poorly or fail on huge files. At one time I implemented a patch to allow reading files in chunks. However I
 believe that the current interface has too many problems to solve by incremental patching,Yeps, that was the general tone of some thread on python-dev. And fromwhat I saw of the source code, it may effectively not be the cleanest
part of the stdlib. But what, it does what it was written for at first :provide working support for zipped packages. and that a zipfile2 module is probably warranted. (Is anyone working on this?)
Seems like Bob Ippolito was on the rank, but I guess you'll get betteranswers on python-dev. In the meantime I think the best solution is often to just run an external zip/unzip utility to do the heavy lifting.
Indeed !-)But while having zip/unzip installed OOTB on a unix-like system is closeto warrented, it may not be the case on Windows.--
http://mail.python.org/mailman/listinfo/python-listShame, I would like to try to improve this but seeing as Roger Miller
has already submitted a patch I don't know how much I can do for this.
In the end I resorted to using an external zip utility via os.system().I'll be interested to know if there is any work
done on improving this as I'm in favour of native python usage, rather
than using os.system() and relying on the operating system having a zip
command, which I'm not convinced is the case on all windows machines,
and also, I'm sure gentoo installs don't have zip by default, since I
had to emerge it on a server for this script to work.
Is it me or is having to use os.system() all the time
symtomatic of a deficiency/things which are missing from python as a
language? Not that I'm complaining, I'm just curious... I'm a
fledgeling programmer so I don't mind being gently corrected by any
veterans around.
Hari
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: What's the best way to wrap a whole script in try..except?

2006-06-21 Thread Hari Sekhon




Jon Ribbens wrote:

  In article [EMAIL PROTECTED], Hari Sekhon wrote:
  
  
I want to wrap a whole script in try ... except. What is the best way of 
doing this?

  
  
You could do this maybe:

  import sys

  def excepthook(exc_type, exc_value, tb):
import modules_needed_to_notify_exception
...

  sys.excepthook = excepthook

  import modules_needed_by_script
  ...
  

I've been trying this out and your method is by far the best I've seen.
This is overriding the default exception handler. 
I don't know the deep magic yet but it works nicely. Thanks!


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: What's the best way to wrap a whole script in try..except?

2006-06-21 Thread Hari Sekhon




Jon Ribbens wrote:

  In article [EMAIL PROTECTED], Hari Sekhon wrote:
  
  
I want to wrap a whole script in try ... except. What is the best way of 
doing this?

  
  
You could do this maybe:

  import sys

  def excepthook(exc_type, exc_value, tb):
import modules_needed_to_notify_exception
...

  sys.excepthook = excepthook

  import modules_needed_by_script
  ...
  

having tested this more, I was worried about a recursive call to
exception should an exception be raised inside the function but luckily
python deals and give you a double traceback.

batteries included indeed.

Thanks again

Hari


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: What's the best way to wrap a whole script in try..except?

2006-06-21 Thread Hari Sekhon




Hari Sekhon wrote:

  
Jon Ribbens wrote:
  
In article [EMAIL PROTECTED], Hari Sekhon wrote:
  

  I want to wrap a whole script in try ... except. What is the best way of 
doing this?



You could do this maybe:

  import sys

  def excepthook(exc_type, exc_value, tb):
import modules_needed_to_notify_exception
...

  sys.excepthook = excepthook

  import modules_needed_by_script
  ...
  
  
having tested this more, I was worried about a recursive call to
exception should an exception be raised inside the function but luckily
python deals and give you a double traceback.
  
batteries included indeed.
  
Thanks again
  
Hari


I see that the exceptionhook is handed 3 things, the name of the error,
the explanation, and the traceback object. 

Does anybody know how I can get the linenumber of the error the way it
does normally, since this is very useful info when debugging. 
I think I need the traceback module but I can't see how to extract the
extra info from the traceback object passed to excepthook.





-- 
http://mail.python.org/mailman/listinfo/python-list

Re: memory error with zipfile module

2006-06-21 Thread Hari Sekhon




Fredrik Lundh wrote:

  Hari Sekhon wrote:

  
  
Is it me or is having to use os.system() all the time symtomatic of a 
deficiency/things which are missing from python as a language?

  
  
it's you.

/F

  

I take it that it's still a work in progress to be able to pythonify
everything, and until then we're just gonna have to rely on shell and
those great C coded coreutils and stuff like that. Ok, I'm rather fond
of Bash+coreutils, highest ratio of code lines to work I've ever
seen it's the real strength of Linux. Shame about Windows...


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: memory error with zipfile module

2006-06-21 Thread Hari Sekhon




Fredrik Lundh wrote:

  Hari Sekhon wrote:

  
  
I take it that it's still a work in progress to be able to pythonify 
everything, and until then we're just gonna have to rely on shell and 
those great C coded coreutils and stuff like that. Ok, I'm rather fond 
of Bash+coreutils, highest ratio of code lines to work I've ever 
seen it's the real strength of Linux. Shame about Windows...

  
  
you make very little sense.

/F

  

how so, this is effectively what we do when we run os.system(). Usually
people are running system commands on unix like machines using
coreutils written in C to do things that are either difficult or near
impossible to do in python...

I've seen people using everything from zip to touch, either out of
laziness or out of the fact it wouldn't work very well in python, this
zip case is a good example. Sometimes when doing system scripts,
they're effectively Bash scripting, but taking longer to do in many
more lines of code cos it's in python. That makes very little sense. 



-- 
http://mail.python.org/mailman/listinfo/python-list

difference between import from mod.func() and x=mod.func()

2006-06-20 Thread Hari Sekhon
What is the difference in terms of efficiency and speed between

from os import path

and

import os
path=os.path


I would think that the import from would be better, just curious since I 
read somewhere on the web, some guy's code tutorial where he did the 
latter and said it was for efficiency/speed.
Not sure if this is right.

Seeing as python folks seem to value simplicity and cleanness, is there 
a preferred coding method? (again I would think the first to the the 
preferred)


Hari
-- 
http://mail.python.org/mailman/listinfo/python-list


WinPops

2006-05-26 Thread Hari Sekhon
Hi,
   Is there a way of sending winpops (Windows Pop-Up / Net Send 
messages) in python?

Perhaps some library or something that I can use under both Windows and 
Linux?

Hari
-- 
http://mail.python.org/mailman/listinfo/python-list


memory error with zipfile module

2006-05-19 Thread Hari Sekhon
I do

import zipfile
zip=zipfile.ZipFile('d:\somepath\cdimage.zip')
zip.namelist()
['someimage.iso']

then either of the two:

A)   file('someimage.iso','w').write(zip.read('someimage.iso'))
or
B)   content=zip.read('someimage.iso')

but both result in the same error:

Traceback (most recent call last):
  File stdin, line 1, in ?
  File D:\u\Python24\lib\zipfile.py, line 357, in read
bytes = dc.decompress(bytes)
MemoryError

I thought python was supposed to handle memory for you?

The python zipfile module is obviously broken...


Any advise?

-- 
http://mail.python.org/mailman/listinfo/python-list


Strange IO Error when extracting zips to a network location

2006-05-17 Thread Hari Sekhon
Hi,
   I've written a script to run on windows to extract all zips under a 
given directory path to another directory path as such:

python extractzips.py fetch all zips under this dir put all extracted 
files under this dir

The purpose of this script is to retrieve backup files which are 
individually zipped under a backup directory tree on a backup server.

This scripts works nicely and has input validation etc, exiting 
gracefully and telling you if you gave a non existent start or target 
path...

When running the script as follows

python extractzips.py \\backupserver\backupshare\machine\folder d:\unziphere

the script works perfectly, but if I do

python extractzips.py \\backupserver\backupshare\machine\folder 
\\anetworkmachine\share\folder

then it unzips a lot of files, recreating the directory tree as it goes 
but eventually fails with the traceback:

  File extractzips.py, line 41, in zipextract
outfile.write(zip.read(x))
IOError: [Errno 22] Invalid argument


But I'm sure the code is correct and the argument is passed properly, 
otherwise a hundred files before it wouldn't have extracted successfully 
using this exact same piece of code (it loops over it). It always fails 
on this same file every time. When I extract the same tree to my local 
drive it works fine without error.

I have no idea why pushing to a network share causes an IO Error, 
shouldn't it be the same as extracting locally from our perspective?

It pulls fine, why doesn't it push fine?


Thanks for any help or suggestions anybody can give me.

Hari
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange IO Error when extracting zips to a network location

2006-05-17 Thread Hari Sekhon
I put a try-pass around the line outfile.write(zip.read(x))  so that 
everything extracts regardless.

After extraction I checked the file size of every single file in the 
directory tree and only 2 files on the network drive are of different 
size to the ones extracted locally.

Both these files are .dbf files that are over 100MB when extracted. Both 
of them is short by exactly 4 KB.

Strange.

I wonder if I'm hitting a size limit or something with this zipfile 
module, or perhaps a python limitation on buffer or something?

I've change the code to

contents=zip.read(x)
outfile.write(contents)

but I still get the same result.

-h

Hari Sekhon wrote:
 Hi,
   I've written a script to run on windows to extract all zips under a 
 given directory path to another directory path as such:

 python extractzips.py fetch all zips under this dir put all 
 extracted files under this dir

 The purpose of this script is to retrieve backup files which are 
 individually zipped under a backup directory tree on a backup server.

 This scripts works nicely and has input validation etc, exiting 
 gracefully and telling you if you gave a non existent start or target 
 path...

 When running the script as follows

 python extractzips.py \\backupserver\backupshare\machine\folder 
 d:\unziphere

 the script works perfectly, but if I do

 python extractzips.py \\backupserver\backupshare\machine\folder 
 \\anetworkmachine\share\folder

 then it unzips a lot of files, recreating the directory tree as it 
 goes but eventually fails with the traceback:

  File extractzips.py, line 41, in zipextract
outfile.write(zip.read(x))
 IOError: [Errno 22] Invalid argument


 But I'm sure the code is correct and the argument is passed properly, 
 otherwise a hundred files before it wouldn't have extracted 
 successfully using this exact same piece of code (it loops over it). 
 It always fails on this same file every time. When I extract the same 
 tree to my local drive it works fine without error.

 I have no idea why pushing to a network share causes an IO Error, 
 shouldn't it be the same as extracting locally from our perspective?

 It pulls fine, why doesn't it push fine?


 Thanks for any help or suggestions anybody can give me.

 Hari

-- 
http://mail.python.org/mailman/listinfo/python-list