Archive

Archive for the ‘Tech.Notes’ Category

Python批量修改文件名

March 24th, 2011 No comments

在做Android GridView 例子的时候, 图片的文件名字只能是小写字母a~z, 数字0~9 特殊符号只能用-
电脑上图片有很多都是大写的格式也比较乱, 于是想批量修改文件名, 用Dos太不爽了, 就用Python写个简单的脚本.

import os,os.path
import shutil,string
dir = 'D:\\Images\\2011.03.24'
count = 0
for i in os.listdir(dir):
    name = i.split(".")
    type = name[len(name)-1]
    newfile = "%03d"%count + "." + type
    #newfile = i.replace('jpg','.jpg')
    oldfullfile = dir+'\\'+i
    newfullfile = dir+'\\'+newfile
    print oldfullfile
    print newfullfile
    shutil.move(oldfullfile,newfullfile)
    print i
    count += 1
Categories: Tech.Notes Tags:

Syntax highlighting for golang

January 29th, 2011 No comments

在网上搜到Allister SanchezSyntaxHighlighter写了个高亮显示的扩展, js代码如下:

shBrushGo.js

SyntaxHighlighter.brushes.Go = function()
{
	// Copyright 2010 Sanchez, Allister Levi

	var declaration_type_words = 'struct func interface map chan package import type const var';

	var control_words = 'goto break continue if else switch default case for range go select return fallthrough defer';

	this.regexList = [
		{ regex: SyntaxHighlighter.regexLib.singleLineCComments,	css: 'comments' },			// one line comments
		{ regex: SyntaxHighlighter.regexLib.multiLineCComments,		css: 'comments' },			// multiline comments
		{ regex: SyntaxHighlighter.regexLib.doubleQuotedString,		css: 'string' },			// strings
		{ regex: SyntaxHighlighter.regexLib.singleQuotedString,		css: 'string' },			// strings
		{ regex: new RegExp(this.getKeywords(declaration_type_words), 'gm'),		css: 'color2 bold' },
		{ regex: new RegExp(this.getKeywords(control_words), 'gm'),		css: 'keyword bold' }
		];
};

SyntaxHighlighter.brushes.Go.prototype	= new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Go.aliases	= ['go', 'golang'];

把这段js代码放到 wp-content/plugins/syntax-highlighter-mt/scripts 目录下, 并修改文件syntax-highlighter-mt/stxhighlightmt.php文件在文中增加:

      'xml xhtml xslt html    $x/scripts/shBrushXml.js',
      'go golang    $x/scripts/shBrushGo.js'

修改完后,在的博客里面可以这样使用:

package main
import "fmt"
func main() {
    fmt.Printf("yoohoo!")
}

显示效果如下:

package main
import "fmt"
func main() {
    fmt.Printf("yoohoo!")
}

参考: http://hackgolang.blogspot.com/2010/05/syntax-highlighting-for-golang.html

Bian Jiang
– EOF–

使用Go读取网页信息

January 29th, 2011 3 comments

打算用Go写个抓取网页的工具,就看了一下HTTP包, 简单的写了两个例子.
格式化后的代码参考: https://gist.github.com/801826

update: 由于新版本对log, http.header包做了调整, 更新代码
log.Exit –> log.Fatal
req.Header = map[string]string{} –> req.Header = map[string][]string{}

1. 直接使用http.Get取得信息

package main

import (
   "log"
   "io/ioutil"
   "http"
)

func main() {
   res, _, err := http.Get("http://bbs.golang-china.org/")
   if err != nil {
      log.Fatal(err)
   }
   defer res.Body.Close()

   body, _ := ioutil.ReadAll(res.Body)
   log.Println(string(body))
}

2. 自己定义HTTP的头信息

package main

import (
	"log"
	"http"
	"net"
	"io/ioutil"
)

func main() {

	url, err := http.ParseURL("http://bbs.golang-china.org/")

	if err != nil {
		log.Fatal(err)
	}

	tcpConn, err := net.Dial("tcp", "", url.Host + ":80")

	if err != nil {
		log.Fatal(err)
	}

	clientConn := http.NewClientConn(tcpConn, nil)

	var req http.Request
	req.URL = url
	req.Method = "GET"
	req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Chrome/9.0.570.0 Safari/534.11"
	req.Header = map[string][]string{}
	req.Header.Add("Connection", "keep-alive")

	err = clientConn.Write(&req)
	if err != nil {
		log.Fatal(err)
	}
	resp, err := clientConn.Read(&req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()		

	log.Println("Http Response: " + resp.Status)
	body, _ := ioutil.ReadAll(resp.Body)

	log.Println(string(body))
}
Categories: golang, Tech.Notes Tags: , , ,

Google Code 与 Github同步

September 28th, 2010 No comments

由于Golang中文翻译项目同时采用Google Code和Github两种方式管理,这里简单的记录一下流程.

1. 从Google Code代码库里面取得历史数据
border@colinux:~/work/golang-china$ git svn clone https://golang-china.googlecode.com/svn/trunk .
border@colinux:~/work/golang-china$ git branch -a
* master
remotes/git-svn

2. 增加Github源
border@colinux:~/work/golang-china$ git remote add github git@github.com:border/golang-china.git
同步github
border@colinux:~/work/golang-china$ git fetch github
remote: Counting objects: 37, done.
remote: Compressing objects: 100% (27/27), done.
remote: Total 30 (delta 14), reused 13 (delta 3)
Unpacking objects: 100% (30/30), done.
From github.com:border/golang-china
* [new branch] master -> github/master

border@colinux:~/work/golang-china$ git branch -a
* master
remotes/git-svn
remotes/github/master

border@colinux:~/work/golang-china$ git branch -r
git-svn
github/master

给github的源创建一个单独的分支
border@colinux:~/work/golang-china$ git checkout -b github github/master
Branch github set up to track remote branch master from github.
Switched to a new branch ‘github’

border@colinux:~/work/golang-china$ git branch
* github
master

border@colinux:~/work/golang-china$ git branch -a
* github
master
remotes/git-svn
remotes/github/master

border@colinux:~/work/golang-china$ git status
# On branch master
nothing to commit (working directory clean)

border@colinux:~/work/golang-china$ git branch
github
* master

3. 在master分支基础之上创建一个自己的分支用于与GoogleCode和Github进行合并
border@colinux:~/work/golang-china$ git checkout -b border
Switched to a new branch ‘border’

查看当前多有得分支
border@colinux:~/work/golang-china$ git branch -a
* border
github
master
remotes/git-svn
remotes/github/master

现在可以在border分支上与github合并
border@colinux:~/work/golang-china$ git merger github

解决一些冲突后,然后border分支目前是最新的版本。

接下来分别切换到master和github分支,并与border进行合并。

border@colinux:~/work/golang-china$ git checkout master
border@colinux:~/work/golang-china$ git merger border
在master分支下,提交相关的版本
border@colinux:~/work/golang-china$ git svn dcommit // 上传本地的代码到Google Code

border@colinux:~/work/golang-china$ git checkout github
border@colinux:~/work/golang-china$ git merger border
border@colinux:~/work/golang-china$ git push

git svn rebase // 本地与Goolge Code 代码同步
git svn dcommit // 上传本地的代码到Google Code

1. git-remote(1) Manual Page http://www.kernel.org/pub/software/scm/git/docs/git-remote.html
2. Google Code 与 Github代码同步 http://wifihack.net/blog/2010/01/google-code-svn-and-github-sync/
3. 混合使用Git SVN 和Git http://zoomquiet.org/res/scrapbook/ZqFLOSS/data/20081212152336/index.html

Categories: golang, Tech.Notes Tags: , , ,

Cross Compile SDL

August 3rd, 2010 No comments

==============================
jpeg-8b

./configure –host=arm-linux –prefix=/opt/rootfs/cross/arm/usr
make
make install

==============================
libpng-1.2.44

./configure –host=arm-linux –prefix=/opt/rootfs/cross/arm/usr/
make
make install

==============================
zlib-1.2.5

export CC=arm-linux-gcc
./configure –shared –prefix=/opt/rootfs/cross/arm/usr
make
make install

==============================
e2fsprogs-1.41.12

./configure –host=arm-linux –prefix=/opt/rootfs/cross/arm/usr
make
make install

==============================
freetype-2.3.12

./configure –host=arm-linux –prefix=/opt/rootfs/cross/arm/usr
make
make install

==============================
DirectFB-1.4.3
依赖: JPEG, libpng, e2fsprogs(uuid), zlib, freetype2
make的时候如果在Makefile里面出现没有相关的头文件就进相关的Makefile修改CFLAGS增加你头文件和库文件的地址, 如果你不是Davinci平台,就进入gfxdrivers/Makefile把”DAVINCI_DIR”相关的东西都屏蔽掉。

./configure –host=arm-linux –prefix=/opt/rootfs/cross/arm/usr
make
make install

==============================
SDL-1.2.14
依赖: JPEG, libpng, e2fsprogs(uuid), zlib,
——

export NM=arm-linux-nm
export LD=arm-linux-ld
export CC=arm-linux-gcc
export CXX=arm-linux-g++
export RANLIB=arm-linux-ranlib
export AR=arm-linux-ar

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/arm/4.2.2-eabi/:/usr/local/arm/4.2.2-eabi/usr/:/opt/rootfs/cross/arm/usr/

如果找不到directfb库,修改Makefile, 在CFLAGS中增加你的相关库文件的路径

CFLAGS = -g -O2 -I/opt/rootfs/cross/arm/usr/include -L/opt/rootfs/cross/arm/usr/lib
LDFLAGS = $(CFLAGS)

./configure –enable-video-qtopia –enable-video-directfb –enable-video-opengl –disable-video-dummy –disable-video-fbcon –disable-video-dga –disable-arts-shared –disable-arts –disable-esd –disable-esd-shared –disable-alsa –disable-cdrom –disable-video-x11 –disable-nasm –disable-diskaudio –disable-mintaudio –disable-video-ps2gs –disable-atari-ldg –disable-video-photon –disable-nas –host=arm-linux –prefix=/opt/rootfs/cross/arm/libsdl

编译sdltest
cd test
修改Makefile增加一些库

CFLAGS = -g -O2 -I/opt/rootfs/cross/arm/libsdl/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -L/opt/rootfs/cross/arm/libsdl/lib -I/opt/rootfs/cross/arm/libsdl/include -I/opt/rootfs/cross/arm/usr/include -L/opt/rootfs/cross/arm/usr/lib -I/opt/rootfs/cross/arm/libsdl/include/SDL
LIBS = -L/opt/rootfs/cross/arm/libsdl/lib -lSDL -lpthread -ldirectfb -ljpeg -lpng -ldirect -lpng12 -lz -lfusion -lfreetype

*****************************************

1. 挂接命令:

mount -o nolock -t nfs 10.0.3.244:/long_nfs /tmp/
mount -o nolock 10.0.3.244:/long_nfs /tmp/

这两个都可以。
2. 可能出现的错误类型
a. 错误类型:

rpcbind: server localhost not responding, timed out
RPC: failed to contact local rpcbind server (errno 5).
lockd_up: makesock failed, error=-5
rpcbind: server localhost not responding, timed out
RPC: failed to contact local rpcbind server (errno 5).
mount: mounting 10.0.3.244:/long_nfs on /tmp/ failed: Input/output error

这个问题是因为挂接命令不对,要加上参数-o nolock就可以了

但是NFS不稳定

nfs: server 192.168.2.213 not responding, still trying
nfs: server 192.168.2.213 not responding, still trying
nfs: server 192.168.2.213 OK
nfs: server 192.168.2.213 OK
nfs: server 192.168.2.213 not responding, still trying
nfs: server 192.168.2.213 not responding, still trying
nfs: server 192.168.2.213 OK
nfs: server 192.168.2.213 OK
nfs: server 192.168.2.213 not responding, still trying
nfs: server 192.168.2.213 OK
nfs: server 192.168.2.213 OK
nfs: server 192.168.2.213 not responding, still trying
nfs: server 192.168.2.213 not responding, still trying

=============================

Categories: Embedded, Tech.Notes Tags: , ,