ffi-magic 2011.04

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 Michael Fellinger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ 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.
@@ -0,0 +1,18 @@
1
+ LICENSE
2
+ MANIFEST
3
+ README.md
4
+ Rakefile
5
+ doc/AUTHORS
6
+ doc/CHANGELOG
7
+ ffi-magic.gemspec
8
+ lib/ffi-magic.rb
9
+ lib/ffi-magic/version.rb
10
+ spec/ffi-magic.rb
11
+ spec/magic.png
12
+ tasks/authors.rake
13
+ tasks/changelog.rake
14
+ tasks/gem.rake
15
+ tasks/manifest.rake
16
+ tasks/release.rake
17
+ tasks/reversion.rake
18
+ tasks/todo.rake
@@ -0,0 +1,12 @@
1
+ # Ruby FFI bindings for libmagic
2
+
3
+ ## Usage
4
+
5
+ require 'ffi-magic'
6
+ magic = Magic.new
7
+ magic.file('spec/magic.png')
8
+ # => "PNG image data, 100 x 67, 8-bit/color RGB, non-interlaced"
9
+ magic.flags = Magic::MIME
10
+ # => 1040
11
+ magic.file('spec/magic.png')
12
+ # => "image/png; charset=binary"
@@ -0,0 +1,32 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/gempackagetask'
4
+ require 'time'
5
+ require 'date'
6
+
7
+ PROJECT_SPECS = Dir['spec/**/*.rb']
8
+ PROJECT_MODULE = 'Magic'
9
+ PROJECT_VERSION = (ENV['VERSION'] || Date.today.strftime("%Y.%m.%d")).dup
10
+
11
+ GEMSPEC = Gem::Specification.new{|s|
12
+ s.name = 'ffi-magic'
13
+ s.author = "Michael 'manveru' Fellinger"
14
+ s.summary = "FFI binding for libmagic."
15
+ s.email = 'm.fellinger@gmail.com'
16
+ s.homepage = 'http://github.com/manveru/ffi-magic'
17
+ s.platform = Gem::Platform::RUBY
18
+ s.version = PROJECT_VERSION
19
+ s.files = `git ls-files`.split("\n").sort
20
+ s.has_rdoc = true
21
+ s.require_path = 'lib'
22
+ s.executables = []
23
+ s.bindir = "bin"
24
+
25
+ s.add_runtime_dependency('ffi', '~> 1.0.7')
26
+ }
27
+
28
+ Dir['tasks/*.rake'].each{|f| import(f) }
29
+
30
+ task :default => [:bacon]
31
+
32
+ CLEAN.include('')
@@ -0,0 +1,4 @@
1
+ Following persons have contributed to ffi-magic.
2
+ (Sorted by number of submitted patches, then alphabetically)
3
+
4
+ 3 Michael Fellinger <m.fellinger@gmail.com>
@@ -0,0 +1,12 @@
1
+ [5a53006 | Fri Apr 22 02:04:14 UTC 2011] Michael Fellinger <m.fellinger@gmail.com>
2
+
3
+ * Version 2011.04
4
+
5
+ [174a767 | Fri Apr 22 01:59:00 UTC 2011] Michael Fellinger <m.fellinger@gmail.com>
6
+
7
+ * prepare for release
8
+
9
+ [cf89376 | Sun Jun 13 22:52:56 UTC 2010] Michael Fellinger <m.fellinger@gmail.com>
10
+
11
+ * Initial commit
12
+
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{ffi-magic}
5
+ s.version = "2011.04"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Michael 'manveru' Fellinger"]
9
+ s.date = %q{2011-04-22}
10
+ s.email = %q{m.fellinger@gmail.com}
11
+ s.files = ["LICENSE", "MANIFEST", "README.md", "Rakefile", "doc/AUTHORS", "doc/CHANGELOG", "ffi-magic.gemspec", "lib/ffi-magic.rb", "lib/ffi-magic/version.rb", "spec/ffi-magic.rb", "spec/magic.png", "tasks/authors.rake", "tasks/changelog.rake", "tasks/gem.rake", "tasks/manifest.rake", "tasks/release.rake", "tasks/reversion.rake", "tasks/todo.rake"]
12
+ s.homepage = %q{http://github.com/manveru/ffi-magic}
13
+ s.require_paths = ["lib"]
14
+ s.rubygems_version = %q{1.3.7}
15
+ s.summary = %q{FFI binding for libmagic.}
16
+
17
+ if s.respond_to? :specification_version then
18
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
19
+ s.specification_version = 3
20
+
21
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
22
+ s.add_runtime_dependency(%q<ffi>, ["~> 1.0.7"])
23
+ else
24
+ s.add_dependency(%q<ffi>, ["~> 1.0.7"])
25
+ end
26
+ else
27
+ s.add_dependency(%q<ffi>, ["~> 1.0.7"])
28
+ end
29
+ end
@@ -0,0 +1,107 @@
1
+ require 'ffi'
2
+
3
+ require 'ffi-magic/version'
4
+
5
+ class Magic
6
+ extend FFI::Library
7
+ ffi_lib ['magic', 'libmagic.so.1']
8
+
9
+ attach_function :magic_open, [:int], :pointer
10
+ attach_function :magic_error, [:pointer], :string
11
+ attach_function :magic_load, [:pointer, :string], :int
12
+ attach_function :magic_file, [:pointer, :string], :string
13
+ attach_function :magic_setflags, [:pointer, :int], :void
14
+ attach_function :magic_buffer, [:pointer, :string, :int], :string
15
+ attach_function :magic_check, [:pointer, :string], :int
16
+ attach_function :magic_compile, [:pointer, :string], :int
17
+ attach_function :magic_close, [:pointer], :void
18
+
19
+ NONE = 0x000000
20
+ DEBUG = 0x000001
21
+ SYMLINK = 0x000002
22
+ COMPRESS = 0x000004
23
+ DEVICES = 0x000008
24
+ MIME_TYPE = 0x000010
25
+ CONTINUE = 0x000020
26
+ CHECK = 0x000040
27
+ PRESERVE_ATIME = 0x000080
28
+ RAW = 0x000100
29
+ ERROR = 0x000200
30
+ MIME_ENCODING = 0x000400
31
+ MIME = MIME_TYPE | MIME_ENCODING
32
+ APPLE = 0x000800
33
+ NO_CHECK_COMPRESS = 0x001000
34
+ NO_CHECK_TAR = 0x002000
35
+ NO_CHECK_SOFT = 0x004000
36
+ NO_CHECK_APPTYPE = 0x008000
37
+ NO_CHECK_ELF = 0x010000
38
+ NO_CHECK_TEXT = 0x020000
39
+ NO_CHECK_CDF = 0x040000
40
+ NO_CHECK_TOKENS = 0x100000
41
+ NO_CHECK_ENCODING = 0x200000
42
+
43
+ Error = Class.new(StandardError)
44
+
45
+ def self.compile(path)
46
+ cookie = magic_open(NONE)
47
+
48
+ unless magic_compile(cookie, path) == 0
49
+ raise Error, "failed compile: #{magic_error(cookie)}"
50
+ end
51
+
52
+ magic_close(cookie)
53
+
54
+ true
55
+ end
56
+
57
+ def initialize(flags = NONE, path = nil)
58
+ @flags = flags
59
+ @path = path
60
+
61
+ unless @cookie = magic_open(@flags)
62
+ raise Error, "failed to initialize magic cookie"
63
+ end
64
+
65
+ if magic_load(@cookie, @path) != 0
66
+ raise Error, "failed to load database: #{magic_error(@cookie)}"
67
+ end
68
+
69
+ begin
70
+ ObjectSpace.define_finalizer(self){ close }
71
+ rescue
72
+ end
73
+ end
74
+
75
+ def file(path)
76
+ unless msg = magic_file(@cookie, path)
77
+ raise Error, "failed lookup: #{magic_error(@cookie)}"
78
+ end
79
+
80
+ msg
81
+ end
82
+
83
+ def buffer(input)
84
+ input.force_encoding(Encoding::BINARY)
85
+ magic_buffer(@cookie, input, input.bytesize.to_s)
86
+ end
87
+
88
+ def flags=(flags)
89
+ @flags = flags
90
+ magic_setflags(@cookie, flags)
91
+ end
92
+
93
+ def valid?(path = nil)
94
+ magic_check(@cookie, path) == 0
95
+ end
96
+
97
+ def load
98
+ unless magic_load(@cookie, path) == 0
99
+ raise Error, "failed load: #{magic_error(@cookie)}"
100
+ end
101
+ end
102
+
103
+ def close
104
+ magic_close(@cookie) if @cookie
105
+ @cookie = nil
106
+ end
107
+ end
@@ -0,0 +1,3 @@
1
+ class Magic
2
+ VERSION = "2011.04"
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'bacon'
2
+ Bacon.summary_on_exit
3
+
4
+ require_relative '../lib/ffi-magic'
5
+
6
+ describe 'Magic' do
7
+ @m = Magic.new
8
+
9
+ file = File.expand_path('../magic.png', __FILE__)
10
+
11
+ should 'recognize file' do
12
+ @m.flags = Magic::NONE
13
+ @m.file(file).should == "PNG image data, 100 x 67, 8-bit/color RGB, non-interlaced"
14
+ end
15
+
16
+ should 'return MIME' do
17
+ @m.flags = Magic::MIME
18
+ @m.file(file).should == "image/png; charset=binary"
19
+ end
20
+
21
+ should 'buffer' do
22
+ @m.flags = Magic::NONE
23
+ @m.buffer(File.read(file)).should == "ASCII C++ program text"
24
+ end
25
+
26
+ should 'buffer MIME' do
27
+ @m.flags = Magic::MIME
28
+ @m.buffer(File.read(file)).should == "text/x-c++; charset=us-ascii"
29
+ end
30
+
31
+ should 'have a valid database' do
32
+ @m.should.be.valid
33
+ end
34
+ end
Binary file
@@ -0,0 +1,17 @@
1
+ desc 'Update doc/AUTHORS'
2
+ task :authors do
3
+ authors = Hash.new(0)
4
+
5
+ `git shortlog -nse`.scan(/(\d+)\s(.+)\s<(.*)>$/) do |count, name, email|
6
+ authors[[name, email]] += count.to_i
7
+ end
8
+
9
+ File.open('doc/AUTHORS', 'w+') do |io|
10
+ io.puts "Following persons have contributed to #{GEMSPEC.name}."
11
+ io.puts '(Sorted by number of submitted patches, then alphabetically)'
12
+ io.puts ''
13
+ authors.sort_by{|(n,e),c| [-c, n.downcase] }.each do |(name, email), count|
14
+ io.puts("%6d %s <%s>" % [count, name, email])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ desc 'update changelog'
2
+ task :changelog do
3
+ File.open('doc/CHANGELOG', 'w+') do |changelog|
4
+ `git log -z --abbrev-commit`.split("\0").each do |commit|
5
+ next if commit =~ /^Merge: \d*/
6
+ ref, author, time, _, title, _, message = commit.split("\n", 7)
7
+ ref = ref[/commit ([0-9a-f]+)/, 1]
8
+ author = author[/Author: (.*)/, 1].strip
9
+ time = Time.parse(time[/Date: (.*)/, 1]).utc
10
+ time = time.strftime('%a %b %d %H:%M:%S %Z %Y')
11
+
12
+ title = title.to_s.strip
13
+
14
+ changelog.puts "[#{ref} | #{time}] #{author}"
15
+ changelog.puts '', " * #{title}"
16
+ changelog.puts '', message.rstrip if message
17
+ changelog.puts
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ require 'rake/gempackagetask'
2
+
3
+ task :gemspec => [:manifest, :changelog] do
4
+ gemspec_file = "#{GEMSPEC.name}.gemspec"
5
+ File.open(gemspec_file, 'w+'){|gs| gs.puts(GEMSPEC.to_ruby) }
6
+ end
7
+
8
+ desc "package and install from gemspec"
9
+ task :install => [:gemspec] do
10
+ sh "gem build #{GEMSPEC.name}.gemspec"
11
+ sh "gem install #{GEMSPEC.name}-#{GEMSPEC.version}.gem"
12
+ end
13
+
14
+ desc "uninstall the gem"
15
+ task :uninstall => [:clean] do
16
+ sh %{gem uninstall -x #{GEMSPEC.name}}
17
+ end
18
+
19
+ Rake::GemPackageTask.new(GEMSPEC) do |p|
20
+ p.need_tar = true
21
+ p.need_zip = true
22
+ end
@@ -0,0 +1,4 @@
1
+ desc 'update manifest'
2
+ task :manifest do
3
+ File.open('MANIFEST', 'w+'){|io| io.puts(*GEMSPEC.files) }
4
+ end
@@ -0,0 +1,65 @@
1
+ namespace :release do
2
+ task :prepare => %w[reversion authors gemspec]
3
+ task :all => %w[release:github release:rubyforge release:gemcutter]
4
+
5
+ desc 'Release on github'
6
+ task :github => :prepare do
7
+ name, version = GEMSPEC.name, GEMSPEC.version
8
+
9
+ sh('git', 'add',
10
+ 'MANIFEST', 'doc/CHANGELOG', 'doc/AUTHORS',
11
+ "#{name}.gemspec",
12
+ "lib/#{name}/version.rb")
13
+
14
+ puts <<-INSTRUCTIONS
15
+ ================================================================================
16
+
17
+ I added the relevant files, you can commit them, tag the commit, and push:
18
+
19
+ git commit -m 'Version #{version}'
20
+ git tag -a -m '#{version}' '#{version}'
21
+ git push
22
+
23
+ ================================================================================
24
+ INSTRUCTIONS
25
+ end
26
+
27
+ desc 'Release on rubyforge'
28
+ task :rubyforge => ['release:prepare', :package] do
29
+ name, version = GEMSPEC.name, GEMSPEC.version
30
+
31
+ pkgs = Dir["pkg/#{name}-#{version}.{tgz,zip}"].map{|file|
32
+ "rubyforge add_file #{name} #{name} '#{version}' '#{file}'"
33
+ }
34
+
35
+ puts <<-INSTRUCTIONS
36
+ ================================================================================
37
+
38
+ To publish to rubyforge do following:
39
+
40
+ rubyforge login
41
+ rubyforge add_release #{name} #{name} '#{version}' pkg/#{name}-#{version}.gem
42
+
43
+ To publish the archives for distro packagers:
44
+
45
+ #{pkgs.join "\n"}
46
+
47
+ ================================================================================
48
+ INSTRUCTIONS
49
+ end
50
+
51
+ desc 'Release on gemcutter'
52
+ task :gemcutter => ['release:prepare', :package] do
53
+ name, version = GEMSPEC.name, GEMSPEC.version
54
+
55
+ puts <<-INSTRUCTIONS
56
+ ================================================================================
57
+
58
+ To publish to gemcutter do following:
59
+
60
+ gem push pkg/#{name}-#{version}.gem
61
+
62
+ ================================================================================
63
+ INSTRUCTIONS
64
+ end
65
+ end
@@ -0,0 +1,8 @@
1
+ desc "update version.rb"
2
+ task :reversion do
3
+ File.open("lib/#{GEMSPEC.name}/version.rb", 'w+') do |file|
4
+ file.puts("class #{PROJECT_MODULE}")
5
+ file.puts(' VERSION = %p' % GEMSPEC.version.to_s)
6
+ file.puts('end')
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+ desc "show a todolist from all the TODO tags in the source"
2
+ task :todo do
3
+ Dir.glob('{lib,spec}/**/*.rb') do |file|
4
+ lastline = todo = comment = long_comment = false
5
+
6
+ File.readlines(file).each_with_index do |line, lineno|
7
+ lineno += 1
8
+ comment = line =~ /^\s*?#.*?$/
9
+ long_comment = line =~ /^=begin/
10
+ long_comment = line =~ /^=end/
11
+ todo = true if line =~ /TODO|FIXME|THINK/ and (long_comment or comment)
12
+ todo = false if line.gsub('#', '').strip.empty?
13
+ todo = false unless comment or long_comment
14
+ if todo
15
+ unless lastline and lastline + 1 == lineno
16
+ puts
17
+ puts "vim #{file} +#{lineno}"
18
+ end
19
+
20
+ l = line.strip.gsub(/^#\s*/, '')
21
+ print ' ' unless l =~ /^-/
22
+ puts l
23
+ lastline = lineno
24
+ end
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-magic
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 2011
7
+ - 4
8
+ version: "2011.04"
9
+ platform: ruby
10
+ authors:
11
+ - Michael 'manveru' Fellinger
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2011-04-22 00:00:00 +09:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: ffi
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 7
31
+ version: 1.0.7
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description:
35
+ email: m.fellinger@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - LICENSE
44
+ - MANIFEST
45
+ - README.md
46
+ - Rakefile
47
+ - doc/AUTHORS
48
+ - doc/CHANGELOG
49
+ - ffi-magic.gemspec
50
+ - lib/ffi-magic.rb
51
+ - lib/ffi-magic/version.rb
52
+ - spec/ffi-magic.rb
53
+ - spec/magic.png
54
+ - tasks/authors.rake
55
+ - tasks/changelog.rake
56
+ - tasks/gem.rake
57
+ - tasks/manifest.rake
58
+ - tasks/release.rake
59
+ - tasks/reversion.rake
60
+ - tasks/todo.rake
61
+ has_rdoc: true
62
+ homepage: http://github.com/manveru/ffi-magic
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options: []
67
+
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.7
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: FFI binding for libmagic.
93
+ test_files: []
94
+