Skip to content

Instantly share code, notes, and snippets.

@koseki
Created June 30, 2010 08:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koseki/458400 to your computer and use it in GitHub Desktop.
Save koseki/458400 to your computer and use it in GitHub Desktop.
update trac wiki using text file.
require 'rubygems'
require 'tempfile'
require 'mechanize'
# *.txt format:
# ----------------------------------------------
# http://your/track/wiki/page 1(version nuber)
# contents
# :
# :
# ----------------------------------------------
def login
@m = Mechanize.new
if File.exist?(ENV["HOME"] + "/.tracrake")
require 'yaml'
account = YAML.load_file(ENV["HOME"] + "/.tracrake")
id = account["id"].to_s.strip
pass = account["pass"].to_s.strip
else
require 'highline'
id = HighLine.new.ask('ID: ')
pass = HighLine.new.ask('Password: ') {|q| q.echo = '*' }
end
@m.auth(id, pass)
end
def get_form(uri)
page = @m.get(uri + "?action=edit")
form = nil
page.forms().each do |f|
tmp = f.field_with("action")
next unless tmp && tmp.value == "edit"
next unless f.field_with("version")
next unless f.field_with("comment")
next unless f.field_with("text")
form = f
break
end
return nil unless form
data = {}
data[:uri] = uri
data[:text] = form.text.strip
data[:text].gsub!(/\r\n?/, "\n")
data[:version] = form.version.to_i
return [form, data]
end
def load_file(file)
data = File.read(file).match(/([^ ]+) (\d+)\n(.+)$/m)
return {:uri => data[1], :version => data[2].to_i, :text => data[3].strip}
end
def save_file(file, data)
File.open(file, "w") {|io| write_file(io, data)}
end
def write_file(io,data)
io << data[:uri] + " #{data[:version]}\n" + data[:text].strip
end
def load_files
Dir.chdir(File.dirname(__FILE__)) do |d|
Dir.glob("*.txt").each do |f|
data = load_file(f)
yield(f, data)
end
end
end
def load_articles
load_files do |f, data|
(form, data2) = get_form(data[:uri])
save_file(f, data2)
puts "saved: #{f}"
end
end
def tmpfile
return Tempfile.new("trac-update-rake")
end
def update_articles
load_files do |f, data|
(form,data2) = get_form(data[:uri])
if data2[:version] != data[:version]
puts "ERROR: #{f}: version: local: #{data[:version]} remote: #{data2[:version]}"
if data2[:text] == data[:text]
print "WARN: #{f}: Same text. Fix version..."
save_file(f, data2)
puts " done."
else
tmpio = tmpfile
write_file(tmpio, data2)
tmpio.close
puts `diff #{tmpio.path} #{f}`
end
next
end
if data2[:text] == data[:text]
puts "INFO: #{f}: Not changed. Skip."
next
end
form.text = data[:text]
form.click_button(form.button_with("save"))
data[:version] += 1
save_file(f, data)
puts "INFO: #{f}: updated."
end
end
def diff_articles
load_files do |f, data|
(form,data2) = get_form(data[:uri])
if data2[:text] != data[:text]
tmpio = tmpfile
write_file(tmpio, data2)
tmpio.close
puts `diff #{tmpio.path} #{f}`
end
if data2[:version] != data[:version]
puts "WARN: #{f}: version not match: local: #{data[:version]} remote: #{data2[:version]}"
end
end
end
namespace :trac do
desc "download trac source and overwrite all *.txt files."
task :download do
login
load_articles
end
desc "diff trac wiki articles and *.txt files."
task :diff do
login
diff_articles
end
desc "upload trac wiki articles from *.txt files."
task :update do
login
update_articles
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment