Slovenian Gentoo community mirror introduction

written by Domen Kožar, on Feb 2, 2010 12:35:00 AM.

I'm very happy first stage of my Gentoo mirror project is done.

More information about server, how to use it with Gentoo and statitics can be read here

Server is an old box of my girlfriend (thanks!). The box used to have issues with rebooting during ubuntu gnome session, I hope it was graphic's card fault (no graphic desktop is running now).

Consider this beta testing period, spread the world of Slovenian Gentoo mirror! Link is 50/50mbit FTTH. Current location of server is temporary, until I find free provider to sponzor full 100mbit link. Leave any questions at domen[AT]dev.si

Rsync mirror
HTTP source mirror
Statistics

pickle and cStringIO

written by Domen Kožar, on Jan 19, 2010 4:15:18 PM.

More of a note to myself:

You just cannot (I guess because of lacking proper pickling C API support) pickle cStringIO.StringIO instances. Use StringIO.StringIO instead.

Small update to wbus.fubar.si

written by Domen Kožar, on Jan 15, 2010 4:15:00 AM.

As most of you already know, I have small wap site running to check when LPP bus is coming to a bus station in Ljubljana. Today I added name of direction for each bus. Happy puncual days! link

upcoming project: spaces

written by Domen Kožar, on Jan 3, 2010 12:00:00 AM.

I have been thinking about my development issue for a while. Working on many projects and quick switching is a pain. I could not find a solution that even thouches this subject, so I started my own development project management called spaces.

Here is the basic configuration file (in YAML) that already works:
--- !project
name: gspaces

--- !chromium-browser
urls:
    - http://docs.python.org/dev/library/logging.html
    - http://docs.python.org/dev/library/subprocess.html
    - http://docs.python.org/dev/library/optparse.html
    - http://en.wikipedia.org/wiki/YAML#Basic_components_of_YAML
    - http://docs.repoze.org/configuration/index.html

--- !gvim
mode: tabbed
open:
    - gspaces/__init__.py
    - setup.py
    - gspaces.yml

--- !screen
windows:
    - title: gspaces 
      cmd: python manage.py runserver
Obviously, it prepares your workspace and opens some stuff for you. I also plan to add "shutdown" support. I will release it under BSD licence and hope somebody else will find it useful.

Fragmenting one big nginx config

written by Domen Kožar, on Nov 15, 2009 3:51:00 PM.

Getting lost in all glory of big nginx.conf is not that uncommon. I could not stand it anymore, so I wrote this little clever script with support of pyparsing module for Python:
from pyparsing import *

nginx_conf_expr = OneOrMore(Suppress(SkipTo('server' + White())) + originalTextFor(Word('server ') + nestedExpr('{', '}'))).parseWithTabs()
server_name_expr = (Suppress(SkipTo('server_name' + White()) + Word('server_name') + White()) +  CharsNotIn(' ;')).parseWithTabs()

nginx_conf = open('nginx.conf').read()
new_nginx_conf = str(nginx_conf)

for server in nginx_conf_expr.parseString(nginx_conf):
    try:
        name = server_name_expr.parseString(server)[0]
        f = open('nginxsite_%s.conf' % name, 'w+')
        f.write(str(server))
        f.close()

	# update nginx config
	new_nginx_conf = new_nginx_conf.replace(str(server), '')
    except:
        print 'Entry failed:\n', server

open('new_nginx.conf', 'w').write(new_nginx_conf)
Warning! Do not use that on production data before making a backup copy! Script has been tested on ~1500 long nginx config, and is not bulletproof

This script basically extracts all server {} entries and writes them to separate files named "nginxsite_sitename.conf" (all in current working directory). It also produces new_nginx.conf that does not include extracted entries. All you need to do is to add include directive to new_nginx.conf like this:

  # path is relative to this global nginx.conf
  include sites/*.conf;
Note that I haven't made it very customizable (it should be with little python knowledge).

Example:
temp $ ls
migrate_nginx_config.py
nginx.conf

temp $ python migrate_nginx_config.py
migrate_nginx_config.py
nginx.conf
new_nginx.conf
nginxsite_www.fubar.si.conf
nginxsite www.tvnext.si.conf
...

Public release of BurnerOnFire

written by Domen Kožar, on Oct 3, 2009 9:59:00 AM.

I'm proud to release first public version of BurnerOnFire.

BurnerOnFire is written in Python, providing simple GUI(GTK+) and CLI interface to burning .iso images simultaneously to multiple CD/DVD burners.

Documentation and install instructions are located at kiberpipa.org.

Features:
  • write iso image to multiple CD/DVD burners
  • count number of successfully burned discs
  • option to limit number of discs to be burned
  • detects write speeds and intersect results
  • eject/close tray burner
  • TODO: write multiple iso images from a folder

Writing post-installation-script to create shortcut on Windows desktop

written by Domen Kožar, on Sep 27, 2009 3:53:00 PM.

I'm building an GUI program that will be used on Windows platform. I already accepted the fact that I will need three installers (Python, GTK stuff, and one for my package).

Now, I want my installer to place shiny little shortcut on my desktop. Here is the command to generate Window installer:
python setup.py egg_info -RDb "" bdist_wininst --install-script postinstall.py
egg_info -RDb will clear and developemnt tags from release name, so the output will be package-0.1 instead of package-0.1dev
bdist_wininst will invoke Windows Installer builder
--install-script postinstall.py commands that postinstall.py script mentioned in setup.py file will be used for post installation.

Here is the setup.py slice. Note that this script must lie in root of our package:
...
setup(name='package',
      ...
      scripts=['postinstall.py'],
      ...
      )
And finally, the postinstall.py:
#! python
# -*- coding: utf-8 -*-

import os
import sys
import shutil
import my_package

DESKTOP_FOLDER = get_special_folder_path("CSIDL_DESKTOPDIRECTORY")
NAME = 'program.lnk'

if sys.argv[1] == '-install':
    create_shortcut(
        os.path.join(sys.prefix, 'pythonw.exe'), # program
        'Description of the shortcut', # description
        NAME, # filename
        mypackage.__file__, # parameters
        '', # workdir
        os.path.join(os.path.dirname(my_package.__file__), 'favicon.ico'), # iconpath
    )
    # move shortcut from current directory to DESKTOP_FOLDER
    shutil.move(os.path.join(os.getcwd(), NAME),
                os.path.join(DESKTOP_FOLDER, NAME))
    # tell windows installer that we created another 
    # file which should be deleted on uninstallation
    file_created(os.path.join(DESKTOP_FOLDER, NAME))

if sys.argv[1] == '-remove':
    pass
    # This will be run on uninstallation. Nothing to do.
[1] Note that I'm using some custom builtin functions, you can read more about here.
[2] I'm invoking pythonw.exe instead of python.exe because I don't want console to be visible (we are using GUI, remember).
[3] In __init__.py of the my_package, use the casual __name__ == '__main__' trick.

vnstat — useful CLI utility to get current network traffic rates

written by Domen Kožar, on Sep 18, 2009 1:28:00 PM.

Today I was looking for a GNU tool to output current rates of networking traffic. Seems there is no built-in tool in GNU Linux.

I decided to go for third-party software and vnstat does it's job pretty good.
$ vnstat -i eth0 -tr
6 packets sampled in 5 seconds
Traffic average for wlan0

      rx           7.34 kB/s              9 packets/s
      tx           0.94 kB/s              7 packets/s
Add a little of PandoraFMS power, and we have a nice looking graph:)

How to please a woman?

written by Domen Kožar, on Sep 15, 2009 9:04:00 AM.

Man asks; how do I please a woman?

- Well, down there... It depends. Whether you want her to enjoy it, suffer or you just need to take a shit and hurry up. It's like a football park.

If you need to take a shit, just go straight to the goal and keep shooting.
If you want her to enjoy it, just play football on whole field, both goals.
But if you want her to suffer, play both halfs plus injury time.

Read popen.stdout object asynchronously (or why low level knowledge holes are killing me)

written by Domen Kožar, on Sep 13, 2009 12:07:00 PM.

I've been programming for about two years, all this time only in Python. I'll say that those 500 lines I wrote in Flash AS2 before going to Python does not count. I'm fluent with standard library that Python provides, but I've noticed quite a few times now; I'm getting stuck on issues that correlate to low level knowledge. I'm thinking it may trigger someday motivation to learn C.

Going on topic, everyone that has used Popen at least once knows that file-alike objects (stdout, stderr, etc.) are blocking. Let me give you an example:
from subprocess import Popen, PIPE

p = Popen(['command doing lots of I/O and takes long to complete'],
    shell=True, stdout=PIPE)

while p.poll() == None:
    print p.stdout.read()
This snippet will block on "p.stdout.read()" line until spawned process will finish. This behaviour happens because Python I/O implemenation reads everything up until EOF (at least I thought so).

Now, let's add few lines of code:
import os
import fcntl
import gobject
from subprocess import Popen, PIPE

p = Popen(['command doing lots of I/O and takes long to complete'],
    shell=True, stdout=PIPE)
fd = p.stdout.fileno()

#file_flags = fcntl.fcntl(fd, fcntl.F_GETFL)
#fcntl.fcntl(fd, fcntl.F_SETFL, file_flags | os.O_NDELAY)

def test_io_watch(f, cond):
    out = f.read()
    if out == '':
        return False
    print out
    return True

gobject.io_add_watch(p.stdout,
                     gobject.IO_IN | gobject.IO_HUP,
                     test_io_watch)
gobject.MainLoop().run()
I'll comment what happens here (leaving the two lines commented). Popen object is created and subprocess spawned. Later we tell gobject to watch p.stdout for data and if there is some, call test_io_watch. test_io_watch function will read file and print it to stdout. This will happen async, leaving mainloop runing. This will also block output and desired effect is the same as in first snippet.

Now, let's uncomment those two lines and explain why it gives us expected results. fcntl will aquire file-descriptor flags and set additional located in os.O_NDELAY. If we Google for O_NDELAY first hit returns:
The O_NDELAY flag causes read() or write() to return zero instead of blocking.
This changes my guessing of how I/O blocking is handled in Python; it behaves by C implementation based on flags used by filedescriptors. Thus, Python does not (literally) wait for EOF (as it is stated in documentation) but it is just behavior of flags passed when opening a file.

This blog post triggers questions in me:
How deep may one's knowledge be to write code of seemingly simple tasks?
How much time would one spend to find that exact flag without Google?
Do I even know... ?


[#]Reference: http://www.mail-archive.com/pygtk@daa.com.au/msg18159.html