rubib 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubib.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sylvain Claudel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Rubib
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rubib'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rubib
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/rubib/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/makebib.rb ADDED
@@ -0,0 +1,37 @@
1
+ # == Synopsis
2
+ #
3
+ # makebib: make a bib from the contents of a directory or a set of directories
4
+ #
5
+ # == Usage
6
+ #
7
+ # makebib [OPTION] ... BASE_DIR EXT_FILTER
8
+ #
9
+ # -p, --password:
10
+ # password for data encryption
11
+ #
12
+ # -h, --help:
13
+ # show help
14
+ #
15
+ # BASE_DIR: The base dir from where makebib should begin to make bibs
16
+ # EXT_FILTER : List of file extensions that will be included into the bib
17
+
18
+ require './bib'
19
+
20
+ # Dossier pour leS BIB (va bouclé sur les rep)
21
+ root = "/home/sylvain/dev/test_bib/images/"
22
+ filter = [".bmp", ".png"]
23
+ password = "BvY,5:#p@snJ=Ss<1OyOD:I6+1=wBJ"
24
+
25
+ # Contient la liste des répertoires qui doivent être Bibés
26
+ bibs = []
27
+
28
+ # Make the list of potential bibs
29
+ Dir.chdir(root)
30
+ bibs = Dir['*']
31
+
32
+ # Make bibs
33
+ bibs.each do |b|
34
+ puts "Creating #{b}.bib"
35
+ bib = Bib.new(b + ".bib", password)
36
+ bib.create(b, filter)
37
+ end
data/bin/unmakebib.rb ADDED
@@ -0,0 +1,32 @@
1
+ require './bib'
2
+
3
+ def list(bib)
4
+ i, s = [0, 0]
5
+ bib.entries.keys.sort.each do |k|
6
+ v = bib.entries[k]
7
+ puts "#{k} (at #{v.offset}, for #{v.size})"
8
+ i += 1
9
+ s += v.size
10
+ end
11
+ puts if i > 0
12
+ puts "#{i} files (#{s} bytes) in the bib"
13
+ end
14
+
15
+ PASSWORD = 'BvY,5:#p@snJ=Ss<1OyOD:I6+1=wBJ'
16
+
17
+ Dir['*.bib'].each do |bib|
18
+ puts "Processing #{bib}"
19
+
20
+ b = Bib.new(bib, PASSWORD)
21
+ b.read_index
22
+ list(b)
23
+
24
+ dir = File.basename(bib, File.extname(bib))
25
+ Dir.mkdir(dir) unless Dir.exists?(dir)
26
+
27
+ b.entries.each_key do |filename|
28
+ File.open(File.join(dir, filename), 'wb') do |f|
29
+ f << b[filename]
30
+ end
31
+ end
32
+ end
data/bin/unmakebib.rb~ ADDED
@@ -0,0 +1,32 @@
1
+ require './bib'
2
+
3
+ def list(bib)
4
+ i, s = [0, 0]
5
+ bib.entries.keys.sort.each do |k|
6
+ v = bib.entries[k]
7
+ puts "#{k} (at #{v.offset}, for #{v.size})"
8
+ i += 1
9
+ s += v.size
10
+ end
11
+ puts if i > 0
12
+ puts "#{i} files (#{s} bytes) in the bib"
13
+ end
14
+
15
+ PASSWORD = 'BvY,5:#p@snJ=Ss<1OyOD:I6+1=wBJ'
16
+
17
+ Dir['../justabib/*.bib'].each do |bib|
18
+ puts "Processing #{bib}"
19
+
20
+ b = Bib.new(bib, PASSWORD)
21
+ b.read_index
22
+ list(b)
23
+
24
+ dir = File.basename(bib, File.extname(bib))
25
+ Dir.mkdir(dir) unless Dir.exists?(dir)
26
+
27
+ b.entries.each_key do |filename|
28
+ File.open(File.join(dir, filename), 'wb') do |f|
29
+ f << b[filename]
30
+ end
31
+ end
32
+ end
data/lib/rubib.rb ADDED
@@ -0,0 +1,182 @@
1
+ require "rubib/version"
2
+ require 'zlib'
3
+ require 'ezcrypto'
4
+ require 'pp'
5
+
6
+ module Rubib
7
+ class Bib
8
+
9
+ module ReadBinaryDataFromFile
10
+ def read_integer
11
+ self.read(4).unpack("V")[0]
12
+ end
13
+
14
+ def read_big_integer
15
+ self.read(8).unpack("q")[0]
16
+ end
17
+
18
+ def read_bib_entry
19
+ offset, size, option = self.read(8*3).unpack("qqq")
20
+ return BibEntry.new(offset, size, option)
21
+ end
22
+ end
23
+
24
+ class BibEntry
25
+ attr_accessor :offset, :size, :option
26
+
27
+ def initialize(offset = 0, size = 0, option = 0)
28
+ @offset = offset
29
+ @size = size
30
+ @option = option
31
+ end
32
+
33
+ def to_bin
34
+ [@offset, @size, @option].pack("qqq")
35
+ end
36
+ end
37
+
38
+ BIB_MAGIC = 0xFC489D87
39
+
40
+ attr_reader :name
41
+ attr_reader :entries
42
+
43
+ def initialize(name, password = nil)
44
+ @name = name
45
+ @password = password
46
+ @entries = nil
47
+ end
48
+
49
+ def read_index
50
+ bib_open do |file|
51
+ magic = file.read_integer
52
+ if BIB_MAGIC != magic
53
+ raise "#{@name} is not a valid bib"
54
+ end
55
+
56
+ index_start_pos = file.read_big_integer
57
+ file.seek(index_start_pos, IO::SEEK_SET)
58
+
59
+ number_of_index_entries = file.read_integer
60
+
61
+ @entries = Hash.new
62
+ number_of_index_entries.times do
63
+ size_of_entry_name = file.read_integer
64
+ file_name = file.read(size_of_entry_name * 2)
65
+ file_name.force_encoding('UTF-16LE').encode!('UTF-8')
66
+ @entries[file_name] = file.read_bib_entry
67
+ end
68
+ end
69
+ end
70
+
71
+ def create(dir_name, filter)
72
+ @entries = Hash.new
73
+
74
+ # Initialises bib index
75
+ d = Dir.new(dir_name)
76
+ d.each do |f|
77
+ if not ((f[0,1] == '.') and File.directory?(File.join(dir_name,f))) and (filter.include? File.extname(f))
78
+ @entries[f] = BibEntry.new
79
+ end
80
+ end
81
+
82
+ pp @entries
83
+
84
+ bib_write(dir_name) unless @entries.empty?
85
+ end
86
+
87
+ def [](name)
88
+ read_index if @entries.nil?
89
+ e = @entries[name]
90
+
91
+ raise "Can not extract #{name} : data does not exists" if e.nil?
92
+ raise "Can not extract #{name} : password required" if (1 == e.option) and (@password.nil?)
93
+
94
+ data = nil
95
+ bib_open do |file|
96
+ file.seek(e.offset, IO::SEEK_SET)
97
+ id = file.read(e.size)
98
+ od = nil
99
+
100
+ if 1 == e.option
101
+ # EzCrypto
102
+ key = EzCrypto::Key.with_password(@password, '', :algorithm => 'aes-128-cbc')
103
+ od = key.decrypt(id)
104
+ else
105
+ od = id
106
+ end
107
+
108
+ z = Zlib::Inflate.new
109
+ data = z.inflate(od)
110
+ z.finish
111
+ z.close
112
+ end
113
+ data
114
+ end
115
+
116
+ private
117
+
118
+ def bib_open
119
+ file = File.open(@name)
120
+ file.extend ReadBinaryDataFromFile
121
+ file.binmode
122
+ if block_given?
123
+ yield file
124
+ file.close
125
+ elsif
126
+ file
127
+ end
128
+ end
129
+
130
+ def bib_write(from_dir)
131
+ file = File.new(@name, "wb")
132
+ #begin
133
+ # Write bib header and reserve some space for future update of the index offset
134
+ file << [BIB_MAGIC, 0].pack("Vq")
135
+
136
+ # Write contents
137
+ @entries.each_pair do |k,e|
138
+ e.offset = file.pos
139
+
140
+ File.open(File.join(from_dir, k)) do |f|
141
+ f.binmode
142
+
143
+ z = Zlib::Deflate.new(Zlib::BEST_COMPRESSION)
144
+ cd = z.deflate(f.read, Zlib::FINISH)
145
+ z.close
146
+
147
+ if not @password.nil?
148
+ key = EzCrypto::Key.with_password(@password, '', :algorithm => 'aes-128-cbc')
149
+ ed = key.encrypt(cd)
150
+ e.option = 1
151
+ else
152
+ ed = cd
153
+ e.option = 0
154
+ end
155
+
156
+ file << ed
157
+ end
158
+
159
+ e.size = file.pos - e.offset
160
+ end
161
+
162
+ # Current position, where the index will be written
163
+ index_pos = file.pos
164
+
165
+ # Number of entries
166
+ file << [@entries.length].pack("V")
167
+
168
+ @entries.each_pair do |k, e|
169
+ file << [k.length, k.downcase.encode('UTF-16LE')].pack("Va#{k.length * 2}")
170
+ file << e.to_bin
171
+ end
172
+
173
+ # Write the offset where the index has been written
174
+ file.pos = 4
175
+ file << ([index_pos].pack("q"))
176
+ #ensure
177
+ file.close
178
+ #end
179
+ end
180
+
181
+ end
182
+ end
@@ -0,0 +1,3 @@
1
+ module Rubib
2
+ VERSION = "0.0.1"
3
+ end
data/rubib.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubib/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubib"
8
+ spec.version = Rubib::VERSION
9
+ spec.authors = ["Sylvain Claudel", "Pierre Yager"]
10
+ spec.email = ["sylvain.claudel@crisalid.com", "pierre.yager@crisalid.com"]
11
+ spec.summary = %q{Gem qui permet la manipulation de fichier BIB}
12
+ spec.description = %q{Gem qui permet la manipulation de fichier BIB}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubib
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sylvain Claudel
14
+ - Pierre Yager
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2015-02-04 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ - 7
33
+ version: "1.7"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 35
45
+ segments:
46
+ - 10
47
+ - 0
48
+ version: "10.0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: Gem qui permet la manipulation de fichier BIB
52
+ email:
53
+ - sylvain.claudel@crisalid.com
54
+ - pierre.yager@crisalid.com
55
+ executables:
56
+ - makebib.rb
57
+ - unmakebib.rb
58
+ - unmakebib.rb~
59
+ extensions: []
60
+
61
+ extra_rdoc_files: []
62
+
63
+ files:
64
+ - .gitignore
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/makebib.rb
70
+ - bin/unmakebib.rb
71
+ - bin/unmakebib.rb~
72
+ - lib/rubib.rb
73
+ - lib/rubib/version.rb
74
+ - rubib.gemspec
75
+ homepage: ""
76
+ licenses:
77
+ - MIT
78
+ post_install_message:
79
+ rdoc_options: []
80
+
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.25
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Gem qui permet la manipulation de fichier BIB
108
+ test_files: []
109
+