2012-07-05

Python Blog

Continuing my whirlwind tour through scripting languages, Here are the main parts of my site generator redone in Python:

Site Generation

os.chdir("entries")
entries = os.listdir(".")
entries.sort()
entries.reverse()

posts = ""
for e in entries:
  cmd = "markdown %s" % e
  post = os.popen(cmd).read()
  date = e

  post_html = template_post % (date,date,post)
  posts += post_html

main_page = template_master % posts

print main_page

Table of Contents

#!/usr/bin/env python

import os
import sys
import re

output = []
for arg in sys.argv:
    with open(arg) as f:
        for line in f:
            if (re.search(r"^###", line)):
                date = arg
                line = re.sub(r"### ", "", line)
                line = re.sub("\n", "", line)
                output_line = '<a href="index.html#%s">%s</a><br>' % (date, line)
                output += [output_line]

output.reverse()
for line in output:
    print line

Python SimpleHTTPServer

Also serving the site using the below code utilizing Python’s included SimpleHTTPServer package.

import SimpleHTTPServer
import SocketServer
import os
import grp
import sys

PORT = int(sys.argv[1])
user = int(sys.argv[2])
group = int(sys.argv[3])

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

os.setgid(group)
os.setuid(user)

print "dropped privileges to user: %s group: %s" % (os.getuid(), os.getgid())
print "Serving at port", PORT
httpd.serve_forever()

Favorite Programming Languages

Currently the list of languages that I am most attracted to is:

  • C
  • Go
  • Perl
  • Erlang
  • JavaScript

This is different from the languages that I would actually claim to be somewhat competent in:

  • C (would like to be more so)
  • Awk
  • Tcl
  • Python
  • JavaScript
  • PHP (somewhat)

I feel like I’m becoming competent in Perl, and I am working to become competent in C# and possibly F# since those are languages that are used or potentially used at work.


Emacs Setup

Setting up yet another emacs instance:

Here are the entries in .emacs that I have to have:

(setq-default indent-tabs-mode 'nil)
 (global-set-key [(control h)] 'delete-backward-char)
Standard

2012-07-04

Interesting PHP Things

Two interesting pieces of software written in PHP. I’ll need to check them out further at some point.

  • FatFreeFramework (F3) – here
  • kelpie (psgi/wsgi/rack style webserver in php) – here

update: Also possibly: this

*SGI/Rack Implementations

Here are the different implementations of web server middleware protocols in the wsgi/psgi/rack family

  • Rack – Ruby – Maybe the original?
  • WSGI – Python – Or maybe this was original?
  • PSGI – Perl
  • JSGI – JavaScript
Standard

2012-07-03

Tkx in Perl

Putting this here so I don’t loose it.

To get Tkx to load without segfaulting, the following environment variable must be set.

export PERL_DL_NONLAZY=1

If it’s set, it works fine

Update: This can be set in a BEGIN block as follows:

BEGIN {
      $ENV{PERL_DL_NONLAZY} = 1;
}

Tcl Version of Blog generator

In my continuing re-implementing of things, I’ve also created a version of the site generation in Tcl.

Here’s the equivalent Tcl for generating the Table of Contents:

#!/usr/bin/env tclsh8.5

set output {}
foreach arg $argv {
    set fh [open $arg "r"]
    while {[gets $fh line] >= 0} {
        if {[regexp {^\#\#\#} $line]} {
            set date $arg
            regsub {^\#\#\# } $line "" line
            lappend output "<a href=\"index.html#$date\">$line</a><br>"
        }
    }
}

foreach line [lreverse $output] {
    puts $line
}

Simple Tcl Webserver

Here’s a simple webserver written in Tcl (Modified from here or here.

I added the ability to choose a port and a user and group to run as, when started as root.

# [Modified from] DustMotePlus - with a subset of CGI support
package require Tclx

set default   index.html

set port      [lindex $argv 0]
set user      [lindex $argv 1]
set group     [lindex $argv 2]
set root      "./public"

set encoding  iso8859-1
proc bgerror msg {puts stdout "bgerror: $msg\n$::errorInfo"}
proc answer {sock host2 port2} {
    fileevent $sock readable [list serve $sock]
}
proc serve sock {
    fconfigure $sock -blocking 0
    gets $sock line
    if {[fblocked $sock]} {
        return
    }
    fileevent $sock readable ""
    set tail /
    regexp {(/[^ ?]*)(\?[^ ]*)?} $line -> tail args
    if {[string match */ $tail]} {
        append tail $::default
    }
    set name [string map {%20 " "} $::root$tail]
    if {[file readable $name]} {
        puts $sock "HTTP/1.0 200 OK"
        if {[file extension $name] eq ".tcl"} {
            set ::env(QUERY_STRING) [string range $args 1 end]
            set name [list |tclsh $name]
        } else {
            puts $sock "Content-Type: text/html;charset=$::encoding\n"
        }
        set inchan [open $name]
        fconfigure $inchan -translation binary
        fconfigure $sock   -translation binary
        fcopy $inchan $sock -command [list done $inchan $sock]
    } else {
#        puts $sock "HTTP/1.0 404 Not found\n"
        puts $sock "Error - Path was: $name"
        close $sock
    }
}
proc done {file sock bytes {msg {}}} {
    close $file
    close $sock
}
socket -server answer $port
id group $group
id user $user
puts "User/Group changed to [id user] : [id group]"
puts "Server ready..."
vwait forever

I’ll probably try serving this website for a little while with this.


Fedora vs Ubuntu

As I’ve been playing around with Tcl, I’ve been struck with how much more up to date the default Tcl and libraries seem to be in Fedora rather than Ubuntu.

The default tcllib and Tclx packages in Ubuntu are still based on Tcl 8.4.

I think that’s probably at least 5 years old.

Standard