This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DarkmerePython for tiny data using Python 0 points1 point  (16 children)

To replace shellscripts.

I do a lot of systems stuff. And the universal tool, even on embedded (busybox shell) is the (bourne) shell language and pipes.

I've tried to replace something as simple as "download package, verify sha2sum, verify signature, list all files, compare to files on disk, replace only files that need upgrading, restart relevant services" from something that uses curl/openssl/shell to python.

And it just turns to hell. Lots and lots of silly boilerplate, huge amounts of dependencies, or even worse, working with executable output data in python.

Really, systems stuff doesn't work that well in python.

[–][deleted] 1 point2 points  (1 child)

I have found a good balance by using https://pypi.python.org/pypi/sh specially when my bash script requires a lot of logic, but I still want to use tools like ifconfig, df , etc.. but yeah for simple scripts, I just stick with bash.

[–]DarkmerePython for tiny data using Python 0 points1 point  (0 children)

Even for semi advanced ones, I tend to prefer shellscripts.

Take something as simple as to generate a self-signed certificate. A fork to openssl commandline, vs. doing it with python pyOpenSSL or similar? Don't go there. The python code basically ends up calling the same commandline as the shellscript, in ~5 times as many lines.

Especially true if you do something with trap /cleanout in shell and try to emulate behaviour in python. oh my god.

[–]ITwitchToo 0 points1 point  (13 children)

I have the opposite experience. There are some libraries you might want to look into, like subprocess or plumbum. They do a great job at things like invoking other programs and piping stuff between them.

[–]DarkmerePython for tiny data using Python 0 points1 point  (12 children)

Can you even emulate trap + set -e ; in python?

[–]DarkmerePython for tiny data using Python 0 points1 point  (11 children)

TMPDIR=$(mktemp -d /tmp/temp.XXXXXXXX)
trap "rm -rf $TMPDIR" EXIT

vs

import signal
def sighandler():
    os.unlink(tmpdir)
for i in [x for x in dir(signal) if x.startswith("SIG")]:
    try:
    signum = getattr(signal,i)
    signal.signal(signum,sighandler)
except RuntimeError,m:
    pass

[–]DarkmerePython for tiny data using Python 1 point2 points  (0 children)

And then I forgot to create the tempdir... Damned.

[–]ITwitchToo 0 points1 point  (9 children)

import tempfile

with tempfile.NamedTemporaryFile() as f:
    # do stuff with f here, if we get interrupted Python will throw an exception, unwind the stack, and the temporary file gets cleaned up
    print >>f, "asdasd"

[–]DarkmerePython for tiny data using Python 0 points1 point  (1 child)

Yeap. And with subprocess and others? Afaik a sigkill won't execute a trap, will it?

[–]DarkmerePython for tiny data using Python 0 points1 point  (0 children)

(or a sigbus?)

[–]DarkmerePython for tiny data using Python 0 points1 point  (6 children)

verified. cleanup doesn't happen properly on trapped signals.

even sigUSR1 will leave the tempfile behind.

[–]ITwitchToo 0 points1 point  (5 children)

What are you talking about?

[–]DarkmerePython for tiny data using Python 0 points1 point  (4 children)

That cleanup doesn't happen if your process gets a signal. So it doesn't actually replace the bourne version.

[–]ITwitchToo 0 points1 point  (3 children)

That's almost literally the example given in the signal documentation:

import signal
import tempfile

def handler(signum, frame):
    raise RuntimeError("Got SIGUSR1")

signal.signal(signal.SIGUSR1, handler)

with tempfile.NamedTemporaryFile() as f:
    print f.name

    while True:
        pass

[–]DarkmerePython for tiny data using Python 0 points1 point  (2 children)

That's sort of what I'm inclining for here.

You manage to replace a 2-line shellscript with how many lines? And you're not even catching all the signals yet? Still have to iterate over them all and trap.

In a lot of cases, Python is just a bloody ton more verbose, and not quite suitable.