Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, May 22, 2013

SimpleHTTPPutServer in python

Sometimes a small HTTP server which can be used with
$ curl -T dafile dahost.local
is handy. You can use it to upload small logfiles etc without any authentication, so something like tftp.

Python's own SimpleHTTPServer is a simple webserver, but it only supports GET and HEAD requests.
By simply deriving from that class you can create a small server supporting PUT.

Thursday, May 2, 2013

Parallel static python checks with Makefiles

Parallel Ripples

To make testing fun I looked at improving the speed of static syntax checks.
The latest incarnation is now using make targets to represent a specific checks. The nice thing about this is, that you can use Make's -j switch to run those tests in parallel.

So basically it looks like this:

check-static-pep8: $(PYTHONSOURCES:%=%.pep8)
 @echo Passed $@

%.pep8:
 PYTHONPATH=. pep8 -r "$*"

The complete makefile is here and the numbers:

Simple:
...
---
 Passed check-local
---

real 0m22.875s
user 0m20.466s
sys 0m2.158s


And in parallel:
---
 Passed check-local
---

real 0m11.459s
user 0m34.780s
sys 0m3.325s

There is so much that can be done.

Monday, September 3, 2012

Screenshotting /dev/vcs

If you ever wanted to know how to take a "screenshot" of a VCS (where your login prompt is displayed) you can use the following snippet:
cat ../tests/igor/libs/common/vcs.py 
#!/bin/env python
#
# bash only:
# su -c 'cat /dev/vcs3 | fold -w $(stty size | cut -d " " -f 2) > /tmp/term'
#


import Image, ImageDraw
import subprocess
import argparse
import sys


def execute(cmd):
    proc = subprocess.Popen(cmd, shell=True, \
                            stdout=subprocess.PIPE, \
                            stderr=subprocess.STDOUT)
    (stdout, stderr) = proc.communicate()
    proc.wait()
    return str(stdout)

def get_size_of_tty(n):
    """Return the number of rows and cols of tty n
    """
    cmd = "stty -F /dev/tty%d size" % n
    rows, cols = execute(cmd).split(" ")
    return (int(rows), int(cols))

def capture_vcs(n, fold_at):
    """Return the contents of vcs n
    Thsi can also be used with (bash) fold -w
    """
    tty = "/dev/vcs%d" % n
    cmd = "cat %s | fold -w %d" % (tty, fold_at)
    return execute(cmd)

def image_from_vcs(n, stridex=6, stridey=12):
    """Create an image from vcs n
    """
    height, width = get_size_of_tty(n)
    im = Image.new(Image.MODES[6], (width * stridex, height * stridey))
    draw = ImageDraw.Draw(im)
    nl = 0
    screen = capture_vcs(n, fold_at=width)
    for line in screen.split("\n"):
        draw.text((0, stridey * nl), line)
        nl += 1
    return im

def screenshot_from_vcs(n, filename, format="png"):
    """Create a screenshot of a vcs and write it to a file
    """
    im = image_from_vcs(args.vcs)
    im.save(open(filename, "wb"), format.upper())

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Create a screenshot from ' +
                                                 'a console.')
    parser.add_argument('vcs', type=int,
                        help='The VCS/TTY to be captured')
    parser.add_argument('--format', metavar='t', type=str,
                        choices=['png'], default="png",
                        help='The image format')
    parser.add_argument('file', metavar='dst', type=str,
                        help='The destination file')
    args = parser.parse_args()
    screenshot_from_vcs(args.file, args.format.upper())
The images can be used to create a screen capture of your console.

Thursday, July 5, 2012

Auto bash wrapper creation for python functions and oVirt Node CI testing




Well Wrapped

There is currently working going on on bringing CI testing to oVirt Node - our smallish Fedora based "hypervisor".
Enabling automated testing is quite a challenge, because Node is not using anaconda/kickstart for installation, works with a read-only rootfs and uses a snack/newt based TUI. Many existing automated testing solutions have problems with some these aspects - because they rely on kickstart or on ATK.

Anyhow, the testcases which are run on Node are typically written in bash or python. There are a couple of common functions that are needed in both languages (e.g. to communicate with the testing server or providing a common logging function).
It's quite error prone to have functions in both languages providing the same functionality, and that was the point where I looked for a method to automatically or "natively" call python functions from bash (not calling bash from python).
Searching didn't lead to any good alternative, therefor I've come up with the this bash snippet which creates bash functions for all callables of a given python module.
This might not be perfet, but it does the job in our case.

The TUI testing - while we are at it - is now done using uinput.

Sunday, November 27, 2011

How to get a link to your public hardware data (smolt profile).

Smolt is a tool to gather data about your hardware in a distribution independent way. Hardware data can be interesting for a couple of reasons, e.g. developers get detailed informations about hardware involved in bugs.
When opted-in, a smolt profile is created for your hardware after the installation, on the first boot (via firstboot). There is smoltSendProfile (part of the smolt package), which can be used to re-send your hw data.
But there is no easy way to get a link to your public profile - this should be changed, but until then ..
- if you are interested - you can use the following lines, to determine the URL to your public profile:
$ python <<EOF
import sys
sys.path.append ("/usr/share/smolt/client")
import smolt 
print (smolt.get_profile_link(smolt.smoonURL, smolt.getPubUUID ()))
EOF