moss_ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/moss_ruby.rb +196 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 661f6ad9b42d35f82f5717e20785c4437c672c7a
4
+ data.tar.gz: 32f0decb5024d8baf1afb356b341da5a7e98b959
5
+ SHA512:
6
+ metadata.gz: 0c2439a3d832bbf53f15e20c67707b2beb4ff16a4b03c412bc8af9ac4fc1548d586eb8b26d1d05ebf312ed2e9d83502af8c5424d1314a87f0ae2175bf8bcc79a
7
+ data.tar.gz: d2e039a786dce1a571bcc1b7e60882acc3502194794ceeacd4ae33326883f82a2bbcfae910e44d5a29fa9c5dd181dd60db19eed52fca35c2c5ff8883ca737c18
data/lib/moss_ruby.rb ADDED
@@ -0,0 +1,196 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014 Andrew Cain
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'socket'
24
+ require 'open-uri'
25
+
26
+ #
27
+ #
28
+ #
29
+ class MossRuby
30
+ attr_accessor :userid
31
+ attr_accessor :server
32
+ attr_accessor :port
33
+ attr_reader :options
34
+
35
+ def self.empty_file_hash
36
+ { base_files: Array.new, files: Array.new }
37
+ end
38
+
39
+ def self.add_base_file ( hash, file )
40
+ hash[:base_files] << file
41
+ hash
42
+ end
43
+
44
+ def self.add_file ( hash, file )
45
+ hash[:files] << file
46
+ hash
47
+ end
48
+
49
+ def initialize(userid, server = "moss.stanford.edu", port = 7690)
50
+ @options = {
51
+ max_matches: 10,
52
+ directory_submission: false,
53
+ show_num_matches: 250,
54
+ experimental_server: false,
55
+ comment: "",
56
+ language: "c"
57
+ }
58
+ @server = server
59
+ @port = port
60
+ @userid = userid
61
+ end
62
+
63
+ def upload_file (moss_server, file, id = 0)
64
+ filename = file.strip.tap do |name|
65
+ name.gsub! /[^\w\-\/.]/, '_'
66
+ end
67
+
68
+ content = IO.read(filename)
69
+ size = content.length
70
+
71
+ moss_server.write "file #{id} #{@options[:language]} #{size} #{file}\n"
72
+ moss_server.write content
73
+ end
74
+
75
+ def check(files_dict)
76
+ # Chech that the files_dict contains valid filenames
77
+ files_dict[:base_files].each do |file_search|
78
+ if Dir[file_search].length == 0
79
+ raise "Unable to locate base file(s) matching #{file_search}"
80
+ end
81
+ end
82
+
83
+ if files_dict[:files].length == 0
84
+ return
85
+ end
86
+
87
+ files_dict[:files].each do |file_search|
88
+ if Dir[file_search].length == 0
89
+ raise "Unable to locate base file(s) matching #{file_search}"
90
+ end
91
+ end
92
+
93
+ # Connect to the server
94
+ moss_server = TCPSocket.new @server, @port
95
+ begin
96
+ # Send header details
97
+ moss_server.write "moss #{@userid}\n"
98
+ moss_server.write "directory #{@options[:directory_submission] ? 1 : 0 }\n"
99
+ moss_server.write "X #{@options[:experimental_server] ? 1 : 0}\n"
100
+ moss_server.write "maxmatches #{@options[:max_matches]}\n"
101
+ moss_server.write "show #{@options[:show_num_matches]}\n"
102
+
103
+ # Send language option
104
+ moss_server.write "language #{@options[:language]}\n"
105
+
106
+ line = moss_server.gets
107
+ if line.strip() != "yes"
108
+ moss_server.write "end\n"
109
+ raise "Invalid language option."
110
+ end
111
+
112
+ files_dict[:base_files].each do |file_search|
113
+ Dir[file_search].each do |file|
114
+ upload_file moss_server, file
115
+ end
116
+ end
117
+
118
+ idx = 1
119
+ files_dict[:files].each do |file_search|
120
+ Dir[file_search].each do |file|
121
+ upload_file moss_server, file, idx
122
+ idx += 1
123
+ end
124
+ end
125
+
126
+ moss_server.write "query 0 #{@options[:comment]}\n"
127
+
128
+ result = moss_server.gets
129
+
130
+ moss_server.write "end\n"
131
+ return result.strip()
132
+ ensure
133
+ moss_server.close
134
+ end
135
+ end
136
+
137
+ def extract_results(uri)
138
+ result = Array.new
139
+ begin
140
+ match = 0
141
+ match_file = Array.new
142
+ data = Array.new
143
+ while true
144
+ # read the two match files
145
+ match_top = open("#{uri}/match#{match}-top.html").read
146
+ match_file[0] = open("#{uri}/match#{match}-0.html").read
147
+ match_file[1] = open("#{uri}/match#{match}-1.html").read
148
+
149
+ # puts match_top
150
+ # puts "---FILE0\n\n"
151
+ # puts match_file[0]
152
+ # puts "---FILE1\n\n"
153
+ # puts match_file[1]
154
+
155
+ data[0] = read_data match_file[0]
156
+ data[1] = read_data match_file[1]
157
+ top = read_pcts match_top
158
+
159
+ result << [
160
+ {
161
+ filename: data[0][:filename],
162
+ html: strip_a("<PRE>#{data[0][:html]}</PRE>"),
163
+ pct: Integer(top[:pct0])
164
+ },
165
+ {
166
+ filename: data[1][:filename],
167
+ html: strip_a("<PRE>#{data[1][:html]}</PRE>"),
168
+ pct: Integer(top[:pct1])
169
+ }
170
+ ]
171
+
172
+ match += 1
173
+ end
174
+ rescue OpenURI::HTTPError
175
+ #end when there are no more matches -- indicated by 404 when accessing matches-n-top.html
176
+ end
177
+
178
+ result
179
+ end
180
+
181
+ private
182
+
183
+ def strip_a(html)
184
+ html.gsub(/<A.*?>.*?<\/A>/, '')
185
+ end
186
+
187
+ def read_data(match_file)
188
+ regex = /<HR>\s+(?<filename>\S+)<p><PRE>\n(?<html>.*)<\/PRE>\n<\/PRE>\n<\/BODY>\n<\/HTML>/xm
189
+ match_file.match(regex)
190
+ end
191
+
192
+ def read_pcts(top_file)
193
+ regex = /<TH>(?<filename0>\S+)\s\((?<pct0>\d+)%\).*<TH>(?<filename1>\S+)\s\((?<pct1>\d+)%\)/xm
194
+ top_file.match(regex)
195
+ end
196
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moss_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Cain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Moss-ruby is an unofficial ruby gem for the Moss system for Detecting
14
+ Software Plagiarism (http://theory.stanford.edu/~aiken/moss/)
15
+ email: acain@swin.edu.au
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/moss_ruby.rb
21
+ homepage: https://bitbucket.org/macite/moss-ruby
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.14
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Moss gem to access system for Detecting Software Plagiarism
45
+ test_files: []