A code autograder for student homework submissions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3 KiB

  1. require 'io/console'
  2. require 'nokogiri'
  3. require 'open3'
  4. module KGrader::Backend
  5. class SVN
  6. def initialize(filesystem, course, config)
  7. @fs = filesystem
  8. @course = course
  9. @config = config
  10. @password = nil
  11. end
  12. def prepare(semester, assignment)
  13. return unless @config['verify']
  14. url = @config['verify'] % {
  15. :semester => semester,
  16. :assignment => assignment
  17. }
  18. unless test_okay url
  19. print "svn: password: "
  20. @password = STDIN.noecho(&:gets).chomp
  21. puts
  22. puts "svn: bad password or other network issues" unless test_okay url
  23. end
  24. end
  25. def revision(repo)
  26. xml = Nokogiri::XML run('log', '--xml', '-l', '1', repo).first
  27. xml.css('logentry').attr('revision').value.to_i
  28. end
  29. def clone(repo, semester, assignment, student)
  30. url = get_url semester, assignment, student
  31. run 'checkout', '--ignore-externals', url, repo
  32. end
  33. def update(repo, revision = nil)
  34. args = 'update', '--ignore-externals', '--accept', 'tf'
  35. args.push "-r#{revision}" unless revision.nil?
  36. run *args, repo
  37. end
  38. def log(repo)
  39. xml = Nokogiri::XML run('log', '--xml', repo).first
  40. xml.css('logentry').map do |elem|
  41. { :rev => elem.attr('revision').to_i,
  42. :date => Time.parse(elem.css('date').text) }
  43. end
  44. end
  45. def commit(repo, message, *paths)
  46. fullpaths = paths.map { |fn| File.join repo, fn }
  47. run 'add', *fullpaths
  48. run 'commit', '-m', message, *fullpaths
  49. end
  50. def commit_date(repo)
  51. xml = Nokogiri::XML run('log', '--xml', '-l', '1', repo).first
  52. Time.parse xml.css('logentry date').text
  53. end
  54. private
  55. def run(*cmd)
  56. if @password
  57. temp = '.svn_temp_' + rand(1000000000).to_s
  58. begin
  59. File.write temp, @password
  60. Open3.capture2e("cat #{temp} | xargs svn #{cmd.join ' '} --password")
  61. ensure
  62. File.unlink temp
  63. end
  64. else
  65. Open3.capture2e('svn', *cmd)
  66. end
  67. end
  68. def get_url(semester, assignment, student)
  69. @config['url'] % {
  70. :semester => semester,
  71. :assignment => assignment,
  72. :student => student
  73. }
  74. end
  75. def test_okay(url)
  76. status = run('list', '--non-interactive', url)[1]
  77. status.exited? && status.exitstatus == 0
  78. end
  79. end
  80. end