filerenamer 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES +8 -0
- data/VERSION +1 -1
- data/bin/renhash +2 -2
- data/bin/rennum +6 -5
- data/bin/renpad +3 -2
- data/bin/renpar +3 -2
- data/bin/renreg +3 -2
- data/bin/rensub +3 -2
- data/bin/rentime +15 -7
- data/example/rennum/1 +0 -0
- data/example/rennum/2 +0 -0
- data/example/rennum/3 +0 -0
- data/example/rennum/4 +0 -0
- data/example/rensub/aa +0 -0
- data/example/rensub/aaa +0 -0
- data/example/rensub/aaaa +0 -0
- data/example/rensub/aaaaa +0 -0
- data/lib/filerenamer.rb +1 -11
- data/lib/filerenamer/commander.rb +217 -207
- data/lib/filerenamer/optionparser.rb +2 -1
- data/lib/filerenamer/renameorderer.rb +153 -0
- data/test/commander/a.file +0 -0
- data/test/commander/b.file +0 -0
- data/test/renameorderer/01/0 +0 -0
- data/test/renameorderer/01/1 +0 -0
- data/test/renameorderer/012/0 +0 -0
- data/test/renameorderer/012/1 +0 -0
- data/test/renameorderer/012/2 +0 -0
- data/test/renameorderer/0123/0 +0 -0
- data/test/renameorderer/0123/1 +0 -0
- data/test/renameorderer/0123/2 +0 -0
- data/test/renameorderer/0123/3 +0 -0
- data/test/renameorderer/02/0 +0 -0
- data/test/renameorderer/02/2 +0 -0
- data/test/test_commander.rb +136 -72
- data/test/test_renameorderer.rb +123 -0
- metadata +26 -4
- data/filerenamer.gemspec +0 -126
@@ -25,9 +25,10 @@ module FileRenamer
|
|
25
25
|
on("-m", "--move" , "Move mode.(default)" ){@options[:move] = true}
|
26
26
|
on("-h", "--hardlink", "Hardlink mode." ){@options[:hardlink]= true}
|
27
27
|
on("-s", "--symlink" , "Symlink mode." ){@options[:symlink] = true}
|
28
|
-
on("-g", "--git" , "Git-mv mode." ){@options[:git]
|
28
|
+
on("-g", "--git" , "Git-mv mode." ){@options[:git] = true}
|
29
29
|
on("-q", "--quiet" , "Quiet mode. Forced non-interactive."){
|
30
30
|
@options[:quiet] = true
|
31
|
+
@options[:yes] = true
|
31
32
|
#このオプションが設定されているときは強制的に --yes として扱われる。
|
32
33
|
#non_interactive_mode になる。
|
33
34
|
}
|
@@ -0,0 +1,153 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
require 'tempfile'
|
5
|
+
|
6
|
+
$DEBUG = false
|
7
|
+
|
8
|
+
#
|
9
|
+
#
|
10
|
+
#
|
11
|
+
class FileRenamer::RenameOrderer
|
12
|
+
attr_reader :rename_processes, # in order
|
13
|
+
:unable_processes
|
14
|
+
# example of 'processes'
|
15
|
+
# [
|
16
|
+
# [old0, new0],
|
17
|
+
# [old1, new1],
|
18
|
+
# ...
|
19
|
+
# ]
|
20
|
+
def initialize(olds_news, dir = '.')
|
21
|
+
@dir = dir
|
22
|
+
#@all_processes = processes.to_h
|
23
|
+
@all_processes = olds_news
|
24
|
+
@unworked_processes = Marshal.load(Marshal.dump(@all_processes))
|
25
|
+
@rename_processes = []
|
26
|
+
@unable_processes = []
|
27
|
+
|
28
|
+
@post_processes = []
|
29
|
+
while ! @unworked_processes.empty?
|
30
|
+
key = @unworked_processes.keys[0]
|
31
|
+
@first_old = key
|
32
|
+
class_process(key)
|
33
|
+
end
|
34
|
+
@rename_processes += @post_processes unless @post_processes.empty?
|
35
|
+
@post_processes = []
|
36
|
+
|
37
|
+
@unable_processes.sort!
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# 再帰的メソッド
|
43
|
+
# その process と、依存する全ての process を @rename_processes と @unable_processes に分類する。
|
44
|
+
# その process が実行可能であるか? を返す。
|
45
|
+
def class_process(old)
|
46
|
+
|
47
|
+
new = @unworked_processes[old]
|
48
|
+
|
49
|
+
if $DEBUG
|
50
|
+
puts "=" * 60
|
51
|
+
pp self
|
52
|
+
print "old "
|
53
|
+
pp old
|
54
|
+
print "new "
|
55
|
+
pp new
|
56
|
+
puts
|
57
|
+
end
|
58
|
+
|
59
|
+
# old と new が一致していれば不可
|
60
|
+
if old == new
|
61
|
+
puts "old(#{old}) == new(#{new}). retrun false." if $DEBUG
|
62
|
+
add_unable(old, new)
|
63
|
+
return false
|
64
|
+
end
|
65
|
+
puts "old(#{old}) != new(#{new})" if $DEBUG
|
66
|
+
|
67
|
+
# old が存在しなければ不可
|
68
|
+
unless FileTest.exist?("#{@dir}/#{old}")
|
69
|
+
puts "old(#{old}) not exist in the file system. return false." if $DEBUG
|
70
|
+
add_unable(old, new)
|
71
|
+
return false
|
72
|
+
end
|
73
|
+
puts "old(#{old}) exist" if $DEBUG
|
74
|
+
|
75
|
+
# new がどれかの dst と重複ならば、不可
|
76
|
+
if @all_processes.values.select{|name| new == name}.size > 1
|
77
|
+
puts "new(#{new}) is dpulicated in #{@all_processes.values}. return false." if $DEBUG
|
78
|
+
add_unable(old, new)
|
79
|
+
return false
|
80
|
+
end
|
81
|
+
puts "new(#{new}) is only one in new of @all_processes( #{@all_processes})" if $DEBUG
|
82
|
+
|
83
|
+
unless File.exist?("#{@dir}/#{new}") # new がファイルとして存在しないならば、可能
|
84
|
+
puts "new(#{new}) does not exist in the filesystem. return true." if $DEBUG
|
85
|
+
add_rename(old, new)
|
86
|
+
return true
|
87
|
+
end
|
88
|
+
puts "new(#{new}) exist in the filesystem" if $DEBUG
|
89
|
+
|
90
|
+
# new がファイルとして存在する
|
91
|
+
|
92
|
+
# 可能リストに入っていたら、ここまでにリネームされているので可能
|
93
|
+
if @rename_processes.map{|o, n| o}.include?(new)
|
94
|
+
puts "new(#{new}) is included in old of @rename_processes(#{@rename_processeses}). return true." if $DEBUG
|
95
|
+
add_rename(old, new)
|
96
|
+
return true
|
97
|
+
end
|
98
|
+
|
99
|
+
# new が、他のいずれかの process の old で ない ならば、空かないので不可
|
100
|
+
unless @unworked_processes[new]
|
101
|
+
puts "@unworked_processes[new](#{@unworked_processes[new] }). return false." if $DEBUG
|
102
|
+
add_unable(old, new)
|
103
|
+
return false
|
104
|
+
end
|
105
|
+
|
106
|
+
# new が、他のいずれかの process の old で ある
|
107
|
+
|
108
|
+
# new が依存性チェーンの先頭と同じであれば、
|
109
|
+
# 先頭を一時ファイルにリネームして、その上でリネーム可能とし、
|
110
|
+
# 最後に一時ファイルからリネームする処理を追加。
|
111
|
+
if new == @first_old
|
112
|
+
puts "new #{new} == first_old #{@first_old}. use tmp file and return true." if $DEBUG
|
113
|
+
#tmp = Tempfile.new("#{@first_old}-", @dir).path
|
114
|
+
tmp = Tempfile.new("#{@all_processes[@first_old]}-#{@first_old}-", @dir).path
|
115
|
+
add_rename(@first_old, tmp)
|
116
|
+
@post_processes << [tmp, @all_processes[@first_old]]
|
117
|
+
pp self if $DEBUG
|
118
|
+
return true
|
119
|
+
end
|
120
|
+
puts "new(#{new}) != first_old(#{@first_old})" if $DEBUG
|
121
|
+
|
122
|
+
puts "class_process(new)" if $DEBUG
|
123
|
+
pp self if $DEBUG
|
124
|
+
#ここでの new からリネームするプロセスについて、再帰的に実行
|
125
|
+
if class_process(new)
|
126
|
+
pp self if $DEBUG
|
127
|
+
puts "depending process can be executed. return true." if $DEBUG
|
128
|
+
#add_rename(old, new) unless @rename_processes.map{|o, n| o}.include?(old)
|
129
|
+
#add_rename(old, new)
|
130
|
+
return true
|
131
|
+
else
|
132
|
+
puts "depending process cannot be executed. return false." if $DEBUG
|
133
|
+
add_unable(old, new)
|
134
|
+
return false
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
private
|
139
|
+
|
140
|
+
def add_rename(old, new)
|
141
|
+
@rename_processes << [old, new]
|
142
|
+
@unworked_processes.delete(old)
|
143
|
+
return true
|
144
|
+
end
|
145
|
+
|
146
|
+
def add_unable(old, new)
|
147
|
+
@unable_processes << [old, new]
|
148
|
+
@unworked_processes.delete(old)
|
149
|
+
return false
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/test/test_commander.rb
CHANGED
@@ -13,17 +13,18 @@ require "capture_stdout"
|
|
13
13
|
|
14
14
|
module FileRenamer
|
15
15
|
class Commander
|
16
|
-
public :check_new_names
|
16
|
+
#public :check_new_names
|
17
17
|
public :ask_yes?
|
18
|
-
public :run
|
19
18
|
public :make_new_names
|
20
19
|
public :paths
|
20
|
+
public :transplant
|
21
|
+
public :rmdir_p
|
22
|
+
public :convert
|
21
23
|
attr_reader :options
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
27
|
class TC_Commander < Test::Unit::TestCase
|
26
|
-
EXIST_FILE = "test/filerenamer/dummy.txt"
|
27
28
|
|
28
29
|
A_0 = "test/filerenamer/a0.txt"
|
29
30
|
A_1 = "test/filerenamer/a1.txt"
|
@@ -144,7 +145,6 @@ class TC_Commander < Test::Unit::TestCase
|
|
144
145
|
|
145
146
|
#:quiet
|
146
147
|
FileUtils.rm(A_1) if FileTest.exist?(A_1)
|
147
|
-
io = StringIO.new
|
148
148
|
#
|
149
149
|
options = {:copy => true, :quiet => true}
|
150
150
|
fr01 = FileRenamer::Commander.new(options, [A_0])
|
@@ -197,17 +197,17 @@ class TC_Commander < Test::Unit::TestCase
|
|
197
197
|
assert_equal(false, FileTest.exist?(A_1))
|
198
198
|
#
|
199
199
|
correct =
|
200
|
-
"Unable files:\n" +
|
201
|
-
" cp -r #{A_0} #{A_0}\n" +
|
202
|
-
" cp -r #{A_1} #{A_0}\n" +
|
203
|
-
"\n" +
|
200
|
+
#"Unable files:\n" +
|
201
|
+
#" cp -r #{A_0} #{A_0}\n" +
|
202
|
+
#" cp -r #{A_1} #{A_0}\n" +
|
203
|
+
#"\n" +
|
204
204
|
"Done. No executable files.\n"
|
205
205
|
t = str
|
206
206
|
assert_equal(correct[0], t[0])
|
207
|
-
assert_equal(correct[1], t[1])
|
208
|
-
assert_equal(correct[2], t[2])
|
209
|
-
assert_equal(correct[3], t[3])
|
210
|
-
assert_equal(correct[4], t[4])
|
207
|
+
#assert_equal(correct[1], t[1])
|
208
|
+
#assert_equal(correct[2], t[2])
|
209
|
+
#assert_equal(correct[3], t[3])
|
210
|
+
#assert_equal(correct[4], t[4])
|
211
211
|
$stdin = STDIN
|
212
212
|
|
213
213
|
## ファイルリストを与えないときはカレントディレクトリの全ファイルが対象。
|
@@ -233,7 +233,6 @@ class TC_Commander < Test::Unit::TestCase
|
|
233
233
|
|
234
234
|
def test_make_new_names
|
235
235
|
options = {}
|
236
|
-
#pp [A_0, A_1]; exit
|
237
236
|
fr00 = FileRenamer::Commander.new(options, [A_0, A_1])
|
238
237
|
assert_equal(
|
239
238
|
{
|
@@ -247,32 +246,61 @@ class TC_Commander < Test::Unit::TestCase
|
|
247
246
|
end
|
248
247
|
|
249
248
|
def test_check_new_names
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
249
|
+
#exist_file = "test/filerenamer/dummy.txt"
|
250
|
+
#files = {
|
251
|
+
# "alice.txt" => exist_file,
|
252
|
+
# "bcdefgh.txt" => exist_file,
|
253
|
+
# "charly.txt" => "C.txt",
|
254
|
+
# "david.txt" => "D.txt",
|
255
|
+
# exist_file => exist_file,
|
256
|
+
# "eva.txt" => "alice.txt",
|
257
|
+
# "f.txt" => "f.txt",
|
258
|
+
# "g.txt" => nil,
|
259
|
+
#}
|
260
|
+
#ok_list, ng_list = @fr00.check_new_names(files)
|
261
|
+
#assert_equal(
|
262
|
+
# { "charly.txt" => "C.txt",
|
263
|
+
# "david.txt" => "D.txt",
|
264
|
+
# },
|
265
|
+
# ok_list
|
266
|
+
#)
|
267
|
+
#assert_equal(
|
268
|
+
# { "alice.txt" => exist_file,
|
269
|
+
# "bcdefgh.txt" => exist_file,
|
270
|
+
# exist_file => exist_file,
|
271
|
+
# "eva.txt" => "alice.txt",
|
272
|
+
# },
|
273
|
+
# ng_list
|
274
|
+
#)
|
275
|
+
|
276
|
+
#a_file = "test/commander/a.file" #exist
|
277
|
+
#b_file = "test/commander/b.file" #exist
|
278
|
+
#c_file = "test/commander/c.file" #not exist
|
279
|
+
#d_file = "test/commander/d.file" #not exist
|
280
|
+
|
281
|
+
## 変化なし
|
282
|
+
#files = { "a.file" => 'a.file', "b.file" => 'b.file', }
|
283
|
+
#ok_list, ng_list = @fr00.check_new_names(files)
|
284
|
+
#assert_equal( {}, ok_list)
|
285
|
+
#assert_equal( {}, ng_list)
|
286
|
+
|
287
|
+
## 相互入れ替え
|
288
|
+
#files = { "a.file" => 'b.file', "b.file" => 'a.file', }
|
289
|
+
#ok_list, ng_list = @fr00.check_new_names(files)
|
290
|
+
#assert_equal( files, ok_list)
|
291
|
+
#assert_equal( {} , ng_list)
|
292
|
+
|
293
|
+
## 宛先が重複
|
294
|
+
#files = { "a.file" => 'c.file', "b.file" => 'c.file', }
|
295
|
+
#ok_list, ng_list = @fr00.check_new_names(files)
|
296
|
+
#assert_equal( {} , ok_list)
|
297
|
+
#assert_equal( files, ng_list)
|
298
|
+
|
299
|
+
## 玉突き
|
300
|
+
#files = { "a.file" => 'b.file', "b.file" => 'c.file', }
|
301
|
+
#ok_list, ng_list = @fr00.check_new_names(files)
|
302
|
+
#assert_equal( files, ok_list)
|
303
|
+
#assert_equal( {} , ng_list)
|
276
304
|
end
|
277
305
|
|
278
306
|
def test_ask_yes?
|
@@ -321,39 +349,56 @@ class TC_Commander < Test::Unit::TestCase
|
|
321
349
|
assert_equal(false, result)
|
322
350
|
end
|
323
351
|
|
324
|
-
def
|
325
|
-
|
326
|
-
FileUtils.
|
327
|
-
|
328
|
-
|
329
|
-
FileUtils.
|
330
|
-
|
331
|
-
|
332
|
-
#
|
333
|
-
FileUtils.
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
#
|
348
|
-
|
349
|
-
|
350
|
-
#
|
351
|
-
#
|
352
|
-
|
353
|
-
FileUtils.
|
354
|
-
|
355
|
-
FileUtils.
|
356
|
-
|
352
|
+
def test_convert
|
353
|
+
root_dir = 'test/commander/convert'
|
354
|
+
FileUtils.rm_rf root_dir
|
355
|
+
|
356
|
+
a0 = "#{root_dir}/a0.file"
|
357
|
+
FileUtils.mkdir_p(File.dirname a0)
|
358
|
+
File.open(a0, 'w').close
|
359
|
+
|
360
|
+
a1 = "#{root_dir}/a1.file"
|
361
|
+
FileUtils.mkdir_p(File.dirname a1)
|
362
|
+
File.open(a1, 'w').close
|
363
|
+
|
364
|
+
assert_equal(true , FileTest.exist?(a0))
|
365
|
+
@fr00.convert({a0 => a1})
|
366
|
+
assert_equal(false, FileTest.exist?(a0))
|
367
|
+
assert_equal(true , FileTest.exist?(a1))
|
368
|
+
|
369
|
+
FileUtils.rm_rf root_dir
|
370
|
+
end
|
371
|
+
|
372
|
+
def test_transplant
|
373
|
+
src_root_dir = "test/commander/transplant"
|
374
|
+
src_dir = "#{src_root_dir}/a/b/c"
|
375
|
+
src_file1 = "#{src_dir}/1.file"
|
376
|
+
src_file2 = "#{src_dir}/2.file"
|
377
|
+
tgt_dir = "test/commander/transplant/d"
|
378
|
+
tgt_file1 = "#{tgt_dir}/1.file"
|
379
|
+
tgt_file2 = "#{tgt_dir}/2.file"
|
380
|
+
|
381
|
+
FileUtils.rm_rf([src_root_dir, tgt_dir])
|
382
|
+
|
383
|
+
FileUtils.mkdir_p src_dir
|
384
|
+
File.open(src_file1, 'w').close
|
385
|
+
File.open(src_file2, 'w').close
|
386
|
+
|
387
|
+
|
388
|
+
@fr00.transplant(src_file1, tgt_file1)
|
389
|
+
assert_equal(false, FileTest.exist?(src_file1))
|
390
|
+
assert_equal(true , FileTest.exist?(src_file2))
|
391
|
+
assert_equal(true , FileTest.exist?(tgt_file1))
|
392
|
+
assert_equal(false, FileTest.exist?(tgt_file2))
|
393
|
+
|
394
|
+
@fr00.transplant(src_file2, tgt_file2)
|
395
|
+
assert_equal(false, FileTest.exist?(src_file1))
|
396
|
+
assert_equal(false, FileTest.exist?(src_file2))
|
397
|
+
assert_equal(true , FileTest.exist?(tgt_file1))
|
398
|
+
assert_equal(true , FileTest.exist?(tgt_file2))
|
399
|
+
assert_equal(false, FileTest.exist?(src_root_dir + '/a'))
|
400
|
+
|
401
|
+
FileUtils.rm_rf(src_root_dir)
|
357
402
|
end
|
358
403
|
|
359
404
|
def test_paths
|
@@ -361,5 +406,24 @@ class TC_Commander < Test::Unit::TestCase
|
|
361
406
|
assert_equal([], @fr00.paths("baz.txt"))
|
362
407
|
assert_equal(["/foo", "/foo/bar"], @fr00.paths("/foo/bar/baz.txt"))
|
363
408
|
end
|
409
|
+
|
410
|
+
def test_rmdir_p
|
411
|
+
root_dir = 'test/commander/rmdir_p'
|
412
|
+
dir = root_dir + '/a/b/c'
|
413
|
+
FileUtils.rm_rf dir
|
414
|
+
FileUtils.mkdir_p dir
|
415
|
+
file = dir + '/file'
|
416
|
+
File.open(file, 'w').close
|
417
|
+
|
418
|
+
@fr00.rmdir_p(dir)
|
419
|
+
assert_equal(true, (FileTest.directory? dir))
|
420
|
+
|
421
|
+
FileUtils.rm(file)
|
422
|
+
@fr00.rmdir_p(dir)
|
423
|
+
assert_equal(false, (FileTest.directory? root_dir))
|
424
|
+
|
425
|
+
#teardown
|
426
|
+
FileUtils.rm_rf dir
|
427
|
+
end
|
364
428
|
end
|
365
429
|
|