mediatype_directory 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.swp
2
+ *.gem
data/README.markdown ADDED
@@ -0,0 +1,51 @@
1
+ # MediatypeDirectory
2
+
3
+ ## DESCRIPTION
4
+
5
+ MediatypeDirectory lets Linux/Mac users create a directory of (soft or hard) links to all files of a specified media type (or types) in a directory tree.
6
+
7
+ ## WHY?
8
+
9
+ I store content (text files, PDFs, HTML pages, office suite files, audio files, video files, etc.) mixed together in directory trees organized by subject. For example, the directory ~/Tech/Ruby/TESTING/RSpec may hold videos, HTML files, PDFs, podcasts, etc. all related to RSpec testing in Ruby.
10
+
11
+ But when I'm programming, I often want to quickly grab a PDF reference document without searching through the directory tree. I want links to PDF files on Jasmine, RSpec, Rails, Ruby, Coffeescript, Underscore, JQuery, Javascript, Backbone, etc. all in one directory.
12
+
13
+ And when I have free time to watch videos, I'd like to quickly see the list of all available programming video files.
14
+
15
+ ## REQUIREMENTS
16
+
17
+ I've run this only on Linux. It likely works on OS X. It probably won't work on Windows.
18
+
19
+ ## BASIC USAGE
20
+
21
+ Create a new MediatypeDirectory object, passing in all your configuration options (or setting them on the object via setters). Then tell the new object to .create_directory:
22
+
23
+ require 'mediatype_directory'
24
+
25
+ config = {}
26
+ config[:extensions] = ['.flv','.mov','.mpg','.mp4']
27
+ config[:mediatype_dirname] = '~/path/to/dir/where/you/want/to/create/links'
28
+ config[:directory_tree] = '~/path/to/top-level-dir/you/want/to/create/mediatype-directory/for'
29
+ config[:linktype] = 'hard' # default: 'soft'
30
+ config[:test_mode] = true # default: false In test_mode, no directories or files are actually created
31
+
32
+ MediatypeDirectory.new(config).create_directory
33
+
34
+ ## EXAMPLE
35
+
36
+ require 'mediatype_directory'
37
+
38
+ md = MediatypeDirectory.new({
39
+ extensions: [".flv",".mov",".mpg",'.mp4'], # List of file extensions for which you wish to generate links
40
+ directory_tree: '/home/jimmy/Tech/Ruby', # Where to look for existing files
41
+ mediatype_dirname: '~/Tech/Docs2/Videos', # Where to store links to existing files
42
+ linktype: 'hard', # Create hard links, not soft links (a.k.a. symbolic links)
43
+ test_mode: true # Show what would happen without actually creating directories or files
44
+ })
45
+
46
+ md.create_directory
47
+
48
+ ## LEGAL DISCLAIMER
49
+
50
+ Please use at your own risk. I guarantee nothing about this program.
51
+
@@ -0,0 +1,10 @@
1
+ require_relative '../lib/mediatype_directory/mediatype_directory'
2
+
3
+ md = MediatypeDirectory.new({
4
+ extensions: ['.pdf'],
5
+ directory_tree: '/home/jimmy/Tech/Ruby/DOCUMENTATION',
6
+ mediatype_dirname: '~/Tech/Docs2/PDFs',
7
+ linktype: 'hard'
8
+ })
9
+
10
+ md.create_directory
@@ -0,0 +1,10 @@
1
+ require_relative '../lib/mediatype_directory/mediatype_directory'
2
+
3
+ md = MediatypeDirectory.new({
4
+ extensions: [".flv",".mov",".mpg",'.mp4'],
5
+ directory_tree: '/home/jimmy/Tech/Ruby',
6
+ mediatype_dirname: '~/Tech/Docs2/Videos',
7
+ linktype: 'hard'
8
+ })
9
+
10
+ md.create_directory
@@ -0,0 +1 @@
1
+ require_relative 'mediatype_directory/mediatype_directory'
@@ -0,0 +1,145 @@
1
+ require 'rubygems'
2
+ require 'fileutils'
3
+ require 'pathname'
4
+
5
+ # MediatypeDirectory.new takes a configuration hash and creates an object with a create_directory() method
6
+ # Calling create_directory() on the MediatypeDirectory object creates a directory, configured according to
7
+ # the configuration hash settings, containing softlinks to all files of the requested file extension(s) in
8
+ # the specified directory tree.
9
+ #
10
+ # Usage:
11
+ # MediatypeDirectory.new(options_hash).create_directory
12
+ #
13
+ # Alternative usage:
14
+ # md = MediatypeDirectory.new
15
+ # md.property = value (for all desired property-value pairs)
16
+ # md.create_directory
17
+ #
18
+ # Example:
19
+ # To create a directory at '~/Tech/Docs/PDFs' with softlinks to all PDF and ODT files under '~/Tech':
20
+ # md = MediatypeDirectory.new
21
+ # md.extensions = ['.pdf','.odt']
22
+ # md.mediatype_dirname = '~/Tech/Docs/PDFs'
23
+ # md.directory_tree = '~/Tech'
24
+ # md.create_directory
25
+ #
26
+ class MediatypeDirectory
27
+
28
+ attr_accessor :extensions, :linktype, :test_mode
29
+ attr_reader :mediatype_dirname, :directory_tree
30
+
31
+ class InvalidDirname < StandardError
32
+ end
33
+
34
+ def initialize(config)
35
+ self.extensions = config[:extensions]
36
+ self.mediatype_dirname = config[:mediatype_dirname]
37
+ self.directory_tree = config[:directory_tree]
38
+ self.linktype = config[:linktype] || 'soft'
39
+ self.test_mode = config[:test_mode] || false
40
+ end
41
+
42
+ def create_directory
43
+ check_directories
44
+ create_links
45
+ end
46
+
47
+ def mediatype_dirname=(dirname)
48
+ @mediatype_dirname = nil_or_convert_dirname(dirname)
49
+ end
50
+
51
+ def directory_tree=(dirname)
52
+ @directory_tree = nil_or_convert_dirname(dirname)
53
+ end
54
+
55
+ private
56
+
57
+ def hardlinks?
58
+ @linktype == 'hard'
59
+ end
60
+
61
+ def create_links
62
+ @mediatype_files = get_all_mediatype_files
63
+ #puts "Found these files: " + @mediatype_files.to_s
64
+ mediatype_files_to_links
65
+ end
66
+
67
+ # returns array of file pathnames in the directory_tree
68
+ # matching one of the file extensions
69
+ def get_all_mediatype_files
70
+ puts "Searching for files in #{directory_tree}"
71
+ Dir.chdir(directory_tree)
72
+ mediatype_files = []
73
+ @extensions.each do |ex|
74
+ search_for = File.join("**", '*' + ex) # example: "**/*.pdf"
75
+ mediatype_files.concat(Dir.glob(search_for))
76
+ end
77
+ puts "Found these files: " + mediatype_files.to_s
78
+ convert_to_pathnames(mediatype_files).delete_if { |mf| mf.dirname.to_s == @mediatype_dirname }
79
+ end
80
+
81
+ def convert_to_pathnames(filenames)
82
+ filenames.map { |mf| Pathname.new(mf).realdirpath }
83
+ end
84
+
85
+ def mediatype_files_to_links
86
+ Dir.chdir(mediatype_dirname) unless test_mode
87
+ @mediatype_files.each do |pathname|
88
+ mediatype_file_to_link pathname
89
+ end
90
+ end
91
+
92
+ def mediatype_file_to_link(pathname)
93
+ puts "Attempting to create link for #{pathname.to_s}"
94
+ link = source_pathname_to_target_pathname(pathname)
95
+ if File.exists?(link.to_s)
96
+ puts "WARNING: #{link.to_s} already exists"
97
+ else
98
+ puts "Creating #{link.to_s}"
99
+ (hardlinks? ? FileUtils.ln(pathname.to_s, link.to_s) : FileUtils.ln_s(pathname.to_s, link.to_s)) unless test_mode
100
+ end
101
+ end
102
+
103
+ def source_pathname_to_target_pathname(source_pathname)
104
+ Pathname.new(mediatype_dirname) + source_pathname.basename
105
+ end
106
+
107
+ def nil_or_convert_dirname(dirname)
108
+ (dirname.nil? || dirname == '') ? nil : convert_dirname(dirname)
109
+ end
110
+
111
+ def check_directories
112
+ validate_directories
113
+ create_missing_directories
114
+ end
115
+
116
+ def create_missing_directories
117
+ [mediatype_dirname, directory_tree].each do|dn|
118
+ make_dirname(dn)
119
+ end
120
+ end
121
+
122
+ def validate_directories
123
+ [mediatype_dirname, directory_tree].each do|dn|
124
+ validate_dirname(dn)
125
+ end
126
+ end
127
+
128
+ def make_dirname(dn)
129
+ unless File.directory? dn
130
+ puts "Creating directory #{dn}"
131
+ FileUtils.mkdir_p(dn) unless test_mode
132
+ end
133
+ end
134
+
135
+ # Ideally, should use a regexp that matches valid directories
136
+ # For now, a simple sanity check
137
+ def validate_dirname(dirname)
138
+ raise MediatypeDirectory::InvalidDirname, "#{dirname} is not a valid directory name" if dirname.match(/\s/) || !dirname.match(/^\//)
139
+ end
140
+
141
+ def convert_dirname(dirname)
142
+ File.expand_path(dirname)
143
+ end
144
+
145
+ end
@@ -0,0 +1,3 @@
1
+ module MediatypeDirectory
2
+ VERSION = "0.0.2"
3
+ end
data/license.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2011 by James K. Lavin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
@@ -0,0 +1,20 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'mediatype_directory/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'mediatype_directory'
6
+ s.version = MediatypeDirectory::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ['James Lavin']
9
+ s.email = ['mediatype_directory@futureresearch.com']
10
+ s.homepage = "https://github.com/JamesLavin/mediatype_directory"
11
+ s.summary = %q{Creates directory of links for all files with specified mediatype in subdirectory tree}
12
+ s.description = %q{Creates directory of hard or soft links for all files with specified mediatype (.pdf, .mp4, etc.) in subdirectory tree}
13
+ #s.add_runtime_dependency = ['fileutils','pathname']
14
+ s.add_development_dependency 'rspec'
15
+ s.add_development_dependency 'fakefs'
16
+ s.require_paths = ['lib']
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ #s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ end
@@ -0,0 +1,183 @@
1
+ require "spec_helper"
2
+ require_relative '../lib/mediatype_directory/mediatype_directory'
3
+ #require 'fakefs'
4
+
5
+ # Overriding FakeFS' File.expand_path
6
+ # because it delegates to the same class
7
+ # method in the REAL file system
8
+ module FakeFS
9
+ class File
10
+ def self.expand_path(*args)
11
+ args[0].gsub(/~/,'/home/xavier')
12
+ end
13
+ end
14
+ end
15
+
16
+ # Overriding File.expand_path
17
+ # so it will provide the fake user's
18
+ # home directory
19
+ class File
20
+ def self.expand_path(*args)
21
+ args[0].gsub(/~/,'/home/xavier')
22
+ end
23
+ end
24
+
25
+ describe MediatypeDirectory do
26
+
27
+ subject { MediatypeDirectory.new(config) }
28
+
29
+ let(:tilde_pdf_dir) { '~/my/pdf_dir' }
30
+ let(:tilde_dir_tree) { '~/my/techfiles' }
31
+ let(:exts) { ['.pdf','.odt'] }
32
+
33
+ before do
34
+ FileUtils.mkdir_p('/home/xavier/Tech2')
35
+ FileUtils.mkdir_p('/home/xavier/Tech2/Ruby/TESTING')
36
+ FileUtils.mkdir_p('/home/xavier/Tech2/JQuery')
37
+ FileUtils.mkdir_p('/home/xavier/Tech2/XML')
38
+ FileUtils.touch('/home/xavier/Tech2/Ruby/ruby.pdf')
39
+ FileUtils.touch('/home/xavier/Tech2/Ruby/TESTING/ruby_testing.pdf')
40
+ FileUtils.touch('/home/xavier/Tech2/JQuery/jquery.pdf')
41
+ FileUtils.touch('/home/xavier/Tech2/XML/xml.xml')
42
+ Dir.chdir('/home/xavier/Tech2')
43
+ end
44
+
45
+ describe "test that FakeFS is properly configured" do
46
+ specify { File.exists?('/home/xavier/Tech2/Ruby/ruby.pdf') }
47
+ specify { Dir.exists?('/home/xavier/Tech2/JQuery') }
48
+ specify { Dir.pwd == '/home/xavier/Tech2' }
49
+ it "should find files using Dir.glob" do
50
+ pending "This test fails because FakeFS is broken"
51
+ # https://github.com/defunkt/fakefs/issues/142
52
+ # https://github.com/defunkt/fakefs/issues/121
53
+ Dir.chdir("/home/xavier/Tech2")
54
+ Dir.getwd.should == "/home/xavier/Tech2"
55
+ Dir.glob(File.join("**","*.pdf")).should_not == []
56
+ end
57
+ end
58
+
59
+ context "when config is empty hash" do
60
+
61
+ let(:config) { {} }
62
+
63
+ it { should be_true }
64
+ it { should respond_to :create_directory }
65
+
66
+ context "when setter sets mediatype_dirname" do
67
+
68
+ before { subject.mediatype_dirname = tilde_pdf_dir }
69
+
70
+ its(:mediatype_dirname) { should == File.expand_path(tilde_pdf_dir) }
71
+
72
+ end
73
+
74
+ context "when setter sets directory_tree" do
75
+
76
+ before { subject.directory_tree = tilde_dir_tree }
77
+
78
+ its(:directory_tree) { should == File.expand_path(tilde_dir_tree) }
79
+
80
+ end
81
+
82
+ context "when setter sets extensions" do
83
+
84
+ before do
85
+ subject.extensions = exts
86
+ end
87
+
88
+ its(:extensions) { should == exts }
89
+
90
+ end
91
+
92
+ end
93
+
94
+ context "when config sets mediatype_dirname" do
95
+
96
+ let(:config) { { mediatype_dirname: tilde_pdf_dir } }
97
+
98
+ it { should be_true }
99
+
100
+ #it "should set mediatype_dirname correctly" do
101
+ specify { subject.mediatype_dirname.should == File.expand_path(tilde_pdf_dir) }
102
+ #its(:mediatype_dirname) { should == File.expand_path(tilde_pdf_dir) }
103
+ #end
104
+
105
+ end
106
+
107
+ context "when config sets directory_tree" do
108
+
109
+ let(:config) { { directory_tree: tilde_dir_tree } }
110
+
111
+ it { should be_true }
112
+ its(:directory_tree) { should == File.expand_path(tilde_dir_tree) }
113
+
114
+ end
115
+
116
+ context "when config sets extensions" do
117
+
118
+ let(:config) { { extensions: exts } }
119
+
120
+ it { should be_true }
121
+ its(:extensions) { should == exts }
122
+
123
+ end
124
+
125
+ describe "#create_directory" do
126
+
127
+ context "when values are set properly (with tildes)" do
128
+
129
+ let(:config) { { mediatype_dirname: tilde_pdf_dir,
130
+ directory_tree: tilde_dir_tree,
131
+ extensions: exts } }
132
+
133
+ it "should call :check directories and :create_links" do
134
+ subject.should_receive(:check_directories)
135
+ Dir.should_receive(:chdir)
136
+ subject.should_receive(:create_links)
137
+ subject.create_directory
138
+ end
139
+
140
+ end
141
+
142
+ context "when values are set properly (with full paths)" do
143
+
144
+ let(:xavier_docs_ruby) { '/home/xavier/Tech3/Docs/Ruby' }
145
+ let(:xavier_ruby) { '/home/xavier/Tech2/Ruby' }
146
+ let(:config) { { mediatype_dirname: xavier_docs_ruby,
147
+ directory_tree: xavier_ruby,
148
+ extensions: ['.pdf'],
149
+ linktype: 'hard' } }
150
+
151
+ it "should create the correct directory" do
152
+ subject.create_directory
153
+ Dir.exists?(xavier_docs_ruby).should be_true
154
+ end
155
+
156
+ it "should create the correct files" do
157
+ pending "This test fails because FakeFS is broken"
158
+ subject.create_directory
159
+ Dir.exists?('/home/xavier/Tech3/Docs/Ruby').should be_true
160
+ Dir.chdir('/home/xavier/Tech3/Docs/Ruby')
161
+ Dir.getwd.should == '/home/xavier/Tech3/Docs/Ruby'
162
+ Dir.glob(File.join("**","*.pdf")).should_not == []
163
+ File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf').should be_true
164
+ File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby.pdf').should be_true
165
+ File.exists?('/home/xavier/Tech3/Docs/jquery.pdf').should be_false
166
+ File.exists?('/home/xavier/Tech3/Docs/xml.xml').should be_false
167
+ end
168
+
169
+ end
170
+
171
+ context "when invalid mediatype_dirname" do
172
+
173
+ let(:config) { { mediatype_dirname: '~/techfiles/ /pd fs' } }
174
+
175
+ it "throws InvalidDirname error" do
176
+ expect { subject.create_directory }.to raise_error(MediatypeDirectory::InvalidDirname)
177
+ end
178
+
179
+ end
180
+
181
+ end
182
+
183
+ end
@@ -0,0 +1,7 @@
1
+ require 'fakefs/spec_helpers'
2
+
3
+ RSpec.configure do |config|
4
+ config.include FakeFS::SpecHelpers
5
+ end
6
+
7
+ Dir["./spec/support/**/*.rb"].each {|f| require f}
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mediatype_directory
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - James Lavin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ none: false
28
+ prerelease: false
29
+ type: :development
30
+ - !ruby/object:Gem::Dependency
31
+ name: fakefs
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ none: false
44
+ prerelease: false
45
+ type: :development
46
+ description: Creates directory of hard or soft links for all files with specified mediatype (.pdf, .mp4, etc.) in subdirectory tree
47
+ email:
48
+ - mediatype_directory@futureresearch.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - README.markdown
55
+ - examples/create_pdf_dir.rb
56
+ - examples/create_video_dir.rb
57
+ - lib/mediatype_directory.rb
58
+ - lib/mediatype_directory/mediatype_directory.rb
59
+ - lib/mediatype_directory/version.rb
60
+ - license.txt
61
+ - mediatype_directory.gemspec
62
+ - spec/mediatype_directory_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: https://github.com/JamesLavin/mediatype_directory
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ none: false
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ none: false
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Creates directory of links for all files with specified mediatype in subdirectory tree
88
+ test_files: []
89
+ ...