ykutils 0.1.0 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/main.yml +27 -0
  3. data/.gitignore +5 -1
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +17 -0
  6. data/.rubocop_todo.yml +332 -0
  7. data/CHANGELOG.md +5 -0
  8. data/CODE_OF_CONDUCT.md +84 -0
  9. data/Gemfile +20 -1
  10. data/Gemfile.lock +76 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +12 -8
  13. data/Rakefile +11 -0
  14. data/_config.yml +1 -0
  15. data/bin/console +2 -1
  16. data/bin/erubix +17 -0
  17. data/bin/erubix2 +13 -0
  18. data/bin/makegrid +23 -0
  19. data/bin/setup +2 -1
  20. data/lib/ykutils/datastructop.rb +58 -0
  21. data/lib/ykutils/debugutils.rb +28 -35
  22. data/lib/ykutils/erubyx.rb +41 -0
  23. data/lib/ykutils/fileoputils.rb +401 -0
  24. data/lib/ykutils/filepermision.rb +8 -9
  25. data/lib/ykutils/gridlist.rb +49 -0
  26. data/lib/ykutils/hasharray.rb +13 -22
  27. data/lib/ykutils/lines.rb +143 -154
  28. data/lib/ykutils/lsutils.rb +71 -72
  29. data/lib/ykutils/nginxconfig.rb +41 -0
  30. data/lib/ykutils/nginxconfigfiles.rb +31 -0
  31. data/lib/ykutils/nkfutil.rb +147 -155
  32. data/lib/ykutils/nkfutil19.rb +88 -84
  33. data/lib/ykutils/osutil.rb +16 -19
  34. data/lib/ykutils/pathop.rb +16 -18
  35. data/lib/ykutils/retcodex.rb +9 -11
  36. data/lib/ykutils/specfileop.rb +82 -91
  37. data/lib/ykutils/stext.rb +425 -462
  38. data/lib/ykutils/stextx.rb +32 -33
  39. data/lib/ykutils/stringutils.rb +111 -125
  40. data/lib/ykutils/treemanager.rb +33 -36
  41. data/lib/ykutils/treemanagera.rb +21 -26
  42. data/lib/ykutils/version.rb +3 -1
  43. data/lib/ykutils/xlines.rb +8 -9
  44. data/lib/ykutils/yamlop.rb +227 -0
  45. data/lib/ykutils/yamlxop.rb +35 -34
  46. data/lib/ykutils.rb +28 -0
  47. data/sig/ykutils.rbs +4 -0
  48. data/test_data/4servers.erb +7 -0
  49. data/test_data/a.bat +5 -0
  50. data/test_data/a_ncn.bat +12 -0
  51. data/test_data/top.yml +0 -0
  52. data/test_data/v103-3-189-127/4servers.erb +0 -0
  53. data/test_data/v103-3-189-127/a.northern-cross.net/base.yml +28 -0
  54. data/test_data/v103-3-189-127/a.northern-cross.net/value_host.yml +1 -0
  55. data/test_data/v103-3-189-127/b.northern-cross.net/base.yml +28 -0
  56. data/test_data/v103-3-189-127/b.northern-cross.net/value_host.yml +1 -0
  57. data/test_data/v103-3-189-127/c.northern-cross.net/base.yml +28 -0
  58. data/test_data/v103-3-189-127/c.northern-cross.net/value_host.yml +1 -0
  59. data/test_data/v103-3-189-127/d.northern-cross.info/base.yml +28 -0
  60. data/test_data/v103-3-189-127/d.northern-cross.info/value_host.yml +1 -0
  61. data/test_data/v103-3-189-127/d.northern-cross.net/base.yml +28 -0
  62. data/test_data/v103-3-189-127/d.northern-cross.net/value_host.yml +1 -0
  63. data/test_data/v103-3-189-127/e.northern-cross.info/base.yml +28 -0
  64. data/test_data/v103-3-189-127/e.northern-cross.info/value_host.yml +1 -0
  65. data/test_data/v103-3-189-127/e.northern-cross.net/base.yml +28 -0
  66. data/test_data/v103-3-189-127/e.northern-cross.net/value_host.yml +1 -0
  67. data/test_data/v103-3-189-127/f.northern-cross.info/base.yml +28 -0
  68. data/test_data/v103-3-189-127/f.northern-cross.info/value_host.yml +1 -0
  69. data/test_data/v103-3-189-127/f.northern-cross.net/base.yml +28 -0
  70. data/test_data/v103-3-189-127/f.northern-cross.net/value_host.yml +1 -0
  71. data/test_data/v103-3-189-127/t_server_1.erb +7 -0
  72. data/test_data/v103-3-189-127/template.erb +21 -0
  73. data/test_data/v103-3-189-127/template_ssl.erb +22 -0
  74. data/test_data/v103-3-189-127/template_ssl_www.erb +12 -0
  75. data/test_data/v103-3-189-127/template_www.erb +24 -0
  76. data/test_data/v103-3-189-127/value.yml +3 -0
  77. data/test_data/v103-3-189-127/value_ssl.yml +5 -0
  78. data/ykutils.gemspec +43 -27
  79. metadata +91 -27
  80. data/.travis.yml +0 -3
  81. data/lib/ykutils/fileoputils2.rb +0 -443
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require "ykutils"
@@ -11,4 +12,4 @@ require "ykutils"
11
12
  # Pry.start
12
13
 
13
14
  require "irb"
14
- IRB.start
15
+ IRB.start(__FILE__)
data/bin/erubix ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ykutils"
6
+
7
+ template_file_path = ARGV[0]
8
+ value_file_path_array = [ARGV[1]]
9
+ ind = 2
10
+ while ARGV.size > ind
11
+ value_file_path_array << ARGV[ind]
12
+ ind += 1
13
+ end
14
+
15
+ scope = nil
16
+ content = Ykutils::Erubyx::erubi_render_with_file(template_file_path, scope, value_file_path_array)
17
+ puts content
data/bin/erubix2 ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ykutils"
6
+ require 'yaml'
7
+ require 'find'
8
+
9
+ ncf = Ykutils::Nginxconfigfiles.new
10
+ re = /base.yml$/
11
+ start_dir = ARGV[0]
12
+ file_list = ncf.get_file_list(start_dir, re)
13
+ ncf.output(file_list)
data/bin/makegrid ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ykutils"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ #require "irb"
15
+ #IRB.start(__FILE__)
16
+ #puts( make_grid(1 , 2) )
17
+
18
+ #puts Ykutils::Erubyx::make_grid_list()
19
+ min_row = ARGV[0].to_i
20
+ max_row = ARGV[1].to_i
21
+ min_colum = ARGV[2].to_i
22
+ max_colum = ARGV[3].to_i
23
+ puts Ykutils::Gridlist::make_grid_list_x(min_row, max_row, min_colum, max_colum)
data/bin/setup CHANGED
@@ -1,6 +1,7 @@
1
- #!/bin/bash
1
+ #!/usr/bin/env bash
2
2
  set -euo pipefail
3
3
  IFS=$'\n\t'
4
+ set -vx
4
5
 
5
6
  bundle install
6
7
 
@@ -0,0 +1,58 @@
1
+ module Ykutils
2
+ module DataStructOp
3
+ def exchange(target_ary, hash, item_key, index_key, content)
4
+ v = hash[item_key]
5
+ if v
6
+ target_ary[v[index_key]] = content
7
+ else
8
+ target_ary << content
9
+ end
10
+ end
11
+
12
+ def select_array(ary, num, &block)
13
+ ret_ary = []
14
+ if block
15
+ ary.each do |line|
16
+ ret_ary << [line, num] if block.call(line, num)
17
+ num += 1
18
+ end
19
+ else
20
+ ary.each do |line|
21
+ ret_ary << [line, num]
22
+ num += 1
23
+ end
24
+ end
25
+ ret_ary
26
+ end
27
+
28
+ def make_array(ary, num, &block)
29
+ ret_ary = []
30
+ if block
31
+ ary.each do |line|
32
+ ary = block.call(line, num)
33
+ ret_ary += ary if ary
34
+ num += 1
35
+ end
36
+ else
37
+ ret_ary = ary
38
+ end
39
+
40
+ ret_ary
41
+ end
42
+
43
+ def make_hash(ary, num, &block)
44
+ kv_ary = []
45
+ if block
46
+ ary.each do |line|
47
+ ary = block.call(line, num)
48
+ kv_ary += ary if ary
49
+ num += 1
50
+ end
51
+ else
52
+ kv_ary = ary.collect { |line| [line, nil] }.flatten
53
+ end
54
+
55
+ Hash[*kv_ary]
56
+ end
57
+ end
58
+ end
@@ -1,32 +1,29 @@
1
- require 'pp'
1
+ require "pp"
2
2
 
3
3
  module Ykutils
4
4
  module DebugUtils
5
5
  class DebugMes
6
- def DebugMes.init( debug = false )
6
+ def self.init(debug = false)
7
7
  @@debug = debug
8
8
  @@buf = []
9
9
  end
10
10
 
11
- def DebugMes.set_debug( debug )
11
+ def self.set_debug(debug)
12
12
  @@debug = debug
13
13
  end
14
14
 
15
- def DebugMes.puts_x( mes )
15
+ def self.puts_x(mes)
16
16
  @@buf ||= []
17
17
  @@buf << mes
18
- # puts "#{@@buf.size} |#{mes}"
19
-
18
+ # puts "#{@@buf.size} |#{mes}"
20
19
  end
21
20
 
22
- def DebugMes.get
23
- buf = @@buf
21
+ def self.get
22
+ @@buf
24
23
  # DebugMes.clear
25
-
26
- buf
27
24
  end
28
25
 
29
- def DebugMes.clear
26
+ def self.clear
30
27
  @@buf ||= []
31
28
  @@buf.clear
32
29
  end
@@ -37,7 +34,7 @@ module Ykutils
37
34
  end
38
35
 
39
36
  def puts_d(mes)
40
- DebugMes.puts_x( mes )
37
+ DebugMes.puts_x(mes)
41
38
  # puts mes
42
39
  end
43
40
 
@@ -56,22 +53,22 @@ module Ykutils
56
53
  end
57
54
 
58
55
  def debug_utils_init
59
- set_debug( false )
60
- set_warn( 0 )
56
+ set_debug(false)
57
+ set_warn(0)
61
58
 
62
- DebugMes.init( false )
59
+ DebugMes.init(false)
63
60
  end
64
61
 
65
- def set_debug( val )
62
+ def set_debug(val)
66
63
  @debugutils = val
67
- DebugMes.set_debug( val )
64
+ DebugMes.set_debug(val)
68
65
  end
69
66
 
70
67
  def get_debug
71
68
  @debugutils
72
69
  end
73
70
 
74
- def set_warn( val )
71
+ def set_warn(val)
75
72
  @warn = val
76
73
  end
77
74
 
@@ -79,45 +76,41 @@ module Ykutils
79
76
  @debugutils
80
77
  end
81
78
 
82
- def d_exit( num )
83
- exit( num ) if @debugutils
79
+ def d_exit(num)
80
+ exit(num) if @debugutils
84
81
  end
85
82
 
86
- def d_puts( str )
83
+ def d_puts(str)
87
84
  puts(str) if @debugutils
88
85
  end
89
86
 
90
- def w1_puts( str )
91
- unless @warn
92
- set_warn( 0 )
93
- end
87
+ def w1_puts(str)
88
+ set_warn(0) unless @warn
94
89
  puts(str) if @debugutils or @warn >= 1
95
90
  end
96
91
 
97
- def w2_puts( str )
98
- unless @warn
99
- set_warn( 0 )
100
- end
92
+ def w2_puts(str)
93
+ set_warn(0) unless @warn
101
94
  puts(str) if @debugutils or @warn >= 2
102
95
  end
103
96
 
104
- def d_caller( num )
105
- pp caller( 0 ) if @debugutils
97
+ def d_caller(_num)
98
+ pp caller(0) if @debugutils
106
99
  end
107
100
 
108
- def puts_no_empty( mes )
101
+ def puts_no_empty(mes)
109
102
  puts mes if mes != ""
110
103
  end
111
104
 
112
- def d_puts_no_empty( mes )
105
+ def d_puts_no_empty(mes)
113
106
  puts mes if mes != "" and @debugutils
114
107
  end
115
108
 
116
- def d_p( it )
109
+ def d_p(it)
117
110
  p it if @debugutils
118
111
  end
119
112
 
120
- def d_pp( it )
113
+ def d_pp(it)
121
114
  if @debugutils
122
115
  pp "@debugutils=#{@debugutils}"
123
116
  pp caller(0)
@@ -0,0 +1,41 @@
1
+ require 'tilt'
2
+ require 'yaml'
3
+
4
+ module Ykutils
5
+ module Erubyx
6
+ module_function
7
+
8
+ def erubi_render(template_hash, scope, value_hash)
9
+ unless template_hash[:OBJ]
10
+ template_hash[:OBJ] = Tilt::ErubiTemplate.new{ template_hash[:TEMPLATE] }
11
+ end
12
+ template_hash[:OBJ].render( scope, value_hash )
13
+ end
14
+
15
+ ##
16
+ # `erubi_render_with_file` takes a template file path, a scope, and a value file path, reads the
17
+ # template file, loads the value file, and then calls `erubi_render` with the template hash, scope,
18
+ # and value hash
19
+ #
20
+ # Args:
21
+ # template_file_path: The path to the template file.
22
+ # scope: the scope of the template. This is the name of the directory that the template is in.
23
+ # value_file_path: The path to the YAML file that contains the values to be used in the template.
24
+ def erubi_render_with_file(template_file_path, scope, value_file_path_array)
25
+ template_text = File.read(template_file_path)
26
+ template_hash = { TEMPLATE: template_text,
27
+ OBJ: nil
28
+ }
29
+ value_hash = value_file_path_array.reduce({}){ |hash, path|
30
+ #p path
31
+ hash0 = YAML.load_file(path)
32
+ #p hash0
33
+ hash = hash.merge(hash0)
34
+ #p hash
35
+ hash
36
+ }
37
+ #puts value_hash
38
+ erubi_render(template_hash, scope, value_hash)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,401 @@
1
+ require "ykutils/stringutils"
2
+ require "ykutils/retcodex"
3
+ require "ykutils/debugutils"
4
+
5
+ module Ykutils
6
+ module FileOpUtils
7
+ include StringUtils
8
+ include DebugUtils
9
+
10
+ WRITABLE_FILE_PATH = 11
11
+ VALID_FILE_PATH = 10
12
+ ABNORMAL_STRING = -5
13
+ INVALID_FILE_PATH = -10
14
+ NOT_WRITABLE_FILE_PATH = -11
15
+
16
+ VALID_PATH = 20
17
+ INVALID_PATH = -20
18
+
19
+ def valid?
20
+ @valid
21
+ end
22
+
23
+ def valid_fpath(fname)
24
+ ret = VALID_FILE_PATH
25
+ if normal_string?(fname)
26
+ ret = INVALID_FILE_PATH unless File.file?(fname)
27
+ else
28
+ ret = ABNORMAL_STRING
29
+ end
30
+ ret
31
+ end
32
+
33
+ def valid_path(path)
34
+ ret = VALID_PATH
35
+ if normal_string?(path)
36
+ ret = INVALID_PATH unless File.exist?(path)
37
+ else
38
+ ret = ABNORMAL_STRING
39
+ end
40
+ ret
41
+ end
42
+
43
+ def valid_fpath_with_message(fname)
44
+ d_puts("valid_fpath_with_message|fname=#{fname}")
45
+
46
+ mes = ""
47
+ ret = valid_fpath(fname)
48
+ ret_bool = (ret == VALID_FILE_PATH)
49
+
50
+ case ret
51
+ when ABNORMAL_STRING
52
+ mes = "abnormal string|#{fname}"
53
+ @valid = false
54
+
55
+ d_puts("1 fname=#{fname}")
56
+ d_caller(0)
57
+ when INVALID_PATH
58
+ mes = "Can't find #{fname}"
59
+ d_caller(0)
60
+ @valid = false
61
+
62
+ d_puts("2 fname=#{fname}")
63
+ d_caller(0)
64
+ else
65
+ mes = ""
66
+ d_puts("3 fname=#{fname}")
67
+ end
68
+ RetCode2.new(ret, ret_bool, mes)
69
+ end
70
+
71
+ def valid_fname?(fname)
72
+ ret = false
73
+ if normal_string?(fname)
74
+ if File.file?(fname)
75
+ ret = true
76
+ else
77
+ d_puts("FileOpUtils:valid_fname? 2")
78
+ end
79
+ else
80
+ d_puts("FileOpUtils:valid_fname? 1")
81
+ end
82
+ ret
83
+ end
84
+
85
+ def valid_readable_fname?(fname)
86
+ ret = false
87
+ ret = true if normal_string?(fname) and File.file?(fname) and File.readable?(fname)
88
+ ret
89
+ end
90
+
91
+ def valid_writable_fpath_with_message(fname)
92
+ mes = ""
93
+
94
+ ret = valid_fpath(fname)
95
+
96
+ ret_bool = (ret == VALID_FILE_PATH)
97
+
98
+ case ret
99
+ when ABNORMAL_STRING
100
+ mes = "abnormal string|#{fname}"
101
+ @valid = false
102
+ d_puts("1 fname=#{fname}")
103
+ d_caller(0)
104
+ when INVALID_FILE_PATH
105
+ mes = "Can't find #{fname}"
106
+ d_caller(0)
107
+ @valid = false
108
+ d_puts("2 fname=#{fname}")
109
+ d_caller(0)
110
+ else
111
+ if File.writable?(fname)
112
+ mes = ""
113
+ ret = WRITABLE_FILE_PATH
114
+ else
115
+ mes = "Can't write #{fname}"
116
+ @valid = false
117
+ ret = NOT_WRITABLE_FILE_PATH
118
+ ret_bool = false
119
+ d_puts("3 fname=#{fname}")
120
+ d_caller(0)
121
+ end
122
+ end
123
+ RetCode2.new(ret, ret_bool, mes)
124
+ end
125
+
126
+ def valid_writable_fname?(fname)
127
+ ret = false
128
+ if normal_string?(fname)
129
+ if File.file?(fname)
130
+ ret = true if File.writable?(fname)
131
+ else
132
+ unless File.directory?(fname)
133
+ dir, filename = File.split(fname)
134
+ ret = valid_writable_directory?(dir)
135
+ end
136
+ end
137
+ end
138
+ ret
139
+ end
140
+
141
+ def valid_readable_directory?(path)
142
+ ret = false
143
+ ret = true if File.directory?(path) && File.readable?(path)
144
+ ret
145
+ end
146
+
147
+ def valid_writable_directory?(path)
148
+ ret = false
149
+ ret = true if File.directory?(path) && File.writable?(path)
150
+ ret
151
+ end
152
+
153
+ def write_file_with_template(input_file_path, output_file)
154
+ ary = []
155
+ begin
156
+ File.open(input_file_path, "r") do |file|
157
+ while line = file.gets
158
+ line.chomp!
159
+ ary << file_content_process(line)
160
+ end
161
+ end
162
+ rescue StandardError => e
163
+ pp e
164
+ pp e.traceback
165
+ @valid = false
166
+ end
167
+ if @valid
168
+ content = ary.join("\n")
169
+ begin
170
+ output_file.write(content)
171
+ rescue StandardError => e
172
+ pp e
173
+ pp e.traceback
174
+ @valid = false
175
+ end
176
+ end
177
+ end
178
+
179
+ def rewrite_file(from_path_1, from_path_2, to_path_1, to_path_2)
180
+ d_p("rewrite_file")
181
+ d_p("from_path_1=#{from_path_1}")
182
+ d_p("from_path_2=#{from_path_2}")
183
+ d_p("to_path_1=#{to_path_1}")
184
+ d_p("to_path_2=#{to_path_2}")
185
+
186
+ mes = prepare_file_copy(from_path_1, from_path_2, to_path_1, to_path_2)
187
+
188
+ puts_no_empty(mes)
189
+
190
+ return false unless @valid
191
+
192
+ from_path = File.join(from_path_1, from_path_2)
193
+ to_path = File.join(to_path_1, to_path_2)
194
+ ary = []
195
+ begin
196
+ File.open(from_path, "r") do |file|
197
+ while line = file.gets
198
+ line.chomp!
199
+ ary << file_content_process(line)
200
+ end
201
+ end
202
+ rescue StandardError => e
203
+ pp e
204
+ pp e.traceback
205
+ @valid = false
206
+ end
207
+
208
+ return false unless @valid
209
+
210
+ content = ary.join("\n")
211
+
212
+ begin
213
+ File.write(to_path, content)
214
+ rescue StandardError => e
215
+ pp e
216
+ pp e.traceback
217
+ @valid = false
218
+ end
219
+ end
220
+
221
+ def prepare_file_copy(from_path_1, from_path_2, to_path_1, to_path_2)
222
+ mes = ""
223
+
224
+ d_p("prepare_file_copy")
225
+ d_p("from_path_1=#{from_path_1}")
226
+ d_p("from_path_2=#{from_path_2}")
227
+ d_p("to_path_1=#{to_path_1}")
228
+ d_p("to_path_2=#{to_path_2}")
229
+
230
+ if prepare_source_dir(from_path_1, from_path_2)
231
+ unless prepare_dest_dir(to_path_1, to_path_2)
232
+ mes = "Can't write #{to_path_1}/#{to_path_1}"
233
+ d_caller(0)
234
+ @valid = false
235
+ end
236
+ else
237
+ mes = "Can't read #{from_path_1}/#{from_path_2}"
238
+ @valid = false
239
+ end
240
+
241
+ mes
242
+ end
243
+
244
+ def prepare_source_dir(from_path_1, from_path_2)
245
+ state = false
246
+ from_path = File.join(from_path_1, from_path_2)
247
+ if valid_readable_directory?(from_path_1)
248
+ dirname, filename = File.split(from_path)
249
+ state = true if valid_readable_directory?(dirname) && valid_readable_fname?(from_path)
250
+ end
251
+ state
252
+ end
253
+
254
+ def prepare_dest_dir(to_path_1, to_path_2)
255
+ state = true
256
+ unless valid_writable_directory?(to_path_1)
257
+ if File.exist?(to_path_1)
258
+ d_caller(0)
259
+ return false
260
+ end
261
+
262
+ begin
263
+ FileUtils.mkdir_p(to_path_1)
264
+ rescue StandardError => e
265
+ state = false
266
+ pp e
267
+ pp e.traceback
268
+ end
269
+ end
270
+ unless valid_writable_directory?(to_path_1)
271
+ d_caller(0)
272
+ return false
273
+ end
274
+
275
+ to_path = File.join(to_path_1, to_path_2)
276
+ dirname, filename = File.split(to_path)
277
+ unless File.exist?(dirname)
278
+ begin
279
+ FileUtils.mkdir_p(dirname)
280
+ rescue StandardError => e
281
+ state = false
282
+ pp caller(0)
283
+ end
284
+ end
285
+
286
+ unless state
287
+ d_caller(0)
288
+ return false
289
+ end
290
+
291
+ unless valid_writable_directory?(dirname)
292
+ d_caller(0)
293
+ return false
294
+ end
295
+
296
+ if File.file?(to_path) && !valid_writable_fname?(to_path)
297
+ d_caller(0)
298
+ return false
299
+ end
300
+
301
+ true
302
+ end
303
+
304
+ def copy_file(from_path_1, from_path_2, to_path_1, to_path_2)
305
+ retry_flag = false
306
+ begin
307
+ from_path = File.join(from_path_1, from_path_2)
308
+ to_path = File.join(to_path_1, to_path_2)
309
+ FileUtils.cp(from_path, to_path)
310
+ d_puts("Copy #{from_path} -> #{to_path}")
311
+ rescue StandardError => e
312
+ retry_flag = prepare_file_copy(from_path_1, from_path_2, to_path_1, to_path_2)
313
+ end
314
+
315
+ if retry_flag
316
+ begin
317
+ FileUtils.cp(from_path, to_path)
318
+ d_puts("Copy #{from_path} -> #{to_path}")
319
+ rescue StandardError => e
320
+ @valid = false
321
+ end
322
+ end
323
+
324
+ d_puts("Don't Copy #{from_path} -> #{to_path}") unless @valid
325
+ end
326
+
327
+ def find_file(path, dirname, filename)
328
+ ret = false
329
+ ary = path.split(File::SEPARATOR)
330
+ left = ary.index(dirname)
331
+ d_p(ary)
332
+ d_p(left)
333
+ if left
334
+ sub_ary = ary[(left + 1)..-1]
335
+ ret = true if sub_ary && sub_ary.index(filename)
336
+ end
337
+
338
+ ret
339
+ end
340
+
341
+ def check_filepath(fname_ary)
342
+ ret = RetCode2.new(ret, false, "")
343
+ mes_ary = []
344
+ fname_ary.each do |fname|
345
+ ret2 = valid_fpath_with_message(fname)
346
+ d_puts("==check_filepath")
347
+ d_puts_no_empty(ret2.mes)
348
+ d_p(ret2)
349
+ if ret2.bool
350
+ ret.ret = fname
351
+ ret.bool = true
352
+ break
353
+ elsif ret2.ret == FileOpUtils::ABNORMAL_STRING
354
+ ret.ret = ret2.ret
355
+ mes_ary << "Can't find common (#{common_fname})"
356
+ end
357
+ end
358
+ ret.mes = mes_ary.join("\n") unless ret.bool
359
+ ret
360
+ end
361
+
362
+ def make_fpath_list(fname, parent_dir_ary)
363
+ ary = [fname]
364
+ parent_dir_ary.each do |dir|
365
+ ary << File.join(dir, fname) if dir
366
+ end
367
+ ary
368
+ end
369
+
370
+ def reform_filename(fname, add_string)
371
+ path = Pathname(fname)
372
+ basename = path.basename(".*")
373
+ dirname = path.dirname
374
+ extname = path.extname
375
+
376
+ dirname.join(basename.to_s + add_string + extname.to_s)
377
+ end
378
+
379
+ def determine_encoding(pn)
380
+ encodings = [Encoding::UTF_8, Encoding::EUC_JP, Encoding::Shift_JIS, Encoding::CP932]
381
+
382
+ valid_enc = nil
383
+ encodings.each do |enc|
384
+ next if valid_enc
385
+
386
+ s = pn.expand_path.read(encoding: enc)
387
+ next unless s.valid_encoding?
388
+
389
+ begin
390
+ cs = s.encode(Encoding::CP932)
391
+ valid_enc = enc
392
+ rescue StandardError => e
393
+ # puts "Conversion Error! #{y}"
394
+ # puts y
395
+ end
396
+ end
397
+
398
+ valid_enc
399
+ end
400
+ end
401
+ end