ruby, DateTime class实现rfc2822转换

January 12th, 2008

比较诡异的事情,ruby的Time类有实现rfc2822(),而DateTime类没有实现。
那我们可以自己动手。

class DateTime
    def to_rfc2822
        sprintf("%.3s, %02d %.3s %04d %02d:%02d:%02d %s",
				Date::DAYNAMES[self.wday],
				self.day, Date::MONTHNAMES[self.mon],
				self.year, self.hour, self.min, self.sec,
				self.zone)
    end
end

好了,Ruby标准库的DateTime类支持到rfc2822的转换方法了。you see, ruby太灵活,自由了。

webflier i.t.

ruby, open-uri发起conditional get

January 12th, 2008

Conditional GET 有两种,一种是etag, 另一种是last-modified。
关于这两种介绍,请移步:http://www.dbanotes.net/web/http_11_etag_lastmodified.html
我们分别来看一下这两种实现。
1。用ETag

require 'open-uri'

 # 1st request -- save the ETag
 etag = nil
 open( "http://redhanded.hobix.com/index.xml" ) do |feed|
   etag = feed.meta['etag']
 end

 # 2nd request -- only retrieve the file if it has changed
 begin
   open( "http://redhanded.hobix.com/index.xml",
         "If-None-Match" => etag ) do |feed|
     puts "File has changed: #{ feed.read.length } bytes read"
   end
 rescue OpenURI::HTTPError
   puts "No file available or file has not changed."
 end

2。用last-modified

require 'open-uri'

 # 1st request -- save the modification time
 mtime = nil
 open( "http://redhanded.hobix.com/index.xml" ) do |feed|
   mtime = feed.last_modified
 end

 # 2nd request -- only retrieve the file if it has changed
 begin
   open( "http://redhanded.hobix.com/index.xml",
         "If-Modified-Since" => mtime.rfc2822 ) do |feed|
     puts "File has changed: #{ feed.read.length } bytes read"
   end
 rescue OpenURI::HTTPError
   puts "No file available or file has not changed."
 end

webflier i.t.

在Godaddy Deluxe运行Ruby脚本

January 11th, 2008

网上有很多文章介绍怎么在Godaddy的空间上跑Ruby on Rails,但是怎么单独运行Ruby脚本,却很少有提及。和Perl、php相比,Ruby更灵活,更省力。
我们看下面这个例子:

#!/usr/local/bin/ruby -w
# index.cgi
puts "Content-Type: text/html"
puts
puts "Hello World!"

把上面这段脚本上传到Godady的空间里,任意目录都可以。然后将这个文件的属性改为755(大部分FTP软件都能做到)。

这样你就可以看这个脚本运行了,比如:http://www.yoursite.com/main.cgi

注意,脚本的名字一定要以cgi作扩展名,而不是rb。

如果要用gems,可以这样

require "rubygems"
require "mysql"

webflier i.t.

svn 版本库的备份和还原

December 20th, 2007

仓库中版本的备份及还原形式主要有两种:
方式一:直接备份仓库整个文件夹(全部版本),重装svn程序后直接还原过去。

方式二:通过svn命令行备份和还原指定版本号的数据
全备份:使用svnadmin hotcopy或svnsync来做,例:
svnadmin hotcopy path/to/repository path/to/backup –clean-logs

增量备份:使用svnadmin dump的–incremental选项来实现
svnadmin dump 版本库路径及名称 –revision 上次导出的版本号:到本次要导出到的版本号 –incremental > 导出的命名

还原版本:svnadmin load 要恢复的版本库路径及名称 < 导出的命名

svnadmin hotcopy path/to/repository path/to/backup –clean-logs

一个技巧:

如果你有一个较大的Subsersion版本库而你又想用最少的空间来将它备份下来,用这个命令(请将/repo替换成你的版本库路径)吧:

svnadmin dump –deltas /repo |bzip2 |tee dump.bz2 | md5sum >dump.md5

分步解释:
最重要的一步是 -deltas,将消耗更多的CPU资源,但拥有更有效的差异存储办法。

bzip2压缩方案比gzip慢,但换来的更好的压缩率。
更有趣的是,tee方法将压缩的数据流转向到文件dump.bz2,同时将其输出到标准输出,后者有转向给了MD5摘要计算工具。

要恢复这个版本库,检查校验值(md5sum创建的),创建一个空的版本库,恢复备份:

md5sum -c dump.md5 <dump.bz2
svnadmin create newrepo
bzcat dump.bz2 | svnadmin load newrepo

请享受压缩后用MD5校验的备份吧,最后别忘记将dump.md5和dump.bz2存储到真正安全的地方!

webflier i.t.

terminal上执行的 python 程序如何能在关闭终端时继续执行?

December 12th, 2007

python /home/neil/www/test/manage.py runserver 0.0.0.0:8888 &
& 加在命令的最后

但是只加&是不够的,终端退出时会向所有进程发SIGHUP信号
这样可以
nohup python … &

webflier i.t.