ykutils 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,443 +0,0 @@
1
- require 'ykutils/stringutils'
2
- require 'ykutils/retcodex'
3
- require 'ykutils/debugutils'
4
- require 'ykutils/fileoputils2'
5
-
6
- module Ykutils
7
- module FileOpUtils
8
- include StringUtils
9
- include DebugUtils
10
-
11
- WRITABLE_FILE_PATH = 11
12
- VALID_FILE_PATH = 10
13
- ABNORMAL_STRING = -5
14
- INVALID_FILE_PATH = -10
15
- NOT_WRITABLE_FILE_PATH = -11
16
-
17
- VALID_PATH = 20
18
- INVALID_PATH = -20
19
-
20
- def valid?
21
- @valid
22
- end
23
-
24
- def valid_fpath( fname )
25
- ret = VALID_FILE_PATH
26
- if normal_string?( fname )
27
- unless File.file?( fname )
28
- ret = INVALID_FILE_PATH
29
- end
30
- else
31
- ret = ABNORMAL_STRING
32
- end
33
- ret
34
- end
35
-
36
- def valid_path( path )
37
- ret = VALID_PATH
38
- if normal_string?( path )
39
- unless File.exist?( path )
40
- ret = INVALID_PATH
41
- end
42
- else
43
- ret = ABNORMAL_STRING
44
- end
45
- ret
46
- end
47
-
48
- def valid_fpath_with_message( fname )
49
- d_puts( "valid_fpath_with_message|fname=#{fname}" )
50
-
51
- mes = ""
52
- ret = valid_fpath(fname)
53
- ret_bool = (ret == VALID_FILE_PATH ? true : false )
54
-
55
- case ret
56
- when ABNORMAL_STRING
57
- mes = "abnormal string|#{fname}"
58
- @valid = false
59
-
60
- d_puts( "1 fname=#{fname}" )
61
- d_caller(0)
62
- when INVALID_PATH
63
- mes = "Can't find #{fname}"
64
- d_caller(0)
65
- @valid = false
66
-
67
- d_puts( "2 fname=#{fname}" )
68
- d_caller(0)
69
- else
70
- mes = ""
71
- d_puts( "3 fname=#{fname}" )
72
- end
73
- RetCode2.new( ret , ret_bool, mes )
74
- end
75
-
76
- def valid_fname?( fname )
77
- ret = false
78
- if normal_string?( fname )
79
- if File.file?( fname )
80
- ret = true
81
- else
82
- d_puts( "FileOpUtils:valid_fname? 2" )
83
- end
84
- else
85
- d_puts( "FileOpUtils:valid_fname? 1" )
86
- end
87
- ret
88
- end
89
-
90
- def valid_readable_fname?( fname )
91
- ret = false
92
- if normal_string?( fname ) and File.file?( fname ) and File.readable?( fname )
93
- ret = true
94
- end
95
- ret
96
- end
97
-
98
- def valid_writable_fpath_with_message( fname )
99
- mes = ""
100
-
101
- ret = valid_fpath(fname)
102
-
103
- ret_bool = (ret == VALID_FILE_PATH ? true : false )
104
-
105
- case ret
106
- when ABNORMAL_STRING
107
- mes = "abnormal string|#{fname}"
108
- @valid = false
109
- d_puts( "1 fname=#{fname}" )
110
- d_caller(0)
111
- when INVALID_FILE_PATH
112
- mes = "Can't find #{fname}"
113
- d_caller(0)
114
- @valid = false
115
- d_puts( "2 fname=#{fname}" )
116
- d_caller(0)
117
- else
118
- if File.writable?( fname )
119
- mes = ""
120
- ret = WRITABLE_FILE_PATH
121
- else
122
- mes = "Can't write #{fname}"
123
- @valid = false
124
- ret = NOT_WRITABLE_FILE_PATH
125
- ret_bool = false
126
- d_puts( "3 fname=#{fname}" )
127
- d_caller(0)
128
- end
129
- end
130
- RetCode2.new( ret , ret_bool, mes )
131
- end
132
-
133
- def valid_writable_fname?( fname )
134
- ret = false
135
- if normal_string?( fname )
136
- if File.file?( fname )
137
- if File.writable?( fname )
138
- ret = true
139
- end
140
- else
141
- unless File.directory?( fname )
142
- dir , filename = File.split( fname )
143
- ret = valid_writable_directory?( dir )
144
- end
145
- end
146
- end
147
- ret
148
- end
149
-
150
- def valid_readable_directory?( path )
151
- ret = false
152
- if File.directory?(path)
153
- if File.readable?(path)
154
- ret = true
155
- end
156
- end
157
- ret
158
- end
159
-
160
- def valid_writable_directory?( path )
161
- ret = false
162
- if File.directory?(path)
163
- if File.writable?(path)
164
- ret = true
165
- end
166
- end
167
- ret
168
- end
169
-
170
- def write_file_with_template(input_file_path , output_file )
171
- ary = []
172
- begin
173
- File.open( input_file_path , "r" ) do |file|
174
- while line = file.gets
175
- line.chomp!
176
- ary << file_content_process(line)
177
- end
178
- end
179
- rescue => ex
180
- pp ex
181
- pp ex.traceback
182
- @valid = false
183
- end
184
- if @valid
185
- content = ary.join("\n")
186
- begin
187
- output_file.write( content )
188
- rescue => ex
189
- pp ex
190
- pp ex.traceback
191
- @valid = false
192
- end
193
- end
194
- end
195
-
196
- def rewrite_file( from_path_1 , from_path_2 , to_path_1 , to_path_2 )
197
- d_p( "rewrite_file" )
198
- d_p( "from_path_1=#{from_path_1}" )
199
- d_p( "from_path_2=#{from_path_2}" )
200
- d_p( "to_path_1=#{to_path_1}" )
201
- d_p( "to_path_2=#{to_path_2}")
202
-
203
- mes = prepare_file_copy( from_path_1, from_path_2 , to_path_1 , to_path_2 )
204
-
205
- puts_no_empty( mes )
206
-
207
-
208
- unless @valid
209
- return false
210
- end
211
-
212
- from_path = File.join( from_path_1, from_path_2 )
213
- to_path = File.join( to_path_1, to_path_2 )
214
- ary = []
215
- begin
216
- File.open( from_path , "r" ) do |file|
217
- while line = file.gets
218
- line.chomp!
219
- ary << file_content_process(line)
220
- end
221
- end
222
- rescue => ex
223
- pp ex
224
- pp ex.traceback
225
- @valid = false
226
- end
227
-
228
- unless @valid
229
- return false
230
- end
231
-
232
- content = ary.join("\n")
233
-
234
- begin
235
- File.open( to_path , "w") do |file|
236
- file.write( content )
237
- end
238
- rescue => ex
239
- pp ex
240
- pp ex.traceback
241
- @valid = false
242
- end
243
- end
244
-
245
- def prepare_file_copy( from_path_1, from_path_2 , to_path_1 , to_path_2 )
246
- mes = ""
247
-
248
- d_p( "prepare_file_copy" )
249
- d_p( "from_path_1=#{from_path_1}" )
250
- d_p( "from_path_2=#{from_path_2}" )
251
- d_p( "to_path_1=#{to_path_1}" )
252
- d_p( "to_path_2=#{to_path_2}" )
253
-
254
- unless prepare_source_dir( from_path_1, from_path_2 )
255
- mes = "Can't read #{from_path_1}/#{from_path_2}"
256
- @valid = false
257
- else
258
- unless prepare_dest_dir( to_path_1, to_path_2 )
259
- mes = "Can't write #{to_path_1}/#{to_path_1}"
260
- d_caller(0)
261
- @valid = false
262
- end
263
- end
264
-
265
- mes
266
- end
267
-
268
- def prepare_source_dir( from_path_1, from_path_2 )
269
- state = false
270
- from_path = File.join( from_path_1, from_path_2 )
271
- if valid_readable_directory?( from_path_1 )
272
- dirname , filename = File.split( from_path )
273
- if valid_readable_directory?( dirname )
274
- if valid_readable_fname?( from_path )
275
- state = true
276
- end
277
- end
278
- end
279
- state
280
- end
281
-
282
- def prepare_dest_dir( to_path_1, to_path_2 )
283
- state = true
284
- unless valid_writable_directory?( to_path_1 )
285
- if File.exist?( to_path_1 )
286
- d_caller(0)
287
- return false
288
- end
289
-
290
- begin
291
- FileUtils.mkdir_p( to_path_1 )
292
- rescue => ex
293
- state = false
294
- pp ex
295
- pp ex.traceback
296
- end
297
- end
298
- unless valid_writable_directory?( to_path_1 )
299
- d_caller(0)
300
- return false
301
- end
302
-
303
- to_path = File.join( to_path_1, to_path_2 )
304
- dirname , filename = File.split( to_path )
305
- unless File.exist?( dirname )
306
- begin
307
- FileUtils.mkdir_p( dirname )
308
- rescue => ex
309
- state = false
310
- pp caller(0)
311
- end
312
- end
313
-
314
- unless state
315
- d_caller(0)
316
- return false
317
- end
318
-
319
- unless valid_writable_directory?( dirname )
320
- d_caller(0)
321
- return false
322
- end
323
-
324
- if File.file?( to_path )
325
- unless valid_writable_fname?( to_path )
326
- d_caller(0)
327
- return false
328
- end
329
- end
330
-
331
- true
332
- end
333
-
334
- def copy_file( from_path_1 , from_path_2 , to_path_1 , to_path_2 )
335
- retry_flag = false
336
- begin
337
- from_path = File.join( from_path_1, from_path_2 )
338
- to_path = File.join( to_path_1 , to_path_2 )
339
- FileUtils.cp( from_path , to_path )
340
- d_puts( "Copy #{from_path} -> #{to_path}" )
341
- rescue => ex
342
- retry_flag = prepare_file_copy( from_path_1 , from_path_2 , to_path_1 , to_path_2 )
343
- end
344
-
345
- if retry_flag
346
- begin
347
- FileUtils.cp( from_path , to_path )
348
- d_puts( "Copy #{from_path} -> #{to_path}" )
349
- rescue => ex
350
- @valid = false
351
- end
352
- end
353
-
354
- unless @valid
355
- d_puts( "Don't Copy #{from_path} -> #{to_path}" )
356
- end
357
- end
358
-
359
- def find_file( path , dirname , filename )
360
- ret = false
361
- ary = path.split( File::SEPARATOR )
362
- left = ary.index( dirname )
363
- d_p( ary )
364
- d_p(left)
365
- if left
366
- sub_ary = ary[(left+1)..-1]
367
- if sub_ary
368
- if sub_ary.index( filename )
369
- ret = true
370
- end
371
- end
372
- end
373
-
374
- ret
375
- end
376
-
377
- def check_filepath( fname_ary )
378
- ret = RetCode2.new( ret , false, "" )
379
- mes_ary = []
380
- fname_ary.each do |fname|
381
- ret2 = valid_fpath_with_message( fname )
382
- d_puts("==check_filepath")
383
- d_puts_no_empty( ret2.mes )
384
- d_p( ret2 )
385
- if ret2.bool
386
- ret.ret = fname
387
- ret.bool = true
388
- break
389
- else
390
- if ret2.ret == FileOpUtils::ABNORMAL_STRING
391
- ret.ret = ret2.ret
392
- mes_ary << "Can't find common (#{common_fname})"
393
- end
394
- end
395
- end
396
- unless ret.bool
397
- ret.mes = mes_ary.join("\n")
398
- end
399
- ret
400
- end
401
-
402
- def make_fpath_list( fname , parent_dir_ary )
403
- ary = [ fname ]
404
- parent_dir_ary.each do |dir|
405
- if dir
406
- ary << File.join( dir , fname )
407
- end
408
- end
409
- ary
410
- end
411
-
412
- def reform_filename( fname , add_string )
413
- path = Pathname(fname)
414
- basename = path.basename(".*")
415
- dirname = path.dirname
416
- extname = path.extname
417
-
418
- dirname.join( basename.to_s + add_string + extname.to_s )
419
- end
420
-
421
- def determine_encoding(pn)
422
- encodings = [Encoding::UTF_8 , Encoding::EUC_JP , Encoding::Shift_JIS , Encoding::CP932]
423
-
424
- valid_enc = nil
425
- encodings.each do |enc|
426
- unless valid_enc
427
- s = pn.expand_path.read(:encoding => enc)
428
- if s.valid_encoding?
429
- begin
430
- cs = s.encode(Encoding::CP932)
431
- valid_enc = enc
432
- rescue => ex
433
- # puts "Conversion Error! #{y}"
434
- # puts y
435
- end
436
- end
437
- end
438
- end
439
-
440
- valid_enc
441
- end
442
- end
443
- end