最近做了个小程序,从网上抓取一些数据,并进行处理,
流程大致是这样的,
java写个spider,通过hibernate写入postgresql,maven运行,并且把maven放在crond。在网上出现需要的数据的大致时间点自动运行;
用shell 和psql 还有sed命令从数据库中取出网上抓来的数据,放入data.ini文件;
c++ stl写一个小程序咀嚼data.ini,输出到stdout;
再用sed命令和psql写回数据库。
搞着么复杂是因为综合了以前很多小程序,完成此次流程。
其中那个c++stl程序原是在vc2005下的,把它移植到freebsd下。在sourceinsight编辑,调用脚本语言发送到服务器,gmake后读取反馈信息(编译错误等),定位到行。
开始装了perl,搞了半天,一个小小的ssh登录搞不定,什么Expect不支持等。
后来用了ruby,挺直接的 ,make_remote.rb:
require 'net/ssh'
require 'net/sftp'
require 'find'
local_path = "#{ARGV[1]}"
puts "local path:#{local_path}"
remote_repo = "/usr/home/zh/project"
remote_path = "#{remote_repo}/#{ARGV[0]}"
puts "remote path:#{remote_path}"
file_perm = 0600
dir_perm = 0700
def do_cmd( session ,cmd)
session.open_channel do |channel|
channel.on_data do |ch, data|
puts "#{data}"
end
channel.exec "#{cmd}",true
end
end
puts 'Connecting to remote server......'
Net::SSH.start('218.193.237.11', 'zh', '') do |ssh|
ssh.sftp.connect do |sftp|
puts 'Checking for files which need updating'
Find.find(local_path) do |file|
next if File.stat(file).directory?
local_file = "#{file}"
remote_file = remote_path + local_file.sub(local_path, '')
begin
remote_dir = File.dirname(remote_file)
sftp.stat(remote_dir)
rescue Net::SFTP::Operations::StatusException => e
raise unless e.code == 2
sftp.mkdir(remote_dir, :permissions => dir_perm)
end
begin
rstat = sftp.stat(remote_file)
rescue Net::SFTP::Operations::StatusException => e
raise unless e.code == 2
puts "Copying #{local_file} to #{remote_file}"
sftp.put_file(local_file, remote_file)
sftp.setstat(remote_file, :permissions => file_perm)
next
end
if File.stat(local_file).mtime > Time.at(rstat.mtime)
puts "Copying #{local_file} to #{remote_file}"
sftp.put_file(local_file, remote_file)
end
end
end
puts 'making project......'
shell = ssh.shell.sync
out=shell.send_command( "cd #{remote_path}&&gmake #{ARGV[2]}")
puts out.stderr
if ARGV[2]=='run'
puts out.stdout
end
puts 'Disconnecting from remote server......'
end
(好像密码都不用填,会去读取putty配置?)
总结一下使用的东西:sourceinsight、shell、c++、java、maven2、postgres、ruby、freebsd、boost serialization
现在这个东西完全自动了,抓取web数据,自动计算,到时看结果就行了