Thursday, March 10, 2011

While generating assembly drawings for a recent PCB, I wanted to printout the silkscreen layer at 2x scale.  The image was in an eps and the following almost did the trick:

pstops 1:0@2.0 silk.eps silk2.eps

However, the bounding box wasn't updated with the scale.  Hand-editing the header of the output file from:

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 217 217;
%%Pages: 1

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 434 434
%%Pages: 1

did the trick nicely.

Monday, January 24, 2011

Run a subprocess with a timeout

While tweaking a supybot IRC bot command for running Qalculate, I had the need to limit the execution time to guard from long-running expressions. From Stack Overflow I found this and modified it for my needs:

from subprocess import Popen, PIPE                                              
import signal
class ProcessTimeout(Exception):

def timeout_handler(signum, frame):
    raise ProcessTimeout

def run_timeout(cmd, timeout=None):
    if timeout:
        signal.signal(signal.SIGALRM, timeout_handler)
    proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
    stdout = stderr = ''
        stdout, stderr = proc.communicate()
    except ProcessTimeout:
        stdout = 'Calculation was taking too long, so I killed it dead.'
    del proc
    return (stdout, stderr)

Then, just call run_timeout with a list of command arguments and move on.