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: 58 inserted, 8 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 | class Language < ActiveRecord::Base |
|
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 | ||
|
|
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 | ||
|
2 | end |
|
24 | end |
@@ -81,14 +81,17 | |||||
|
81 | return nil |
|
81 | return nil |
|
82 | end |
|
82 | end |
|
83 |
|
83 | ||
|
84 | - def self.find_language_in_source(source) |
|
84 | + def self.find_language_in_source(source, source_filename="") |
|
85 | langopt = find_option_in_source(/^LANG:/,source) |
|
85 | langopt = find_option_in_source(/^LANG:/,source) |
|
86 | - if language = Language.find_by_name(langopt) |
|
86 | + if langopt |
|
87 | - return language |
|
87 | + return (Language.find_by_name(langopt) || |
|
88 |
- |
|
88 | + Language.find_by_pretty_name(langopt)) |
|
89 | - return language |
|
||
|
90 | else |
|
89 | else |
|
91 | - return nil |
|
90 | + if source_filename |
|
|
91 | + return Language.find_by_extension(source_filename.split('.').last) | ||
|
|
92 | + else | ||
|
|
93 | + return nil | ||
|
|
94 | + end | ||
|
92 | end |
|
95 | end |
|
93 | end |
|
96 | end |
|
94 |
|
97 | ||
@@ -114,7 +117,8 | |||||
|
114 | end |
|
117 | end |
|
115 |
|
118 | ||
|
116 | def assign_language |
|
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 | end |
|
122 | end |
|
119 |
|
123 | ||
|
120 | # validation codes |
|
124 | # validation codes |
@@ -9,7 +9,7 | |||||
|
9 | # |
|
9 | # |
|
10 | # It's strongly recommended to check this file into your version control system. |
|
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 | create_table "announcements", :force => true do |t| |
|
14 | create_table "announcements", :force => true do |t| |
|
15 | t.string "author" |
|
15 | t.string "author" |
@@ -59,6 +59,7 | |||||
|
59 | t.string "name", :limit => 10 |
|
59 | t.string "name", :limit => 10 |
|
60 | t.string "pretty_name" |
|
60 | t.string "pretty_name" |
|
61 | t.string "ext", :limit => 10 |
|
61 | t.string "ext", :limit => 10 |
|
|
62 | + t.string "common_ext" | ||
|
62 | end |
|
63 | end |
|
63 |
|
64 | ||
|
64 | create_table "messages", :force => true do |t| |
|
65 | create_table "messages", :force => true do |t| |
You need to be logged in to leave comments.
Login now