mediatype_directory 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown
CHANGED
@@ -27,7 +27,8 @@ Create a new MediatypeDirectory object, passing in all your configuration option
|
|
27
27
|
config[:from] = '~/path/to/top-level-dir/where/your/files/are'
|
28
28
|
config[:to] = '~/path/to/dir/where/you/want/to/create/links'
|
29
29
|
config[:linktype] = 'hard' # default: 'soft'
|
30
|
-
config[:test_mode] = true # (or 'true'
|
30
|
+
config[:test_mode] = true # (or 'true', default: false) In test_mode, no directories or files are actually created and no old links are removed
|
31
|
+
config[:remove_old_links] = true # (or 'true', default: false) If true, all existing links in the link directory are deleted before creating new links
|
31
32
|
|
32
33
|
MediatypeDirectory.new(config).create_directory
|
33
34
|
|
@@ -37,7 +38,7 @@ config[:from] has aliases config[:source] & config[:directory_tree]. You can als
|
|
37
38
|
|
38
39
|
config[:to] has aliases config[:target] & config[:mediatype_dirname]. You can also call "md.target =", "md.to = " or "md.mediatype_dirname ="
|
39
40
|
|
40
|
-
It's safe to re-run this program as many times as you want. If a link already exists, it will be skipped. But if a new file is found that matches the criteria, a new link will be added.
|
41
|
+
It's safe to re-run this program as many times as you want. If a link already exists, it will be skipped (unless you set :remove_old_links = true). But if a new file is found that matches the criteria, a new link will be added.
|
41
42
|
|
42
43
|
If the original content of a hard link changes, the hard link will continue referring to the original version. You can delete the hard link (which should remove the old version from your system) and regenerate a new hardlink, which should point to the new version.
|
43
44
|
|
@@ -29,8 +29,8 @@ class MediatypeDirectory
|
|
29
29
|
'.divx','.dvdmedia','.f4v','.fbr','.flv','.hdmov',
|
30
30
|
'.imovieproj','.m2p','.m4v','.mod','.moi','.mov',
|
31
31
|
'.mp4','.mpeg','.mpg','.mts','.ogm','.ogv','.ogx',
|
32
|
-
'.rm','.rmvb','.scm','.srt','.
|
33
|
-
'.
|
32
|
+
'.rm','.rmvb','.scm','.srt','.vob','.vro','.wmv',
|
33
|
+
'.xvid','.yuv']
|
34
34
|
|
35
35
|
AUDIO_FILE_EXTENSIONS = ['.aa','.aa3','.acd','.acm','.aif','.amr','.ape',
|
36
36
|
'.at3','.caf','.dcf','.dss','.emp','.emx','.flac',
|
@@ -40,7 +40,7 @@ class MediatypeDirectory
|
|
40
40
|
'.wav','.wma']
|
41
41
|
|
42
42
|
attr_accessor :extensions, :linktype
|
43
|
-
attr_reader :mediatype_dirname, :directory_tree, :test_mode
|
43
|
+
attr_reader :mediatype_dirname, :directory_tree, :test_mode, :mediatype_files, :remove_old_links
|
44
44
|
|
45
45
|
class InvalidDirname < StandardError
|
46
46
|
end
|
@@ -51,10 +51,12 @@ class MediatypeDirectory
|
|
51
51
|
self.directory_tree = config[:directory_tree] || config[:source] || config[:from]
|
52
52
|
self.linktype = config[:linktype] || 'soft'
|
53
53
|
self.test_mode = config[:test_mode] || false
|
54
|
+
self.remove_old_links = config[:remove_old_links] || false
|
54
55
|
end
|
55
56
|
|
56
57
|
def create_directory
|
57
58
|
check_directories
|
59
|
+
delete_old_links if remove_old_links
|
58
60
|
create_links
|
59
61
|
end
|
60
62
|
|
@@ -80,6 +82,10 @@ class MediatypeDirectory
|
|
80
82
|
@test_mode = (val == true || val == 'true')
|
81
83
|
end
|
82
84
|
|
85
|
+
def remove_old_links=(val)
|
86
|
+
@remove_old_links = (val == true || val == 'true')
|
87
|
+
end
|
88
|
+
|
83
89
|
alias_method :what, :extensions
|
84
90
|
alias_method :what=, :extensions=
|
85
91
|
alias_method :source, :directory_tree
|
@@ -99,7 +105,7 @@ class MediatypeDirectory
|
|
99
105
|
|
100
106
|
def create_links
|
101
107
|
@mediatype_files = get_all_mediatype_files
|
102
|
-
#puts "Found these files: " +
|
108
|
+
#puts "Found these files: " + mediatype_files.to_s
|
103
109
|
mediatype_files_to_links
|
104
110
|
end
|
105
111
|
|
@@ -107,44 +113,51 @@ class MediatypeDirectory
|
|
107
113
|
# matching one of the file extensions
|
108
114
|
def get_all_mediatype_files
|
109
115
|
puts "Searching for files in #{directory_tree}"
|
110
|
-
Dir.chdir
|
111
|
-
|
116
|
+
# Rewrote to use absolute search paths because FakeFS chokes on Dir.chdir
|
117
|
+
matching_files = []
|
112
118
|
@extensions.each do |ex|
|
113
|
-
search_for = File.join("**", '*' + ex) # example: "
|
114
|
-
|
119
|
+
search_for = File.join(directory_tree, "**", '*' + ex) # example: "/home/xavier/Tech/Docs/**/*.pdf"
|
120
|
+
matching_files.concat(Dir.glob(search_for))
|
115
121
|
end
|
116
|
-
puts "Found these files: " +
|
117
|
-
convert_to_pathnames(
|
122
|
+
#puts "Found these files: " + matching_files.to_s
|
123
|
+
convert_to_pathnames(matching_files).delete_if { |file| file.dirname.to_s == mediatype_dirname.to_s }
|
118
124
|
end
|
119
125
|
|
120
|
-
def convert_to_pathnames(
|
121
|
-
|
126
|
+
def convert_to_pathnames(path_string_array)
|
127
|
+
path_string_array.map { |path_string| Pathname.new(path_string) }
|
122
128
|
end
|
123
129
|
|
124
130
|
def mediatype_files_to_links
|
125
|
-
|
126
|
-
@mediatype_files.each do |pathname|
|
131
|
+
mediatype_files.each do |pathname|
|
127
132
|
mediatype_file_to_link pathname
|
128
133
|
end
|
129
134
|
end
|
130
135
|
|
136
|
+
# In test_mode with delete_old_links, we want to pretend the links
|
137
|
+
# have been deleted, even though we're not actually deleting them
|
138
|
+
def pretend_links_do_not_exist
|
139
|
+
test_mode && remove_old_links
|
140
|
+
end
|
141
|
+
|
131
142
|
def mediatype_file_to_link(pathname)
|
132
143
|
puts "Attempting to create link for #{pathname.to_s}"
|
133
144
|
link = source_pathname_to_target_pathname(pathname)
|
134
|
-
if File.exists?(link.to_s)
|
145
|
+
if File.exists?(link.to_s) && !pretend_links_do_not_exist
|
135
146
|
puts "WARNING: #{link.to_s} already exists"
|
136
147
|
else
|
137
148
|
puts "Creating #{link.to_s}"
|
138
|
-
|
149
|
+
# Switched from File.link to FileUtils.ln because FakeFS doesn't know FileUtils.ln but knows File.link
|
150
|
+
(hardlinks? ? File.link(pathname.to_s, link.to_s) : FileUtils.ln_s(pathname.to_s, link.to_s)) unless test_mode
|
139
151
|
end
|
140
152
|
end
|
141
153
|
|
142
154
|
def source_pathname_to_target_pathname(source_pathname)
|
143
|
-
Pathname.new(mediatype_dirname) + source_pathname.basename
|
155
|
+
#Pathname.new(mediatype_dirname) + source_pathname.basename
|
156
|
+
mediatype_dirname + source_pathname.basename
|
144
157
|
end
|
145
158
|
|
146
159
|
def nil_or_convert_dirname(dirname)
|
147
|
-
(dirname.nil? || dirname == '') ? nil :
|
160
|
+
(dirname.nil? || dirname == '') ? nil : convert_to_pathname(expand_path(dirname))
|
148
161
|
end
|
149
162
|
|
150
163
|
def check_directories
|
@@ -154,7 +167,7 @@ class MediatypeDirectory
|
|
154
167
|
|
155
168
|
def create_missing_directories
|
156
169
|
[mediatype_dirname, directory_tree].each do|dn|
|
157
|
-
make_dirname(dn)
|
170
|
+
make_dirname(dn.to_s)
|
158
171
|
end
|
159
172
|
end
|
160
173
|
|
@@ -164,21 +177,43 @@ class MediatypeDirectory
|
|
164
177
|
end
|
165
178
|
end
|
166
179
|
|
167
|
-
def make_dirname(
|
168
|
-
unless File.directory?
|
169
|
-
puts "Creating directory #{
|
170
|
-
FileUtils.mkdir_p(
|
180
|
+
def make_dirname(dn_string)
|
181
|
+
unless File.directory? dn_string
|
182
|
+
puts "Creating directory #{dn_string}"
|
183
|
+
FileUtils.mkdir_p(dn_string) unless test_mode
|
171
184
|
end
|
172
185
|
end
|
173
186
|
|
174
187
|
# Ideally, should use a regexp that matches valid directories
|
175
188
|
# For now, a simple sanity check
|
176
189
|
def validate_dirname(dirname)
|
177
|
-
|
190
|
+
if !dirname.to_s.match(/^\//)
|
191
|
+
raise MediatypeDirectory::InvalidDirname, "#{dirname.to_s} is not a valid directory name"
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def expand_path(path)
|
196
|
+
File.expand_path(path)
|
178
197
|
end
|
179
198
|
|
180
|
-
def
|
181
|
-
|
199
|
+
def convert_to_pathname(path)
|
200
|
+
Pathname.new(path)
|
201
|
+
end
|
202
|
+
|
203
|
+
def delete_old_links
|
204
|
+
old_links.each do |ol|
|
205
|
+
puts "Deleting file #{ol}"
|
206
|
+
File.unlink(ol) unless test_mode
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
def old_links
|
211
|
+
old_links = []
|
212
|
+
@extensions.each do |ex|
|
213
|
+
search_for = File.join(mediatype_dirname, "**", '*' + ex) # example: "/home/xavier/Tech/Docs/**/*.pdf"
|
214
|
+
old_links.concat(Dir.glob(search_for))
|
215
|
+
end
|
216
|
+
old_links
|
182
217
|
end
|
183
218
|
|
184
219
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require_relative '../lib/mediatype_directory/mediatype_directory'
|
3
|
-
#require 'fakefs'
|
4
3
|
|
5
4
|
# Overriding FakeFS' File.expand_path
|
6
5
|
# because it delegates to the same class
|
@@ -11,6 +10,8 @@ module FakeFS
|
|
11
10
|
args[0].gsub(/~/,'/home/xavier')
|
12
11
|
end
|
13
12
|
end
|
13
|
+
# Started overriding FileUtils.ln because FakeFS doesn't know about it
|
14
|
+
# Instead switched to using File.link, which FakeFS knows about
|
14
15
|
end
|
15
16
|
|
16
17
|
# Overriding File.expand_path
|
@@ -50,12 +51,10 @@ describe MediatypeDirectory do
|
|
50
51
|
specify { Dir.exists?('/home/xavier/Tech2/JQuery') }
|
51
52
|
specify { Dir.pwd == '/home/xavier/Tech2' }
|
52
53
|
it "should find files using Dir.glob" do
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
Dir.getwd.should == "/home/xavier/Tech2"
|
58
|
-
Dir.glob(File.join("**","*.pdf")).should_not == []
|
54
|
+
Dir.glob(File.join("/home/xavier/Tech2","**","*.pdf")).should ==
|
55
|
+
["/home/xavier/Tech2/JQuery/jquery.pdf",
|
56
|
+
"/home/xavier/Tech2/Ruby/TESTING/ruby_testing.pdf",
|
57
|
+
"/home/xavier/Tech2/Ruby/ruby.pdf"]
|
59
58
|
end
|
60
59
|
end
|
61
60
|
|
@@ -70,7 +69,7 @@ describe MediatypeDirectory do
|
|
70
69
|
|
71
70
|
before { subject.mediatype_dirname = tilde_pdf_dir }
|
72
71
|
|
73
|
-
its(:mediatype_dirname) { should == File.expand_path(tilde_pdf_dir) }
|
72
|
+
its(:mediatype_dirname) { should == Pathname.new(File.expand_path(tilde_pdf_dir)) }
|
74
73
|
|
75
74
|
end
|
76
75
|
|
@@ -78,7 +77,7 @@ describe MediatypeDirectory do
|
|
78
77
|
|
79
78
|
before { subject.directory_tree = tilde_dir_tree }
|
80
79
|
|
81
|
-
its(:directory_tree) { should == File.expand_path(tilde_dir_tree) }
|
80
|
+
its(:directory_tree) { should == Pathname.new(File.expand_path(tilde_dir_tree)) }
|
82
81
|
|
83
82
|
end
|
84
83
|
|
@@ -99,7 +98,7 @@ describe MediatypeDirectory do
|
|
99
98
|
let(:config) { { mediatype_dirname: tilde_pdf_dir } }
|
100
99
|
|
101
100
|
it { should be_true }
|
102
|
-
its(:mediatype_dirname) { should == File.expand_path(tilde_pdf_dir) }
|
101
|
+
its(:mediatype_dirname) { should == Pathname.new(File.expand_path(tilde_pdf_dir)) }
|
103
102
|
|
104
103
|
end
|
105
104
|
|
@@ -117,8 +116,8 @@ describe MediatypeDirectory do
|
|
117
116
|
let(:config) { { target: tilde_pdf_dir, source: tilde_dir_tree } }
|
118
117
|
|
119
118
|
it { should be_true }
|
120
|
-
its(:mediatype_dirname) { should == File.expand_path(tilde_pdf_dir) }
|
121
|
-
its(:directory_tree) { should == File.expand_path(tilde_dir_tree) }
|
119
|
+
its(:mediatype_dirname) { should == Pathname.new(File.expand_path(tilde_pdf_dir)) }
|
120
|
+
its(:directory_tree) { should == Pathname.new(File.expand_path(tilde_dir_tree)) }
|
122
121
|
|
123
122
|
end
|
124
123
|
|
@@ -127,8 +126,8 @@ describe MediatypeDirectory do
|
|
127
126
|
let(:config) { { to: tilde_pdf_dir, from: tilde_dir_tree } }
|
128
127
|
|
129
128
|
it { should be_true }
|
130
|
-
its(:mediatype_dirname) { should == File.expand_path(tilde_pdf_dir) }
|
131
|
-
its(:directory_tree) { should == File.expand_path(tilde_dir_tree) }
|
129
|
+
its(:mediatype_dirname) { should == Pathname.new(File.expand_path(tilde_pdf_dir)) }
|
130
|
+
its(:directory_tree) { should == Pathname.new(File.expand_path(tilde_dir_tree)) }
|
132
131
|
|
133
132
|
end
|
134
133
|
|
@@ -155,7 +154,7 @@ describe MediatypeDirectory do
|
|
155
154
|
let(:config) { { directory_tree: tilde_dir_tree } }
|
156
155
|
|
157
156
|
it { should be_true }
|
158
|
-
its(:directory_tree) { should == File.expand_path(tilde_dir_tree) }
|
157
|
+
its(:directory_tree) { should == Pathname.new(File.expand_path(tilde_dir_tree)) }
|
159
158
|
|
160
159
|
end
|
161
160
|
|
@@ -201,18 +200,38 @@ describe MediatypeDirectory do
|
|
201
200
|
end
|
202
201
|
|
203
202
|
it "should create the correct files" do
|
204
|
-
pending "This test fails because FakeFS is broken"
|
205
203
|
subject.create_directory
|
206
204
|
Dir.exists?('/home/xavier/Tech3/Docs/Ruby').should be_true
|
207
|
-
Dir.
|
208
|
-
|
209
|
-
|
205
|
+
Dir.glob(File.join("/home/xavier/Tech3","**","*.pdf")).should ==
|
206
|
+
["/home/xavier/Tech3/Docs/Ruby/ruby.pdf",
|
207
|
+
"/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf"]
|
210
208
|
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf').should be_true
|
211
209
|
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby.pdf').should be_true
|
212
210
|
File.exists?('/home/xavier/Tech3/Docs/jquery.pdf').should be_false
|
213
211
|
File.exists?('/home/xavier/Tech3/Docs/xml.xml').should be_false
|
214
212
|
end
|
215
213
|
|
214
|
+
context "with remove_old_links = true" do
|
215
|
+
|
216
|
+
before do
|
217
|
+
FileUtils.mkdir_p('/home/xavier/Tech3/Docs/Ruby')
|
218
|
+
FileUtils.touch('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf')
|
219
|
+
FileUtils.touch('/home/xavier/Tech3/Docs/Ruby/ruby.pdf')
|
220
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf').should be_true
|
221
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby.pdf').should be_true
|
222
|
+
subject.remove_old_links = true
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should remove old links" do
|
226
|
+
subject.should_receive(:check_directories)
|
227
|
+
subject.should_receive(:create_links)
|
228
|
+
subject.create_directory
|
229
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf').should be_false
|
230
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby.pdf').should be_false
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
216
235
|
end
|
217
236
|
|
218
237
|
context "with test_mode = true" do
|
@@ -224,22 +243,65 @@ describe MediatypeDirectory do
|
|
224
243
|
Dir.exists?(xavier_docs_ruby).should be_false
|
225
244
|
end
|
226
245
|
|
227
|
-
it "should create
|
228
|
-
pending "This test fails because FakeFS is broken"
|
246
|
+
it "should not create any files" do
|
229
247
|
subject.create_directory
|
230
248
|
Dir.exists?('/home/xavier/Tech3/Docs/Ruby').should be_false
|
231
|
-
Dir.glob(File.join("**","*.pdf")).
|
249
|
+
Dir.glob(File.join("/home/xavier/Tech3/Docs/Ruby","**","*.pdf")).should == []
|
232
250
|
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf').should be_false
|
233
251
|
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby.pdf').should be_false
|
234
252
|
end
|
235
253
|
|
254
|
+
context "with remove_old_links = true" do
|
255
|
+
|
256
|
+
before do
|
257
|
+
FileUtils.mkdir_p('/home/xavier/Tech3/Docs/Ruby')
|
258
|
+
FileUtils.touch('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf')
|
259
|
+
FileUtils.touch('/home/xavier/Tech3/Docs/Ruby/ruby.pdf')
|
260
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf').should be_true
|
261
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby.pdf').should be_true
|
262
|
+
subject.remove_old_links = true
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should not remove old links" do
|
266
|
+
subject.should_receive(:check_directories)
|
267
|
+
subject.should_receive(:create_links)
|
268
|
+
subject.create_directory
|
269
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf').should be_true
|
270
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby.pdf').should be_true
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
context "when mediatype_dirname & directory_tree are valid" do
|
280
|
+
|
281
|
+
let(:config) { { mediatype_dirname: '/home/xavier/Tech3',
|
282
|
+
directory_tree: '/home/xavier/Tech2' } }
|
283
|
+
|
284
|
+
it "throws InvalidDirname error" do
|
285
|
+
expect { subject.create_directory }.to_not raise_error(MediatypeDirectory::InvalidDirname)
|
236
286
|
end
|
237
287
|
|
238
288
|
end
|
239
289
|
|
240
290
|
context "when invalid mediatype_dirname" do
|
241
291
|
|
242
|
-
let(:config) { { mediatype_dirname: '
|
292
|
+
let(:config) { { mediatype_dirname: 'not/valid/xavier/Tech3',
|
293
|
+
directory_tree: '/home/xavier/Tech2' } }
|
294
|
+
|
295
|
+
it "throws InvalidDirname error" do
|
296
|
+
expect { subject.create_directory }.to raise_error(MediatypeDirectory::InvalidDirname)
|
297
|
+
end
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
context "when invalid directory_tree" do
|
302
|
+
|
303
|
+
let(:config) { { mediatype_dirname: '/home/xavier/Tech3',
|
304
|
+
directory_tree: 'totally/invalid/xavier/Tech2' } }
|
243
305
|
|
244
306
|
it "throws InvalidDirname error" do
|
245
307
|
expect { subject.create_directory }.to raise_error(MediatypeDirectory::InvalidDirname)
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: mediatype_directory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- James Lavin
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|