kelan (owner)

Revisions

gist: 122774 Download_button fork
public
Public Clone URL: git://gist.github.com/122774.git
Embed All Files: show embed
run-queued-trac-post-commits.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/ruby
 
tracDir = '/home/myUser/trac_projects/myProject'
logFilePath = tracDir + '/queued_post_commits.txt'
doneFilePath = tracDir + '/queued_post_commits_done.txt'
errorFilePath = tracDir + '/queued_post_commits_errors.txt'
postCommitScriptPath = tracDir + '/trac-post-commit-hook.py'
tracURL = 'http://trac.myDomain.com/myProject/'
errorEmailTo = 'me@myDomain.com'
 
queuedCommits = Array.new
begin
  File.open(logFilePath, "r") do |file|
    file.each_line do |line|
      queuedCommits << line
    end
  end
rescue
  # no queued commits to process
  exit
end
 
# Empty out the file
system("echo '' > #{logFilePath}")
 
queuedCommits.each do |line|
  line.chomp!
  repo,rev = line.split(':')
  next unless repo and rev
  
  logMessage = `/usr/bin/svnlook log -r #{rev} #{repo}`
  author = `/usr/bin/svnlook author -r #{rev} #{repo}`
  
  # to make sure python can fine the trac module
  exportCmd = 'export PYTHONPATH="$HOME/packages/lib/python2.4/site-packages/:$PYTHONPATH"'
  postCommitCmd = %{/usr/bin/python #{postCommitScriptPath} -p "#{tracDir}" -r "#{rev}" -u "#{author}" -m "#{logMessage}" -s "#{tracURL}"}
  
  if system("#{exportCmd}; #{postCommitCmd}")
    # log the success
    File.open(doneFilePath, "a") do |file|
      file.puts("#{Time.now}:#{repo}:#{rev}:#{author}:#{logMessage}")
    end
  else
    # write an error
    errorCode = $?
    File.open(errorFilePath, "a") do |file|
      file.puts("#{Time.now}:#{errorCode}:#{repo}:#{rev}:#{author}:#{logMessage}")
    end
    # and put the commit info back in the queue file
    File.open(logMessage, "a") do |file|
      file.puts line
    end
    # send an email
    system("echo #{errorFilePath} | mail -s 'Post Commit Errors' #{errorEmailTo}")
  end
end