Module: Ykutils::FileOpUtils

Includes:
DebugUtils, StringUtils
Defined in:
lib/ykutils/fileoputils.rb

Constant Summary collapse

WRITABLE_FILE_PATH =
11
VALID_FILE_PATH =
10
ABNORMAL_STRING =
-5
INVALID_FILE_PATH =
-10
NOT_WRITABLE_FILE_PATH =
-11
VALID_PATH =
20
INVALID_PATH =
-20

Instance Method Summary collapse

Methods included from DebugUtils

#clear_d, #d, #d_caller, #d_exit, #d_p, #d_pp, #d_puts, #d_puts_no_empty, #debug_utils_init, #error_exit, #puts_current_method, #puts_d, #puts_no_empty, #w1_puts, #w2_puts

Methods included from StringUtils

#get_basename_ext, #get_normalized_string, #indent, #make_content_from_template, #nil_to_nullstring, #normal_string?, #print_hier, #print_hier2

Instance Method Details

#check_filepath(fname_ary) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/ykutils/fileoputils.rb', line 341

def check_filepath(fname_ary)
  ret = RetCode2.new(ret, false, "")
  mes_ary = []
  fname_ary.each do |fname|
    ret2 = valid_fpath_with_message(fname)
    d_puts("==check_filepath")
    d_puts_no_empty(ret2.mes)
    d_p(ret2)
    if ret2.bool
      ret.ret = fname
      ret.bool = true
      break
    elsif ret2.ret == FileOpUtils::ABNORMAL_STRING
      ret.ret = ret2.ret
      mes_ary << "Can't find common (#{common_fname})"
    end
  end
  ret.mes = mes_ary.join("\n") unless ret.bool
  ret
end

#copy_file(from_path1, from_path2, to_path1, to_path2) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/ykutils/fileoputils.rb', line 304

def copy_file(from_path1, from_path2, to_path1, to_path2)
  retry_flag = false
  begin
    from_path = File.join(from_path1, from_path2)
    to_path = File.join(to_path1, to_path2)
    FileUtils.cp(from_path, to_path)
    d_puts("Copy #{from_path} -> #{to_path}")
  rescue StandardError
    retry_flag = prepare_file_copy(from_path1, from_path2, to_path1, to_path2)
  end

  if retry_flag
    begin
      FileUtils.cp(from_path, to_path)
      d_puts("Copy #{from_path} -> #{to_path}")
    rescue StandardError
      @valid = false
    end
  end

  d_puts("Don't Copy #{from_path} -> #{to_path}") unless @valid
end

#determine_encoding(pathn) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/ykutils/fileoputils.rb', line 379

def determine_encoding(pathn)
  encodings = [Encoding::UTF_8, Encoding::EUC_JP, Encoding::Shift_JIS, Encoding::CP932]

  valid_enc = nil
  encodings.each do |enc|
    next if valid_enc

    s = pathn.expand_path.read(encoding: enc)
    next unless s.valid_encoding?

    begin
      s.encode(Encoding::CP932)
      valid_enc = enc
    rescue StandardError
      #        puts "Conversion Error! #{y}"
      #            puts y
    end
  end

  valid_enc
end

#find_file(path, dirname, filename) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/ykutils/fileoputils.rb', line 327

def find_file(path, dirname, filename)
  ret = false
  ary = path.split(File::SEPARATOR)
  left = ary.index(dirname)
  d_p(ary)
  d_p(left)
  if left
    sub_ary = ary[(left + 1)..]
    ret = true if sub_ary&.index(filename)
  end

  ret
end

#make_fpath_list(fname, parent_dir_ary) ⇒ Object



362
363
364
365
366
367
368
# File 'lib/ykutils/fileoputils.rb', line 362

def make_fpath_list(fname, parent_dir_ary)
  ary = [fname]
  parent_dir_ary.each do |dir|
    ary << File.join(dir, fname) if dir
  end
  ary
end

#prepare_dest_dir(to_path1, to_path2) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/ykutils/fileoputils.rb', line 254

def prepare_dest_dir(to_path1, to_path2)
  state = true
  unless valid_writable_directory?(to_path1)
    if File.exist?(to_path1)
      d_caller(0)
      return false
    end

    begin
      FileUtils.mkdir_p(to_path1)
    rescue StandardError => e
      state = false
      pp e
      pp e.traceback
    end
  end
  unless valid_writable_directory?(to_path1)
    d_caller(0)
    return false
  end

  to_path = File.join(to_path1, to_path2)
  dirname, = File.split(to_path)
  unless File.exist?(dirname)
    begin
      FileUtils.mkdir_p(dirname)
    rescue StandardError
      state = false
      pp caller(0)
    end
  end

  unless state
    d_caller(0)
    return false
  end

  unless valid_writable_directory?(dirname)
    d_caller(0)
    return false
  end

  if File.file?(to_path) && !valid_writable_fname?(to_path)
    d_caller(0)
    return false
  end

  true
end

#prepare_file_copy(from_path1, from_path2, to_path1, to_path2) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/ykutils/fileoputils.rb', line 221

def prepare_file_copy(from_path1, from_path2, to_path1, to_path2)
  mes = ""

  d_p("prepare_file_copy")
  d_p("from_path_1=#{from_path1}")
  d_p("from_path_2=#{from_path2}")
  d_p("to_path_1=#{to_path1}")
  d_p("to_path_2=#{to_path2}")

  if prepare_source_dir(from_path1, from_path2)
    unless prepare_dest_dir(to_path1, to_path2)
      mes = "Can't write #{to_path1}/#{to_path1}"
      d_caller(0)
      @valid = false
    end
  else
    mes = "Can't read #{from_path1}/#{from_path2}"
    @valid = false
  end

  mes
end

#prepare_source_dir(from_path1, from_path2) ⇒ Object



244
245
246
247
248
249
250
251
252
# File 'lib/ykutils/fileoputils.rb', line 244

def prepare_source_dir(from_path1, from_path2)
  state = false
  from_path = File.join(from_path1, from_path2)
  if valid_readable_directory?(from_path1)
    dirname, = File.split(from_path)
    state = true if valid_readable_directory?(dirname) && valid_readable_fname?(from_path)
  end
  state
end

#reform_filename(fname, add_string) ⇒ Object



370
371
372
373
374
375
376
377
# File 'lib/ykutils/fileoputils.rb', line 370

def reform_filename(fname, add_string)
  path = Pathname(fname)
  basename = path.basename(".*")
  dirname = path.dirname
  extname = path.extname

  dirname.join(basename.to_s + add_string + extname.to_s)
end

#rewrite_file(from_path1, from_path2, to_path1, to_path2) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/ykutils/fileoputils.rb', line 179

def rewrite_file(from_path1, from_path2, to_path1, to_path2)
  d_p("rewrite_file")
  d_p("from_path_1=#{from_path1}")
  d_p("from_path_2=#{from_path2}")
  d_p("to_path_1=#{to_path1}")
  d_p("to_path_2=#{to_path2}")

  mes = prepare_file_copy(from_path1, from_path2, to_path1, to_path2)

  puts_no_empty(mes)

  return false unless @valid

  from_path = File.join(from_path1, from_path2)
  to_path = File.join(to_path1, to_path2)
  ary = []
  begin
    File.open(from_path, "r") do |file|
      while (line = file.gets)
        line.chomp!
        ary << file_content_process(line)
      end
    end
  rescue StandardError => e
    pp e
    pp e.traceback
    @valid = false
  end

  return false unless @valid

  content = ary.join("\n")

  begin
    File.write(to_path, content)
  rescue StandardError => e
    pp e
    pp e.traceback
    @valid = false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/ykutils/fileoputils.rb', line 19

def valid?
  @valid
end

#valid_fname?(fname) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ykutils/fileoputils.rb', line 71

def valid_fname?(fname)
  ret = false
  if normal_string?(fname)
    if File.file?(fname)
      ret = true
    else
      d_puts("FileOpUtils:valid_fname? 2")
    end
  else
    d_puts("FileOpUtils:valid_fname? 1")
  end
  ret
end

#valid_fpath(fname) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/ykutils/fileoputils.rb', line 23

def valid_fpath(fname)
  ret = VALID_FILE_PATH
  if normal_string?(fname)
    ret = INVALID_FILE_PATH unless File.file?(fname)
  else
    ret = ABNORMAL_STRING
  end
  ret
end

#valid_fpath_with_message(fname) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ykutils/fileoputils.rb', line 43

def valid_fpath_with_message(fname)
  d_puts("valid_fpath_with_message|fname=#{fname}")

  mes = ""
  ret = valid_fpath(fname)
  ret_bool = (ret == VALID_FILE_PATH)

  case ret
  when ABNORMAL_STRING
    mes = "abnormal string|#{fname}"
    @valid = false

    d_puts("1 fname=#{fname}")
    d_caller(0)
  when INVALID_PATH
    mes = "Can't find #{fname}"
    d_caller(0)
    @valid = false

    d_puts("2 fname=#{fname}")
    d_caller(0)
  else
    mes = ""
    d_puts("3 fname=#{fname}")
  end
  RetCode2.new(ret, ret_bool, mes)
end

#valid_path(path) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/ykutils/fileoputils.rb', line 33

def valid_path(path)
  ret = VALID_PATH
  if normal_string?(path)
    ret = INVALID_PATH unless File.exist?(path)
  else
    ret = ABNORMAL_STRING
  end
  ret
end

#valid_readable_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
145
# File 'lib/ykutils/fileoputils.rb', line 141

def valid_readable_directory?(path)
  ret = false
  ret = true if File.directory?(path) && File.readable?(path)
  ret
end

#valid_readable_fname?(fname) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/ykutils/fileoputils.rb', line 85

def valid_readable_fname?(fname)
  ret = false
  ret = true if normal_string?(fname) && File.file?(fname) && File.readable?(fname)
  ret
end

#valid_writable_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
# File 'lib/ykutils/fileoputils.rb', line 147

def valid_writable_directory?(path)
  ret = false
  ret = true if File.directory?(path) && File.writable?(path)
  ret
end

#valid_writable_fname?(fname) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ykutils/fileoputils.rb', line 126

def valid_writable_fname?(fname)
  ret = false
  if normal_string?(fname)
    if File.file?(fname)
      ret = true if File.writable?(fname)
    else
      unless File.directory?(fname)
        dir, = File.split(fname)
        ret = valid_writable_directory?(dir)
      end
    end
  end
  ret
end

#valid_writable_fpath_with_message(fname) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ykutils/fileoputils.rb', line 91

def valid_writable_fpath_with_message(fname)
  mes = ""

  ret = valid_fpath(fname)

  ret_bool = (ret == VALID_FILE_PATH)

  case ret
  when ABNORMAL_STRING
    mes = "abnormal string|#{fname}"
    @valid = false
    d_puts("1 fname=#{fname}")
    d_caller(0)
  when INVALID_FILE_PATH
    mes = "Can't find #{fname}"
    d_caller(0)
    @valid = false
    d_puts("2 fname=#{fname}")
    d_caller(0)
  else
    if File.writable?(fname)
      mes = ""
      ret = WRITABLE_FILE_PATH
    else
      mes = "Can't write #{fname}"
      @valid = false
      ret = NOT_WRITABLE_FILE_PATH
      ret_bool = false
      d_puts("3 fname=#{fname}")
      d_caller(0)
    end
  end
  RetCode2.new(ret, ret_bool, mes)
end

#write_file_with_template(input_file_path, output_file) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ykutils/fileoputils.rb', line 153

def write_file_with_template(input_file_path, output_file)
  ary = []
  begin
    File.open(input_file_path, "r") do |file|
      while (line = file.gets)
        line.chomp!
        ary << file_content_process(line)
      end
    end
  rescue StandardError => e
    pp e
    pp e.traceback
    @valid = false
  end
  return unless @valid

  content = ary.join("\n")
  begin
    output_file.write(content)
  rescue StandardError => e
    pp e
    pp e.traceback
    @valid = false
  end
end