mago 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +45 -0
- data/lib/mago.rb +14 -0
- data/lib/mago/detector.rb +33 -0
- data/lib/mago/file_finder.rb +22 -0
- data/lib/mago/formatter.rb +31 -0
- data/lib/mago/magic_number.rb +14 -0
- data/lib/mago/report.rb +10 -0
- data/lib/mago/ruby_file.rb +10 -0
- data/lib/mago/sexp_processor.rb +37 -0
- data/lib/mago/version.rb +3 -0
- data/spec/fixtures/code.rb +10 -0
- data/spec/mago_spec.rb +7 -0
- data/spec/spec_helper.rb +12 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 77fd55c4d56257d4dc41f3a1cdf03954ba2b6b16
|
4
|
+
data.tar.gz: 9a301c32e80fd137e237da87c1ca58b070173315
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 382966a551072fc4efe10f8341b138e5724b17a777b8df0f56f064c07b9c696fb04d75222c963980056abc0a84195419ca38564fce665a0f4c5095eb588f645b
|
7
|
+
data.tar.gz: b7d9a3691cf8ee754ecc1ef82187e6a2fdc4fc74d1fb8d619917acfb21953444cc61051cc0b7d04f807d226ec47e7d2d79ae4ba5dc675522ff3321a365c00b15
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Potapov Sergey
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Mago
|
2
|
+
|
3
|
+
Tool to detect magic numbers in ruby code.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
```
|
9
|
+
gem install mago
|
10
|
+
```
|
11
|
+
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Detect magic numbers in particular ruby file:
|
16
|
+
```sh
|
17
|
+
mago ./path/to/file.rb
|
18
|
+
```
|
19
|
+
|
20
|
+
In all ruby files of directory:
|
21
|
+
```sh
|
22
|
+
mago ./path/to/project/
|
23
|
+
```
|
24
|
+
|
25
|
+
## Example
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
P = 3.14
|
29
|
+
|
30
|
+
radius = 5
|
31
|
+
square = P * radius ** 2
|
32
|
+
```
|
33
|
+
|
34
|
+
```sh
|
35
|
+
mago ./square.rb
|
36
|
+
./square.rb:3 detected magic number 5
|
37
|
+
./square.rb:4 detected magic number 2
|
38
|
+
```
|
39
|
+
|
40
|
+
|
41
|
+
## Copyright
|
42
|
+
|
43
|
+
Copyright (c) 2013 Sergey Potapov. See LICENSE.txt for
|
44
|
+
further details.
|
45
|
+
|
data/lib/mago.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'ruby_parser'
|
2
|
+
require 'sexp_processor'
|
3
|
+
|
4
|
+
require 'mago/magic_number'
|
5
|
+
require 'mago/ruby_file'
|
6
|
+
require 'mago/sexp_processor'
|
7
|
+
require 'mago/detector'
|
8
|
+
require 'mago/report'
|
9
|
+
require 'mago/formatter'
|
10
|
+
require 'mago/file_finder'
|
11
|
+
require 'mago/version'
|
12
|
+
|
13
|
+
module Mago
|
14
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Mago
|
2
|
+
class Detector
|
3
|
+
def initialize(file_paths = [], options = {})
|
4
|
+
@file_paths = file_paths
|
5
|
+
@report = Report.new
|
6
|
+
@ignore = options[:ignore] || []
|
7
|
+
end
|
8
|
+
|
9
|
+
def run
|
10
|
+
@file_paths.each do |path|
|
11
|
+
process_file(path)
|
12
|
+
end
|
13
|
+
@report
|
14
|
+
end
|
15
|
+
|
16
|
+
def process_file(path)
|
17
|
+
code = File.read(path)
|
18
|
+
sexp_node = RubyParser.new.parse(code)
|
19
|
+
|
20
|
+
file = Mago::RubyFile.new(path)
|
21
|
+
|
22
|
+
sexp_processor = Mago::SexpProcessor.new(file, @ignore)
|
23
|
+
sexp_processor.process sexp_node
|
24
|
+
|
25
|
+
@report.files << file
|
26
|
+
rescue Errno::ENOENT => err
|
27
|
+
@report.errors << err.message
|
28
|
+
rescue Racc::ParseError => err
|
29
|
+
msg = "#{path} has invalid ruby code. " << err.message
|
30
|
+
@report.errors << msg
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Mago
|
2
|
+
class FileFinder
|
3
|
+
def initialize(paths)
|
4
|
+
@paths = paths
|
5
|
+
end
|
6
|
+
|
7
|
+
def find
|
8
|
+
file_paths = []
|
9
|
+
|
10
|
+
@paths.each do |path|
|
11
|
+
if File.directory?(path)
|
12
|
+
dir_files = Dir["#{path}/**/*.rb"]
|
13
|
+
file_paths.concat(dir_files)
|
14
|
+
else
|
15
|
+
file_paths << path
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
file_paths
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Mago
|
2
|
+
class Formatter
|
3
|
+
def initialize(report)
|
4
|
+
@report = report
|
5
|
+
end
|
6
|
+
|
7
|
+
def format
|
8
|
+
out = ''
|
9
|
+
|
10
|
+
@report.files.each do |file|
|
11
|
+
format_file(file, out)
|
12
|
+
end
|
13
|
+
|
14
|
+
@report.errors.each do |error|
|
15
|
+
format_error(error, out)
|
16
|
+
end
|
17
|
+
|
18
|
+
out
|
19
|
+
end
|
20
|
+
|
21
|
+
def format_file(file, out)
|
22
|
+
file.magic_numbers.each do |num|
|
23
|
+
out << "%s:%d detected magic number %s\n" % [file.path, num.line, num.value]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def format_error(error, out)
|
28
|
+
out << "ERROR: %s\n" % [error]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/mago/report.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Mago
|
2
|
+
class SexpProcessor < ::SexpProcessor
|
3
|
+
|
4
|
+
def initialize(file, ignore = [])
|
5
|
+
super()
|
6
|
+
self.warn_on_default = false
|
7
|
+
self.strict = false
|
8
|
+
|
9
|
+
@file = file
|
10
|
+
@ignore = ignore
|
11
|
+
end
|
12
|
+
|
13
|
+
def process_cdecl(exp)
|
14
|
+
process_default(exp)
|
15
|
+
end
|
16
|
+
|
17
|
+
def process_lit(exp)
|
18
|
+
exp.shift
|
19
|
+
value = exp.shift
|
20
|
+
|
21
|
+
if value.is_a?(Numeric) && !@ignore.include?(value)
|
22
|
+
@file.magic_numbers << MagicNumber.new(:value => value, :line => exp.line)
|
23
|
+
end
|
24
|
+
|
25
|
+
s()
|
26
|
+
end
|
27
|
+
|
28
|
+
def process_default(exp)
|
29
|
+
until exp.size == 0
|
30
|
+
exp.shift
|
31
|
+
end
|
32
|
+
s()
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
data/lib/mago/version.rb
ADDED
data/spec/mago_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'mago'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mago
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Potapov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ruby_parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sexp_processor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Provides a command and API to detect magic numbers in ruby code
|
42
|
+
email:
|
43
|
+
- blake131313@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files:
|
47
|
+
- LICENSE.txt
|
48
|
+
- README.markdown
|
49
|
+
files:
|
50
|
+
- ./lib/mago.rb
|
51
|
+
- ./lib/mago/report.rb
|
52
|
+
- ./lib/mago/file_finder.rb
|
53
|
+
- ./lib/mago/version.rb
|
54
|
+
- ./lib/mago/ruby_file.rb
|
55
|
+
- ./lib/mago/sexp_processor.rb
|
56
|
+
- ./lib/mago/detector.rb
|
57
|
+
- ./lib/mago/formatter.rb
|
58
|
+
- ./lib/mago/magic_number.rb
|
59
|
+
- LICENSE.txt
|
60
|
+
- README.markdown
|
61
|
+
- ./spec/mago_spec.rb
|
62
|
+
- ./spec/spec_helper.rb
|
63
|
+
- ./spec/fixtures/code.rb
|
64
|
+
homepage: https://github.com/greyblake/mago
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.0.3
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Tool to detect magic numbers in ruby code
|
88
|
+
test_files:
|
89
|
+
- ./spec/mago_spec.rb
|
90
|
+
- ./spec/spec_helper.rb
|
91
|
+
- ./spec/fixtures/code.rb
|
92
|
+
has_rdoc:
|