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.

80 lines
2.0 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. status = run('list', '--non-interactive', url)[1]
  19. if status.exited? && status.exitstatus != 0
  20. print "svn: password: "
  21. @password = STDIN.noecho(&:gets).chomp
  22. end
  23. end
  24. def revision(repo)
  25. xml = Nokogiri::XML run('log', '--xml', '-l', '1', repo).first
  26. xml.css('logentry').attr('revision').value.to_i
  27. end
  28. def clone(repo, semester, assignment, student)
  29. url = get_url semester, assignment, student
  30. run 'checkout', '--ignore-externals', url, repo
  31. end
  32. def update(repo, revision = nil)
  33. args = 'update', '--ignore-externals', '--accept', 'tf'
  34. args.push "-r#{revision}" unless revision.nil?
  35. run *args, repo
  36. end
  37. def log(repo)
  38. xml = Nokogiri::XML run('log', '--xml', repo).first
  39. xml.css('logentry').map do |elem|
  40. { :rev => elem.attr('revision').to_i,
  41. :date => Time.parse(elem.css('date').text) }
  42. end
  43. end
  44. def commit(repo, message, paths = nil)
  45. run 'commit', '-m', message, *paths.map { |fn| File.join repo, fn }
  46. end
  47. def commit_date(repo)
  48. xml = Nokogiri::XML run('log', '--xml', '-l', '1', repo).first
  49. Time.parse xml.css('logentry date').text
  50. end
  51. private
  52. def run(*cmd)
  53. if @password
  54. cmd.unshift '--password'
  55. cmd.unshift @password
  56. end
  57. Open3.capture2e('svn', *cmd)
  58. end
  59. def get_url(semester, assignment, student)
  60. @config['url'] % {
  61. :semester => semester,
  62. :assignment => assignment,
  63. :student => student
  64. }
  65. end
  66. end
  67. end