Thursday, March 23, 2006

A Python script to get files from FTP site easily

#!/usr/bin/python

from ftplib import FTP
import netrc
import re
import sys

directory = {"siteid":["hostname", "root directory of your ftp site"], "another_site":["another host", "another root directory"]}

if len(sys.argv) > 2:
   host = sys.argv[1]
   off_file = 2
else:
   host = "hostname"
   off_file = 1

if not directory.has_key(host):
   print host, "is not currently supported"
   sys.exit(1)

# we use .netrc to store userid and password info.

net = netrc.netrc()
(user, acct, passwd) = net.authenticators(directory[host][0])

def main(argv):
   print "connecting ...", directory[host][0]
   ftp = FTP(directory[host][0])
   print "login'ing ...", directory[host][0]
   ftp.login(user, passwd)
   for i in range(len(argv)):
   sys.stdout.write("getting ... " + argv[i])
   p = re.compile('(.*)(/[^/]+)')
   m = p.match(argv[i])
   if m:
      path = m.group(1)
      filename = m.group(2)[1:]
      ftp.cwd(directory[host][1] + path)
      if len(path):
         file = open(argv[i], 'wb')
      else:
         file = open(filename, 'wb')
      ftp.retrbinary("RETR " + filename, file.write)
      print " done"
      file.close()
   else:
      print "no matching .."
   ftp.close()

if __name__ == '__main__':
   if (len(sys.argv) > 1):
      main(sys.argv[off_file:])
   else:
      print "Usage: get [host] file_to_be_get\nHost: siteid(default), another_site\n"

Thursday, March 16, 2006

My user script to keep only news content in creaders.net and wenxuecity.com

// ==UserScript==
// @name Keep Only Interested Content
// @namespace http://leochen.net
// @description A script to remove all the un-necessary elements and display plain interested content only (version 0.3)
// @include http://*.wenxuecity.com/*
// @include http://*.creaders.net/*
// @include http://*.bbsland.com/*
// ==/UserScript==

var body = document.body;
var theContent = new Array();
var numContent = 0;

/* for wenxuecity.com */
if (/wenxuecity/.test(document.URL)) {
if (/www/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[3].childNodes[1].childNodes[10].childNodes[1].childNodes[0].childNodes[1].childNodes[0].childNodes[5].childNodes[2].childNodes[1].childNodes[2].childNodes[1].childNodes[0];
} else if (/news/.test(document.URL)) {
var content = document.body.childNodes[3].childNodes[1].childNodes[10].childNodes[1].childNodes[0].childNodes[1].childNodes[0].childNodes[5].childNodes[2].childNodes[1].childNodes[0].childNodes[1].childNodes[6];
theContent[numContent++] = content.childNodes[1].childNodes[2].childNodes[1].childNodes[1].childNodes[1].childNodes[0];
}
}

/* for creaders.net */
if (/creaders.net/.test(document.URL)) {
if (/headline/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[9].childNodes[1].childNodes[0].childNodes[3].childNodes[3];
} else if (/digest/.test(document.URL)) {
if (/pool/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[5].childNodes[13];
theContent[numContent++] = document.body.childNodes[5].childNodes[19].childNodes[1].childNodes[2].childNodes[3];
} else {
theContent[numContent++] = document.body.childNodes[5].childNodes[0].childNodes[8].childNodes[7];
}
} else if (/dailynews/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[5].childNodes[3].childNodes[1].childNodes[0].childNodes[3].childNodes[1];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[13];
theContent[numContent++] = document.body.childNodes[1].childNodes[19];
theContent[numContent++] = document.body.childNodes[1].childNodes[23];
theContent[numContent++] = document.body.childNodes[1].childNodes[27];
theContent[numContent++] = document.body.childNodes[1].childNodes[31];
}
}

if (/bbsland.com/.test(document.URL)) {
if (/bcchinese/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[16];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[13];
theContent[numContent++] = document.body.childNodes[1].childNodes[15];
theContent[numContent++] = document.body.childNodes[1].childNodes[25];
theContent[numContent++] = document.body.childNodes[1].childNodes[36];
}
}
if (/life/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[14].childNodes[7];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[13];
theContent[numContent++] = document.body.childNodes[1].childNodes[15];
theContent[numContent++] = document.body.childNodes[1].childNodes[20];
theContent[numContent++] = document.body.childNodes[1].childNodes[29];
}
}
if (/military/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[16];
theContent[numContent++] = document.body.childNodes[1].childNodes[19];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[16];
theContent[numContent++] = document.body.childNodes[1].childNodes[18];
theContent[numContent++] = document.body.childNodes[1].childNodes[22];
theContent[numContent++] = document.body.childNodes[1].childNodes[29];
theContent[numContent++] = document.body.childNodes[1].childNodes[33];
}
}
if (/general/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[19];
}
}
if (/politics/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[19];
} else {
theContent[numContent++] = document.body.childNodes[11];
}
}
if (/sports/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[14];
} else {
theContent[numContent++] = document.body.childNodes[0].childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[0].childNodes[1].childNodes[13];
theContent[numContent++] = document.body.childNodes[0].childNodes[1].childNodes[15];
theContent[numContent++] = document.body.childNodes[0].childNodes[1].childNodes[22];
theContent[numContent++] = document.body.childNodes[0].childNodes[1].childNodes[33];
}
}
if (/child/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[14];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[5];
theContent[numContent++] = document.body.childNodes[1].childNodes[6];
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[21];
}
}
if (/tea/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[16];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[13];
theContent[numContent++] = document.body.childNodes[1].childNodes[15];
theContent[numContent++] = document.body.childNodes[1].childNodes[20];
theContent[numContent++] = document.body.childNodes[1].childNodes[31];
}
}
if (/joke/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[13];
theContent[numContent++] = document.body.childNodes[1].childNodes[15];
theContent[numContent++] = document.body.childNodes[1].childNodes[20];
theContent[numContent++] = document.body.childNodes[1].childNodes[29];
}
}
if (/iwish/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[15];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[13];
theContent[numContent++] = document.body.childNodes[1].childNodes[15];
theContent[numContent++] = document.body.childNodes[1].childNodes[22];
theContent[numContent++] = document.body.childNodes[1].childNodes[33];
}
}
if (/education/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[5];
theContent[numContent++] = document.body.childNodes[1].childNodes[6].childNodes[8];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[13];
theContent[numContent++] = document.body.childNodes[1].childNodes[17];
theContent[numContent++] = document.body.childNodes[1].childNodes[27];
theContent[numContent++] = document.body.childNodes[1].childNodes[41];
}
}
if (/newland/.test(document.URL)) {
if (/messages/.test(document.URL)) {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[15];
} else {
theContent[numContent++] = document.body.childNodes[1].childNodes[11];
theContent[numContent++] = document.body.childNodes[1].childNodes[22];
theContent[numContent++] = document.body.childNodes[1].childNodes[33];
theContent[numContent++] = document.body.childNodes[1].childNodes[37];
}
}


}

var len = body.childNodes.length;

/* remove all content */
for (i=0; i< len; i++) {
body.removeChild(body.childNodes[0]);
}

/* shown only interested elements */
for (i=0; i< numContent; i++) {
body.appendChild(theContent[i]);
}

my .screenrc file

startup_message off # default: on

# Affects the copying of text regions
crlf off # default: off

#vbell off
vbell_msg " __bell__ ! "

defscrollback 3300 # default: 100

#nethack on

bindkey -k kI copy

bindkey "^n" screen bash
bindkey "^b" next
bindkey "^v" prev
#bindkey "^p" prev
#bindkey "^1" select 0
#bindkey "^2" select 1
#bindkey "^3" select 2

hardstatus alwayslastline " %{= wk} %c | %d.%m.%Y | %{= Bw} %w %{= dd} "
screen -t "bash" 0 bash

Tuesday, March 14, 2006

how to extract an RPM file

rpm2cpio /path/to/file.rpm | cpio -id

Tuesday, March 07, 2006

CD dos path in unix environment

I am working under Windows, but I use cygwin in most of the time.
Sometimes, I have to change directory in cygwin to a path with UNC format (\\machine\path\to\xx)

I'm tired of typing the path myself, because in Unix, we have to use (//machine/path/to/xx) format.

Here comes a simple bash function to do all the conversion and change directory for me.
------------------------
function cddos () {
dos_path=$1;
cd `echo $dos_path | sed 's/\\\/\//g'`;
}
------------------------
Usage:
cddos '\\your\dos\path'

I love Bash function! It should be better to bash function instead of external bash script for such kind of small function.

Thursday, March 02, 2006

A Perl oneliner to extract opcode from a formatted asm source file.

I created a simple perl script to help my co-worker to extract the opcode from some assembly source files.
-----------------------------
Sample Input:
Reset_Handler
$a
Init
0xc0200000: e59ff190 .... LDR pc,[pc,#400] ; [0xc0200198] = 0xc0200004
Instruct_2
0xc0200004: e59f0190 .... LDR r0,[pc,#400] ; [0xc020019c] = 0xc01e0000
0xc0200008: e321f0d1 ..!. MSR CPSR_c,#0xd1

Sample Output:
90
f1
9f
e5
//
90
01
9f
e5
//
d1
f0
21
e3
//
00
d0
40
e2
//

----------------------------------------------
My oneliner version:

perl -e 'map {print "$4\n$3\n$2\n$1\n//\n" if (/^\s+0x\S+:\s+(\S\S)(\S\S)(\S\S)(\S\S)\s+/);} <>; '