Description:
added language identification using file extension
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@363 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r166:7417e965c968 - - 4 files changed: 57 inserted, 7 deleted
@@ -0,0 +1,23 | |||
|
1 | + class AddCommonExtToLanguages < ActiveRecord::Migration | |
|
2 | + def self.up | |
|
3 | + # language.common_ext is a comma-separated list of common file | |
|
4 | + # extensions. | |
|
5 | + add_column :languages, :common_ext, :string | |
|
6 | + | |
|
7 | + # updating table information | |
|
8 | + Language.reset_column_information | |
|
9 | + common_ext = { | |
|
10 | + 'c' => 'c', | |
|
11 | + 'cpp' => 'cpp,cc', | |
|
12 | + 'pas' => 'pas' | |
|
13 | + } | |
|
14 | + Language.find(:all).each do |lang| | |
|
15 | + lang.common_ext = common_ext[lang.name] | |
|
16 | + lang.save | |
|
17 | + end | |
|
18 | + end | |
|
19 | + | |
|
20 | + def self.down | |
|
21 | + remove_column :languages, :common_ext | |
|
22 | + end | |
|
23 | + end |
@@ -1,2 +1,24 | |||
|
1 | 1 | class Language < ActiveRecord::Base |
|
2 | + | |
|
3 | + @@languages_by_ext = {} | |
|
4 | + | |
|
5 | + def self.cache_ext_hash | |
|
6 | + @@languages_by_ext = {} | |
|
7 | + Language.find(:all).each do |language| | |
|
8 | + language.common_ext.split(',').each do |ext| | |
|
9 | + @@languages_by_ext[ext] = language | |
|
2 | 10 | end |
|
11 | + end | |
|
12 | + end | |
|
13 | + | |
|
14 | + def self.find_by_extension(ext) | |
|
15 | + if @@languages_by_ext.length == 0 | |
|
16 | + Language.cache_ext_hash | |
|
17 | + end | |
|
18 | + if @@languages_by_ext.has_key? ext | |
|
19 | + return @@languages_by_ext[ext] | |
|
20 | + else | |
|
21 | + return nil | |
|
22 | + end | |
|
23 | + end | |
|
24 | + end |
@@ -72,58 +72,62 | |||
|
72 | 72 | if s =~ option |
|
73 | 73 | words = s.split |
|
74 | 74 | return words[1] |
|
75 | 75 | end |
|
76 | 76 | i = i + 1 |
|
77 | 77 | if i==10 |
|
78 | 78 | return nil |
|
79 | 79 | end |
|
80 | 80 | end |
|
81 | 81 | return nil |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | - def self.find_language_in_source(source) | |
|
84 | + def self.find_language_in_source(source, source_filename="") | |
|
85 | 85 | langopt = find_option_in_source(/^LANG:/,source) |
|
86 | - if language = Language.find_by_name(langopt) | |
|
87 | - return language | |
|
88 |
- |
|
|
89 | - return language | |
|
86 | + if langopt | |
|
87 | + return (Language.find_by_name(langopt) || | |
|
88 | + Language.find_by_pretty_name(langopt)) | |
|
89 | + else | |
|
90 | + if source_filename | |
|
91 | + return Language.find_by_extension(source_filename.split('.').last) | |
|
90 | 92 | else |
|
91 | 93 | return nil |
|
92 | 94 | end |
|
93 | 95 | end |
|
96 | + end | |
|
94 | 97 | |
|
95 | 98 | def self.find_problem_in_source(source) |
|
96 | 99 | prob_opt = find_option_in_source(/^TASK:/,source) |
|
97 | 100 | if problem = Problem.find_by_name(prob_opt) |
|
98 | 101 | return problem |
|
99 | 102 | else |
|
100 | 103 | return nil |
|
101 | 104 | end |
|
102 | 105 | end |
|
103 | 106 | |
|
104 | 107 | def assign_problem |
|
105 | 108 | if self.problem_id!=-1 |
|
106 | 109 | begin |
|
107 | 110 | self.problem = Problem.find(self.problem_id) |
|
108 | 111 | rescue ActiveRecord::RecordNotFound |
|
109 | 112 | self.problem = nil |
|
110 | 113 | end |
|
111 | 114 | else |
|
112 | 115 | self.problem = Submission.find_problem_in_source(self.source) |
|
113 | 116 | end |
|
114 | 117 | end |
|
115 | 118 | |
|
116 | 119 | def assign_language |
|
117 |
- self.language = Submission.find_language_in_source(self.source |
|
|
120 | + self.language = Submission.find_language_in_source(self.source, | |
|
121 | + self.source_filename) | |
|
118 | 122 | end |
|
119 | 123 | |
|
120 | 124 | # validation codes |
|
121 | 125 | def must_specify_language |
|
122 | 126 | return if self.source==nil |
|
123 | 127 | |
|
124 | 128 | # for output_only tasks |
|
125 | 129 | return if self.problem!=nil and self.problem.output_only |
|
126 | 130 | |
|
127 | 131 | if self.language==nil |
|
128 | 132 | errors.add('source',"must specify programming language") unless self.language!=nil |
|
129 | 133 | end |
@@ -1,24 +1,24 | |||
|
1 | 1 | # This file is auto-generated from the current state of the database. Instead of editing this file, |
|
2 | 2 | # please use the migrations feature of Active Record to incrementally modify your database, and |
|
3 | 3 | # then regenerate this schema definition. |
|
4 | 4 | # |
|
5 | 5 | # Note that this schema.rb definition is the authoritative source for your database schema. If you need |
|
6 | 6 | # to create the application database on another system, you should be using db:schema:load, not running |
|
7 | 7 | # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations |
|
8 | 8 | # you'll amass, the slower it'll run and the greater likelihood for issues). |
|
9 | 9 | # |
|
10 | 10 | # It's strongly recommended to check this file into your version control system. |
|
11 | 11 | |
|
12 |
- ActiveRecord::Schema.define(:version => 20090 |
|
|
12 | + ActiveRecord::Schema.define(:version => 20090416235658) do | |
|
13 | 13 | |
|
14 | 14 | create_table "announcements", :force => true do |t| |
|
15 | 15 | t.string "author" |
|
16 | 16 | t.text "body" |
|
17 | 17 | t.boolean "published" |
|
18 | 18 | t.datetime "created_at" |
|
19 | 19 | t.datetime "updated_at" |
|
20 | 20 | t.boolean "frontpage", :default => false |
|
21 | 21 | t.boolean "contest_only", :default => false |
|
22 | 22 | end |
|
23 | 23 | |
|
24 | 24 | create_table "configurations", :force => true do |t| |
@@ -50,24 +50,25 | |||
|
50 | 50 | t.datetime "created_at" |
|
51 | 51 | t.datetime "updated_at" |
|
52 | 52 | t.integer "task_id" |
|
53 | 53 | t.string "task_type" |
|
54 | 54 | end |
|
55 | 55 | |
|
56 | 56 | add_index "grader_processes", ["host", "pid"], :name => "index_grader_processes_on_ip_and_pid" |
|
57 | 57 | |
|
58 | 58 | create_table "languages", :force => true do |t| |
|
59 | 59 | t.string "name", :limit => 10 |
|
60 | 60 | t.string "pretty_name" |
|
61 | 61 | t.string "ext", :limit => 10 |
|
62 | + t.string "common_ext" | |
|
62 | 63 | end |
|
63 | 64 | |
|
64 | 65 | create_table "messages", :force => true do |t| |
|
65 | 66 | t.integer "sender_id" |
|
66 | 67 | t.integer "receiver_id" |
|
67 | 68 | t.integer "replying_message_id" |
|
68 | 69 | t.text "body" |
|
69 | 70 | t.boolean "replied" |
|
70 | 71 | t.datetime "created_at" |
|
71 | 72 | t.datetime "updated_at" |
|
72 | 73 | end |
|
73 | 74 |
You need to be logged in to leave comments.
Login now