file_crawler 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/file_crawler/finder/command/collect.rb +43 -30
- data/lib/file_crawler/finder/command/move.rb +80 -78
- data/lib/file_crawler/finder/command/search.rb +23 -31
- data/lib/file_crawler/finder/command.rb +17 -1
- data/lib/file_crawler/finder.rb +2 -62
- data/lib/file_crawler/version.rb +1 -1
- data/lib/file_crawler.rb +3 -0
- metadata +3 -4
- data/lib/file_crawler/finder/command/resemble.rb +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df450fdad5aebd14ecd141e4264d9057df597798173557a2739fe7a6776f3fb6
|
4
|
+
data.tar.gz: 02d972b1d68d21fe2614f4bc46f717b5b064718a5c6369d1473567f53681586f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f218741b1df108a251c0c782724f4438b919017b8908f8394dd74a615a863036a586fd2e7a357a1fcccaefcdbe2224c6ce34781e9fa3a980642a7f0c149828f
|
7
|
+
data.tar.gz: 0ffac33279d95fb1fa917be2b91afe188c2f0310808dd5cd8abc029eb27b8d2840d06a460be31dac713bfef991e62f1509282b80ac1512c86a753a97c8031932
|
@@ -1,4 +1,5 @@
|
|
1
1
|
module FileCrawler
|
2
|
+
|
2
3
|
class Regex
|
3
4
|
attr_accessor :regexp_start, :regexp_end
|
4
5
|
|
@@ -16,50 +17,62 @@ module FileCrawler
|
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
class Finder
|
20
|
-
module Command
|
21
|
-
module Collect
|
22
20
|
|
23
|
-
|
21
|
+
end
|
22
|
+
|
23
|
+
module FileCrawler::Finder::Command
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
end
|
25
|
+
module Collect
|
26
|
+
include Base
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
attr_accessor :regexs
|
29
|
+
|
30
|
+
def regexs
|
31
|
+
@regexs ||= []
|
32
|
+
end
|
33
|
+
|
34
|
+
def collect(options={})
|
35
|
+
tap {
|
36
|
+
if options[:regexs].is_a?(Array)
|
37
|
+
options[:regexs].each {|o|
|
38
|
+
regexs << FileCrawler::Regex.new(o[0], o[1]) if o.size == 2
|
32
39
|
}
|
33
40
|
end
|
34
41
|
|
35
|
-
|
36
|
-
|
42
|
+
@collections = Organizer.new.run(@files, regexs)
|
43
|
+
}
|
44
|
+
end
|
37
45
|
|
38
|
-
|
39
|
-
filename = File.basename(file_path)
|
40
|
-
term = decide_index_for_collect(filename)
|
41
|
-
hash[term] ||= []
|
42
|
-
hash[term] << file_path
|
43
|
-
}
|
46
|
+
class Organizer
|
44
47
|
|
45
|
-
|
46
|
-
|
48
|
+
def run(filepaths, regexs)
|
49
|
+
hash = {}
|
47
50
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
filepaths.each {|filepath|
|
52
|
+
filename = File.basename(filepath)
|
53
|
+
term = decide_index(filename, regexs)
|
54
|
+
hash[term] ||= []
|
55
|
+
hash[term] << filepath
|
56
|
+
}
|
54
57
|
|
55
|
-
|
56
|
-
|
57
|
-
return result.strip unless result.nil?
|
58
|
+
hash
|
59
|
+
end
|
58
60
|
|
59
|
-
|
61
|
+
def decide_index(string, regexs=[])
|
62
|
+
if !regexs.empty?
|
63
|
+
regexs.each {|regex|
|
64
|
+
return $1.strip unless regex.pattern.match(string).nil?
|
65
|
+
}
|
60
66
|
end
|
61
67
|
|
68
|
+
pattern = /[\p{Hiragana}|\p{Katakana}|\p{Han}|[a-zA-Z0-9]ー ]+/
|
69
|
+
result = string.strip.scan(pattern).first
|
70
|
+
return result.strip unless result.nil?
|
71
|
+
|
72
|
+
string.strip
|
62
73
|
end
|
63
74
|
end
|
75
|
+
|
64
76
|
end
|
77
|
+
|
65
78
|
end
|
@@ -1,105 +1,107 @@
|
|
1
1
|
require 'fileutils'
|
2
2
|
|
3
|
-
module FileCrawler
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
3
|
+
module FileCrawler::Finder::Command
|
4
|
+
|
5
|
+
module Move
|
6
|
+
|
7
|
+
def move(destination, options={dry_run: true})
|
8
|
+
tap {
|
9
|
+
target = @collections.empty? ? @files : @collections
|
10
|
+
fixer = Fixer.new
|
11
|
+
@targets = fixer.make_new_path(target, destination)
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
if !options[:dry_run]
|
14
|
+
fixer.make_mv(@targets).each {|cmd|
|
15
|
+
exec(cmd)
|
17
16
|
}
|
18
17
|
end
|
18
|
+
}
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
not_move_targets = []
|
23
|
-
rename_targets = []
|
24
|
-
|
25
|
-
source.each {|directory|
|
26
|
-
if is_same?(directory, destination)
|
27
|
-
not_move_targets << directory
|
28
|
-
next
|
29
|
-
end
|
21
|
+
def cmds
|
22
|
+
return nil if @targets.nil?
|
30
23
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
24
|
+
fixer = Fixer.new
|
25
|
+
fixer.make_mv(@targets)
|
26
|
+
end
|
35
27
|
|
36
|
-
|
37
|
-
rename_targets << directory
|
38
|
-
next
|
39
|
-
end
|
28
|
+
class Fixer
|
40
29
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
30
|
+
def make_new_path(sources, destination)
|
31
|
+
case sources
|
32
|
+
when Hash
|
33
|
+
return make_new_path_for_collection(sources, destination)
|
34
|
+
when Array
|
35
|
+
return make_new_path_for_array(sources, destination)
|
36
|
+
when String
|
37
|
+
return make_new_path_for_array([sources], destination)
|
38
|
+
end
|
46
39
|
|
47
|
-
|
48
|
-
|
49
|
-
filename = find_free_filename(directory, destination)
|
50
|
-
to = destination + '/' + filename
|
51
|
-
FileUtils.mv(directory, to)
|
40
|
+
ArgumentError
|
41
|
+
end
|
52
42
|
|
53
|
-
|
54
|
-
|
43
|
+
def make_mv(filepaths)
|
44
|
+
cmds = []
|
45
|
+
make_fixed_paths(filepaths).map {|file|
|
46
|
+
cmds << "mv #{file[0]} #{file[1]}"
|
47
|
+
}
|
55
48
|
|
56
|
-
|
57
|
-
|
58
|
-
} + not_move_targets + renamed_targets
|
59
|
-
end
|
49
|
+
cmds
|
50
|
+
end
|
60
51
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
52
|
+
def make_fixed_paths(filepaths)
|
53
|
+
dest = []
|
54
|
+
filepaths.each {|filepath|
|
55
|
+
fixed_path = fix_path(filepath[1], dest)
|
56
|
+
filepath[1] = fixed_path
|
57
|
+
dest << fixed_path
|
58
|
+
}
|
66
59
|
|
67
|
-
|
68
|
-
|
60
|
+
filepaths
|
61
|
+
end
|
69
62
|
|
70
|
-
|
71
|
-
|
72
|
-
|
63
|
+
def fix_path(filepath, check_array, index=0)
|
64
|
+
newpath = filepath
|
65
|
+
newpath = "#{filepath} (#{index})" if index > 0
|
66
|
+
return fix_path(filepath, check_array, index + 1) if exist?(newpath, check_array)
|
73
67
|
|
74
|
-
|
75
|
-
|
68
|
+
newpath
|
69
|
+
end
|
76
70
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
index += 1
|
81
|
-
new_filename = "#{filename} (#{index})"
|
82
|
-
end
|
71
|
+
private
|
72
|
+
def exist?(filepath, check_array)
|
73
|
+
return true if File.exist?(filepath)
|
83
74
|
|
84
|
-
|
85
|
-
|
75
|
+
check_array.include?(filepath)
|
76
|
+
end
|
86
77
|
|
87
|
-
|
88
|
-
|
78
|
+
def make_new_path_for_array(sources, destination)
|
79
|
+
result = []
|
80
|
+
sources.each {|path|
|
81
|
+
result << [path, new_path(path, destination)]
|
82
|
+
}
|
89
83
|
|
90
|
-
|
91
|
-
|
84
|
+
result
|
85
|
+
end
|
92
86
|
|
93
|
-
|
94
|
-
|
95
|
-
|
87
|
+
def make_new_path_for_collection(sources, destination)
|
88
|
+
result = []
|
89
|
+
sources.each {|dirname, paths|
|
90
|
+
new_dir = destination + '/' + dirname.to_s
|
91
|
+
paths.each {|path|
|
92
|
+
result << [path, new_path(path, new_dir)]
|
93
|
+
}
|
94
|
+
}
|
96
95
|
|
97
|
-
|
98
|
-
|
99
|
-
File.exist?(next_path)
|
100
|
-
end
|
96
|
+
result
|
97
|
+
end
|
101
98
|
|
99
|
+
def new_path(source, destination)
|
100
|
+
destination + '/' + File.basename(source)
|
102
101
|
end
|
102
|
+
|
103
103
|
end
|
104
|
+
|
104
105
|
end
|
106
|
+
|
105
107
|
end
|
@@ -1,41 +1,33 @@
|
|
1
|
-
module FileCrawler
|
2
|
-
class Finder
|
3
|
-
module Command
|
4
|
-
module Search
|
1
|
+
module FileCrawler::Finder::Command
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
@rows = search_directories(path)
|
9
|
-
}
|
10
|
-
end
|
11
|
-
|
12
|
-
def search_directories(path)
|
13
|
-
directories = search_directories_in_path(path)
|
14
|
-
return [path] if directories.empty?
|
3
|
+
module Search
|
4
|
+
include Base
|
15
5
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
6
|
+
def search(path, options={})
|
7
|
+
tap {
|
8
|
+
@files = search_directories(path, options)
|
9
|
+
}
|
10
|
+
end
|
20
11
|
|
21
|
-
|
22
|
-
|
12
|
+
def search_directories(path, options={})
|
13
|
+
result = []
|
14
|
+
cmd = "find #{path} -type d"
|
15
|
+
if options[:maxdepth] && options[:maxdepth] > 0
|
16
|
+
cmd = "find #{path} -maxdepth #{options[:maxdepth]} -type d"
|
17
|
+
end
|
23
18
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
19
|
+
exec(cmd).each_line(chomp: true) {|item|
|
20
|
+
valid = true
|
21
|
+
if options[:exclude_invisible_file]
|
22
|
+
filename = File.basename(item)
|
23
|
+
valid = !filename.start_with?('.')
|
28
24
|
end
|
25
|
+
result << item if valid
|
26
|
+
}
|
29
27
|
|
30
|
-
|
31
|
-
Dir.entries(path).select {|item|
|
32
|
-
!item.start_with?('.')
|
33
|
-
}.map {|item|
|
34
|
-
path + '/' + item
|
35
|
-
}
|
36
|
-
end
|
37
|
-
end
|
28
|
+
result
|
38
29
|
end
|
30
|
+
|
39
31
|
end
|
40
32
|
|
41
33
|
end
|
@@ -1,4 +1,20 @@
|
|
1
|
+
|
2
|
+
class FileCrawler::Finder
|
3
|
+
|
4
|
+
module Command
|
5
|
+
|
6
|
+
module Base
|
7
|
+
|
8
|
+
def exec(str)
|
9
|
+
`#{str}`
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
1
18
|
require_relative 'command/collect'
|
2
19
|
require_relative 'command/move'
|
3
|
-
require_relative 'command/resemble'
|
4
20
|
require_relative 'command/search'
|
data/lib/file_crawler/finder.rb
CHANGED
@@ -3,76 +3,16 @@ require_relative 'finder/command'
|
|
3
3
|
module FileCrawler
|
4
4
|
|
5
5
|
class Finder
|
6
|
-
|
6
|
+
attr_accessor :files, :collections
|
7
7
|
|
8
8
|
include Command::Collect
|
9
9
|
include Command::Move
|
10
|
-
include Command::Resemble
|
11
10
|
include Command::Search
|
12
11
|
|
13
12
|
def initialize
|
14
13
|
@rows = []
|
14
|
+
@collections = []
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
# @param conditions
|
19
|
-
# directory [Boolean]
|
20
|
-
# exntesion [Array]
|
21
|
-
# extension_in_directory [Array]
|
22
|
-
def self.search(path, conditions = {})
|
23
|
-
finder = FileCrawler::Finder.new
|
24
|
-
finder.search(path, conditions)
|
25
|
-
|
26
|
-
finder.rows
|
27
|
-
end
|
28
|
-
|
29
|
-
# conditions
|
30
|
-
# - if dont have extension_in_directory, directory true
|
31
|
-
# - move check? if include, need condition[:force]
|
32
|
-
def self.move(path, destination, conditions = {})
|
33
|
-
raise ArgumentError unless File.directory?(destination)
|
34
|
-
|
35
|
-
finder = FileCrawler::Finder.new
|
36
|
-
finder.search(path).move(destination)
|
37
|
-
|
38
|
-
finder.rows
|
39
|
-
end
|
40
|
-
|
41
|
-
# conditions
|
42
|
-
# - if dont have extension_in_directory, directory true
|
43
|
-
def self.collect(path, conditions = {})
|
44
|
-
finder = FileCrawler::Finder.new
|
45
|
-
unless conditions[:regexs].nil?
|
46
|
-
conditions[:regexs].each {|regex|
|
47
|
-
finder.regexs << regex
|
48
|
-
}
|
49
|
-
end
|
50
|
-
|
51
|
-
finder.search(path).collect(conditions)
|
52
|
-
|
53
|
-
finder.rows
|
54
|
-
end
|
55
|
-
|
56
|
-
def self.organize(path, destination, conditions = {})
|
57
|
-
finder = FileCrawler::Finder.new
|
58
|
-
|
59
|
-
unless conditions[:regexs].nil?
|
60
|
-
conditions[:regexs].each {|regex|
|
61
|
-
finder.regexs << regex
|
62
|
-
}
|
63
|
-
end
|
64
|
-
|
65
|
-
finder.search(path).collect(conditions).move_from_collection(destination)
|
66
|
-
|
67
|
-
finder.rows
|
68
|
-
end
|
69
|
-
|
70
|
-
def self.resemble(path, conditions = {})
|
71
|
-
finder = FileCrawler::Finder.new
|
72
|
-
|
73
|
-
finder.search(path).collect(conditions).resemble()
|
74
|
-
|
75
|
-
finder.rows
|
76
|
-
end
|
77
|
-
|
78
18
|
end
|
data/lib/file_crawler/version.rb
CHANGED
data/lib/file_crawler.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_crawler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hirohisa Kawasaki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,7 +67,6 @@ files:
|
|
67
67
|
- lib/file_crawler/finder/command.rb
|
68
68
|
- lib/file_crawler/finder/command/collect.rb
|
69
69
|
- lib/file_crawler/finder/command/move.rb
|
70
|
-
- lib/file_crawler/finder/command/resemble.rb
|
71
70
|
- lib/file_crawler/finder/command/search.rb
|
72
71
|
- lib/file_crawler/version.rb
|
73
72
|
homepage: https://github.com/hirohisa/file_crawler
|
@@ -90,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
89
|
version: '0'
|
91
90
|
requirements: []
|
92
91
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.
|
92
|
+
rubygems_version: 2.7.3
|
94
93
|
signing_key:
|
95
94
|
specification_version: 4
|
96
95
|
summary: FileCrawler searches and controls files in local directory
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module FileCrawler
|
2
|
-
class Finder
|
3
|
-
module Command
|
4
|
-
module Resemble
|
5
|
-
|
6
|
-
def resemble()
|
7
|
-
tap {
|
8
|
-
@rows = resemble_in_collection(@rows.values)
|
9
|
-
}
|
10
|
-
end
|
11
|
-
|
12
|
-
# use rows after using #collect
|
13
|
-
def resemble_in_collection(collection)
|
14
|
-
files = []
|
15
|
-
|
16
|
-
pattern = /\(\d+\)$/
|
17
|
-
|
18
|
-
collection.each {|files_in_same_directory|
|
19
|
-
files_in_same_directory.each {|file|
|
20
|
-
filename = File.basename(file)
|
21
|
-
files << file unless pattern.match(filename).nil?
|
22
|
-
}
|
23
|
-
}
|
24
|
-
|
25
|
-
files
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|