Monday, 26 April 2010

Installed Erlang on OS X 10.6.3 Snow Leopard

I bought 'Seven Languages in Seven Weeks' and one of those languages is Erlang. The version on Fink is v.12, but the latest is version 13, so I decided to install it myself from source. I discovered the directory path for installation is not allowed to have a space in it, so I made a path with no spaces and downloaded there. First the download and unpack; OS X doesnt have wget by default, but curl is there:

curl 'http://www.erlang.org/download/otp_src_R13B04.tar.gz' -o 'otp_src_R13B04.tar.gz'
tar -xzvf otp_src_R13B04.tar.gz
curl 'http://www.erlang.org/download/otp_doc_man_R13B04.tar.gz' -o 'otp_doc_man_R13B04.tar.gz'

Create the directory for installation:
sudo mkdir -p /sw/erlang


Now configure, make and install:
cd otp_src_R13B04
./configure --prefix=/sw/erlang --enable-threads -enable-smp-support --enable-kernel-poll
make
sudo make install

Copy the documentation into place and extract it there:
cp ../otp_doc_man_R13B04.tar.gz /sw/erlang/
cd /sw/erlang
sudo tar -xzvf otp_doc_man_R13B04.tar.gz
sudo rm otp_doc_man_R13B04.tar.gz

Edit your ~/.bashrc to include the documentation and the new erlang binary in the appropriate paths; I use the 'pico' editor, but the lines you add are:
export PATH=$PATH:/sw/erlang/bin
export MANPATH=$MANPATH:/sw/erlang/man

...and source that script to get the new paths with . ~/.bashrc.
Now type 'erl' to get in the Erlang repl, and 'ctrl-C a' to exit.

Thursday, 22 April 2010

W3C schema for the RFC 1123 date format

Here's a quickie I didn't find anywhere: XSD for the RFC 1123 date format, as used in http headers; note its only for GMT and years 2000 -2099 (my particular case):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation><xs:documentation>Schema for RFC 1123 date format type</xs:documentation></xs:annotation>

<xs:simpleType name="RFC1123_date">
<xs:annotation><xs:documentation>RFC 1123 date format.</xs:documentation></xs:annotation>

<xs:restriction base="xs:string">
<xs:pattern value="(Mon|Tue|Wed|Thu|Fri|Sat|Sun), [0-3][0-9] (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) 20[0-9][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] GMT">
</xs:restriction>
</xs:simpleType>
</xs:schema>

Wednesday, 17 March 2010

Python http PUT request

I have a CherryPy service which accepts 'PUT' requests with a named payload parameter (it's name is , surprisingly, 'payload'). From cURL, this is easy to specify with the -F option, but I couldn't find anywhere how to do this with Python. I discovered something a bit odd, which seems to be that the payload needs to be specified twice:

import urllib, httplib
import time, calendar
import base64

if __name__=='__main__':
timenow=time.gmtime()
t=time.asctime(timenow)
iov1=str(calendar.timegm(timenow))+'000000000'
iov2=str(calendar.timegm(timenow) + 1) +'000000000'
p=''+t+''
px=urllib.quote_plus(p)
pe=urllib.urlencode({"payload":p})
urlbase='putserver.cern.ch:8081'
testPut='DEVDB10/ATLAS_SCT_COMMCOND_DEV/PUTS200/TEST/PUT/Multi/timespan/'+iov1+''+iov2+"?payload="+px
test='/cooldb'
testMerge='/cooldb/merge'
print px
print urlbase
authHeader={"Authorization": "Basic "+base64.encodestring('name:pwd'), "Content-Length":str(len(px))}
#payload=px
h=httplib.HTTPConnection(urlbase)
h.request('PUT',testPut,px,authHeader)
r=h.getresponse()
print r.status, r.reason
print r.read()
h.close()

Wednesday, 3 March 2010

I wanted an iomanipulator to escape XML

...and so began a steep learning curve. I had some strings to insert into XML being output on the cout stream, and ideally I would simply like to do something like this:
string myXml("xml&<>");
cout<<escapexml<<myXml;

..and the output should be 'escaped'.
Of course I could have made a function and simply transformed one string into another, but I was also keen to learn about the iostream framework. Here are a few ways which iterate towards the final goal
1. 'Adapt' the string
Custom inserters can be made for a given class, so by wrapping the string in a class, I cold simply make an inserter for that class:
Here is the header:

class Wrapper{

std::string &m_data;

public:

typedef std::string::value_type value_type;Font size

Wrapper(std::string & data):m_data(data){ }

std::string &str(){

return m_data;

}

};


std::ostream &

operator << (std::ostream & os, Wrapper & s);


...and the definition for that inserter:

std::ostream &

operator<< (std::ostream & os, Wrapper & s){

XmlEscape x(os);

for_each(s.str().begin(),s.str().end(),x);

return os;

}


where XmlEscape is a functor:


class XmlEscape{

static const char * m_chars;

static const char * m_replacements[];

static const unsigned int m_nchars=5;

std::ostream & m_os;

public:

XmlEscape(std::ostream & os);

void operator()(char c);

std::ostream & ostream();

};


with implementation:


const char * XmlEscape::m_chars ="&\'\"><";

const char * XmlEscape::m_replacements[]={"&amp;", "&apos;", "&quot;", "&gt;", "&lt;"};


XmlEscape::XmlEscape(std::ostream & os):m_os(os){

//nop

}


void

XmlEscape::operator()(char c){

unsigned int i(m_nchars);

while(i--){

if (c==m_chars[i]) {

m_os<<m_replacements[i];

return;

}

}

m_os<<c;

}

2. Adapt the stream