img-sort 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem 'rmagick'
9
+ group :development do
10
+ gem "rspec", "~> 2.3.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rmagick (2.13.1)
13
+ rspec (2.3.0)
14
+ rspec-core (~> 2.3.0)
15
+ rspec-expectations (~> 2.3.0)
16
+ rspec-mocks (~> 2.3.0)
17
+ rspec-core (2.3.1)
18
+ rspec-expectations (2.3.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.3.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ jeweler (~> 1.5.2)
28
+ rcov
29
+ rmagick
30
+ rspec (~> 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Andrey Cherkashin
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.
@@ -0,0 +1,34 @@
1
+ = img-sort
2
+
3
+ img-sort images by their resolution.
4
+
5
+ == Usage
6
+
7
+ img-sort [options] <source dir> <dst dir>
8
+ -v, --version Show version
9
+ -u, --usage Show usage
10
+ -V, --verbose Be verbose
11
+ -q, --quiet Be quite
12
+ -h, --rename Change file name to hash from that file (default)
13
+ -n, --no-rename Save original name
14
+
15
+ == Examples
16
+
17
+ img-sort -V ~/Desktop/wallpaper_tmp ~/Pictures/Wallpapers
18
+ img-sort -qn ~/Desktop/wallpaper_tmp ~/Pictures/Wallpapers
19
+
20
+ == Contributing to img-sort
21
+
22
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
23
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
24
+ * Fork the project
25
+ * Start a feature/bugfix branch
26
+ * Commit and push until you are happy with your contribution
27
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
28
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
29
+
30
+ == Copyright
31
+
32
+ Copyright (c) 2011 Andrey Cherkashin. See LICENSE.txt for
33
+ further details.
34
+
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "img-sort"
16
+ gem.homepage = "http://github.com/andoriyu/img-sort"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Image sorter}
19
+ gem.description = %Q{Sort images by their resolution and rename it if needed}
20
+ gem.email = "with.out@me.com"
21
+ gem.authors = ["Andrey Cherkashin"]
22
+
23
+ gem.add_runtime_dependency 'rmagick'
24
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
25
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
26
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
27
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rspec/core'
32
+ require 'rspec/core/rake_task'
33
+ RSpec::Core::RakeTask.new(:spec) do |spec|
34
+ spec.pattern = FileList['spec/**/*_spec.rb']
35
+ end
36
+
37
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
38
+ spec.pattern = 'spec/**/*_spec.rb'
39
+ spec.rcov = true
40
+ end
41
+
42
+ task :default => :spec
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "img-sort #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
+ require 'img-sort'
4
+
5
+ app = ImgSort.new(ARGV)
6
+ app.run
@@ -0,0 +1,72 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{img-sort}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Andrey Cherkashin"]
12
+ s.date = %q{2011-04-22}
13
+ s.description = %q{Sort images by their resolution and rename it if needed}
14
+ s.email = %q{with.out@me.com}
15
+ s.executables = ["img-sort"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/img-sort",
30
+ "img-sort.gemspec",
31
+ "lib/img-sort.rb",
32
+ "spec/img-sort_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/andoriyu/img-sort}
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.7.2}
39
+ s.summary = %q{Image sorter}
40
+ s.test_files = [
41
+ "spec/img-sort_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<rmagick>, [">= 0"])
50
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
51
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
53
+ s.add_development_dependency(%q<rcov>, [">= 0"])
54
+ s.add_runtime_dependency(%q<rmagick>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<rmagick>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
60
+ s.add_dependency(%q<rcov>, [">= 0"])
61
+ s.add_dependency(%q<rmagick>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rmagick>, [">= 0"])
65
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
66
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
67
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
68
+ s.add_dependency(%q<rcov>, [">= 0"])
69
+ s.add_dependency(%q<rmagick>, [">= 0"])
70
+ end
71
+ end
72
+
@@ -0,0 +1,144 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'ostruct'
5
+ require 'digest/md5'
6
+ require 'fileutils'
7
+ require 'rmagick'
8
+
9
+ class ImgSort
10
+ attr_reader :options
11
+
12
+ VERSION="0.1.0"
13
+
14
+ def initialize(arguments = {})
15
+ @arguments = arguments
16
+ # Set defaults
17
+ @options = OpenStruct.new
18
+ @options.verbose = false
19
+ @options.quiet = false
20
+ @options.rename = true
21
+ @options.hash_function = 'md5'
22
+ # Init options parser
23
+ @opt_parser = self.opts_parser
24
+ end
25
+
26
+ # Parse options, check arguments, then process the command
27
+ def run
28
+
29
+ if parsed_options? && arguments_valid?
30
+
31
+ @start_date = Time.now
32
+ puts "Start at #{@start_date}\n" if @options.verbose
33
+
34
+ output_options if @options.verbose
35
+
36
+ case sort!(@arguments[0], @arguments[1])
37
+ when 1
38
+ puts "Source directory doesn't exists!"
39
+ exit 1
40
+ when 2
41
+ puts "Source directory is empty!"
42
+ exit 2
43
+ end
44
+
45
+ @end_date = Time.now
46
+ puts "Finished at #{@end_date}\n" if @options.verbose
47
+ puts "Time wasted: #{Time.at(@end_date - @start_date).gmtime.strftime('%R:%S')}" unless @options.quite
48
+
49
+ else
50
+ output_usage
51
+ end
52
+
53
+ end
54
+
55
+ protected
56
+ def opts_parser
57
+ opts = OptionParser.new do |x|
58
+ x.banner= "img-sort [options] <source dir> <dst dir>"
59
+ x.on('-v', '--version', 'Show version') { output_version ; exit 0 }
60
+ x.on('-u', '--usage', 'Show usage') { puts @opt_parser; exit }
61
+ x.on('-V', '--verbose', 'Be verbose') { @options.verbose = true }
62
+ x.on('-q', '--quiet', 'Be quite') { @options.quiet = true }
63
+ x.on('-h', '--rename', 'Change file name to hash from that file (default)') {@options.rename = true}
64
+ x.on('-n', '--no-rename', 'Save original name') {@options.rename = false}
65
+ end
66
+ return opts
67
+
68
+ end
69
+
70
+
71
+ def parsed_options?
72
+ @opt_parser.parse!(@arguments) rescue puts "Argument error.\nArguments: #{@arguments.inspect}\n" ; return false
73
+ process_options
74
+ true
75
+ end
76
+
77
+ # Performs post-parse processing on options
78
+ def process_options
79
+ @options.verbose = false if @options.quiet
80
+ end
81
+
82
+ def output_options
83
+ puts "Options:\n"
84
+
85
+ @options.marshal_dump.each do |name, val|
86
+ puts " #{name} = #{val}\n"
87
+ end
88
+ puts "Arguments: #{@arguments.inspect}\n"
89
+ end
90
+
91
+ # True if required arguments were provided
92
+ def arguments_valid?
93
+ # TO DO - implement your real logic here
94
+ true if (@arguments[0].is_a?(String) && @arguments[1].is_a?(String))
95
+ end
96
+
97
+ def output_help
98
+ puts @opt_parser
99
+ exit 0
100
+ end
101
+
102
+ def output_usage
103
+ puts @opt_parser
104
+ exit 1
105
+ end
106
+
107
+ def output_version
108
+ puts "#{File.basename(__FILE__)} version #{VERSION}"
109
+ end
110
+
111
+ def sort!(from, to)
112
+ @from = File.expand_path(from)
113
+ @to = File.expand_path(to)
114
+ return 1 unless Dir.exists?(@from)
115
+ return 2 if Dir.entries(@from).count <= 2
116
+ @files = Dir.glob("#{@from}/*.{png,jpg,jpeg,gif,tiff,bmp}")
117
+ puts "Found #{@files.count} files" unless @options.quite
118
+ return 2 if @files.empty?
119
+ FileUtils.mkdir @to unless Dir.exists?(@to)
120
+ count = 0
121
+ @total = @files.length
122
+ @files.each do |file|
123
+ count += 1
124
+ img = Magick::Image::read(file).first
125
+ wh_dir_name = img.columns.to_s + 'x' + img.rows.to_s
126
+ FileUtils.mkdir "#{@to}/#{wh_dir_name}" unless Dir.exists?("#{@to}/#{wh_dir_name}")
127
+ if @options.rename
128
+ case @options.hash_function
129
+ when "md5"
130
+ digest = Digest::MD5.hexdigest(File.read(file))
131
+ else
132
+ return "11"
133
+ end
134
+ ext = File.extname(file)
135
+ FileUtils.cp(file, "#{@to}/#{wh_dir_name}/#{digest}#{ext}")
136
+ else
137
+ FileUtils.cp(file, "#{@to}/#{wh_dir_name}/")
138
+ end
139
+ puts file + ' copied' if @options.verbose
140
+ end
141
+ end
142
+
143
+ end
144
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ImgSort" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -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 'img-sort'
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,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: img-sort
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Andrey Cherkashin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-22 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rmagick
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: jeweler
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.5.2
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: rcov
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: rmagick
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ description: Sort images by their resolution and rename it if needed
82
+ email: with.out@me.com
83
+ executables:
84
+ - img-sort
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - .rspec
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - VERSION
99
+ - bin/img-sort
100
+ - img-sort.gemspec
101
+ - lib/img-sort.rb
102
+ - spec/img-sort_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: http://github.com/andoriyu/img-sort
105
+ licenses:
106
+ - MIT
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: -2357653575370366196
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: "0"
127
+ requirements: []
128
+
129
+ rubyforge_project:
130
+ rubygems_version: 1.7.2
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Image sorter
134
+ test_files:
135
+ - spec/img-sort_spec.rb
136
+ - spec/spec_helper.rb