星期二, 4月 22, 2008

[tips] poorman's uudecode

For those who cursed by ash and awk, welcome to 80's !

now you can send everything in ASCII -- hooray!!

#!/bin/sh
#
# uudecode.sh
#
# Author: Ximian, Inc.
#
# Modified: by timchen119 at http://timchen119.blogspot.com from Ximian go-gnome's GPL code
#
# Usage: uudecode.sh encodedfile > decodefile
#
# License: GPL
#
# Download: http://kalug.linux.org.tw/~tim/gpl/uudecode.sh
#
# Ref: http://ftp.cesnet.cz/pub/ximian-gnome/installers/go-gnome
#
# The Ximian Desktop Pre-Installer
#
# Comments to:
# distribution@ximian.com
#
# Copyright 2000-2001, Ximian, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
# USA.
#
#
# This script and its embedded programs are distributed with
# absolutely, positively NO WARRANTY WHATSOEVER, without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. The author and Ximian, Inc. take no responsibility for
# the consequences of running this script.
#
#

CACHEDIR=/tmp
_awk="awk"

# poor man's uudecode
_awkprog="$CACHEDIR/uudecode.awk"
# encoded file
_uudecode_in=$1

#awk-script
cat > ${_awkprog} <<EOF
function x(l, p) {
n="!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_";
return index(n,substr(l,p+1,1));
}
/^begin/ {}
/^[^be]/ {
len = x(\$0, 0);
for (i=1;len>0;i+=4) {
a=x(\$0,i);b=x(\$0,i+1);c=x(\$0,i+2);d=x(\$0,i+3);
printf("%c",a*4+b/16);
if (len>1) {
printf("%c",b*16+c/4);
if (len>2) {
printf("%c",c*64+d);
}
}
len-=3;
}
}
EOF
${_awk} -f ${_awkprog} < ${_uudecode_in}
rm -f ${_awkprog}


get uudecode.sh at here.

[funny] [links] [kuso] Python がイマイチ人気にならないたった一つの理由

http://d.hatena.ne.jp/seiunsky/20080417/1208454191

話說不小心點到這... 原本還想認真看看理由的... 結果... @@


1. 日本Python的入門書不好 !?


* 「最初(に読むPython本)はみんPyだよねー」

==> 「最初はみんなおっぱいだいすきからスタートする」

2. 著名書商 O'reilly 也很糟糕 ?!


* 「おれ、今Pyチューで勉強してるんだ!」

==> こんなセリフを女子に聞かれたら間違いなく「おっぱいにチューして何の勉強をしているの!へんたい!」と思われるのですから、これは本当に卑猥ですね。

3. PyPy實作不好 !?

* 「おれ、いま PyPy の開発してるんだ!」
* 「ただの人間には興味ありません。PyPy 開発に興味のある人は私のところに来なさい!以上!」
* 「PyPyを高速で動かす方法を思いついた!」

接下來就開始把python跟おっぱい扯在一起的大爆走了 XD (pychinko, Dive Into Python... 原來Python是完全的成人向啊...orz)

星期二, 4月 15, 2008

[tips] upgrading to gentoo 2008.0

Well just as usuals, you can upgrades to gentoo 2008.0 quickly. (beta?)

#eselect profile list
Available profile symlink targets:
[1] default-linux/x86/2006.1
[2] default-linux/x86/no-nptl
[3] default-linux/x86/2006.1/desktop
[4] default-linux/x86/2007.0
[5] default-linux/x86/2007.0/desktop
[6] hardened/x86/2.6
[7] selinux/2007.0/x86
[8] selinux/2007.0/x86/hardened
[9] default/linux/x86/2008.0
[10] default/linux/x86/2008.0/desktop *
[11] default/linux/x86/2008.0/developer
[12] default/linux/x86/2008.0/no-nptl
[13] default/linux/x86/2008.0/server
[14] hardened/linux/x86

For my case, just type 'eselect profile set 10'
and emerge world again.

I'm not migrating to OpenRC and baselayout2 yet, however I'm using KDE4.0.3 now, so I think I have enough un-stable-ness to worry about. ;0

[tips] using pyrex to do the argv[0] trick in linux using python.

A trick to rename python process in output of 'top' and 'ps'.


set_argv.c:

#include
#include
#include

int get_argc(){
int argc;
char **argv;
Py_GetArgcArgv(&argc, &argv);
return argc;
}

void set_argv_via_number(int num,char *str){
int argc;
char **argv;
Py_GetArgcArgv(&argc, &argv);
strncpy(argv[num], str , strlen(str));
memset(&argv[num][strlen(str)], '\0', strlen(&argv[num][strlen(str)]));
}


set_processname.pyx:

#Warning: this module is not safe and may involve some dirty hacks.
import sys,dl,glob
cdef extern int get_argc()
cdef extern void set_argv_via_number(int num,char *buf)
def set_argv0(thename):
set_argv_via_number(0,thename)

def set_argv(thename):
argc = get_argc()
print argc
for i in xrange(argc+1):
if i==0:
set_argv0(thename)
else:
pass
def set_name(thename):
if sys.platform == 'linux2':
# Set process name. Only works on Linux >= 2.1.57.
try:
libc_loc = glob.glob("/lib/libc.so*")[0]
libc = dl.open(libc_loc)
libc.call('prctl', 15, thename, 0, 0, 0) # 15 is PR_SET_NAME
except:
pass

def set_processname(thename):
space_padding_num = len(sys.argv[0])-len(thename)
if space_padding_num < 0:
#thename is longer than original name
newname = thename[:len(sys.argv[0])]
else:
#thename is shorter. add padding.
newname = thename + " " * space_padding_num

set_name(newname)
set_argv(newname)

compile.sh:
#!/bin/sh
CC=mipsel-linux-gcc

pyrexc set_processname.pyx
$CC -c -fPIC -I/usr/include/python2.5/ set_argv.c
$CC -c -fPIC -I/usr/include/python2.5/ set_processname.c
$CC -shared set_argv.o set_processname.o -o set_processname.so

[tips] building psyco on non-x86 platform. (ie. linux mips little endian)

The trick is to build psyco in ivm mode. The mode slower than the native mode(ivm mode involves a prolog generated-vm, you guess), however it's the only way to compile psyco on non-x86 system. And cross-compiling with distutils is not a trivial stuff, so I just compile it directly.


mipsel-linux-gcc -pthread -fno-strict-aliasing -DNDEBUG -fPIC -DALL_STATIC=1 -Ic/ivm -I/usr/include/python2.5 -c c/psyco.c -o build/temp.linux-mipsel-2.5/c/psyco.o
mipsel-linux-gcc -pthread -fno-strict-aliasing -DNDEBUG -fPIC -DALL_STATIC=1 -Ic/ivm -I/usr/include/python2.5 -c c/platform.c -o build/temp.linux-mipsel-2.5/c/platform.o
mipsel-linux-gcc -pthread -shared build/temp.linux-i686-2.5/c/psyco.o build/temp.linux-mipsel-2.5/c/platform.o -L/usr/lib -lpython2.5 -o build/lib.linux-i686-2.5/psyco/_psyco.so


This generates the C-module extension, and just copy all of py files to correct path and enjoy psyco speed-up! (well, not quite a case for me. It just use way too much memory on our embedded system. But I think maybe it'll useful in other project.)

ps: I should mentioned that I'm doing this on a uclibc linux system.

[tips] if as a pure function in python

http://okmij.org/ftp/Scheme/if-function.txt

here is a trick that we could rewrite a customized not-a-special-form IF in python.


iftrick.py:

#!/usr/bin/env python
def func_true():
print "true!"

def func_false():
print "false!"

def my_if(my_cond,then_func,else_func):
eval((then_func, else_func)[not my_cond].func_code,vars())

condition = True

my_if(condition,func_true,func_false)

condition = False

my_if(condition,func_true,func_false)

===
#python iftrick.py
true!
false!