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.

88 lines
2.2 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. cmd.unshift @password
  58. cmd.unshift '--password'
  59. end
  60. Open3.capture2e('svn', *cmd)
  61. end
  62. def get_url(semester, assignment, student)
  63. @config['url'] % {
  64. :semester => semester,
  65. :assignment => assignment,
  66. :student => student
  67. }
  68. end
  69. def test_okay(url)
  70. status = run('list', '--non-interactive', url)[1]
  71. status.exited? && status.exitstatus == 0
  72. end
  73. end
  74. end